1 Reply Latest reply on Sep 22, 2018 11:04 PM by Fifi Riri

    Is there a fast way to get Name2 and ComponentReference for all components?

    Dan Markoff

      I have a need to get the Name and ComponentReference of all parts in a rather large assembly. 

       

              Dim partsList As Object() = swModel.GetComponents(False)

              For Each activeComp In partsList

                  activePart = New CustomPartClass

                  activePart.PN = activeComp.Name2

                  activePart.refdes = activeComp.ComponentReference

                  partsLists.Add(activePart.PN, activePart)

              Next

      This works, but is REALLY slow.  For some of my assemblies, this takes 30 seconds or more. I inserted some telemetry and the GetComponents call alone is about half the time and then the Name2 and ComponentReference calls take up the other half.

       

      Is there a faster way to do this?


      Thanks!

        • Re: Is there a fast way to get Name2 and ComponentReference for all components?
          Fifi Riri

          Hello

          You could try the code below. It will traverse an assembly and output in a Excel file

          To go faster, you could use document manager

          Read Component Reference using Solidworks Document Manager APIs

           

          Option Explicit

          Dim swApp As SldWorks.SldWorks

          Dim swAssy As SldWorks.AssemblyDoc

           

          Sub main()

              Set swApp = Application.SldWorks

              Dim swConf As SldWorks.Configuration

              Set swAssy = swApp.ActiveDoc

              Set swConf = swAssy.GetActiveConfiguration

              TraverseComponent swConf.GetRootComponent3(True)

          End Sub

           

          Sub TraverseComponent(swComp As SldWorks.Component2)

              Dim vChild As Variant

              Dim swChildComp As SldWorks.Component2

              For Each vChild In swComp.GetChildren

                  Set swChildComp = vChild

                  'Debug.Print swChildComp.Name2  & "     " & swChildComp.ComponentReference

                  WriteCSVFile swChildComp.Name2 & "," & swChildComp.ComponentReference

                  TraverseComponent swChildComp

              Next

          End Sub

           

          Sub WriteCSVFile(logSTR As String)

              Dim My_filenumber As Integer

              My_filenumber = FreeFile

              Open "C:\Sample.csv" For Append As #My_filenumber

                  Print #My_filenumber, logSTR

              Close #My_filenumber

          End Sub