I am running the interactive python shell from within Pcbnew, and am able to modify board elements. Two issues:
How do I “update” or “refresh” the board from the script? If I move a module it does not move on the screen (until I click on it). If I add a track or a pad, it does not appear on the screen, but it is present in the pcbnew.GetBoard() object
Is there a way to save an undo/redo action? I would like to programatically make save-points as I go.
The Python API that we currently have lacks a lot of features - it’s a work in progress and will be changed and extended in the future.
Concerning the undo & view update stuff (the annoying F9/F11 thing you mentioned): we are going to expose the COMMIT class to Python. Commits let you update the BOARD and the associated views/observers in an atomic way as well as create undo buffer entries. For example:
# modification of a via diameter and adding a track. "via" comes from the existing BOARD object, track is a new item created by the script
commit = pcbnew.COMMIT()
# inform PCBnew that we're going to modify the via object
commit.Modify(via)
via.SetDiameter()
track = TRACK()
track.SetStart(...)
track.SetEnd(...)
# add a new item (track) to the commit
commit.Add(track)
# push the commit - this will update the board view, connectivity (ratsnest), commit changes to the BOARD object and create an entry in the undo buffer
commit.Push("Change a via and add a new track")
Also, I’m considering exposing functions for manipulating the selections in PCBnew - initially a few functions to add/remove items to the selection and retrieve the current selection. With these, I hope, script development will be much easier.
I’ll try to push the changes to the nightly build within a couple of weeks.
The changes that you outline are literally all that I need for the grand ideas that I have in mind. I look forward to seeing your changes committed to nightly.
twl’s response probably addresses the elements not appearing until canvas change. But just for reference, here are some posts and bugs that are related: