main
John Lauer Component record TPS16850VMAR: files batch 1 6db00ed 18h ago
#!/usr/bin/env python3
"""Hand fixes to the EAGLE/Fusion .lbr written by adom-lbr 2.47.5 (generate) for TPS16850VMAR:
  1. NC pins 11, 17, 21 come out direction="io"; set direction="nc" (KiCad type no_connect).
  2. The fab outline (KiCad fp_poly with fill none, F.Fab) comes out as a filled <polygon> on tDocu (51);
     replace it with wires so it stays an outline.
  3. Exposed pad 24 has cream="no" (its KiCad pad has no paste); add TI's six 0.98 x 1.04 mm stencil windows
     (SLVSHA1D p.56) as rectangles on tCream (31). EAGLE y = -KiCad y.
  4. Pin names: make sure every <pin> shows its name and number (visible="both"), the EAGLE default made explicit.
Copper is generated from tps16850vmar-lbr-input.kicad_mod (custom pads as exact rectangle unions), because
adom-lbr does not carry KiCad custom-pad primitives or paste-only pads.
usage: lbr_fix.py file.lbr fp-extra.json"""
import json, re, sys
p, extra = sys.argv[1], json.load(open(sys.argv[2]))
s = open(p).read()
n_nc = 0
def nc(m):
    global n_nc
    t = m.group(0)
    if re.search(r'name="NC(@\d+)?"', t):
        n_nc += 1; t = t.replace('direction="io"', 'direction="nc"')
    return t
s = re.sub(r'<pin [^>]*/>', nc, s)
s = re.sub(r'<pin ((?![^>]*visible=)[^>]*)/>', r'<pin \1 visible="both"/>', s)
fab = [(-2.5, -2.5), (3.0, -2.5), (3.0, 2.5), (-3.0, 2.5), (-3.0, -2.0)]
wires = "".join(f'  <wire x1="{a[0]:.4f}" y1="{-a[1]:.4f}" x2="{b[0]:.4f}" y2="{-b[1]:.4f}" width="0.1000" layer="51"/>\n'
                for a, b in zip(fab, fab[1:] + fab[:1]))
s, k = re.subn(r'  <polygon width="0.1000" layer="51">.*?</polygon>\n', wires, s, flags=re.S)
cream = "".join(f'  <rectangle x1="{x - w/2:.4f}" y1="{-y - h/2:.4f}" x2="{x + w/2:.4f}" y2="{-y + h/2:.4f}" layer="31"/>\n'
                for x, y, w, h in extra["ep_paste"])
i = s.find("</package>"); assert s.count("</package>") == 1
s = s[:i] + cream + s[i:]
open(p, "w").write(s)
print(f"OK {p}: {n_nc} NC pins -> nc, fab polygon replaced: {k}, 6 tCream windows, pins visible=both")