diff options
Diffstat (limited to 'tools/prune_copper.py')
| -rw-r--r-- | tools/prune_copper.py | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/tools/prune_copper.py b/tools/prune_copper.py new file mode 100644 index 0000000..acac211 --- /dev/null +++ b/tools/prune_copper.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python3 +"""Remove footprints, optionally cut the board outline down, and delete the copper left dangling. + +Run with KiCad's bundled Python: + + $KPY tools/prune_copper.py board.kicad_pcb [out.kicad_pcb] [--remove J3,J4] [--clip X0,Y0,X1,Y1] + [--netlist board.net] + + --remove footprints to delete + --clip new rectangular board outline in mm: Edge.Cuts is replaced, zone outlines are + intersected with it, and tracks/vias reaching outside it are deleted + --netlist rename nets to the names in a kicad-cli netlist, matched by pad (e.g. after root-sheet + labels were removed and the nets take their hierarchical names) + +Then dangling copper is pruned until nothing changes, like KiCad's "Cleanup Tracks & Vias": +zones are refilled, connectivity rebuilt, and every track or via that DRC would call dangling +(KiCad's TestTrackEndpointDangling) is deleted. Plane drops and stitching vias in fills stay. + +pcbnew.SaveBoard rewrites the .kicad_pro; it is copied before and restored after. Close the +board in KiCad before writing to its own path. +""" +import argparse +import os +import re + +import pcbnew + + +def rename(b, netlist): + """Give every net the name the schematic now has, matched by the pads on it.""" + want = {} + text = open(netlist).read() + for chunk in re.split(r'\(net\s', text[text.index("(nets"):])[1:]: + name = re.search(r'\(name "((?:[^"\\]|\\.)*)"\)', chunk).group(1) + for ref, pin in re.findall(r'\(node\s+\(ref "([^"]+)"\)\s+\(pin "([^"]+)"\)', chunk): + want[(ref, pin)] = name + old2new = {} + for f in b.GetFootprints(): + for p in f.Pads(): + new = want.get((f.GetReference(), p.GetNumber())) + if new and p.GetNetname() and new != p.GetNetname(): + old2new[p.GetNetname()] = new + nets = b.GetNetsByName() + made = {} + for old, new in old2new.items(): + if new not in nets and new not in made: + made[new] = pcbnew.NETINFO_ITEM(b, new) + b.Add(made[new]) + def net(name): + return made.get(name) or b.GetNetsByName()[name] + for f in b.GetFootprints(): + for p in f.Pads(): + if p.GetNetname() in old2new: + p.SetNet(net(old2new[p.GetNetname()])) + for t in list(b.GetTracks()) + list(b.Zones()): + if t.GetNetname() in old2new: + t.SetNet(net(old2new[t.GetNetname()])) + print("renamed %d nets" % len(old2new)) + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) + ap.add_argument("board") + ap.add_argument("out", nargs="?") + ap.add_argument("--remove", default="") + ap.add_argument("--clip") + ap.add_argument("--netlist", help="kicad-cli netlist: rename nets to the schematic's names (pads, tracks, vias, zones)") + a = ap.parse_args() + out = a.out or a.board + b = pcbnew.LoadBoard(a.board) + edges = [d for d in b.GetDrawings() if d.GetLayer() == pcbnew.Edge_Cuts] + + for ref in filter(None, a.remove.split(",")): + b.Delete(b.FindFootprintByReference(ref)) # Delete, not Remove: Remove() on a footprint breaks Tracks()/Zones() afterwards + + if a.netlist: + rename(b, a.netlist) + + if a.clip: + x0, y0, x1, y1 = (float(v) for v in a.clip.split(",")) + for d in edges: + b.Delete(d) + r = pcbnew.PCB_SHAPE(b) + r.SetShape(pcbnew.SHAPE_T_RECT) + r.SetStart(pcbnew.VECTOR2I_MM(x0, y0)) + r.SetEnd(pcbnew.VECTOR2I_MM(x1, y1)) + r.SetLayer(pcbnew.Edge_Cuts) + r.SetWidth(pcbnew.FromMM(0.1)) + b.Add(r) + rect = pcbnew.SHAPE_POLY_SET() + rect.NewOutline() + for x, y in ((x0, y0), (x1, y0), (x1, y1), (x0, y1)): + rect.Append(pcbnew.FromMM(x), pcbnew.FromMM(y)) + for z in b.Zones(): + o = z.Outline() + o.BooleanIntersection(rect) + box = pcbnew.BOX2I(pcbnew.VECTOR2I_MM(x0, y0), pcbnew.VECTOR2I_MM(x1 - x0, y1 - y0)) + for t in list(b.GetTracks()): + if not box.Contains(t.GetBoundingBox()): + b.Delete(t) + + filler = pcbnew.ZONE_FILLER(b) + removed = [0, 0] + while True: + filler.Fill(b.Zones()) + b.BuildConnectivity() + conn = b.GetConnectivity() + kill = [t for t in b.GetTracks() if conn.TestTrackEndpointDangling(t, False)] + if not kill: + break + for t in kill: + removed[t.GetClass() == "PCB_VIA"] += 1 + b.Delete(t) + + filler.Fill(b.Zones()) + pro = out[:-len(".kicad_pcb")] + ".kicad_pro" + keep = open(pro).read() if os.path.exists(pro) else None + pcbnew.SaveBoard(out, b) + if keep is not None: + open(pro, "w").write(keep) + print("removed %d tracks, %d vias; %d tracks, %d vias left" % (removed[0], removed[1], + sum(1 for t in b.GetTracks() if t.GetClass() == "PCB_TRACK"), + sum(1 for t in b.GetTracks() if t.GetClass() == "PCB_VIA"))) + + +if __name__ == "__main__": + main() |
