文件
phonerail/.claude/r2/shell/place_twopiece.py
T

212 行
8.2 KiB
Python
原始文件 Blame 文件历史

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
# -*- coding: utf-8 -*-
u"""总装换成**两件式外壳**:TopShell_v1(一体顶盖)+ BottomPlate_v1(一整块底盖)。
删:Tray_v9、EndPlate_v13 ×2、SouthPanel_v4、NorthPanel_v3、TopPanel_v4(六件式全部)
插:TopShell_v1 dz +74.4、BottomPlate_v1 dz −10(纯平移)
不动:底板、5 片刀片、2 只风扇
判据:
1. 两件装配 box;分风腔深度复测(西 15.01 / 东 15.51)
2. 干涉 = 360 基线
3. 顶盖墙底 ↔ 底盖顶面 **面贴合**(z=−10)
4. 同轴:底盖四角 Ø3.4 过孔 ↔ 顶盖角柱 Ø4.2 螺母孔(轴 z)
5. 同轴:顶盖端墙 Ø3.4 风扇孔 ↔ 风扇模型 R2.15 安装孔(轴 x)—— 六件式时代没核过
6. 截图
"""
import io
import os
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', write_through=True)
import pythoncom
import win32com.client as wc
OUT = r'C:\Users\35138\.claude\jobs\11836093\tmp'
DIR = u'D:\\DCDC\\新建文件夹\\5blades\\'
ASM = 'PhoneRail_5blades'
EMPTY = wc.VARIANT(pythoncom.VT_DISPATCH, None)
IDENT = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
OLD = ('TRAY', 'ENDPLATE', 'SOUTHPANEL', 'NORTHPANEL', 'TOPPANEL', 'TOPSHELL', 'BOTTOMPLATE')
def prop(o, n):
v = getattr(o, n)
return v() if type(v).__name__ in ('method', 'function', 'builtin_function_or_method') else v
def D(x):
return x if hasattr(x, '_oleobj_') else wc.Dispatch(x)
def P(*a):
print(*a)
app = wc.GetActiveObject('SldWorks.Application')
doc = None
for d in app.GetDocuments or []:
if ASM in (prop(d, 'GetTitle') or ''):
doc = d
break
e0 = wc.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, 0)
app.ActivateDoc3(prop(doc, 'GetTitle'), False, 0, e0)
doc = app.ActiveDoc
did = app._oleobj_.GetIDsOfNames('GetMathUtility')
mu = wc.Dispatch(app._oleobj_.Invoke(did, 0, pythoncom.DISPATCH_METHOD, 1))
d2 = mu._oleobj_.GetIDsOfNames('CreateTransform')
def setx(c, t):
v = wc.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8,
IDENT + [t[0] / 1000.0, t[1] / 1000.0, t[2] / 1000.0, 1.0, 0.0, 0.0, 0.0])
xf = wc.Dispatch(mu._oleobj_.Invoke(d2, 0, pythoncom.DISPATCH_METHOD, 1, v))
try:
c.SetTransformAndSolve2(xf)
except Exception:
c.Transform2 = xf
doc.EditRebuild3
return [round(x * 1000.0, 3) for x in c.GetBox(False, False)]
allok = True
P(u'[1] 删六件式')
for c in list(doc.GetComponents(True)):
nm = prop(c, 'Name2')
if any(k in nm.upper() for k in OLD):
doc.ClearSelection2(True)
doc.Extension.SelectByID2(nm, 'COMPONENT', 0, 0, 0, False, 0, EMPTY, 0)
doc.Extension.DeleteSelection2(0)
P(u' 删掉 %s' % nm)
doc.EditRebuild3
left = [prop(c, 'Name2') for c in doc.GetComponents(True)
if any(k in prop(c, 'Name2').upper() for k in OLD)]
if left:
P(u' ⛔ 没删干净(EditDelete 式假成功?)%s' % left); sys.exit(2)
P(u'\n[2] 插两件')
for fn, t, want in ((u'TopShell_v1.SLDPRT', (0, 0, 74.4), [-38.4, -4.055, -10.0, 136.4, 106.745, 74.4]),
(u'BottomPlate_v1.SLDPRT', (0, 0, -10.0), [-38.4, -4.055, -12.4, 136.4, 106.745, 0.0])):
c = doc.AddComponent4(DIR + fn, '', 0.0, 0.0, 0.0)
if c is None:
P(u' ⛔ %s AddComponent4 → None' % fn); sys.exit(3)
b = setx(c, t)
ok = all(abs(b[i] - want[i]) < 0.05 for i in range(6))
allok &= ok
P(u' %-22s x %.2f~%.2f y %.2f~%.2f z %.2f~%.2f %s'
% (fn, b[0], b[3], b[1], b[4], b[2], b[5], u'✅' if ok else u'⛔ 期望 %s' % want))
tops = {prop(c, 'Name2'): c for c in doc.GetComponents(True)}
box = {n: [x * 1000 for x in c.GetBox(False, False)] for n, c in tops.items()}
fans = sorted([b for n, b in box.items() if 'OD6025' in n.upper()], key=lambda b: b[0])
blades = [b for n, b in box.items() if 'BLADE' in n.upper()]
pw = min(b[0] for b in blades) - fans[0][3]
pe = fans[1][0] - max(b[3] for b in blades)
P(u' 分风腔 西 %.2f / 东 %.2f %s' % (pw, pe, u'✅' if pw > 14 and pe > 14 else u'⛔'))
allok &= pw > 14 and pe > 14
P(u'\n[3] 干涉(基线 360)')
idm = doc.InterferenceDetectionManager
idm.TreatCoincidenceAsInterference = False
idm.IncludeMultibodyPartInterferences = False
idm.TreatSubAssembliesAsComponents = True
idm.IgnoreHiddenBodies = True
idm.UseTransform = False
try:
n = idm.GetInterferenceCount
except AttributeError:
dd = idm._oleobj_.GetIDsOfNames('GetInterferenceCount')
n = idm._oleobj_.Invoke(dd, 0, pythoncom.DISPATCH_PROPERTYGET, 1)
hits = []
if n and n != 360:
try:
items = idm.GetInterferences
except AttributeError:
dd = idm._oleobj_.GetIDsOfNames('GetInterferences')
items = idm._oleobj_.Invoke(dd, 0, pythoncom.DISPATCH_PROPERTYGET, 1)
for it in items:
o = it if hasattr(it, 'Volume') else wc.Dispatch(it)
nms = [wc.Dispatch(x).Name2.split('/')[0] for x in o.Components]
if any(k in y.upper() for y in nms for k in ('TOPSHELL', 'BOTTOMPLATE')):
hits.append((o.Volume * 1e9, nms))
idm.Done()
P(u' 干涉 %s %s' % (n, u'✅' if n == 360 else u'⛔'))
for vv, nms in sorted(hits, reverse=True)[:8]:
P(u' %.3f mm³ %s ↔ %s' % (vv, nms[0][:26], nms[1][:26]))
allok &= n == 360
P(u'\n[4] 贴合:顶盖墙底 ↔ 底盖顶面')
ts = [b for nn, b in box.items() if 'TOPSHELL' in nn.upper()][0]
bp = [b for nn, b in box.items() if 'BOTTOMPLATE' in nn.upper()][0]
P(u' 顶盖 z 下沿 %.3f / 底盖板面 z −10(底盖 box 上沿 %.3f 是 PCB 立柱顶)'
% (ts[2], bp[5]))
allok &= abs(ts[2] + 10.0) < 0.05
def world_holes(comp, radius, tol=0.01):
xf = list(prop(comp, 'Transform2').ArrayData)
R, T = xf[0:9], [xf[9] * 1000, xf[10] * 1000, xf[11] * 1000]
out = []
for bd in comp.GetBodies2(0) or []:
for f in prop(D(bd), 'GetFaces'):
s = prop(D(f), 'GetSurface')
if not prop(s, 'IsCylinder'):
continue
cp = list(prop(s, 'CylinderParams'))
if abs(cp[6] * 1000 - radius) > tol:
continue
p = [cp[0] * 1000, cp[1] * 1000, cp[2] * 1000]
a = cp[3:6]
gp = [p[0] * R[0] + p[1] * R[3] + p[2] * R[6] + T[0],
p[0] * R[1] + p[1] * R[4] + p[2] * R[7] + T[1],
p[0] * R[2] + p[1] * R[5] + p[2] * R[8] + T[2]]
ga = [a[0] * R[0] + a[1] * R[3] + a[2] * R[6],
a[0] * R[1] + a[1] * R[4] + a[2] * R[7],
a[0] * R[2] + a[1] * R[5] + a[2] * R[8]]
out.append((max(range(3), key=lambda i: abs(ga[i])), gp))
return out
def pair(tag, A, B, axis, want):
A = [p for ax, p in A if ax == axis]
B = [p for ax, p in B if ax == axis]
o = [i for i in range(3) if i != axis]
ok = 0
for p in A:
lat = min((max(abs(p[i] - q[i]) for i in o) for q in B), default=99)
ok += lat < 0.05
P(u' %s (%s=%7.2f, %s=%6.2f) 横偏 %.3f %s'
% (tag, 'xyz'[o[0]], p[o[0]], 'xyz'[o[1]], p[o[1]], lat, u'✅' if lat < 0.05 else u'⛔'))
P(u' %s 对上 %d / %d(期望 %d)' % (tag, ok, len(A), want))
return ok == want and len(A) == want
shell = [c for n, c in tops.items() if 'TOPSHELL' in n.upper()][0]
plate = [c for n, c in tops.items() if 'BOTTOMPLATE' in n.upper()][0]
fanc = [c for n, c in tops.items() if 'OD6025' in n.upper()]
P(u'\n[5] 同轴:底盖四角过孔 ↔ 顶盖角柱螺母孔')
thru = [h for h in world_holes(plate, 1.7)]
nuts = [h for h in world_holes(shell, 2.1)]
allok &= pair(u'底盖过孔', thru, nuts, 2, 4)
P(u'\n[6] 同轴:顶盖端墙风扇孔 ↔ 风扇安装孔(R2.15)')
wall = world_holes(shell, 1.7)
fan_h = []
for fc in fanc:
fan_h += world_holes(fc, 2.15)
allok &= pair(u'端墙风扇孔', wall, fan_h, 0, 8)
for nm2, vid in ((u'tp_iso', 7), (u'tp_south', 6), (u'tp_bottom', 2)):
try:
doc.ShowNamedView2('', vid)
doc.ViewZoomtofit2
doc.SaveBMP(os.path.join(OUT, '%s.bmp' % nm2), 1600, 1100)
except Exception as ex:
P(u'截图 %s 失败 %s' % (nm2, str(ex)[:40]))
e3 = wc.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, 0)
w3 = wc.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, 0)
doc.Extension.SaveAs3(prop(doc, 'GetPathName'), 0, 1, EMPTY, EMPTY, e3, w3)
P(u'\n存盘 err=%s 顶层件 %d 总判 %s'
% (e3.value, len(doc.GetComponents(True)), u'✅ 全过' if allok else u'⛔ 有项没过'))