KiCAD PCBNew Scripting: Basic scripting beyond the built in terminal
2 minute read
The internal WX based scripting terminal in KiCAD’s PCBNew is as bare bones as it gets. You can use a normal interface with the LoadBoard function in the pcbnew scripting interface. There isn’t any documentation to speak of for the API but once you’re using the a improved interface the file structure is intuitive. I don’t know who did the naming and structure within the project but it is much better than most large APIs I’ve used (looking at you matplotlib). For power users the ability to automate tasks is astonishing so it’s worth an explore to see what can be done. For me the first step was to figure out how to get out of the integrated terminal.
import pcbnew
board = pcbnew.LoadBoard("demo.kicad_pcb")
According the the C++ API documentation we should be able to call the SettingsManager, set the project, and proceed exactly as if we were using the integrated terminal. I have not been able to get this to work. This is what I have tried which the C++ documentation seems to approve of :
import pcbnew
settings_manager = pcbnew.GetSettingsManager()
success = settings_manager.LoadProject("demo.kicad_pro")
assert(success) # This succeeds
board = pcbnew.GetBoard() # returns None
Interacting with the board without the GUI can be used for building proper unittests for plugins which I haven’t seen much.