blob: de52a8e5f3c4650fccb28894daabcdbf88f82d8a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)
|