diff options
| author | Louis <louis@dnge.de> | 2026-11-11 10:08:41 +0100 |
|---|---|---|
| committer | Louis <louis@dnge.de> | 2026-11-11 10:08:41 +0100 |
| commit | 2e433fcd0d1afadd8ab786dd30d7d4546bdee43d (patch) | |
| tree | f2a5603703d56b08c1eaa672eaf529ec4b2cc3fa /tools/placement_slots.py | |
| download | hw-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/placement_slots.py')
| -rw-r--r-- | tools/placement_slots.py | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/tools/placement_slots.py b/tools/placement_slots.py new file mode 100644 index 0000000..ec49adb --- /dev/null +++ b/tools/placement_slots.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python3 +"""Assign same-size parts to fixed slots by hill-climbing placement_check's penalties. + +Hand placement sets the slots (a row of 0603s under an IC, a cap column); this script only +decides which part sits in which slot and which way it faces. Run with KiCad's Python: + + $KPY tools/placement_slots.py board.kicad_pcb slots.json [--out moved.kicad_pcb] + +slots.json: + {"ignore": ["J3"], # parts to leave out, as for placement_check + "groups": [{"parts": ["C20", "R12", ...], # parts that may trade places + "spares": [[131.7, 114.1, 270], ...], # empty slots they may move to (optional) + "flip": true}], # also try each part turned 180 deg + "turns": {"U4": [90, 180, 270]}} # extra in-place turns to try + +Cost = ratsnest crossings + 0.02 x ratsnest mm + 3 per decoupling cap over its limit + 1 per +uncovered IC power pin + 10 per courtyard conflict (see placement_check.py). Moves are +applied while they lower the cost; the result is printed as "REF": (x, y, rot) lines. +Look at the result before using it: the ratsnest cannot see routing channels. +""" +import argparse +import itertools +import json +import os +import sys + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import pcbnew # noqa: E402 +from placement_check import DEFAULT_FILL, Check # noqa: E402 + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) + ap.add_argument("board") + ap.add_argument("slots") + ap.add_argument("--out", help="save a copy of the board with the result") + a = ap.parse_args() + cfg = json.load(open(a.slots)) + board = pcbnew.LoadBoard(a.board) + F = {f.GetReference(): f for f in board.GetFootprints()} + ignore = cfg.get("ignore", []) + groups, spare = [], {} + for g in cfg["groups"]: + members = list(g["parts"]) + for k, s in enumerate(g.get("spares", [])): + key = "_spare%d_%d" % (len(groups), k) + spare[key] = (pcbnew.FromMM(s[0]), pcbnew.FromMM(s[1]), s[2]) + members.append(key) + groups.append((members, g.get("flip", True))) + turns = {r: t for r, t in cfg.get("turns", {}).items()} + for members, flip in groups: + if flip: + for r in members: + if not r.startswith("_spare"): + turns.setdefault(r, [180]) + + def cost(): + c = Check(board, DEFAULT_FILL, ignore) + r = c.ratsnest() + caps, unc = c.decoupling() + return (len(c.crossings(r)) + 3 * sum(1 for k in caps if not k["ok"]) + len(unc) + + 0.02 * sum(e[3] for e in r) + 10 * len(c.courtyards())) + + def pose(ref): + if ref in spare: + return spare[ref] + p = F[ref].GetPosition() + return (p.x, p.y, F[ref].GetOrientationDegrees()) + + def put(ref, p): + if ref in spare: + spare[ref] = p + else: + F[ref].SetPosition(pcbnew.VECTOR2I(int(p[0]), int(p[1]))) + F[ref].SetOrientationDegrees(p[2]) + + def swap(x, y): + px, py = pose(x), pose(y) + put(x, (py[0], py[1], px[2])) + put(y, (px[0], px[1], py[2])) + + best = cost() + print("start cost %.2f" % best) + while True: + found = None + for members, _ in groups: + for x, y in itertools.combinations(members, 2): + if x in spare and y in spare: + continue + swap(x, y) + c = cost() + swap(x, y) + if c < best - 1e-6 and (found is None or c < found[0]): + found = (c, ("swap", x, y)) + for r, ts in turns.items(): + for t in ts: + p = pose(r) + put(r, (p[0], p[1], (p[2] + t) % 360)) + c = cost() + put(r, p) + if c < best - 1e-6 and (found is None or c < found[0]): + found = (c, ("turn", r, t)) + if not found: + break + best, m = found + if m[0] == "swap": + swap(m[1], m[2]) + else: + p = pose(m[1]) + put(m[1], (p[0], p[1], (p[2] + m[2]) % 360)) + print("%.2f %s" % (best, " ".join(map(str, m)))) + refs = [r for members, _ in groups for r in members if r not in spare] + [r for r in turns if r not in spare] + for r in dict.fromkeys(refs): + p = F[r].GetPosition() + print(' "%s": (%g, %g, %d),' % (r, round(p.x / 1e6, 3), round(p.y / 1e6, 3), round(F[r].GetOrientationDegrees()) % 360)) + if a.out: + pcbnew.SaveBoard(a.out, board) + + +if __name__ == "__main__": + main() |
