15 Replies Latest reply on Aug 17, 2017 8:29 AM by Cad Admin

    VBA - Move Component in assembly Tree

    Cad Admin

      I noticed in the API that ReorderComponents requires an array.  Is an array ALWAYS required?

      Is there any additional way to move a part in the tree.

      I'm trying to move some components with 10000 & 20000 in the part name to a specified folders. I was thinking to traverse the assembly and add conditionals and then move.  but reorder is all i have found.

        • Re: VBA - Move Component in assembly Tree
          Josh Brady

          So... It sounds like an array is exactly what you want.  What does "add conditionals" mean?  Traverse the tree, adding components to your 10000 and 20000 arrays as you go using redim preserve.  When you get through the tree, move 'em using reorder.  If you  move them one at a time as you traverse the tree, performance will likely be worse.  Also, I'm not sure what reordering the tree during a traverse would do to the traverse process.

            • Re: VBA - Move Component in assembly Tree
              Cad Admin

              Think i understand the performance, i am worse at arrays.  Do you have example.  Would it be multiple arrays? 

                • Re: VBA - Move Component in assembly Tree
                  Josh Brady

                  An array is just a list.  Putting the component in an array is just putting it in a list.  Its existence in the list should survive rebuilds etc.

                   

                  roughly it would look like this:

                   

                  dim 1array() as sldworks.component2

                  dim 2array() as sldworks.component2

                   

                  Traversing components

                       if 20000 in component name then

                            redim preserve 2array(ubound(2array)+1)

                            2array(ubound(2array)) = component

                       if 10000 in (do same for 1array)

                  next component

                  reorder 1array

                  reorder 2array

                   

                   

                  Arrays are very useful.  There are loads of tutorials on how they work, so I won't retype one here.

                    • Re: VBA - Move Component in assembly Tree
                      Amen Allah Jlili

                      Use Collections. Their size is dynamic. You can and remove items from a collection.

                      • Re: VBA - Move Component in assembly Tree
                        Cad Admin

                        I'm so lost in the multiple array thing (single array below works)...i cant find any examples types in the VBA API i have searched.

                         

                        Option Explicit

                         

                        Dim swApp               As SldWorks.SldWorks

                        Dim Assy                As SldWorks.AssemblyDoc

                        Dim featureMgr          As SldWorks.FeatureManager

                        Dim feature             As SldWorks.feature

                        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 retVal              As Boolean

                        Dim boolstatus          As Boolean

                        Dim longstatus          As Long, longwarnings As Long

                         

                        Dim TestTest            As String

                         

                        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).Name2 Like "*10000*" 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

                         

                            Set featureMgr = Assy.FeatureManager

                            Set feature = Assy.FeatureByName("10000")

                          

                            retVal = Assy.ReorderComponents(swBOMComp, feature, swReorderComponents_LastInFolder)

                           

                        End Sub

                          • Re: VBA - Move Component in assembly Tree
                            Ivana Kolin

                            or here is your way

                             

                            Option Explicit
                            
                            Dim swApp               As SldWorks.SldWorks
                            Dim Assy                As SldWorks.assemblyDoc
                            Dim featureMgr          As SldWorks.FeatureManager
                            Dim feature             As SldWorks.feature
                            Dim ComponentArr        As Variant
                            
                            Dim swBOMComp10000()         As SldWorks.Component2 'New array to store the components in.
                            Dim swBOMComp20000()         As SldWorks.Component2 'New array to store the components in.
                            Dim retVal              As Boolean
                            Sub main()
                            
                                Dim iCount10000 As Integer
                                Dim iCount20000 As Integer
                                Dim i As Integer
                              
                                Set swApp = Application.SldWorks
                                Set Assy = swApp.ActiveDoc
                                ComponentArr = Assy.GetComponents(True)
                              
                                iCount10000 = 0 'Counter for the new array
                                iCount20000 = 0
                                
                                ReDim swBOMComp10000(UBound(ComponentArr)) ' redim array for maximum amount of components
                                ReDim swBOMComp20000(UBound(ComponentArr))
                                
                                For i = 0 To UBound(ComponentArr)
                                    If ComponentArr(i).Name2 Like "*10000*" Then
                                        Set swBOMComp10000(iCount10000) = ComponentArr(i) 'Set the new position in the swBOMComp array to be ComponentArr(i).
                                        iCount10000 = iCount10000 + 1 'Prepare the counter if the next position in ComponentArr should be added to swBOMComp
                                    End If
                                    If ComponentArr(i).Name2 Like "*20000*" Then
                                        Set swBOMComp20000(iCount20000) = ComponentArr(i) 'Set the new position in the swBOMComp array to be ComponentArr(i).
                                        iCount20000 = iCount20000 + 1 'Prepare the counter if the next position in ComponentArr should be added to swBOMComp
                                    End If
                                Next
                                If iCount10000 > 0 Then
                                    ReDim Preserve swBOMComp10000(iCount10000 - 1) ' schrink array - this performs better then redim for each component.
                                    Set featureMgr = Assy.FeatureManager
                                    Set feature = Assy.FeatureByName("10000")
                                    retVal = Assy.ReorderComponents(swBOMComp10000, feature, swReorderComponents_LastInFolder)
                                End If
                                If iCount20000 > 0 Then
                                    ReDim Preserve swBOMComp20000(iCount20000 - 1)
                                    Set feature = Assy.FeatureByName("20000")
                                    retVal = Assy.ReorderComponents(swBOMComp20000, feature, swReorderComponents_LastInFolder)
                                End If
                            End Sub
                            
                    • Re: VBA - Move Component in assembly Tree
                      Cad Admin

                      Sooo...I'm getting a headache...LOL 

                      Believe me all help is appreciated and options are great.

                       

                      Yes i like to stick with VBA

                      • Re: VBA - Move Component in assembly Tree
                        Ivana Kolin
                        Option Explicit
                        
                        
                        Sub Main()
                            MoveToFolder ("10000")
                            MoveToFolder ("20000")
                        End Sub
                        
                        
                        Private Sub MoveToFolder(ComponentName As String)
                            Dim swApp As SldWorks.SldWorks
                            Dim modelDoc2 As SldWorks.modelDoc2
                            Dim assemblyDoc As SldWorks.assemblyDoc
                            Dim featureMgr As SldWorks.FeatureManager
                            Dim selectionMgr As SldWorks.selectionMgr
                            Dim feature As SldWorks.feature
                            Dim count As Long
                            Dim componentToMove As SldWorks.Component2
                            Dim componentsToMove() As Object
                            Dim i As Long
                            Dim retVal As Boolean
                            Set swApp = Application.SldWorks
                            Set modelDoc2 = swApp.ActiveDoc
                            Set assemblyDoc = modelDoc2
                            Set selectionMgr = modelDoc2.SelectionManager
                            modelDoc2.ClearSelection2 True
                            Set feature = modelDoc2.FirstFeature
                            Do While Not feature Is Nothing
                                If feature.GetTypeName2 = "Reference" And feature.Name Like "*" & ComponentName & "*" Then
                                    feature.Select2 True, 0
                                End If
                                Set feature = feature.GetNextFeature
                            Loop
                            count = selectionMgr.GetSelectedObjectCount2(0)
                            ReDim componentsToMove(count - 1)
                            For i = 0 To count - 1
                                Set componentToMove = selectionMgr.GetSelectedObjectsComponent4(i + 1, 0)
                                Set componentsToMove(i) = componentToMove
                            Next
                            Set featureMgr = modelDoc2.FeatureManager
                            Set feature = assemblyDoc.FeatureByName(ComponentName)
                            If feature Is Nothing Then
                                Set feature = featureMgr.InsertFeatureTreeFolder2(swFeatureTreeFolder_EmptyBefore)
                                feature.Name = ComponentName
                            End If
                            Set feature = assemblyDoc.FeatureByName(ComponentName)
                            retVal = assemblyDoc.ReorderComponents(componentsToMove, feature, swReorderComponents_LastInFolder)
                        End Sub