1 Reply Latest reply on Dec 4, 2014 3:49 PM by Daniel Andersson

    Add Components to an Array

    Alice Caspari

      I am missing something here...

      My array does not populate with values, any help would be appreciated.

       

       

      Option Explicit

      Dim swApp As SldWorks.SldWorks

      Dim Assy As SldWorks.AssemblyDoc

      Dim Component As Variant

      Dim swComponent As Component2

      Dim i As Long

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

      Sub main()

          Set swApp = Application.SldWorks
           Set Assy = swApp.ActiveDoc
          Component = Assy.GetComponents(True)
          For i = 0 To UBound(Component)

      boolstatus = Component(i).ExcludeFromBOM
      If Not boolstatus Then

          Set swComponent = Component(i)       
          End If
          Nex
      End Sub

        • Re: Add Components to an Array
          Daniel Andersson

          Here is some comments about the current code:

          Sub main()

              Set swApp = Application.SldWorks
              Set Assy = swApp.ActiveDoc  
              ComponentArr = Assy.GetComponents(True) 'Here you get ALL top level components in the assembly


              For i = 0 To UBound(ComponentArr) 'This loops thru ALL components.

                    boolstatus = ComponentArr(i).ExcludeFromBOM 'Here you get the value for the specific component i.
                    If Not boolstatus Then

                    Set swComponent = ComponentArr(i) 'Here you declare/assign swComponent the component that is in the array at position i.

              End If

              Next

          End Sub

          So the code do not add anything to an array execpt from in the begining when you get all of the components into the array ComponentArr. It is easier to create a new array and populate that one with the components you want. Like this...

           

          Option Explicit

          Dim swApp As SldWorks.SldWorks
          Dim Assy As SldWorks.AssemblyDoc
          Dim ComponentArr As Variant
          Dim swComponent As SldWorks.Component2 'This one could be removed...

          Dim swBOMComp() As SldWorks.Component2 'New array to store the components in.

          Dim boolstatus As Boolean
          Dim longstatus As Long, longwarnings As Long

          Sub main()

              Dim iCount As Integer
              Dim i As Integer
             
              Set swApp = Application.SldWorks
              Set Assy = swApp.ActiveDoc
              ComponentArr = Assy.GetComponents(True)
             
              iCount = 0 'Counter for the new array
             
              For i = 0 To UBound(ComponentArr)
                  If ComponentArr(i).ExcludeFromBOM = False Then 'If the component (ComponentArr(i)) is not excluded then...
                      ReDim Preserve swBOMComp(iCount) 'Redimension array swBOMComp and preserve existing data.
                      Set swBOMComp(UBound(swBOMComp)) = ComponentArr(i) 'Set the new position in the swBOMComp array to be ComponentArr(i).
                      iCount = iCount + 1 'Prepare the counter if the next position in ComponentArr should be added to swBOMComp
                  End If
              Next
             
              For i = 0 To UBound(swBOMComp) 'Just loop thru and demostrate the usage of the swBOMComp array.
                  Debug.Print swBOMComp(i).Name2 & "         Excluded from BOM: " & swBOMComp(i).ExcludeFromBOM
              Next

          End Sub

          Note that you have accessed the IComponent2 directy with the array. So there is no need to Set swComponent = swBOMcomp(i) etc etc etc...

          Just let me know if you have any further questions.

          1 person found this helpful