AnsweredAssumed Answered

Design Tree Sort

Question asked by Hrituc Alexandru on Jan 25, 2016
Latest reply on May 18, 2016 by Taylor Rassi


I want to sort component from design tree, and if found a folder to ignore it...

From example i have this:


After i run the macro, obtain this:

2.jpg 3.jpg

Why all components are inserted in folder????? inside the folder are sorted correctly, but useless...

How can write so if find a folder to ignore it????...and obtain this:




this is the code i use:


Option Explicit



Dim swApp As SldWorks.SldWorks

Dim Part As ModelDoc2

Dim swAssyDoc As AssemblyDoc

Dim selMgr As SelectionMgr

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

Dim source As Object

Dim target As Object

Dim components As Variant

Dim swComp As Component2

Dim nComponents As Integer

Dim sArray() As String

Dim iArray() As Integer

Dim i As Integer

Dim x As Integer

Dim y As Integer

Dim sTemp As String

Dim iTemp As Integer



Sub main()



Set swApp = Application.SldWorks



Set Part = swApp.ActiveDoc

Set swAssyDoc = Part

Set selMgr = Part.SelectionManager



' Get all the top level components

components = swAssyDoc.GetComponents(True)



' Get the upper bound of  array of components

nComponents = UBound(components)

ReDim sArray(nComponents)

ReDim iArray(nComponents)



' Add component name and index to arrays

For i = 0 To nComponents

    Set swComp = components(i)

    sArray(i) = swComp.Name2

    iArray(i) = i

Next i



' Do a bubble sort on the arrays

For x = 0 To (nComponents - 1)

    For y = (x + 1) To nComponents

        If sArray(x) > sArray(y) Then

            sTemp = sArray(x)

            iTemp = iArray(x)

            sArray(x) = sArray(y)

            iArray(x) = iArray(y)

            sArray(y) = sTemp

            iArray(y) = iTemp

        End If

    Next y

Next x



' Reorder the components in reverse order

For i = (nComponents - 1) To 0 Step -1

    boolstatus = swAssyDoc.ReorderComponents(components(iArray(i)), components(iArray(i + 1)), swReorderComponents_Before)

Next i



End Sub