|
首先看图和代码:
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim myFeature As Feature
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
swDoc.ResolveAllLightWeightComponents (True) '将所有轻化还原
Set myFeature = swDoc.FirstFeature
Do While Not myFeature Is Nothing
If (myFeature.GetTypeName2 = "Reference" Or myFeature.GetTypeName2 = "ReferencePattern") Then TraFeature swDoc, myFeature.Name '遍历子装配体函数
Set myFeature = myFeature.GetNextFeature
Loop
End Sub
Private Sub TraFeature(ParModeldoc As SldWorks.ModelDoc2, ParName As String) '遍历子装配体函数
Dim myFeatureT As Feature
Dim curmodeldoc As SldWorks.ModelDoc2
Dim curcomponent As Component2
Set curcomponent = ParModeldoc.GetComponentByName(ParName)
If curcomponent Is Nothing Then Exit Sub
If curcomponent.IsSuppressed <> False Then Exit Sub '如被仰制则退出
Set curmodeldoc = curcomponent.GetModelDoc2
Debug.Print curmodeldoc.GetPathName() '获取完整路径
If curmodeldoc.GetType = 2 Then '如果是装配体则遍历装配内部一层
Set myFeatureT = curmodeldoc.FirstFeature
Do While Not myFeatureT Is Nothing
If (myFeatureT.GetTypeName2 = "Reference" Or myFeatureT.GetTypeName2 = "ReferencePattern") Then TraFeature curmodeldoc, myFeatureT.Name '遍历子装配体函数
Set myFeatureT = myFeatureT.GetNextFeature
Loop
End If
End Sub
图1可见,装配体中有子装配体1(配置1),子装配体1(配置2),子装配体中有零件1-6。
用以上代码获取路径,所得到的结果是:装配体1、零件1、零件3、零件5、装配体1、零件1、零件3、零件5。
但按顶级装配上面所显示的应该为:装配体1、零件2、零件4、零件6、装配体1、零件1、零件3、零件5。
---------这是发现问题所在------------
然后结合代码和多配置相关发现,问题在于是否被仰制这一句:
If curcomponent.IsSuppressed <> False Then Exit Sub '如被仰制则退出
当装配体1激活在配置1状态(切换窗口或关闭装配体1)回到顶级装配中,用此代码只能获取到装配体1的配置1的状态,不管顶级装配体中的,装配体1配置是一种配置都是一样的结果。
所以,要如何修改或变通才能获取到,和顶级装配所显示的那样的结果?
|
|