main
John Lauer Full write-up: design, simulation (ngspice and TI PSpice), sourcing, board, thermal and current, scaffold with probe pl… ccbfcc5
#!/usr/bin/env python3
"""Rewrite every footprint's 3D model binding in the board from MODELS below (portable ${KIPRJMOD} paths).

Model source order used (adom-aiflow components step): the part's Adom wiki component page STEP,
else the manufacturer's own STEP, else KiCad's library model for the standard package, else an
AI-created model from the datasheet. Y-up vendor models get a -90 degree X rotation (checked in the
native KiCad 3D viewer).

Run: python3 tools/set_models.py buck-12v5v-molecule.kicad_pcb  -> rewrites in place, prints a table
"""
import json, sys, os
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
import sexp
from sexp import Sym as S

P = "${KIPRJMOD}/3d/"
YUP = (-90, 0, 0)
# ref-prefix/MPN -> (model path, offset xyz, rotate xyz, source)
BY_MPN = {
    "TPS54202DDCR": (P + "TPS54202DDCR.step", (0, 0, 0), (0, 0, 0), "wiki adom/tps54202ddcr"),
    "0402N680J500CT": (P + "0402N680J500CT.step", (0, 0, 0), (0, 0, 0), "wiki adom/0402n680j500ct"),
    "0603B104J500CT": (P + "0603B104J500CT.step", (0, 0, 0), (0, 0, 0), "wiki adom/0603b104j500ct"),
    "CL21A226MPQNNNE": (P + "CL21A226MPQNNNE.step", (0, 0, 0), YUP, "wiki adom/cl21a226mpqnnne (Y-up)"),
    "IN-S63BTG": (P + "IN-S63BTG.step", (0, 0, 0), (0, 0, 0), "wiki adom/in-s63btg"),
    "AMPLH5030S-150MT": (P + "AMPLH5030S-150MT_Abracon.step", (0, 0, 0.05), YUP, "Abracon Support/STEP/AMPLH5030S.STEP.zip (Y-up)"),
    "EEE-FK1V470P": (P + "EEE-FK1V470P_Panasonic_D.step", (0, 0, 0), YUP, "Panasonic CAD DS_Alumi_D_5.zip, size code D (Y-up)"),
    "MachinePinMediumShort": (P + "MachinePinMediumShort.step", (0, 0, 0), (0, 0, 0), "Adom KiCad Library 1.2.3"),
    "MachineContactMedium": (P + "MachineContactMedium.step", (0, 0, 0), (0, 0, 0), "Adom KiCad Library 1.2.3"),
}
KICAD_GENERIC = {"CC1206KKX7R8BB106": "no manufacturer CAD found (Yageo site search); KiCad C_1206_3216Metric library model"}


def prop(fp, name):
    for p in sexp.find(fp, "property"):
        if p[1] == name:
            return p[2]
    return ""


path = sys.argv[1]
b = sexp.parse(open(path, encoding="utf-8").read())
rows = []
for fp in sexp.find(b, "footprint"):
    ref, mpn = prop(fp, "Reference"), prop(fp, "MPN")
    if mpn.startswith("CR0402"):
        spec = (P + mpn + ".step", (0, 0, 0), (0, 0, 0), f"wiki adom/{mpn.lower()}")
    else:
        spec = BY_MPN.get(mpn)
    if spec is None:
        rows.append((ref, mpn, "unchanged", KICAD_GENERIC.get(mpn, "no model (bare copper)" if ref.startswith("TP") else "unchanged")))
        continue
    model, off, rot, src = spec
    fp[:] = [e for e in fp if not (isinstance(e, list) and e and e[0] == "model")]
    fp.append([S("model"), model, [S("offset"), [S("xyz")] + list(off)], [S("scale"), [S("xyz"), 1, 1, 1]],
               [S("rotate"), [S("xyz")] + list(rot)]])
    rows.append((ref, mpn, os.path.basename(model), src))
open(path, "w", encoding="utf-8").write(sexp.dump(b) + "\n")
for r in rows:
    print(f"{r[0]:5s} {r[1]:22s} {r[2]:34s} {r[3]}")