main
John Lauer Add symbol, footprint, Fusion and Altium libraries, viewers, provenance, README ab389b2 6h ago
#!/usr/bin/env python3
"""Quick matplotlib render of a .kicad_sym (rectangle, polylines, captions, pins with names and numbers) for a
visual check. usage: render_sym.py in.kicad_sym out.png"""
import re, sys
import matplotlib; matplotlib.use("Agg")
import matplotlib.pyplot as plt
s = open(sys.argv[1]).read()
fig, ax = plt.subplots(figsize=(10, 7), dpi=130); ax.set_facecolor("#0d1117"); fig.patch.set_facecolor("#0d1117")
for m in re.finditer(r"\(rectangle \(start (\S+) (\S+)\) \(end (\S+) (\S+)\)", s):
    x1, y1, x2, y2 = map(float, m.groups()); ax.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], color="#8b949e", lw=1.2)
for m in re.finditer(r"\(polyline\s*\(pts(.*?)\)\s*\(stroke", s, re.S):
    pts = [(float(a), float(b)) for a, b in re.findall(r"\(xy (\S+) (\S+)\)", m.group(1))]
    ax.plot([p[0] for p in pts], [p[1] for p in pts], color="#e6edf3", lw=0.5)
for m in re.finditer(r'\(text "([^"]+)" \(at (\S+) (\S+) 0\).*?justify (\w+)', s, re.S):
    ax.text(float(m.group(2)), float(m.group(3)), m.group(1), color="#8C6BF7", fontsize=7, ha=m.group(4), va="center", weight="bold")
for m in re.finditer(r'\(pin (\w+) line \(at (\S+) (\S+) (\d+)\) \(length (\S+)\) \(name "([^"]+)"\) \(number "([^"]+)"\)', s):
    t, x, y, a, L, nm, no = m.groups(); x, y, L = float(x), float(y), float(L)
    dx = L if a == "0" else -L
    ax.plot([x, x + dx], [y, y], color="#00e6dc", lw=1.2)
    ax.text(x + dx + (0.8 if a == "0" else -0.8), y, nm, color="white", fontsize=7, ha="left" if a == "0" else "right", va="center")
    ax.text(x + dx / 2, y + 0.4, no, color="#8b949e", fontsize=6, ha="center")
for prop in ("Reference", "Value"):
    m = re.search(r'\(property "%s" "([^"]*)"\s*\(at (\S+) (\S+)' % prop, s)
    if m: ax.text(float(m.group(2)), float(m.group(3)), m.group(1), color="#c9d1d9", fontsize=8, ha="center", va="center")
ax.set_aspect("equal"); ax.axis("off"); fig.savefig(sys.argv[2], bbox_inches="tight", facecolor=fig.get_facecolor()); print(sys.argv[2])