Kicad 7 scripting, position errors

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]
1 Like

Wow you’re fast! replied within 10 minutes!
I see what your doing there, and edited my baby script

#via.SetPosition(point) #It crashes here on this line.
via.SetPosition(pcbnew.VECTOR2I(point))

It doesn’t crash anymore (jay!). Unfortunately it places the via at [0,0] instead of [100,100].

Thanks again for helping me!
Pascal

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()

You can use pcbnew.VECTOR2I_MM(100,100) to construct correct vector directly.

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:

  1. 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.
  2. 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])
  3. 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.

It will in some cases show types that are not actually exposed in swig so you can’t use the method but such is life with current api.

2 Likes

Thank you! this also removes the need for the addition of raising the x and y values by 10^6.

Yes this is exactly what I was trying to do. The forum link is very insightfull thank you!

This is a pretty good summery of my experience with kicad scripting.

There are also more generic pcbnew.ToMM(), FromMM() etc. so you have to learn only one style for conversions everywhere.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.