"""Update a board from its schematic's netlist, like KiCad's Update PCB from Schematic, without the GUI. Footprints whose reference is no longer in the netlist are deleted (board-only ones such as housing posts stay); new symbols get their footprint from the library, placed at --at for the placer to move. Every footprint gets the netlist's value, LCSC/MPN/Datasheet/Description fields (and FT Rotation Offset for the JLC plugin), schematic path and pad nets. Footprint changes on existing parts are reported, not applied. With --strip-routing all tracks and vias go too (for a board that tools/pnr.sh routes again). Pours, outline, rule areas and positions of existing parts stay. usage (KiCad's Python, from the repo root, board closed in KiCad): kicad-cli sch export netlist -o /tmp/b.net board.kicad_sch .../Python.framework/Versions/3.9/bin/python3 tools/sync_board.py /tmp/b.net board.kicad_pcb \ [--at X,Y] [--strip-routing] """ import argparse import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import pcbnew # noqa: E402 from sexpr import find, first, parse # noqa: E402 from kicad_paths import ROOT, FOOTPRINTS, SYMBOLS, MODELS3D # noqa: E402 KFP = FOOTPRINTS HWFP = os.path.join(ROOT, "libraries/footprints/HW.pretty") ap = argparse.ArgumentParser() ap.add_argument("netlist") ap.add_argument("board") ap.add_argument("--at", default=None, help="X,Y in mm for new footprints (default: board centre)") ap.add_argument("--strip-routing", action="store_true") a = ap.parse_args() net = parse(open(a.netlist).read()) comps = {} for c in find(first(net, "components"), "comp"): fields = {first(f, "name")[1]: f[2] for f in find(first(c, "fields") or [], "field") if len(f) > 2} sp = first(c, "sheetpath") comps[first(c, "ref")[1]] = dict(value=first(c, "value")[1], fp=first(c, "footprint")[1], path=first(sp, "tstamps")[1] + first(c, "tstamps")[1], sheet=first(sp, "names")[1], fields=fields) sheetfiles = {} for s in find(first(net, "design"), "sheet"): src = first(first(s, "title_block"), "source")[1] sheetfiles[first(s, "name")[1]] = os.path.basename(src) pinnet = {} for n in find(first(net, "nets"), "net"): for nd in find(n, "node"): pinnet[(first(nd, "ref")[1], first(nd, "pin")[1])] = first(n, "name")[1] pro = a.board[:-len(".kicad_pcb")] + ".kicad_pro" keep = open(pro).read() if os.path.exists(pro) else None b = pcbnew.LoadBoard(a.board) if a.at: at = pcbnew.VECTOR2I_MM(*map(float, a.at.split(","))) else: at = b.GetBoardEdgesBoundingBox().GetCenter() nets = {str(k): v for k, v in b.GetNetsByName().items()} def getnet(name): if name not in nets: ni = pcbnew.NETINFO_ITEM(b, name) b.Add(ni) nets[name] = ni return nets[name] have = {} gone = [] for fp in b.GetFootprints(): ref = fp.GetReference() if ref not in comps and not fp.IsBoardOnly(): # board-only parts (housing posts) have no symbol gone.append(ref) for ref in gone: print("removed", ref) b.Delete(b.FindFootprintByReference(ref)) # Delete, not Remove: Remove() breaks the bindings (see prune_copper.py) for fp in b.GetFootprints(): have[fp.GetReference()] = fp for ref, c in sorted(comps.items()): fp = have.get(ref) nick, name = c["fp"].split(":") if fp is None: fp = pcbnew.FootprintLoad(HWFP if nick == "HW" else os.path.join(KFP, nick + ".pretty"), name) assert fp, c["fp"] fp.SetFPID(pcbnew.LIB_ID(nick, name)) b.Add(fp) fp.SetReference(ref) fp.SetPosition(at) fp.Reference().SetTextSize(pcbnew.VECTOR2I_MM(0.8, 0.8)) fp.Reference().SetTextThickness(pcbnew.FromMM(0.12)) print("added", ref, c["value"], c["fp"]) elif fp.GetFPIDAsString() != c["fp"]: print("footprint differs, not changed:", ref, fp.GetFPIDAsString(), "->", c["fp"]) fp.SetValue(c["value"]) for k in ("LCSC", "MPN", "FT Rotation Offset"): # the last one: JLC plugin CPL correction if k in c["fields"]: fp.SetField(k, c["fields"][k]) fp.GetField(k).SetVisible(False) for k in ("Datasheet", "Description"): if k in c["fields"]: fp.GetField(k).SetText(c["fields"][k]) fp.SetPath(pcbnew.KIID_PATH(c["path"])) fp.SetSheetname(c["sheet"].strip("/") or "Root") if c["sheet"] in sheetfiles: fp.SetSheetfile(sheetfiles[c["sheet"]]) for pad in fp.Pads(): n = pinnet.get((ref, pad.GetNumber())) old = pad.GetNetname() if n and n != old: print("net %s.%s: %s -> %s" % (ref, pad.GetNumber(), old or "-", n)) pad.SetNet(getnet(n) if n else b.FindNet(0)) if a.strip_routing: tr = list(b.GetTracks()) for t in tr: b.Delete(t) print("removed %d tracks and vias" % len(tr)) pcbnew.SaveBoard(a.board, b) if keep is not None: open(pro, "w").write(keep) # SaveBoard resets the project's net settings