From 2e433fcd0d1afadd8ab786dd30d7d4546bdee43d Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 11 Nov 2026 10:08:41 +0100 Subject: 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 --- tools/connector_tab.py | 141 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 tools/connector_tab.py (limited to 'tools/connector_tab.py') diff --git a/tools/connector_tab.py b/tools/connector_tab.py new file mode 100644 index 0000000..7fbd932 --- /dev/null +++ b/tools/connector_tab.py @@ -0,0 +1,141 @@ +"""Put edge connectors onto a connector tab: on a finished board (keeping the routing), or from a +module's gen_pcb.py (add_tabs). + +DIN housing rule (knowledge/topics/din-rail-format.md, edge connectors): where a connector sits at +the bottom edge, the PCB runs out as a tab (2.0 mm) into the shell's wall notch, as wide as the +connector bodies, and the mating face or wire entry ends flush with the tab edge. + +As a script, for each given footprint this moves it by --dy (+ = outward, toward the bottom edge), +extends every track that ended on one of its pads by a straight stub along the move (same layer and +width), then replaces the straight bottom edge under the connectors with a tab TAB deep: one tab per +group of connector bodies (F.Fab extent) closer than --merge. The zones are refilled and the +.kicad_pro that SaveBoard rewrites is restored. Tracks that ran where the pads now are show up in +DRC: run it. + +usage (KiCad's Python, from the repo root, board closed in KiCad): + .../Python.framework/Versions/3.9/bin/python3 tools/connector_tab.py BOARD.kicad_pcb REF [REF ...] \ + --dy 2.0 [--tab 2.0] [--merge 3.0] +--dy is how far the connector moves: 2.0 for a face that was flush with the edge, 0 with --tab 2.5 +for one that already sticks out 2.5 mm (e.g. an RJ45 jack). + +From gen_pcb.py: draw the plain rectangular outline, place the connectors with their faces TAB past +the block edge, then `add_tabs(board, ["J1", ...], TAB)` before saving. +Notes: knowledge/tooling/connector-tab.md +""" +import argparse +import os + +import pcbnew + +TAB, MERGE = 2.0, 3.0 +EPS = pcbnew.FromMM(0.001) + + +def add_tabs(b, refs, tab=TAB, merge=MERGE, side="bottom"): + """Split the board's straight bottom (or top) edge into a tab under the bodies of footprints `refs`.""" + mm, tomm = pcbnew.FromMM, pcbnew.ToMM + fps = {f.GetReference(): f for f in b.GetFootprints()} + missing = [r for r in refs if r not in fps] + assert not missing, "not on the board: %s" % missing + + # body extents (F.Fab), merged into tab intervals: bodies closer than `merge` share one tab and + # one wall notch, so the printed shell gets no thin webs + spans = [] + for r in refs: + # bounding boxes include half the line width: take it off, the body edge is the line's centre + xs = [x for g in fps[r].GraphicalItems() if g.GetLayer() == pcbnew.F_Fab and g.GetClass() != "PCB_TEXT" + for x in (g.GetBoundingBox().GetLeft() + g.GetWidth() // 2, + g.GetBoundingBox().GetRight() - g.GetWidth() // 2)] + assert xs, "%s has no F.Fab outline" % r + spans.append([min(xs), max(xs)]) + spans.sort() + tabs = [spans[0]] + for x0, x1 in spans[1:]: + if x0 - tabs[-1][1] < mm(merge): + tabs[-1][1] = max(tabs[-1][1], x1) + else: + tabs.append([x0, x1]) + + # the bottom edge: the lowest horizontal Edge.Cuts segment(s); each tab splits the one it lies on + # (other outline shapes, e.g. rounded corners, stay as they are). side="top": the highest one, + # for the top row of a base board, with the tab going up + edges = [d for d in b.GetDrawings() if d.GetLayer() == pcbnew.Edge_Cuts and d.GetShape() == pcbnew.SHAPE_T_SEGMENT] + if side == "top": + yb = min(min(d.GetStart().y, d.GetEnd().y) for d in edges) + T = -mm(tab) + else: + yb = max(max(d.GetStart().y, d.GetEnd().y) for d in edges) + T = mm(tab) + for x0, x1 in tabs: + seg = [d for d in edges if abs(d.GetStart().y - yb) < EPS and abs(d.GetEnd().y - yb) < EPS + and min(d.GetStart().x, d.GetEnd().x) <= x0 + EPS and max(d.GetStart().x, d.GetEnd().x) >= x1 - EPS] + assert len(seg) == 1, "no single %s edge segment under %.2f..%.2f mm" % (side, tomm(x0), tomm(x1)) + d = seg[0] + xa, xb = sorted((d.GetStart().x, d.GetEnd().x)) + pts = [(xa, yb), (x0, yb), (x0, yb + T), (x1, yb + T), (x1, yb), (xb, yb)] + width = d.GetWidth() + b.Remove(d) + edges.remove(d) + for p, q in zip(pts, pts[1:]): + if p == q: + continue + s = pcbnew.PCB_SHAPE(b) + s.SetShape(pcbnew.SHAPE_T_SEGMENT) + s.SetStart(pcbnew.VECTOR2I(p[0], p[1])) + s.SetEnd(pcbnew.VECTOR2I(q[0], q[1])) + s.SetLayer(pcbnew.Edge_Cuts) + s.SetWidth(width) + b.Add(s) + edges.append(s) + print("tab x %.2f..%.2f, y %.2f..%.2f" % (tomm(x0), tomm(x1), tomm(min(yb, yb + T)), tomm(max(yb, yb + T)))) + + +def move_with_stubs(b, refs, dy): + """Move footprints `refs` by dy mm; extend each track that ended on one of their pads.""" + DY = pcbnew.FromMM(dy) + fps = {f.GetReference(): f for f in b.GetFootprints()} + stubs = {} # one stub per end point and layer (several tracks can meet on a pad), the widest + for r in refs: + for pad in fps[r].Pads(): + for t in b.GetTracks(): + if t.GetClass() != "PCB_TRACK" or t.GetNetCode() != pad.GetNetCode(): + continue + for end in (t.GetStart(), t.GetEnd()): + if pad.IsOnLayer(t.GetLayer()) and pad.HitTest(end): + k = (end.x, end.y, t.GetLayer()) + if k not in stubs or t.GetWidth() > stubs[k][1]: + stubs[k] = (t.GetNet(), t.GetWidth()) + for r in refs: + fps[r].Move(pcbnew.VECTOR2I(0, DY)) + if not DY: + return 0 + for (x, y, layer), (net, width) in stubs.items(): + s = pcbnew.PCB_TRACK(b) + s.SetStart(pcbnew.VECTOR2I(x, y)) + s.SetEnd(pcbnew.VECTOR2I(x, y + DY)) + s.SetLayer(layer) + s.SetWidth(width) + s.SetNet(net) + b.Add(s) + return len(stubs) + + +if __name__ == "__main__": + ap = argparse.ArgumentParser() + ap.add_argument("board") + ap.add_argument("refs", nargs="+") + ap.add_argument("--dy", type=float, required=True) + ap.add_argument("--tab", type=float, default=TAB) + ap.add_argument("--merge", type=float, default=MERGE, help="bodies closer than this share one tab") + a = ap.parse_args() + + pro = a.board[:-len(".kicad_pcb")] + ".kicad_pro" + keep = open(pro).read() if os.path.exists(pro) else None + b = pcbnew.LoadBoard(a.board) + n = move_with_stubs(b, a.refs, a.dy) + add_tabs(b, a.refs, a.tab, a.merge) + pcbnew.ZONE_FILLER(b).Fill(b.Zones()) + pcbnew.SaveBoard(a.board, b) + if keep is not None: + open(pro, "w").write(keep) # SaveBoard resets the project's net settings + print("moved %s by %.2f mm, %d stubs" % (" ".join(a.refs), a.dy, n)) -- cgit v1.3