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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
"""Check a footprint's 3D model placement against its pads.
python3 tools/model_check.py libraries/footprints/HW.pretty/<name>.kicad_mod [-D HW_LIB=...]
Reads the footprint's (model ...) entry, loads the STEP file's vertices (single-part STEP
only: no assembly transforms), applies KiCad's model transform (scale, rotate X/Y/Z, offset;
3D Y is footprint -Y) and reports:
- the model's extent in footprint coordinates and its height range above the board;
- every drilled pad (THT and NPTH) with the nearest model feature below the board surface
(pin tips, pegs, shell legs). A good placement has every hole matched within ~0.1 mm and
the body bottom at z ~ 0.
A model whose features are all above z = 0 is upside down or floating; one with the body
below z = 0 is sunk into the board.
"""
import math
import os
import re
import sys
import numpy as np
from kicad_paths import ROOT, FOOTPRINTS, SYMBOLS, MODELS3D # noqa: E402
def step_vertices(path):
txt = open(path, encoding="latin-1").read().replace("\r", "").replace("\n", "")
ents = dict(re.findall(r"#(\d+)\s*=\s*(.*?);(?=\s*#\d+\s*=|\s*ENDSEC)", txt))
if any(v.startswith("NEXT_ASSEMBLY_USAGE_OCCURRENCE") for v in ents.values()):
sys.exit("%s is an assembly; transforms are not supported" % path)
pts = []
for v in ents.values():
m = re.match(r"VERTEX_POINT\s*\(\s*'[^']*'\s*,\s*#(\d+)", v)
if m:
nums = re.search(r"\(\s*([^()]*)\)\s*\)\s*$", ents[m.group(1)]).group(1)
pts.append([float(x) for x in nums.split(",")])
return np.array(pts)
def xyz(block, key):
m = re.search(r"\(%s\s*\(xyz\s+([-\d.e]+)\s+([-\d.e]+)\s+([-\d.e]+)\s*\)" % key, block)
return np.array([float(v) for v in m.groups()]) if m else None
def rot(axis, deg):
c, s = math.cos(math.radians(deg)), math.sin(math.radians(deg))
i, j = [(1, 2), (2, 0), (0, 1)][axis]
R = np.eye(3)
R[i, i], R[i, j], R[j, i], R[j, j] = c, -s, s, c
return R
def main():
fp = sys.argv[1]
env = {"HW_LIB": os.path.join(ROOT, "libraries")}
for a, b in zip(sys.argv[2:], sys.argv[3:]):
if a == "-D":
k, v = b.split("=", 1)
env[k] = v
src = open(fp).read()
mb = src[src.index("(model "):]
path = re.match(r'\(model\s+"([^"]+)"', mb).group(1)
path = re.sub(r"\$\{(\w+)\}", lambda m: env[m.group(1)], path)
off, sc, rt = xyz(mb, "offset"), xyz(mb, "scale"), xyz(mb, "rotate")
P = step_vertices(path) * sc
# KiCad: translate(offset) * rotZ(-z) * rotY(-y) * rotX(-x) * scale
R = rot(2, -rt[2]) @ rot(1, -rt[1]) @ rot(0, -rt[0])
P = P @ R.T + off
P[:, 1] = -P[:, 1] # 3D Y up -> footprint Y down
print("model", os.path.basename(path), "offset", off, "rotate", rt)
print("extent x %.2f..%.2f y %.2f..%.2f z %.2f..%.2f" % (
P[:, 0].min(), P[:, 0].max(), P[:, 1].min(), P[:, 1].max(), P[:, 2].min(), P[:, 2].max()))
below = P[P[:, 2] < -0.05]
holes = []
for m in re.finditer(r'\(pad\s+"([^"]*)"\s+(thru_hole|np_thru_hole)\b(.*?)\(drill\s+(?:oval\s+)?([\d.]+)', src, re.S):
at = re.search(r"\(at\s+([-\d.]+)\s+([-\d.]+)", m.group(3))
holes.append((m.group(1) or "NPTH", float(at.group(1)), float(at.group(2)), float(m.group(4))))
if not holes:
print("no drilled pads")
return
if not len(below):
print("nothing of the model reaches below the board surface: upside down or floating?")
return
worst = 0.0
for name, x, y, d in holes:
near = below[np.hypot(below[:, 0] - x, below[:, 1] - y) < d / 2 + 1.0]
if len(near):
c = near[:, :2].mean(0)
e = math.hypot(c[0] - x, c[1] - y)
worst = max(worst, e)
print(" %-5s (%7.3f, %7.3f) drill %.2f: model feature centre off by %.2f mm" % (name, x, y, d, e))
else:
worst = math.inf
print(" %-5s (%7.3f, %7.3f) drill %.2f: no model feature below the board within 1 mm" % (name, x, y, d))
print("worst hole mismatch %.2f mm" % worst)
if __name__ == "__main__":
main()
|