Python scripting - how to duplicate footprint (module)?

Hi all,

I’m developing a script to panelize KiCad boards automatically, e.g. for a v-scored panel of a few small identical boards. The idea is that it will be fed an existing board design and, given an array size, make an appropriate panel of the given board, adding siderails, v-scoring information and fiducials automatically.

Right now, I can easily create an appropriate array of tracks, zones, text, and everything except modules by using the Duplicate() function. Here’s an example codeblock from my script that create an array of tracks:

# array of tracks
tracks = sourceBoard.GetTracks()
newTracks = []
for sourceTrack in tracks:                          # Iterate through each track to be copied
    for x in range(0,arrayXnum):                    # Iterate through x direction
        for y in range(0, arrayYnum):               # Iterate through y direction
            if((x!=0)or(y!=0)):                     # Don't duplicate source object to location
                newTrack = sourceTrack.Duplicate()
                newTrack.Move(wxPoint(x*boardWidth, y*boardWidth)) # Move to correct location
                newTracks.append(newTrack)              # Add to temporary list of tracks

for track in newTracks:  # add new tracks to board
    tracks.Append(track)

print("tracks copied.")

The above works perfectly for tracks, and similarly for zones and text.

When I try to do something similar with modules, the Duplicate() function doesn’t work as it requires two arguments for modules:

>>> module.Duplicate()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Duplicate() missing 2 required positional arguments: 'aItem' and 'aIncrementPadNumbers'

I’ve tried passing it the item I want to duplicate and tried both True and False for aIncrementPadNumbers, but I’m having no luck.

I also looked into simply loading another footprint via pcbnew.PCB_IO().LoadFootprint(), but that requires a footprint library path to be specified, and I couldn’t figure out any way of getting that information from an existing footprint.

As you can probably tell, I’m pretty new to Python in general, and this is also my first time trying to script KiCad. Any help or pointers here would be greatly appreciated!

In such cases look at the cpp source. You will see that module class is a special case, it’s not an EDA_BOARD_ITEM like tracks or other drawings, it’s a container class. It has it’s own Duplicate() method which does different thing from what you expect. Comment will give you a clue:

To create a duplicate of a module create a copy of it and add it to the board like this:

b = pcbnew.GetBoard()
m = list(b.GetModules())[0]
n = pcbnew.MODULE(m)  # creates new module n as a copy of m
b.Add(n)  # inserts new copy into the board
n.SetPosition(m.GetPosition())  # add any offset as needed
pcbnew.Refresh()  # only do this once at the end
3 Likes

Thanks, my script is now well on its way to working 100%. I’ll be open-sourcing it and hopefully making it a plugin sometime soon.

1 Like

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