import sys,json,hashlib
from pathlib import Path
sys.path.insert(0,"/Applications/FreeCAD.app/Contents/Resources/lib")
import FreeCAD as A, Part
V=A.Vector
p=Path(sys.argv[1])
d=json.loads((p/'QA/geometry.json').read_text());par=json.loads((p/'Source/parameters.json').read_text())
results={}
for k in 'BD':
 q=Part.read(str(p/'STEP'/d[k]['file'])); b=q.optimalBoundingBox(False,False)
 assert all(abs(a-b)<.003 for a,b in zip([b.XLength,b.YLength,b.ZLength],[100,250,100]))
 def thickness(origin, direction):
  cut=q.common(Part.makeLine(V(*origin),V(*direction)))
  return sum(e.Length for e in cut.Edges)
 # Sample the unornamented rear and a clean central floor. Exclude the
 # relief (which is only added) and measure the cut logo thickness separately.
 floor=thickness((0,125,-1),(0,125,4))
 rear=thickness((0,245,80),(0,251,80))
 rear_min=d['rear_signature'][k]['remaining_wall_mm']
 assert abs(floor-1.6)<.0001 and abs(rear-1.4)<.0001 and abs(rear_min-1.15)<.0001,(floor,rear,rear_min)
 assert d['slot']['pose_count_per_slot']==5 and d['slot']['insert_pose_intersections_mm3']==0
 results[k]={'envelope_width_depth_height_mm':[b.XLength,b.YLength,b.ZLength],'measured_floor_mm':round(floor,4),'measured_rear_wall_mm':round(rear,4),'recess_wall_mm':rear_min}
d['size_revision_checks']=results
(p/'QA/geometry.json').write_text(json.dumps(d,indent=2,ensure_ascii=False)+'\n')
print(json.dumps(results))
