3 Replies Latest reply on Oct 7, 2018 3:01 PM by Keith Rice

    Traverse Assembly VBA does not show components in the correct order!

    Darren Smith

      Can anybody help?

      I have a small assembly that have all the components listed in the correct order on the left hand side of the screen (1, 2, 3, 4 etc).

      If I insert a BOM list into the assembly the results are showing in the correct order (Please see Screen-1).

      However,

      When I run the macro, the output is not in the correct order (Please see Screen-2)

      Please see the attached swp macro

      or

      2015 SOLIDWORKS API Help - Traverse Assembly at Component Level Example (VBA)

       

      Does anybody know what is wrong with the macro????

       

      MANY THANKS IN ADVANCE!!!

       

      Darren

        • Re: Traverse Assembly VBA does not show components in the correct order!
          Fifi Riri

          Hello, try this:

          (btw : the macro you linked is empty)

           

          Option Explicit

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swFeat As SldWorks.Feature

          Dim entity As entity

          Dim component As Component2

          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swFeat = swModel.FirstFeature

              Dim ArrayName() As String

              Dim ArrayQty() As String

              Dim i As Long, j As Long

              ReDim Preserve ArrayName(0)

              While Not swFeat Is Nothing

                  Set entity = swFeat

                  If entity.GetType = swSelectType_e.swSelCOMPONENTS Then

                      Set component = swFeat.GetSpecificFeature2

                     ' Debug.Print "Name: " & component.GetPathName

                      If UBound(Filter(ArrayName, component.GetPathName)) = 0 Then

                          For j = 0 To UBound(ArrayName)

                              If ArrayName(j) = component.GetPathName Then

                                  ArrayQty(j) = ArrayQty(j) + 1

                              End If

                          Next

                      Else

                          ReDim Preserve ArrayName(i)

                          ReDim Preserve ArrayQty(i)

                          ArrayName(i) = component.GetPathName

                          ArrayQty(i) = 1

                          i = i + 1

                      End If

                  End If

                  Set swFeat = swFeat.GetNextFeature

              Wend

              Dim fileName As String

              For i = 0 To UBound(ArrayName)

                  fileName = Left(ArrayName(i), InStr(ArrayName(i), ".") - 1)

                  fileName = Right(fileName, Len(fileName) - InStrRev(fileName, "\"))

                  Debug.Print "Name: " & fileName & " , Qty: " & ArrayQty(i)

              Next

          End Sub

          • Re: Traverse Assembly VBA does not show components in the correct order!
            Attilio Colangelo

              This is the help for the GetComponents method but I believe it applies in general .

             

            From SolidWorks 2018 API help for GetComponents (emphasis mine)

             

            Remarks

            The components returned by this method can be in any order. You should not rely on the order to indicate anything about children or parents. If the hierarchy and order are important, then use IModelDoc2::FirstFeature, IFeature::GetTypeName2, IFeature::GetSpecificFeature2, IComponent2::FirstFeature, and IFeature::GetNextFeature to retrieve your information.

            • Re: Traverse Assembly VBA does not show components in the correct order!
              Keith Rice

              If you want to traverse components in order then you can use my free macro "Traverse features in order using recursion", which is available in our Macro Library. This macro is superior in virtually every way to the component traversal examples in the API Help: It's shorter, simpler, and returns components in order.

               

              Keith

              SOLIDWORKS API Training and Services