DennisRen 发表于 2017-11-24 18:12:08

SOLIDWORKS中装配体有序遍历零件和子装配体

SOLIDWORKS中装配体遍历零件和子装配体,所得到的零件顺序与特征树的顺序不一致,请问有没有好的方法控制?


备注:
1、通过GetComponents或者GetChildren()遍历装配体得到的零件顺序与特征树的顺序不一致;
2、http:      fans.SOLIDWORKS.com.cn/forum.php?mod=viewthread&tid=61410&extra=page%3D1 这个贴的方法就是通过GetChildren()遍历装配体,得到的零件顺序与特征树是不一致的;
3、SW API Help 是这么说的( If the hierarchy and order are important, then use IModelDoc2::FirstFeature, IFeature::GetTypeName2, IFeature::GetSpecificFeature2, IComponent2::FirstFeature, and IFeature::GetNextFeature to retrieve your information.),但是我通过这么方法得不到想要的结果;




麻烦各位大侠江湖救急!!!!!(就好)

DennisRen 发表于 2017-11-27 09:01:37

没有人吗?:(

DennisRen 发表于 2017-11-28 11:11:29

问题已经解决了,下面是参考代码:
Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.ModelDoc2

Dim myModelDoc() As SldWorks.ModelDoc2
Sub main()
    Set swApp = Application.SldWorks
    Set swPart = swApp.ActiveDoc
    ReDim myCommonet(0)
    If Not swPart Is Nothing Then
      Dim myFeature As Feature
      Set myFeature = swPart.FirstFeature
      ReDim myModelDoc(0)
      Do While Not myFeature Is Nothing
            If (myFeature.GetTypeName2 = "Reference" Or myFeature.GetTypeName2 = "ReferencePattern") And swPart.GetType = 2 Then
                TraFeature swPart, myFeature.Name
            End If
            Set myFeature = myFeature.GetNextFeature
      Loop
    End If
End Sub

Private Sub TraFeature(ByVal ParModeldoc As SldWorks.ModelDoc2, ByVal ParName As String)
    Dim curcomponent As Component2
   
    Set curcomponent = ParModeldoc.GetComponentByName(ParName)
    If curcomponent Is Nothing Then
       Exit Sub
    End If
   
    If curcomponent.IsSuppressed = False Then
      Dim curmodeldoc As SldWorks.ModelDoc2
      Set curmodeldoc = curcomponent.GetModelDoc2
      ReDim Preserve myModelDoc(UBound(myModelDoc) + 1)
      Set myModelDoc(UBound(myModelDoc)) = curmodeldoc
      Debug.Print curcomponent.Name2
      If curmodeldoc.GetType = 2 Then
            Dim myFeatureT As Feature
            Set myFeatureT = curmodeldoc.FirstFeature
            Do While Not myFeatureT Is Nothing

                If (myFeatureT.GetTypeName2 = "Reference" Or myFeatureT.GetTypeName2 = "ReferencePattern") And curmodeldoc.GetType = 2 Then
                  TraFeature curmodeldoc, myFeatureT.Name
                End If
                Set myFeatureT = myFeatureT.GetNextFeature
            Loop
      End If
    End If
End Sub

hejunxin 发表于 2017-11-28 23:28:36

能把你的代码共享吗

hejunxin 发表于 2017-11-28 23:38:01

能分享你的代码吗

懒懒猪 发表于 2017-12-12 08:41:11

楼主果然高明,学习了!

super 发表于 2017-12-12 18:13:08

高手!

qxzch 发表于 2018-2-17 18:10:57

正为有序添加页码找不到方法,看看能不能借鉴楼主的代码,谢谢分享!

zhang961670949 发表于 2019-8-29 14:19:42

楼主厉害
页: [1]
查看完整版本: SOLIDWORKS中装配体有序遍历零件和子装配体