Correct way to copy a footprint with python

I want to copy a footprint from one board to a other with python, to make a panel. I read in a forum post [1] i should use <source board>.GetModules(), pcbnew.MODULE(<module>) and <target board>.Add(<copy>). However, this only works when source and the target board is the same. I get a segment fault if i do it from one board to a other*. Is there a other way to do this which is the correct way or is this way already correct and a bug in the KiCAD python library**?

I attached a zip-file [2] which includes a project and a python script, create_panel.py, which returns in a segment fault.

Sources/Footnotes

*I get a segmentation fault in the moment i save the new board with <target board>.Save(<path>).
**In this case i would make a proper bug report, but i want to ask for the correct way before i “complain”.
[1] Python scripting - how to duplicate footprint (module)?
[2] test2empty_segfault.zip (11.0 KB)

You must have been poking around and show as a ‘basic’ user instead of new. You should now be able to attach files.

My code does the following:

  # Footprints
  for mod in list(source.board.GetModules()):
    source.board.Remove(mod)
    self.board.Add(mod)
    mod.Move(offset)

A module cannot be part of two boards.

The trick is to remove it from the source board, and only then to place it in the target panel.

Workflow: Load a board, for each item remove it from the source board and then add it to the target panel. Repeat for each board, if you need two copies of the same source board, reload the source board from disk.

Edit: as far as I remember I had a working method to copy modules but the Python interface changed, so I decided for a simpler more robust method.

Version of pcbnew? (Argh, post must be 20 characters)

Another perhaps more productive question: you are copying the nets too I hope? I tried removing that and I get a segfault too if I ignore the nets. It seems these do not need to be removed, I simply use

for net in list(source.board.GetNetsByName().values()):
  self.board.Add(net)
1 Like

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