1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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))
|