7 Replies Latest reply on Oct 10, 2012 7:58 AM by Oleg Bezyaev

    Get a unique components names in assembly

    Oleg Bezyaev

      How to get a unique names the components in the top level assembly?

      Name2 - not appropriate, because that adds indexes.

        • Re: Get a unique components names in assembly
          Keith Rice

          Oleg,

           

          This prints all of the names of top-level components, without instance numbers, to the immediate window:

           

          =======

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swAssy As SldWorks.AssemblyDoc

          Dim vComps As Variant

          Dim swComp As SldWorks.Component2

          Dim i As Integer

           

          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swAssy = swModel

              vComps = swAssy.GetComponents(True)

              For i = 0 To UBound(vComps)

                  Set swComp = vComps(i)

                  Debug.Print Left(swComp.Name2, InStrRev(swComp.Name2, "-") - 1)

              Next i

          End Sub

           

          =======

           

          Hope this helps.

           

          Keith

          Video Tutorials for the SolidWorks API

            • Re: Get a unique components names in assembly
              Oleg Bezyaev

              Hi Keith,

               

              In this case I got:

              Part

              Part

              Part

              AnotherPart

              AnotherPart

              MorePart

              MorePart

              I need a list like this:

              Part

              AnotherPart

              MorePart

                • Re: Get a unique components names in assembly
                  Ivana Kolin

                  you must set reference to "Microsoft scripting runtime"

                   

                  Option Explicit

                   

                   

                      Dim swApp As SldWorks.SldWorks

                      Dim swModel As SldWorks.ModelDoc2

                      Dim swAssy As SldWorks.AssemblyDoc

                      Dim vComps As Variant

                      Dim swComp As SldWorks.Component2

                      Dim i As Integer

                      Dim Labels As New dictionary

                     

                  Sub main()

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      Set swAssy = swModel

                      vComps = swAssy.GetComponents(False)

                      For i = 0 To UBound(vComps)

                          Set swComp = vComps(i)

                          Dim s1 As String

                          s1 = Left(swComp.Name2, InStrRev(swComp.Name2, "-") - 1)

                          If Not Labels.Exists(s1) Then

                              Labels.Add s1, ""

                          End If

                      Next i

                      For i = 0 To Labels.Count - 1

                          Debug.Print Labels.Keys(i)

                      Next

                   

                   

                  End Sub

                  • Re: Get a unique components names in assembly
                    Rais Patel

                    Hi, use collection object to build unique list.

                     

                    Option Explicit

                    Public colComponents As Collection

                     


                    Public Sub addCompInCollection(ByVal compName As String)
                        On Error GoTo err
                        colComponents.Add compName, compName
                        Exit Sub
                    err:
                    End Sub

                     


                    Sub main()


                    Set colComponents = New Collection

                    addCompInCollection "1"
                    addCompInCollection "1"
                    addCompInCollection "2"
                    addCompInCollection "3"
                    addCompInCollection "1"

                    Dim l As Integer

                    For l = 1 To colComponents.Count
                    Debug.Print colComponents.Item(l)
                    Next

                     

                    End Sub

                • Re: Get a unique components names in assembly
                  Oleg Bezyaev

                  That is the direct method or function isn't present? Without traverse a tree and reading of names of all components.