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
|
"""Add a grid of thermal vias to the exposed pads of the given footprints, after routing.
The routers (tools/router, tools/placer) refuse footprints with bottom-side pads, so the
KiCad "_ThermalVias" footprint variants cannot be used. Use the plain exposed-pad footprint,
keep B.Cu clear under the pad with a router cost zone (route.toml), and run this on the routed
board. Each via takes the pad's net; a via is skipped where it would come within the clearance
of B.Cu copper of another net or of another hole. Then the zones are refilled.
usage (KiCad's Python, from the repo root, board closed in KiCad):
.../Python.framework/Versions/3.9/bin/python3 tools/thermal_vias.py BOARD.kicad_pcb REF [REF ...] \
[--pitch 1.2] [--via 0.6] [--drill 0.3]
Notes: knowledge/tooling/thermal-vias.md
"""
import argparse
import os
import pcbnew
ap = argparse.ArgumentParser()
ap.add_argument("board")
ap.add_argument("refs", nargs="+")
ap.add_argument("--pitch", type=float, default=1.2)
ap.add_argument("--via", type=float, default=0.6)
ap.add_argument("--drill", type=float, default=0.3)
ap.add_argument("--clearance", type=float, default=0.2)
ap.add_argument("--hole", type=float, default=0.25, help="hole-to-hole minimum")
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)
mm = pcbnew.FromMM
R = mm(a.via / 2)
def exposed_pad(fp):
"""The footprint's largest SMD pad (the thermal pad)."""
pads = [p for p in fp.Pads() if p.GetAttribute() == pcbnew.PAD_ATTRIB_SMD]
return max(pads, key=lambda p: p.GetSizeX() * p.GetSizeY())
def holes():
for t in b.GetTracks():
if t.Type() == pcbnew.PCB_VIA_T:
yield t.GetPosition(), t.GetDrill() // 2
for fp in b.GetFootprints():
for p in fp.Pads():
if p.HasHole():
yield p.GetPosition(), max(p.GetDrillSizeX(), p.GetDrillSizeY()) // 2
def free(pos, net):
"""No B.Cu copper of another net within clearance, no hole closer than the hole rule."""
probe = pcbnew.SHAPE_CIRCLE(pos, R)
cl = mm(a.clearance)
for t in b.GetTracks():
if t.GetNetCode() != net and t.IsOnLayer(pcbnew.B_Cu):
if t.GetEffectiveShape(pcbnew.B_Cu).Collide(probe, cl):
return False
for fp in b.GetFootprints():
for p in fp.Pads():
if p.GetNetCode() != net and p.IsOnLayer(pcbnew.B_Cu):
if p.GetEffectiveShape(pcbnew.B_Cu).Collide(probe, cl):
return False
for c, r in holes():
if (c - pos).EuclideanNorm() < mm(a.drill / 2) + r + mm(a.hole):
return False
return True
total = 0
for ref in a.refs:
fp = b.FindFootprintByReference(ref)
assert fp, ref
pad = exposed_pad(fp)
net = pad.GetNetCode()
box = pad.GetBoundingBox() # thermal pads are rectangles; turned 90 degrees the box is still exact
inset = R + mm(0.1)
x0, x1 = box.GetLeft() + inset, box.GetRight() - inset
y0, y1 = box.GetTop() + inset, box.GetBottom() - inset
p = mm(a.pitch)
nx, ny = max(1, int((x1 - x0) // p) + 1), max(1, int((y1 - y0) // p) + 1)
ox, oy = (x0 + x1 - (nx - 1) * p) // 2, (y0 + y1 - (ny - 1) * p) // 2 # centre the grid
added = 0
for i in range(nx):
for j in range(ny):
pos = pcbnew.VECTOR2I(int(ox + i * p), int(oy + j * p))
if not pad.HitTest(pos) or not free(pos, net):
continue
v = pcbnew.PCB_VIA(b)
v.SetPosition(pos)
v.SetWidth(mm(a.via))
v.SetDrill(mm(a.drill))
v.SetNetCode(net)
b.Add(v)
added += 1
print("%s: %d vias on %s (grid %dx%d)" % (ref, added, pad.GetNetname(), nx, ny))
total += added
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("added", total)
|