Private Function getasmchild(swmodelComps As Variant) Dim swmodelComp As SldWorks.Component2 Dim swmodel As ModelDoc2 Dim swAsm As SldWorks.AssemblyDoc For i = 0 To UBound(swmodelComps) Set swmodelComp = swmodelComps(i) Set swmodel = swmodelComp.GetModelDoc2 If swmodel.GetType = 1 Then MsgBox (swmodel.GetTitle) ElseIf swmodel.GetType = 2 Then swAsm = swmodel getasmchild (swAsm.GetComponents(True)) End If Next End Function
理解问题Private Function getasmchild(swmodelComps As Variant) Dim swmodelComp As SldWorks.Component2 Dim swmodel As ModelDoc2 Dim swAsm As SldWorks.AssemblyDoc For i = 0 To UBound(swmodelComps) Set swmodelComp = swmodelComps(i) Set swmodel = swmodelComp.GetModelDoc2 If swmodel.GetType = 1 Then MsgBox (swmodel.GetTitle) ElseIf swmodel.GetType = 2 Then swAsm = swmodel getasmchild (swAsm.GetComponents(True)) End If Next End Function
已完成理解「Private Function getasmchild(swmodelComps As Variant) Dim swmodelComp As SldWorks.Component2 Dim swmodel As ModelDoc2 Dim swAsm As SldWorks.AssemblyDoc For i = 0 To UBound(swmodelComps) Set swmodelComp = swmodelComps(i) Set swmodel = swmodelComp.GetModelDoc2 If swmodel.GetType = 1 Then MsgBox (swmodel.GetTitle) ElseIf swmodel.GetType = 2 Then swAsm = swmodel getasmchild (swAsm.GetComponents(True)) End If Next End Function」
展开阅读网页
代码功能概述
- 功能:递归获取装配体中的子组件。
代码分析
- 要点总结描述1:定义函数
getasmchild
,接收组件数组swmodelComps
作为参数。 - 要点总结描述2:遍历组件数组,获取每个组件的模型文档
swmodel
。 - 要点总结描述3:判断模型文档类型,如果是零件则显示标题,如果是装配体则递归调用
getasmchild
。
代码实现
Private Function getasmchild(swmodelComps As Variant)
Dim swmodelComp As SldWorks.Component2
Dim swmodel As ModelDoc2
Dim swAsm As SldWorks.AssemblyDoc
For i = 0 To UBound(swmodelComps)
Set swmodelComp = swmodelComps(i)
Set swmodel = swmodelComp.GetModelDoc2
If swmodel.GetType = 1 Then
MsgBox (swmodel.GetTitle)
ElseIf swmodel.GetType = 2 Then
Set swAsm = swmodel
getasmchild (swAsm.GetComponents(True))
End If
Next
End Function