Skip to content

Instantly share code, notes, and snippets.

@mcbridejc
Created April 10, 2022 18:04
Show Gist options
  • Save mcbridejc/abeeb7976d6d89cd2c0c80fd3a1ab94f to your computer and use it in GitHub Desktop.
Save mcbridejc/abeeb7976d6d89cd2c0c80fd3a1ab94f to your computer and use it in GitHub Desktop.
How to group KiCad tracks created in python
"""
When creating a lot of board features (tracks, vias, etc) from a python script,
grouping them allows them to be easily selected together in pcbnew. This can be
useful if, for exampe, you want to move them together after generating, or if
you find yourself iterating and need to delete the old tracks each time before
re-running the script.
Luckily, it's easy to create a group and assign it to elements as they are
created in python; here's a quick example.
"""
import pcbnew
def pcbpoint(p):
return pcbnew.wxPointMM(float(p[0]), float(p[1]))
# Get the board. This assume you are running in pcbnew with a board already
# open. It's also possible to use LoadBoard to open a .kicad_pcb file.
board = pcbnew.GetBoard()
# Create a new group, and add it to the board
group = pcbnew.PCB_GROUP(board)
board.Add(group)
# Create a new element, and add it to the group (as well as the board)
track = pcbnew.PCB_TRACK(board)
track.SetStart(pcbnew.wxPointMM(100, 100))
track.SetEnd(pcbnew.wxPointMM(100, 110))
track.SetWidth(int(0.2 * 1e6))
track.SetLayer(pcbnew.F_Cu)
board.Add(track)
group.AddItem(track)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment