main
Drew Owens Add KiCad 10 Spine PCB, STEP, Gerbers/BOM/CPL, and parametric mechanical CAD/STL/STEP e614ae8
#!/usr/bin/env python3
"""Verify and normalize ${KIPRJMOD}/3d/<step> 3D model bindings on every board footprint."""

from __future__ import annotations

import json
from pathlib import Path

import pcbnew

ROOT = Path(__file__).resolve().parent.parent
KICAD_DIR = ROOT / "kicad"
PROVENANCE_PATH = KICAD_DIR / "project-lib" / "provenance.json"
BOARD_PATH = KICAD_DIR / "microgoose-spine.kicad_pcb"


def main() -> None:
    prov = json.loads(PROVENANCE_PATH.read_text())
    mpn_to_step = {p["mpn"]: p["model"] for p in prov["parts"]}

    board = pcbnew.LoadBoard(str(BOARD_PATH))
    bound_count = 0
    for fp in board.GetFootprints():
        ref = fp.GetReference()
        if ref.startswith("TP"):
            want = "${KIPRJMOD}/3d/Adom_TestPoint_D0.5mm.step"
        else:
            mpn = fp.GetFieldText("MPN") if fp.HasField("MPN") else ""
            step_file = mpn_to_step.get(mpn)
            assert step_file, f"No 3D model in provenance.json for {ref} (MPN={mpn})"
            want = f"${{KIPRJMOD}}/3d/{step_file}"

        models = fp.Models()
        if models.size() == 0:
            m = pcbnew.FP_3DMODEL()
            m.m_Filename = want
            m.m_Show = True
            fp.Add3DModel(m)
        else:
            models[0].m_Filename = want
            models[0].m_Show = True

        local_step = KICAD_DIR / "3d" / want.split("/3d/", 1)[1]
        assert local_step.is_file(), f"Missing local STEP file {local_step} for {ref}"
        bound_count += 1

    pcbnew.SaveBoard(str(BOARD_PATH), board)
    print(f"OK: verified ${{KIPRJMOD}}/3d/<step> bindings on {bound_count} footprints in {BOARD_PATH}")


if __name__ == "__main__":
    main()