Repository navigation
Expand file tree
/
Copy pathcomponent_models.py
More file actions
133 lines (124 loc) · 6.91 KB
/
Copy pathcomponent_models.py
File metadata and controls
133 lines (124 loc) · 6.91 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Read-only STEP placement and bounded tessellation for QuickTherm.
KiCad owns model placement; FreeCAD reads the exported solids. The private
PRODUCT identity technique follows WayriCAD Mechanical Check's extractor.
Geometry supplies display surfaces, never thermal material properties.
"""
from __future__ import annotations
import hashlib
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import time
def discover():
cli = os.environ.get('WAYRICAD_THERM_KICAD_CLI') or shutil.which('kicad-cli')
native = os.environ.get('WAYRICAD_THERM_KICAD_PYTHON')
cad = os.environ.get('WAYRICAD_THERM_FREECAD_PYTHON')
if os.name == 'nt':
roots = [Path(os.environ.get('PROGRAMFILES', 'C:/Program Files')),
Path(os.environ.get('LOCALAPPDATA', ''), 'Programs'),
Path.home()/'AppData/Local/Programs']
candidates = [p for root in roots for p in root.glob('KiCad/10.*/bin/kicad-cli.exe')]
if not cli and candidates:
cli = str(sorted(candidates, reverse=True)[0])
if cli and not native and Path(cli).with_name('python.exe').is_file():
native = str(Path(cli).with_name('python.exe'))
candidates = [p for root in roots for p in root.glob('FreeCAD*/bin/python.exe')]
if not cad and candidates:
cad = str(sorted(candidates, reverse=True)[0])
else:
import importlib.util
if not native and importlib.util.find_spec('pcbnew') is not None:
native = sys.executable
if not cad and importlib.util.find_spec('FreeCAD') is not None:
cad = sys.executable
missing = [name for name, value in [('KiCad CLI', cli), ('KiCad Python', native),
('FreeCAD Python', cad)] if not value]
if missing:
raise RuntimeError('STEP view needs '+', '.join(missing)+'. Install FreeCAD or configure '
'WAYRICAD_THERM_FREECAD_PYTHON; configure WAYRICAD_THERM_KICAD_CLI/'
'WAYRICAD_THERM_KICAD_PYTHON for a custom KiCad installation.')
return cli, native, cad
def _run(args, root, cancelled, deadline):
from wayricad_runtime.analysis_process import worker_options, stop_worker
if cancelled and cancelled():
raise InterruptedError('Component model loading cancelled.')
env = os.environ.copy()
for key in ('PYTHONHOME', 'VIRTUAL_ENV', 'PYTHONPATH'):
env.pop(key, None)
env['WAYRICAD_ANALYSIS_SCRATCH'] = str(root)
with (root/'process.log').open('w+', encoding='utf-8') as log:
process = subprocess.Popen(list(map(str, args)), stdin=subprocess.DEVNULL,
stdout=log, stderr=log, env=env,
**worker_options())
try:
while process.poll() is None:
if cancelled and cancelled():
raise InterruptedError('Component model loading cancelled.')
if time.monotonic() > deadline:
raise TimeoutError('STEP model loading exceeded 180 seconds. Reduce model complexity.')
time.sleep(.1)
finally:
if process.poll() is None:
stop_worker(process)
if process.returncode:
log.seek(0)
raise RuntimeError('STEP model process failed: '+log.read()[-2000:])
def load_component_models(board_path, expected_source_sha256, cancelled=None, progress=None):
"""Return placed triangle meshes in KiCad XY, bottom Z=0, top Z=thickness.
Results contain hashes and coverage, without private file paths. No source
board or model is changed. Cancellation terminates only this owned process.
"""
path = Path(board_path).resolve()
before = hashlib.sha256(path.read_bytes()).hexdigest()
if before != expected_source_sha256:
raise ValueError('Saved board changed before STEP model loading; reload it.')
cli, native, cad = discover()
worker = Path(__file__).with_name('component_model_worker.py')
started=time.monotonic()
deadline = started+180
def show_progress(stage,percent):
if progress:
progress({'stage':stage,'percent':percent,'elapsed_s':time.monotonic()-started,'eta_s':None})
with tempfile.TemporaryDirectory(prefix='wayricad-thermal-models-') as temporary:
root = Path(temporary)
job = {'board_path':str(path), 'source_sha256':before, 'root':str(root)}
job_path = root/'job.json'
job_path.write_text(json.dumps(job), encoding='utf-8')
show_progress('Resolve STEP models',0)
_run([native, worker, 'prepare', job_path], root, cancelled, deadline)
prepared = json.loads((root/'prepared.json').read_text(encoding='utf-8'))
if prepared['model_map']:
show_progress('Place STEP models with KiCad',25)
_run([cli, 'pcb', 'export', 'step', '--force', '--subst-models',
'--user-origin', '0x0mm', '-o', root/'board.step', root/'snapshot.kicad_pcb'],
root, cancelled, deadline)
show_progress('Read STEP component surfaces',65)
_run([cad, worker, 'mesh', job_path], root, cancelled, deadline)
output = json.loads((root/'meshes.json').read_text(encoding='utf-8'))
else:
output = {'components':{}, 'missing':[]}
# Source/model races invalidate the result instead of showing old solids.
if hashlib.sha256(path.read_bytes()).hexdigest() != before:
raise ValueError('Saved board changed while loading STEP models; reload it.')
for model in prepared['model_files']:
if hashlib.sha256(Path(model['path']).read_bytes()).hexdigest() != model['sha256']:
raise ValueError('A STEP model changed while loading; run again.')
evidence = prepared.get('saved_board_provenance')
if evidence is not None:
for name, expected in evidence['source_fingerprints'].items():
source = Path(name)
actual = hashlib.sha256(source.read_bytes()).hexdigest() if source.is_file() else None
if actual != expected:
raise ValueError('Saved PCB or project rules changed while loading STEP models; reload it.')
if hashlib.sha256(Path(evidence['converter_path']).read_bytes()).hexdigest() != evidence['converter_sha256']:
raise ValueError('Analysis CLI changed while loading STEP models; run again.')
return {'components':output['components'], 'source_sha256':before,
**({'saved_board_provenance': evidence} if evidence is not None else {}),
'coverage':{'loaded':sorted(output['components']),
'missing':prepared['missing']+output['missing']},
'coordinate_system':'KiCad XY mm; board bottom Z=0, top Z=board thickness',
'thermal_meaning':'One reviewed RC temperature per component; geometry has no material model.'}