main
John Lauer Add symbol, footprint, Fusion and Altium libraries, viewers, provenance, README ab389b2 4h ago
#!/usr/bin/env python3
"""Post-process an adom-symbol 1.2.6 .kicad_sym: put every pin end on the 2.54 mm grid (the tool places the first
pin of each column at y = -2.00), move each group caption with its first pin, set Value to the exact MPN and the
Footprint to the page's library footprint. usage: snap_symbol.py in.kicad_sym out.kicad_sym MPN LIB:FOOTPRINT"""
import re, sys
src, out, mpn, fp = sys.argv[1:5]
s = open(src).read()
g = 2.54
moves = {}
def pin(m):
    x, y = float(m.group(2)), float(m.group(3)); ny = round(y / g) * g
    moves.setdefault(round(y, 2), round(ny, 2))
    return f"{m.group(1)}(at {x:.2f} {ny:.2f} {m.group(4)})"
s = re.sub(r"(\(pin \w+ line )\(at (-?[\d.]+) (-?[\d.]+) (\d+)\)", pin, s)
def cap(m):
    y = float(m.group(3)); py = round(y - 2.0, 2)
    ny = moves.get(py, py + 2.0 - 2.0) + 2.0 if py in moves else y
    return f'{m.group(1)}(at {m.group(2)} {ny:.2f} 0)'
s = re.sub(r'(\(text "[^"]+" )\(at (-?[\d.]+) (-?[\d.]+) 0\)', cap, s)
low = min(moves.values())
s = re.sub(r"\(rectangle \(start (-?[\d.]+) ([\d.]+)\) \(end (-?[\d.]+) (-?[\d.]+)\)",
           lambda m: f"(rectangle (start {m.group(1)} {m.group(2)}) (end {m.group(3)} {low - g:.2f})", s, count=1)
s = re.sub(r'\(property "Value" "[^"]*"', f'(property "Value" "{mpn}"', s, count=1)
s = re.sub(r'\(property "Footprint" "[^"]*"', f'(property "Footprint" "{fp}"', s, count=1)
open(out, "w").write(s)
print("pin y moves:", moves)