main
John Lauer
Review cumulative land candidates and correct complete STEP assembly clearance checks
ca3da55
"""Check the isolated nominal Q1 study against an explicit board, models and holes."""
from pathlib import Path
import json, hashlib, math, argparse
import cadquery as cq
from step_geometry import load_step
import sexp
root=Path(__file__).resolve().parents[1]
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--board',type=Path,default=root/'aiflow/run/board-26.kicad_pcb');parser.add_argument('--out',type=Path,default=root/'mechanical/q1-formed-study');args=parser.parse_args();out=args.out.resolve();out.mkdir(parents=True,exist_ok=True);board=args.board.resolve()
b=sexp.parse(board.read_text())
study=root/'mechanical/q1-formed-study/ixtp15n50l2-formed-study.step'
q=load_step(study)
assert q.isValid()
fps={next(p[2] for p in sexp.find(f,'property') if p[1]=='Reference'):f for f in sexp.find(b,'footprint')}
def pose(shape,f,m):
tr={k:list(map(float,sexp.first(sexp.first(m,k),'xyz')[1:])) for k in ['offset','scale','rotate']}
assert tr=={'offset':[0.,0.,0.],'scale':[1.,1.,1.],'rotate':[0.,0.,0.]},tr
assert sexp.first(f,'layer')[1]=='F.Cu'
at=list(map(float,sexp.first(f,'at')[1:]));angle=at[2] if len(at)>2 else 0
return shape.mirror('XZ').rotate((0,0,0),(0,0,1),-angle).translate((at[0],at[1],0))
qworld=pose(q,fps['Q1'],sexp.first(fps['Q1'],'model'))
rows=[];cache={}
for ref,f in fps.items():
if ref=='Q1':continue
for m in sexp.find(f,'model'):
path=Path(m[1].replace('${KIPRJMOD}',str(board.parent)))
if path not in cache:cache[path]=load_step(path)
try: shape=pose(cache[path],f,m)
except AssertionError as e:
rows.append({'ref':ref,'unsupported':str(e)});continue
a,bbox=qworld.BoundingBox(),shape.BoundingBox()
overlap=all(max(getattr(a,k+'min'),getattr(bbox,k+'min'))<min(getattr(a,k+'max'),getattr(bbox,k+'max')) for k in ['x','y','z'])
volume=qworld.intersect(shape).Volume() if overlap else 0
rows.append({'ref':ref,'modelSha256':hashlib.sha256(path.read_bytes()).hexdigest(),'intersectionMm3':volume})
# At board height, leads must land inside the declared pad holes. The slice also
# detects a wide shoulder incorrectly passing into the PCB.
slice_box=cq.Solid.makeBox(30,30,1.6,cq.Vector(-10,-25,-1.6))
cross=q.intersect(slice_box)
tail_rows=[]
for pad in sexp.find(fps['Q1'],'pad'):
if not pad[1]:continue
x,y=map(float,sexp.first(pad,'at')[1:3]);diameter=float(sexp.first(pad,'drill')[1])
# STEP Y is opposite footprint Y; all three pad Y coordinates are zero.
hole=cq.Solid.makeCylinder(diameter/2,1.6,cq.Vector(x,-y,-1.6))
tail_rows.append({'pin':pad[1],'holeMm':diameter,'leadVolumeInsideHoleMm3':cross.intersect(hole).Volume()})
outside=cross.Volume()-sum(row['leadVolumeInsideHoleMm3'] for row in tail_rows)
result={'boardSha256':hashlib.sha256(board.read_bytes()).hexdigest(),'modelSha256':hashlib.sha256(study.read_bytes()).hexdigest(),'shapeValid':q.isValid(),'solidCount':len(q.Solids()),'bodyCollisionRows':rows,'collisions':[r for r in rows if r.get('intersectionMm3',0)>1e-6],'unsupported':[r for r in rows if 'unsupported' in r],'padChecks':tail_rows,'leadVolumeOutsideHolesInBoardThicknessMm3':outside,'scope':'Nominal solid and pin landing screen only; no solder, copper clearance, tolerances, actual forming or cooling qualification','adopted':False}
(out/'board-geometry-screen.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps({k:v for k,v in result.items() if k!='bodyCollisionRows'},indent=2))