main
John Lauer Verify native USB slots and document prototype assembly sequence cafd733
"""Check Board32 native Excellon slot extents and orientation against pad geometry."""
from pathlib import Path
from collections import Counter
import hashlib,json,math,re
import sexp
R=Path(__file__).resolve().parents[1];board=R/'aiflow/run/board-32.kicad_pcb';drill=R/'manufacturing-review/board32/drill/board-32-PTH.drl'
b=sexp.parse(board.read_text());expected=[]
def key(ends,width):return tuple(sorted(tuple(round(v,3)for v in p) for p in ends))+(round(width,3),)
for f in sexp.find(b,'footprint'):
 a=sexp.first(f,'at');x,y=map(float,a[1:3]);theta=math.radians(float(a[3])if len(a)>3 else 0)
 ref=next(p[2] for p in sexp.find(f,'property')if p[1]=='Reference')
 for p in sexp.find(f,'pad'):
  d=sexp.first(p,'drill')
  if not d or 'oval' not in d:continue
  assert p[2]=='thru_hole' and len(d)==4,'Unsupported slot format'
  at=sexp.first(p,'at');u,v=map(float,at[1:3]);angle=math.radians(float(at[3])if len(at)>3 else 0)
  w,h=map(float,d[2:4]);cx=x+u*math.cos(theta)+v*math.sin(theta);cy=-y+u*math.sin(theta)-v*math.cos(theta)
  # Pad orientation in saved board is absolute; export frame reverses board Y.
  longangle=angle+(math.pi/2 if h>w else 0);travel=abs(h-w);dx=travel/2*math.cos(longangle);dy=travel/2*math.sin(longangle)
  ends=[[cx-dx,cy-dy],[cx+dx,cy+dy]]
  expected.append({'reference':ref,'pad':p[1],'endpointsMm':ends,'widthMm':min(w,h),'overallLengthMm':max(w,h),'key':key(ends,min(w,h))})
actual=[];tools={};tool=None
for line in drill.read_text().splitlines():
 m=re.fullmatch(r'T(\d+)C([\d.]+)',line)
 if m:tools[m[1]]=float(m[2]);continue
 m=re.fullmatch(r'T(\d+)',line)
 if m:tool=m[1];continue
 if 'G85' in line:
  ends=[list(map(float,c)) for c in re.findall(r'X(-?[\d.]+)Y(-?[\d.]+)',line)];assert len(ends)==2
  width=tools[tool];actual.append({'endpointsMm':ends,'widthMm':width,'overallLengthMm':math.dist(*ends)+width,'key':key(ends,width)})
assert Counter(x['key']for x in expected)==Counter(x['key']for x in actual),'Slot endpoint/width mismatch'
for x in expected+actual:x.pop('key')
out={'status':'pass-slot-geometry-review','boardSha256':hashlib.sha256(board.read_bytes()).hexdigest(),'drillSha256':hashlib.sha256(drill.read_bytes()).hexdigest(),'slotCount':len(actual),'comparisonResolutionMm':.001,'expected':expected,'exported':actual,'limits':'Checks source-to-export slot geometry only; finished plated slot tolerances and fabrication capability are not qualified.'}
(R/'manufacturing-review/board32/slot-review.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps({'status':out['status'],'slots':len(actual),'widthsMm':[x['widthMm']for x in actual],'lengthsMm':[round(x['overallLengthMm'],3)for x in actual]}))