import sys, io sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace', write_through=True) import win32com.client as wc def prop(o,n): v=getattr(o,n); return v() if type(v).__name__ in ('method','function','builtin_function_or_method') else v D=lambda x: x if hasattr(x,'_oleobj_') else wc.Dispatch(x) app=wc.GetActiveObject('SldWorks.Application') doc=[d for d in app.GetDocuments if 'PhoneRail_5blades' in (prop(d,'GetTitle') or '')][0] comp=[c for c in doc.GetComponents(False) if prop(c,'Name2').endswith('/USB-C_SMD-TYPE-C-31-M-12_1~USB-C_SMD-TYPE-C-31-M-12_1-1') or (('/J1~USB-C' in prop(c,'Name2')) and prop(c,'Name2').count('/')==2)] cands=[c for c in doc.GetComponents(False) if 'BASE-PCB' in prop(c,'Name2').upper() and 'USB-C' in prop(c,'Name2') and (c.GetBodies2(0) or [])] c=[k for k in cands if abs(k.GetBox(False,False)[0]*1000-8.03)<0.01][0] print('comp', prop(c,'Name2')[-50:]) b=D((c.GetBodies2(0))[0]) fs=prop(b,'GetFaces'); print('faces', len(fs)) rows=[] for f in fs: fc=D(f); s=prop(fc,'GetSurface'); fb=[round(v*1000,3) for v in prop(fc,'GetBox')] if fb[1] > -4.9: continue # only faces touching the front region (local y near -5.05) if prop(s,'IsPlane'): n=list(prop(s,'PlaneParams'))[:3] if prop(fc,'FaceInSurfaceSense'): n=[-v for v in n] kind='PLN n=(%.0f,%.0f,%.0f)' % tuple(n) elif prop(s,'IsCylinder'): cp=list(prop(s,'CylinderParams')); kind='CYL R%.3f ax=(%.0f,%.0f,%.0f) c=(%.3f,%.3f)' % (cp[6]*1000, cp[3],cp[4],cp[5], cp[0]*1000, cp[2]*1000) else: kind='OTHER' rows.append((kind, fb, prop(fc,'GetArea')*1e6)) for kind, fb, a in sorted(rows, key=lambda r:(r[1][1], r[0])): print(' %-48s x %7.3f~%7.3f y %7.3f~%7.3f z %7.3f~%7.3f A=%.2f' % (kind, fb[0],fb[3],fb[1],fb[4],fb[2],fb[5],a))