Having trouble copying pads into a new board with the Python bindings

Hi!

All the forum posts I could find about this seemed to be for an older version of the API, so I’m making a new post.

I create a new board in python like so:

new_board = pcbnew.NewBoard("file path")

And then I try adding a pad:

new_pad = old_pad.Duplicate()
new_board.Add(new_pad)

But this doesn’t seem to work.

From what I can tell from the older posts, this may be because I’m not copying over the nets, but I don’t really know how to do that. And setting the pad’s netcode to -1 doesn’t seem to work either.

Another method I tried was to copy the entire board and then remove everything:

old_board.Save("file path")
new_board = pcbnew.LoadBoard("file path")

But when I try to remove the items, items in the original board seem to get removed.

Am I copying the board wrong? And is anything to copy over that I’m forgetting

I don’t think you can add only a pad, as pad is a part of footprint.

If you look at the Save/Restore Layout plugin code, specifically SaveLayout you should see how I removed items (footprints, tracks, …) and saved only part of original board.

In the RestoreLayout class you can see how the saved items in partial board are copied to a new board.

How does it not work? Are the items not in the new board? Did you save the board after modification and reload it in pcbnew to verify? Is there an exception? Error text anywhere?

Thanks you for responding!

So if I’m getting this right,
You create a new PCB_GROUP and add it to the board
Create clones of items, copy over the attributes and then add them to that group?
Did I get that right?

Also how exactly are you copying the nets over?

Hello again!

I’m using Save(). It doesn’t show up, no exceptions or errors.

It probably would not hurt knowing what your endgoal is.

Anyhow, the description of the SaveRestore pligin is:
This plugin implements footprint modules or snippets or layout blocks feature. The plugin saves partial layout corresponding to footprints from one hirearchical sheet so that it can be restored in other projects. All projects have to use the same hierarchical sheet schematics.

SaveLayout

This part of the plugin takes current board and it saves it under another name. This is done in order to keep the currently opened board unmodified. Then it loads (into memory) the newly saved board and removes all off the items but the selection from the board and it saves the modifed board. Along the board The selection consists only of footprints, tracks zones and drawings which belong to selected hierarchical sheet. Selection is determined by the footprint which was selected befor the plugin was run.

RestoreLayout

This part of the plugin restores the layout into the opened layout. It does this by opening the saved board and it places the footprints relatively to each other. The footprints are matched by the footprint ID. Note that NO footprints are copied between boards.
The connectivity is also derived from the footprints where the plugin build a map to figure out which net in the target board is matched by the net in the saved board (look into RestoreLayout.get_net_pairs().
Tracks, Zones and Drawings on the other hand are copied between boards. More specificly an item is duplicated and than added to the board. During this process the item is also attached to correct net using the mapind derived from the footprints.

This plugin does not create any new nets.

I hope this helps

I don’t know the exact reason but I am using pcbnew.IO_MGR.Save and pcbnew.IO_MGR.Load in V7 or pcbnew.PCB_IO_MGR.Save and pcbnew.PCB_IO_MGR.Load in V8 for saving and loading boards. I think is has something to do with pcbnew.SaveBoard from scripting console or an action plugin messes up underlying data structures (#11323) · Issues · KiCad / KiCad Source Code / kicad · GitLab

I have program that finds nets that fit a specific condition, and I want to display those nets. I think one possible way would be to create a new board and copy over the pads and tracks of that net on to the new board.

I tried saving the board and removing all the components, but when I tried removing them, the components on the original board got removed. But that might be because I used pcbnew.Save() and Remove() instead of RemoveNative()

People have already written web viewers for kicad files, also my plugin can highlight nets, demo (switch to netlist mode on top toolbar).
Maybe other solutions will work for you or you can slightly modify that code to fit your needs instead of trying to write something from scratch.

If you want to do this for a board that is currently opened in pcb editor, there is an easier way. On the items that you want to show to the user set the SetBrightened() on them and call the pcbnew.Refresh(). The items should be highlighted in the viewport.

But I also want to be able to export them, so I dont think just brightening them is enough

Ahh, yes, then copying the whole board and removing the rest is your best option.

As for the pads, you’ll only be able to keep whole footprints. You might get around by creating the zones on the place of the pad and then deleting the footprint

Sorry for the late reply, Thank you for your help!