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
|
"""Export what the router needs from a saved .kicad_pcb into JSON.
Run with KiCad's bundled Python (pcbnew); the board's .kicad_pro is read for net classes:
$KPY tools/router/dump_board.py board.kicad_pcb board.json
Output keys:
bounds [x0, y0, x1, y1] of Edge.Cuts (mm)
pads ref, num, net, x, y, drill, F / B (copper outline on that layer or null), npth
fps ref -> x, y, rot, value
netclass net -> class name; classes: name -> track width, via diameter, clearance
fpcopper footprint copper that is not a pad (ref, layer, poly)
keepouts polygons of rule areas that forbid tracks (board and footprint rule areas)
keepout_refs footprint reference owning each keepout ("" for board rule areas), parallel to keepouts
zones copper pours (not rule areas): net, layer, pts (outline), one entry per copper layer
tracks / vias existing copper, so a run can keep it (Router.load_existing)
"""
import json
import sys
import pcbnew
MM = 1e6
def outline(ps):
if ps.OutlineCount() == 0:
return None
best = max(range(ps.OutlineCount()), key=lambda k: ps.Outline(k).Area())
ol = ps.Outline(best)
return [[ol.CPoint(i).x / MM, ol.CPoint(i).y / MM] for i in range(ol.PointCount())]
def shape_poly(item, layer):
ps = pcbnew.SHAPE_POLY_SET()
item.TransformShapeToPolygon(ps, layer, 0, pcbnew.FromMM(0.005), pcbnew.ERROR_INSIDE)
return outline(ps)
def main(src, out):
b = pcbnew.LoadBoard(src)
bb = b.GetBoardEdgesBoundingBox()
d = dict(bounds=[bb.GetLeft() / MM, bb.GetTop() / MM, bb.GetRight() / MM, bb.GetBottom() / MM],
pads=[], fps={}, netclass={}, classes={}, fpcopper=[], keepouts=[], keepout_refs=[], zones=[], tracks=[], vias=[])
for f in b.GetFootprints():
ref = f.GetReference()
d["fps"][ref] = dict(x=f.GetPosition().x / MM, y=f.GetPosition().y / MM, rot=f.GetOrientationDegrees(), value=f.GetValue())
for p in f.Pads():
npth = p.GetAttribute() == pcbnew.PAD_ATTRIB_NPTH
drill = p.GetDrillSize().x / MM if p.HasHole() else 0.0
cu = {}
for key, layer in (("F", pcbnew.F_Cu), ("B", pcbnew.B_Cu)):
cu[key] = None if npth or not p.IsOnLayer(layer) else shape_poly(p, layer)
d["pads"].append(dict(ref=ref, num=p.GetNumber(), net=p.GetNetname(), x=p.GetPosition().x / MM,
y=p.GetPosition().y / MM, drill=drill, npth=npth, **cu))
for g in f.GraphicalItems():
for key, layer in (("F", pcbnew.F_Cu), ("B", pcbnew.B_Cu)):
if g.GetLayer() == layer:
poly = shape_poly(g, layer)
if poly:
d["fpcopper"].append(dict(ref=ref, layer=key, poly=poly))
for z in f.Zones():
if z.GetIsRuleArea() and z.GetDoNotAllowTracks():
d["keepouts"].append(outline(z.Outline()))
d["keepout_refs"].append(ref)
for z in b.Zones():
if z.GetIsRuleArea() and z.GetDoNotAllowTracks():
d["keepouts"].append(outline(z.Outline()))
d["keepout_refs"].append("")
for z in b.Zones():
if z.GetIsRuleArea():
continue
for key, layer in (("F.Cu", pcbnew.F_Cu), ("B.Cu", pcbnew.B_Cu)):
if z.IsOnLayer(layer):
d["zones"].append(dict(net=z.GetNetname(), layer=key, pts=outline(z.Outline())))
for name, ni in b.GetNetInfo().NetsByName().items():
name = str(name)
if name:
d["netclass"][name] = ni.GetNetClassName()
pro = json.load(open(src[:-len(".kicad_pcb")] + ".kicad_pro"))
for c in pro["net_settings"]["classes"]:
d["classes"][c["name"]] = dict(track=c["track_width"], via=c["via_diameter"], drill=c["via_drill"], clearance=c["clearance"])
for t in b.GetTracks():
if t.GetClass() == "PCB_VIA":
d["vias"].append(dict(net=t.GetNetname(), x=t.GetPosition().x / MM, y=t.GetPosition().y / MM,
d=t.GetWidth(pcbnew.F_Cu) / MM, drill=t.GetDrillValue() / MM))
elif t.GetClass() == "PCB_TRACK":
d["tracks"].append(dict(net=t.GetNetname(), layer=b.GetLayerName(t.GetLayer()), x1=t.GetStart().x / MM,
y1=t.GetStart().y / MM, x2=t.GetEnd().x / MM, y2=t.GetEnd().y / MM, w=t.GetWidth() / MM))
# KiCad orders footprints by UUID, which a regenerated board draws anew: sort, so the same
# placement always gives the same dump and the router (order-sensitive) the same result
d["pads"].sort(key=lambda p: (p["ref"], p["num"], p["x"], p["y"]))
d["fps"] = dict(sorted(d["fps"].items()))
for k in ("fpcopper", "zones", "tracks", "vias"):
d[k].sort(key=json.dumps)
pairs = sorted(zip(d["keepouts"], d["keepout_refs"]), key=lambda kr: json.dumps(kr[0]))
d["keepouts"] = [k for k, _ in pairs]
d["keepout_refs"] = [r for _, r in pairs]
json.dump(d, open(out, "w"))
print("pads %d, footprints %d, keepouts %d, fp copper %d, zones %d, tracks %d, vias %d, bounds %s"
% (len(d["pads"]), len(d["fps"]), len(d["keepouts"]), len(d["fpcopper"]), len(d["zones"]), len(d["tracks"]), len(d["vias"]), d["bounds"]))
if __name__ == "__main__":
main(*sys.argv[1:3])
|