Is it possible to use pcbnew.GetBoard().GetFootprint() from python?

I have found KiCAD pcbnew scripting: pcbnew.BOARD Class Reference: def pcbnew.BOARD.GetFootprint in the API, so I thought I’d try it.

So I just placed a component (here Housings_DIP:DIP-8_W10.16mm) in an empty Pcbnew file, and I tried this:

import wx, pcbnew
pcbnew.GetBoard().GetFootprint(wx.Point(19,19), -1, False)

The response to that I get, is:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/path/to/kicad/usr/local/lib/python2.7/dist-packages/pcbnew.py", line 14964, in GetFootprint
    return _pcbnew.BOARD_GetFootprint(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'BOARD_GetFootprint'.
  Possible C/C++ prototypes are:
    BOARD::GetFootprint(wxPoint const &,PCB_LAYER_ID,bool,bool)
    BOARD::GetFootprint(wxPoint const &,PCB_LAYER_ID,bool)

But then,

  • I did try passing the Python counterpart to C++ wxPoint, which is wx.Point, as first argument (altough since it is wx, its coordinate system probably expects pixels, and not mm/inch coordinates of the PCB layout design);
  • kicad-source-mirror/layers_id_colors_and_visibility.h notes that enum PCB_LAYER_ID: int and UNDEFINED_LAYER = -1,
  • The API link notes third argument is bool aVisibleOnly, and I do use False there

Has anyone ever tried to use this command, and has it ever worked? Maybe it is just autoexported by something that should generate a Python interface from C++ (there was software for these kinds of things, was it swig, cannot remember), and its not meant to work in its current state?

Got it - turns out you shouldn’t use wx.Point (from import wx) - instead, there is actually a defined proxy Python class in pcbnew.py called wxPoint (like the C++ name) - so this ended up working for me:

>>> pcbnew.GetBoard().GetFootprint(pcbnew.wxPointMM(19,19), -1, False)
<pcbnew.MODULE; proxy of <Swig Object of type 'MODULE *' at 0xda419578> >

>>> pcbnew.GetBoard().GetFootprint(pcbnew.wxPointMM(19,19), -1, False).GetReference()
u'U1'
2 Likes

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