from pathlib import Path import os,traceback import FreeCAD as A,Part,Import R=Path(r'C:\projects\wheel');O=R/'models'/'switch_2020_direct_profiles' try: d=A.openDocument(str(O/'Assembly.FCStd'));d.recompute(); lines=[] for n in ('Rail0','Rail1','NewRail0','NewRail1','Motor','CamShaft','Corner0','Corner1','Corner2','Corner3','MotorBaseCorner0','MotorBaseCorner1','CompactPlate0','CompactPlate1','SwitchCross2020'): o=d.getObject(n);assert o and not o.Shape.isNull() and o.Shape.isValid(),n lines.append('%s valid=%s bbox=%s'%(n,o.Shape.isValid(),str(o.Shape.BoundBox))) assert d.getObject('MotorShim0') is None and d.getObject('MotorShim1') is None for i in range(2): aa=d.getObject('Rail%d'%i).Shape;bb=d.getObject('NewRail%d'%i).Shape assert aa.distToShape(bb)[0]<1e-5 and aa.common(bb).Volume<1e-5 assert abs(aa.BoundBox.YMin+55)<1e-5 motor=d.getObject('Motor').Shape.BoundBox;shaft=d.getObject('CamShaft').Shape.BoundBox assert abs((motor.YMin+motor.YMax)/2-10)<1e-5 assert abs((shaft.YMin+shaft.YMax)/2-10)<0.1 assert d.getObject('SwitchEnvelope').Shape.distToShape(d.getObject('SwitchCross2020').Shape)[0]>4.99 lines.append('Motor and shaft y centres = %.5f / %.5f'%((motor.YMin+motor.YMax)/2,(shaft.YMin+shaft.YMax)/2)) A.closeDocument(d.Name) v=A.newDocument('VerifySTEP'); Import.insert(str(O/'Assembly.step'),v.Name);v.recompute() shapes=[o.Shape for o in v.Objects if hasattr(o,'Shape') and not o.Shape.isNull()] assert shapes and all(s.isValid() for s in shapes) lines.append('Imported STEP shape count=%d valid=True; solids=%d'%(len(shapes),sum(len(s.Solids) for s in shapes))) (O/'验证.txt').write_text('\n'.join(lines)+'\n',encoding='utf8');A.closeDocument(v.Name);os._exit(0) except Exception: (O/'VERIFY_ERROR.txt').write_text(traceback.format_exc(),encoding='utf8');os._exit(1)