aboutsummaryrefslogtreecommitdiff
path: root/kicad/lib/tools/kicad_sym_grid.py
diff options
context:
space:
mode:
Diffstat (limited to 'kicad/lib/tools/kicad_sym_grid.py')
-rwxr-xr-xkicad/lib/tools/kicad_sym_grid.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/kicad/lib/tools/kicad_sym_grid.py b/kicad/lib/tools/kicad_sym_grid.py
new file mode 100755
index 0000000..de52a8e
--- /dev/null
+++ b/kicad/lib/tools/kicad_sym_grid.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-3.0-or-later
+"""Snap kipart symbol pins onto the 2.54 mm grid.
+
+kipart centres each pin column, which puts columns with an even pin count
+(and box edges) on a 1.27 mm half grid; it also writes float noise such as
+3.55e-15. Pins are moved by half a grid step along their column, and pins
+whose end point is off grid are lengthened so the symbol body stays put.
+
+ kicad_sym_grid.py IN.kicad_sym > OUT.kicad_sym
+"""
+import re
+import sys
+
+
+def on_grid(v):
+ return abs(v / 2.54 - round(v / 2.54)) < 0.004
+
+
+def fix(m):
+ kind, x, y, a = m.group(1), float(m.group(2)), float(m.group(3)), m.group(4)
+ length = float(m.group(5))
+ vertical = a in ("90", "270")
+ if vertical and not on_grid(x):
+ x += 1.27
+ if not vertical and not on_grid(y):
+ y += 1.27
+ if vertical and not on_grid(y):
+ y += 1.27 if y > 0 else -1.27
+ length += 1.27
+ if not vertical and not on_grid(x):
+ x += 1.27 if x > 0 else -1.27
+ length += 1.27
+
+ def f(v):
+ v = round(v, 4)
+ return "0" if v == 0 else f"{v:g}"
+ return f"(pin {kind} (at {f(x)} {f(y)} {a}) (length {f(length)})"
+
+
+text = open(sys.argv[1]).read()
+text = re.sub(r"\(pin ([\w ]+?)\s*\(at ([-\d.e+]+) ([-\d.e+]+) (\d+)\)\s*\(length ([\d.]+)\)",
+ fix, text)
+sys.stdout.write(text)