aboutsummaryrefslogtreecommitdiff
path: root/tools/placer/apply.py
diff options
context:
space:
mode:
authorLouis <louis@dnge.de>2026-11-11 10:08:41 +0100
committerLouis <louis@dnge.de>2026-11-11 10:08:41 +0100
commit2e433fcd0d1afadd8ab786dd30d7d4546bdee43d (patch)
treef2a5603703d56b08c1eaa672eaf529ec4b2cc3fa /tools/placer/apply.py
downloadhw-toolchain-2e433fcd0d1afadd8ab786dd30d7d4546bdee43d.tar.gz
hw-toolchain: placer, routers and KiCad helpers for agent-driven hardware work
Rust placer (simulated annealing) and negotiated-congestion router for 2-layer KiCad boards, pnr.sh to place, route, DRC-check and install candidates, Python KiCad helpers, repo conventions (AGENTS.md) and the tool documentation under knowledge/. Konnect (KiCad MCP, AGPL) as a submodule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tools/placer/apply.py')
-rw-r--r--tools/placer/apply.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/tools/placer/apply.py b/tools/placer/apply.py
new file mode 100644
index 0000000..db080dc
--- /dev/null
+++ b/tools/placer/apply.py
@@ -0,0 +1,50 @@
+"""Write a placer result into a copy of a board: footprint positions and turns, no copper.
+
+Run with KiCad's bundled Python (pcbnew), with the source board closed or saved:
+
+ $KPY tools/placer/apply.py board.kicad_pcb placement.json out.kicad_pcb
+
+placement.json maps ref -> [x, y, rotation in degrees], at the top level or under
+"placement" (placer's rank_NN.json). Tracks and vias are deleted (the old routing belongs to
+the old placement); zones are kept and refilled by the router's write step. The board's
+.kicad_pro is copied next to the output, because pcbnew.SaveBoard would write default net
+settings into it (knowledge/tooling/kicad.md, pcbnew quirks).
+"""
+import json
+import os
+import shutil
+import sys
+
+import pcbnew
+
+
+def main(src, placement, out):
+ d = json.load(open(placement))
+ d = d.get("placement", d)
+ b = pcbnew.LoadBoard(src)
+ seen = set()
+ for f in b.GetFootprints():
+ r = f.GetReference()
+ if r in d:
+ x, y, rot = d[r]
+ f.SetPosition(pcbnew.VECTOR2I(pcbnew.FromMM(x), pcbnew.FromMM(y)))
+ f.SetOrientationDegrees(rot)
+ seen.add(r)
+ missing = set(d) - seen
+ if missing:
+ sys.exit("not on the board: %s" % ", ".join(sorted(missing)))
+ for t in list(b.GetTracks()):
+ b.Delete(t) # not Remove: it breaks the board lists (knowledge/tooling/kicad.md)
+ pro_src = src[:-len(".kicad_pcb")] + ".kicad_pro"
+ pro_out = out[:-len(".kicad_pcb")] + ".kicad_pro"
+ keep = open(pro_src).read()
+ pcbnew.SaveBoard(out, b)
+ open(pro_out, "w").write(keep)
+ dru = src[:-len(".kicad_pcb")] + ".kicad_dru"
+ if os.path.exists(dru):
+ shutil.copy(dru, out[:-len(".kicad_pcb")] + ".kicad_dru")
+ print("placed %d footprints -> %s" % (len(seen), out))
+
+
+if __name__ == "__main__":
+ main(*sys.argv[1:4])