Hello!
I tried out Kicad 7 and liked the new version a lot! I cannot however use my own scripts. I’ve made a little baby python script/plugin that should only place a via at [100,100]. It doesn’t work and I do not know why.
import pcbnew
class TEST_SCRIPT(pcbnew.ActionPlugin):
def defaults( self ):
self.name = "My Kicad 7 test script"
self.category = "Modify PCB"
self.description = "Adds a via at x=100, y=100."
self.show_toolbar_button = True
def Run( self ):
board = pcbnew.GetBoard()
point = pcbnew.wxPoint(100,100)
via = pcbnew.PCB_VIA(board)
via.SetPosition(point) #It crashes here on this line.
board.Add(via)
TEST_SCRIPT().register()
The error message is less than helpful
TypeError: in method ‘EDA_ITEM_SetPosition’, argument 2 of type ‘VECTOR2I const &’
Thank you for taking your valuable time, helping me!
Pascal
The error message tells you that the position requires a VECTOR argument and you are passing a POINT, here an extract from the RF Tools from here:
def createVias(self, viaPoints, viaDrill, viaSize, netCode):
newVias = []
for viaPoint in viaPoints:
if not(hasattr(pcbnew,'DRAWSEGMENT')):
newVia = pcbnew.PCB_VIA(self.boardObj)
else:
newVia = pcbnew.VIA(self.boardObj)
if hasattr(newVia, 'SetTimeStamp'):
ts = 55
newVia.SetTimeStamp(ts) # adding a unique number as timestamp to mark this via as generated by this script
self.boardObj.Add(newVia)
if hasattr(pcbnew, 'EDA_RECT'): # kv5,kv6
newVia.SetPosition(pcbnew.wxPoint(viaPoint[0], viaPoint[1]))
else: #kv7
newVia.SetPosition(pcbnew.VECTOR2I(pcbnew.wxPoint(viaPoint[0], viaPoint[1])))
newVia.SetWidth(viaSize)
newVia.SetDrill(viaDrill)
if hasattr(pcbnew, 'VIA_THROUGH'):
newVia.SetViaType(pcbnew.VIA_THROUGH)
else:
newVia.SetViaType(pcbnew.VIATYPE_THROUGH)
newVia.SetNetCode(netCode)
newVias += [newVia]
Ah I found it!
The via wasn’t placed at [0,0] but rather [0.0001,0.0001].
So i got the order of magnitude wrong. I’ve placed the “working” baby script below.
Thank you again!
import pcbnew
class TEST_SCRIPT(pcbnew.ActionPlugin):
def defaults( self ):
self.name = "My Kicad 7 test script"
self.category = "Modify PCB"
self.description = "Adds a via at x=100, y=100."
self.show_toolbar_button = True
def Run( self ):
board = pcbnew.GetBoard()
point = pcbnew.wxPoint(100e6,100e6)
via = pcbnew.PCB_VIA(board)
via.SetPosition(pcbnew.VECTOR2I(point))
board.Add(via)
TEST_SCRIPT().register()
If you’re anything like me, you were migrating code from 5.x to 7 and that’s what caused your confusion about wxPoint. Someone actually made a very short list of some of the more obvious changes in the python interface on the forum here.
A tip for how I usually navigate these errors of not knowing how to use the interface:
Run the function in a console with no args and / or the wrong type of argument to see the error produced and what type it expects.
If I don’t know how to generate the type, I run something similar to pprint([entry for entry in dir(pcbnew) if "VECTOR2I" in entry])
If it’s still not obvious, poke around in the cpp source code in the Gitlab Repo and it’s usually pretty obvious without needing to dive in too deep.
Good hints, I would add one:
Before going poking into cpp source, lookup the function in scripting console. It will show you proper signature with types, including overrides if there are any.