Skip to content

Instantly share code, notes, and snippets.

@td2sk
Created January 25, 2023 15:43
Show Gist options
  • Save td2sk/3edb0ae812320299e51c150e008ef0a2 to your computer and use it in GitHub Desktop.
Save td2sk/3edb0ae812320299e51c150e008ef0a2 to your computer and use it in GitHub Desktop.
Grid placement in Kicad PCB editor
import pcbnew
# index (1-origin) to Reference Text
def diode(index: int) -> str:
return f"D{index}"
def switch(index: int) -> str:
return f"SW{index}"
def led(index: int) -> int:
i = [13, 14, 15, 16, 9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4][index - 1]
return f"LED{i}"
def move(target: pcbnew.FOOTPRINT, base: pcbnew.FOOTPRINT, dx: int, dy: int):
d = pcbnew.wxPointMM(dx, dy)
target.SetPosition(base.GetPosition())
target.Move(d)
target.SetOrientation(base.GetOrientation())
target.Reference().SetPosition(base.Reference().GetPosition())
target.Reference().Move(d)
target.Value().SetPosition(base.Value().GetPosition())
target.Value().Move(d)
dx = dy = 19
board: pcbnew.BOARD = pcbnew.GetBoard()
# Index 1 footprints should be placed in the PCB editor beforehand.
base_switch: pcbnew.FOOTPRINT = board.FindFootprintByReference(switch(1))
base_diode: pcbnew.FOOTPRINT = board.FindFootprintByReference(diode(1))
base_led: pcbnew.FOOTPRINT = board.FindFootprintByReference(led(1))
# Other footprints are placed relative to index 1 footprint.
for i in range(2, 17):
x = (i - 1) % 4
y = (i - 1) // 4
s: pcbnew.FOOTPRINT = board.FindFootprintByReference(switch(i))
d: pcbnew.FOOTPRINT = board.FindFootprintByReference(diode(i))
l: pcbnew.FOOTPRINT = board.FindFootprintByReference(led(i))
move(s, base_switch, x * dx, y * dy)
move(d, base_diode, x * dx, y * dy)
move(l, base_led, x * dx, y * dy)
pcbnew.Refresh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment