Workflow with Python boards

I’m wondering what the proper workflow is to create boards, place footprints, add tracks, etc… with Python in Pcbnew. I’m using Windows, so I can only source scripts from the Pcbnew scripting console. I want to create a new board and place components, and periodically run the script to make a board from scratch see how my math is working out. To get to the scripting console, I need to start a blank Pcbnew project. If I run

board = pcbnew.GetBoard()
mod = pcb.FootprintLoad(lib_location, "RF_SMA_Vertical_5-1814832-1");
position1 = pcbnew.wxPoint(0,0);
mod.SetPosition(position1)
board.Add(mod)
pcbnew.Refresh()
board.Save(new_filename)
pcbnew.Refresh()
os.startfile(new_filename)

Then I see the existing empty board get populated, and the “new_filename” board opens up as well with the part populated. If I close “new_filename” and run the script again, I get a duplicate part placed on top of the original file. This leads me to believe that GetBoard() is just always getting the board from which the scripting console was opened from.

I want to be able to run this script again and again and start from scratch every time. Once I save “new_filename”, I’ve also tried doing

board = pcbnew.LoadBoard(new_filename)
board.Save(filename)

But I get the same issue. I don’t see a way to display the new board without saving it first, which leads to the overwriting of parts issue.

I see this script here which includes these lines:

for t in pcb.GetTracks():
        pcb.Delete(t)
for d in pcb.GetDrawings():
	pcb.Remove(d)

However, pcb.GetDrawings() returns nothing for me. Does it only return schematic drawings? The docs have a function Board.Footprints() here which look like they should return all the footprints on a board. But my version (5.1.3) doesn’t seem to have that function. And the docs page seems to be based on an even earlier version (4.0.0 according to this header here).

So to summarize, is there an easy workflow to be able to adjust an entire script while continually running it as you go, without getting all your parts overlapped on the old versions?

Just so you know, you don’t have to, you can just launch python.exe from kicad\bin and do import pcbnew and do whatever. You don’t have to start a blank pcbnew project first.

That’s right. If you want a completely new board just do board = pcbnew.BOARD(). Unlike GetBoard() this also works in standalone python mode that I described above.

If you are working on a script, your goal is to quickly iterate and view the result of your modifications starting from scratch every time then I would create a board using pcbnew.BOARD(), modify it, save it and then launch pcbnew.exe with path to file as argument right from the script to instantly view the result.

Yes, footprints are not drawings, they are more of a submodule, which is why they also used to be called MODULE prior to v6 and which is why you need to get them using GetModules() in v5.1.

That comment refers to swig version, not kicad version. Documentation you linked is for nightly (future v6). For v5.1 documentation see here https://kicad-downloads.s3.cern.ch/doxygen-python/namespacepcbnew.html

2 Likes

Very helpful post. Thank you so much! That answers pretty much all my questions. And I like the Warrior Within profile pic.

1 Like

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