main
John Lauer U1: part number along the package length (re-etched flat white mark, outline name rot 90, upright with pins 1 to 3 towa… 45864ee
#!/usr/bin/env python3
"""Build U1's Adom-style symbol (TPS54202DDCR) into ../../lib/buck12v5v.kicad_sym.

1. `adom-symbol create` from TPS54202DDCR-create.json (2-column Adom style: Input VIN/EN and Ground
   GND on the left, Output BOOT/SW/FB on the right) with the white 3D-chip outline baked into the
   body. The outline is service-step2glb's /outline (OCCT HLR) of the plain STEP with the part number as
   flat text at rot=90, the same line, direction and size as the board's etched U1 model, so the symbol and the
   3D chip carry one marking, not two. 2026-10-03 (John: the part number reads upright and left to right,
   the way a real part is marked): the service's iso camera looks at a KiCad-frame part from the pins 4-6
   side, which showed the mark upside down, so the STEP is first turned 180 deg about Z
   (adom-aiflow rotate_step_xcaf.py) and drawn with pins 1-3 and the pin-1 dot toward the camera; the
   etched mark (reading from the pin-1 dot end) then reads upright and the rot=90 name lies on it.
   Rule (John): the part number runs ALONG THE LONGEST top-face dimension (the 2.9 mm length, parallel
   to the pin rows), as large as fits, upright, left to right; rot=0/180 run across the 1.6 mm width
   and are never used. The etched model was re-etched to match (adom-aiflow-etch --along long
   --frac 0.75 --mark light; its mark and this name share the same extents in the outline). Earlier: `adom-chip-thumbnailer once TPS54202DDCR`, run on the STEP
   from the adom/tps54202ddcr wiki page (copied, never edited).
2. Snap it to the 50 mil grid: adom-symbol 1.2.6 puts the first pin row at y = -2.00 mm and the
   Ground group at -12.89 mm, off the 1.27 mm grid, so wires could not meet the pins on grid. The
   whole symbol is moved down 0.54 mm (rows land on -2.54 / -5.08 / -7.62) and the GND pin row
   another 0.54 mm (to -13.97). Footprint is set to the board's TSOT-23-6.
Pin numbers and names are exactly the TPS54202 DDC pinout used by the board and netlist.json.
"""
import os, subprocess, sys, tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", ".."))
import sexp
from sexp import Sym as S

out = tempfile.mkdtemp()
subprocess.run(["adom-symbol", "create", "--file", os.path.join(HERE, "TPS54202DDCR-create.json"), "--out", out,
                "--outline-svg", os.path.join(HERE, "TPS54202DDCR-3d-outline-dark-named90.svg"), "--outline-name", "none"], check=True)
lib = sexp.parse(open(os.path.join(out, "TPS54202DDCR.kicad_sym"), encoding="utf-8").read())
DY = -0.54


def r(v):
    return S(f"{round(float(v), 4):.2f}")


def shift(node):
    if not isinstance(node, list) or not node:
        return
    if node[0] in ("at", "xy", "start", "end", "center", "mid") and len(node) >= 3:
        node[2] = r(float(node[2]) + DY)
        node[1] = r(float(node[1]))
    for e in node[1:]:
        shift(e)


sym = sexp.first(lib, "symbol")
shift(sym)
for unit in sexp.find(sym, "symbol"):
    for p in sexp.find(unit, "pin"):
        at = sexp.first(p, "at")
        if sexp.first(p, "name")[1] == "GND":
            at[2] = r(float(at[2]) - 0.54)
        x, y = float(at[1]), float(at[2])
        assert abs(x / 1.27 - round(x / 1.27)) < 1e-6 and abs(y / 1.27 - round(y / 1.27)) < 1e-6, (x, y)
for p in sexp.find(sym, "property"):
    if p[1] == "Footprint":
        p[2] = "Package_TO_SOT_SMD:TSOT-23-6"
# same search keywords / footprint filter as KiCad's Regulator_Switching:TPS54202DDC (v1's U1), so
# the only netlist difference from v1 is U1's libsource
last = max(i for i, e in enumerate(sym) if isinstance(e, list) and e and e[0] == "property")
for k, v in (("ki_keywords", "switching buck converter power-supply voltage regulator emi spread spectrum"),
             ("ki_fp_filters", "SOT?23*")):
    sym.insert(last + 1, [S("property"), k, v, [S("at"), S("0"), S("0"), S("0")],
                          [S("effects"), [S("font"), [S("size"), S("1.27"), S("1.27")]], [S("hide"), S("yes")]]])
    last += 1
lib[2] = [S("generator"), "adom"]  # keep the adom generator tag
dst = os.path.join(HERE, "..", "..", "lib", "buck12v5v.kicad_sym")
if os.path.exists(dst):   # keep the other project symbols (L1 / C1 / D1 from tools/passive-symbols), replace U1 only
    old = sexp.parse(open(dst, encoding="utf-8").read())
    lib += [e for e in old if isinstance(e, list) and e and e[0] == "symbol" and e[1] != sym[1]]
open(dst, "w", encoding="utf-8").write(sexp.dump(lib) + "\n")
for f in ("TPS54202DDCR.svg", "TPS54202DDCR-viewer.html"):
    os.replace(os.path.join(out, f), os.path.join(HERE, f))
print("wrote", os.path.normpath(dst))