2 Replies Latest reply on Mar 10, 2014 8:23 AM by Atte Peltola

    Traversing thru Assembly

    Atte Peltola

      Hi all,

      I am fairly new with Solidworks API. Mostly I have been in forums and API Help to copy paste macro's and combining those for my needs. In below macro I traverse thru an Assembly and my purpose is to choose only SW parts(I want to do more but just for testing). I I get this to work with sub assemblies(swDocASSEMBLY) but with parts(swDocPART) debugger just prints top level assembly. Could anyone help me to understand why it does not work?

       

      Option Explicit

       

      Sub main()

       

        Dim swApp                  As SldWorks.SldWorks

        Dim swModel               As SldWorks.ModelDoc2

        Dim swConfigMgr        As SldWorks.ConfigurationManager

        Dim swAssy                 As SldWorks.AssemblyDoc

        Dim swConf                 As SldWorks.Configuration

        Dim swRootComp       As SldWorks.Component2

       

        Set swApp = Application.SldWorks

        Set swModel = swApp.ActiveDoc

        Set swConfigMgr = swModel.ConfigurationManager

        Set swConf = swConfigMgr.ActiveConfiguration

        Set swRootComp = swConf.GetRootComponent3(True)

       

        TraverseComponent swRootComp, 1

       

      End Sub

       

      Sub TraverseComponent(swComp As SldWorks.Component2, _

        nLevel As Long)

       

        Dim vChildComp                  As Variant

        Dim swChildComp                 As SldWorks.Component2

        Dim swCompConfig                As SldWorks.Configuration

        Dim i                           As Long

        Dim bRet                        As Boolean

        Dim swCompModel                 As SldWorks.ModelDoc2

       

        vChildComp = swComp.GetChildren

        For i = 0 To UBound(vChildComp)

          Set swChildComp = vChildComp(i)

                

          Set swCompModel = swComp.GetModelDoc2

              If swCompModel.GetType = swDocPART Then

              'If swCompModel.GetType = swDocASSEMBLY Then

                      Debug.Print "Component: " & swComp.Name2

                      bRet = swComp.Select4(True, Nothing, True)

              End If

           

          TraverseComponent swChildComp, nLevel + 1

        Next i

      End Sub

        • Re: Traversing thru Assembly
          Artem Taturevych

          Your second function should look like this:

           

          Sub TraverseComponent(swComp As SldWorks.Component2, _

            nLevel As Long)

           

            Dim vChildComp                  As Variant

            Dim swChildComp                 As SldWorks.Component2

            Dim swCompConfig                As SldWorks.Configuration

            Dim i                           As Long

            Dim bRet                        As Boolean

            Dim swCompModel                 As SldWorks.ModelDoc2

           

              Set swCompModel = swComp.GetModelDoc2

               

              If swCompModel.GetType() = swDocPART Then

                  'If swCompModel.GetType = swDocASSEMBLY Then

                  Debug.Print "Component: " & swComp.Name2

                  bRet = swComp.Select4(True, Nothing, True)

              End If

                 

              vChildComp = swComp.GetChildren

             

              For i = 0 To UBound(vChildComp)

                  Set swChildComp = vChildComp(i)

             

                  TraverseComponent swChildComp, nLevel + 1

           

              Next i

           

          End Sub