Sort part tree macro

Hi There.  I know this is probably a simple question, but has anyone seen or written a macro for SW2010 to alphabetise the part tree in an assembly?  I'm finding it kind of annoying to spend 10 minutes trying to root out and hide all of my fasteners, but alphabetising would put them all in one block.  I think I saw somewhere that SW2012 has a reorder components tool, but I couldn't find anything about it in 2010.  Thanks in advance.

Damon

SolidworksApi macros
Answers 13
AW
AW 2011-10-03 Best Answer

Could you use the Filter at the top of the Feature Tree to do that?

Filter on only your fasteners, then Hide them.

Cheers,

Anna

DF
DF 2011-10-03

I'm not gonna lie, I didn't even know there was a filter at the top of the Feature Tree.  That answers my immediate need, but I'd still be interested in sorting the Feature Tree if that's possible.  Thanks.

WM
WM 2011-10-03

Another thing you can do is use folders. Put all your fastners in a folder.

I should add, you cannot reorder components using the API.

My mistake, you can reorder components in the tree using a macro. IAssemblyDoc::ReorderComponent method can reorder the tree.

DF
DF 2011-10-03

I do use folders now for a lot of parts, so I'll just have to get in the habit of moving all fasteners to one when I dissolve assemblies.  That too is pretty time consuming. 

Oh well, if I can't reorder them through a macro then I'll just have to do it manually.  Thanks Wayne.

WM
WM 2011-10-03

You can move components into a folder using the API. Not sure if anyone has one that would work for you though.

DF
DF 2011-10-03

That would be awesome if you could point me towards one.  Do you have any thoguhts of were to look?  Or what to search for?  I guess I haven't been using the right search terms since I hadn't seen anything like that.

WM
WM 2011-10-03

I was wrong when I said you cannot reorder components using the API. You can using the method IAssemblyDoc:;ReorderComponent.

DF
DF 2011-10-03

Thanks Wayne, do you know of a prewritten macro that takes advantage of that method?  I really need to learn to write them myself.

WM
WM 2011-10-05

Here is one I just wrote. Note, it will move components that were in folders out of the folder. Also it will not reorder components in a pattern or history based components.

MA
MA 2016-06-23

Hi Wayne, we use 4 digit and 6 digit PartNumbers (4 digit for hardware). Is there an easy way to modify the macro to seperate out the 4 digit PNs from the array and sort them separately and put them at the top? Else they will mix with the 6 digit pns.

WM
WM 2016-06-23

I will look at it this weekend.

MA
MA 2016-06-24

Thanks Wayne, I was worried that because the original post was so long ago, this addition would not make it to you. I spent about 30 minutes on it this morning, had a bright idea and I think it is working. Our Part Titles are structured as follows: PartNumber_Revision_Title1__Title2_Title3, so I pulled out the PartNumber and if it is less than 5 put it in one array and the rest in another. Then sorted and reordered as you had done. Sometimes we have ancillary drawings that we use to check alignment etc so I checked the PartNumber to make sure it was numeric before putting into the arrays.

I am having trouble with it tho. There is always an out of order part at the top.

Option Explicit

' ******************************************************************************

' C:\Users\wmatus\AppData\Local\Temp\swx8572\Macro1.swb - macro recorded on 10/03/11 by wmatus

' Added to by Matt 6/24/16 to address 4 digit vs 6 digit part numbers

' ******************************************************************************

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 sArrayShort() As String

Dim iArrayShort() As Integer

Dim i As Integer

Dim x As Integer

Dim y As Integer

Dim sTemp As String

Dim iTemp As Integer

Dim PartNum As String

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)

ReDim sArrayShort(nComponents)

ReDim iArrayShort(nComponents)

' Add component name and index to arrays

For i = 0 To nComponents

    Set swComp = components(i)

    PartNum = VBA.Left(swComp.Name2, InStr(swComp.Name2, "_") - 1)

    If IsNumeric(PartNum) Then

        If Len(PartNum) < 5 Then

              sArrayShort(i) = swComp.Name2

              iArrayShort(i) = i

        Else

            sArray(i) = swComp.Name2

            iArray(i) = i

        End If

    End If

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

        If sArrayShort(x) > sArrayShort(y) Then

            sTemp = sArrayShort(x)

            iTemp = iArrayShort(x)

            sArrayShort(x) = sArrayShort(y)

            iArrayShort(x) = iArrayShort(y)

            sArrayShort(y) = sTemp

            iArrayShort(y) = iTemp

        End If

    Next y

Next x

' Reorder the components in reverse order

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

    Debug.Print components(iArray(i)).Name2 & " - " & components(iArray(i + 1)).Name2

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

Next i

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

    Debug.Print components(iArrayShort(i)).Name2 & " - " & components(iArrayShort(i + 1)).Name2

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

Next i

End Sub

comment imagecomment image
WM
WM 2016-06-26

Matt, I was working for a VAR for a few years, so did not have time to post. I am back to consulting now.

Unless I am missing something, you should be able to just add two zeros at the start of the component names that for the 4 digit part numbers so they will come out at the top of the list. Also needed to add the non part number component names to the array. This was why it was not completing the tree sort.

Option Explicit

' ******************************************************************************

' C:\Users\wmatus\AppData\Local\Temp\swx8572\Macro1.swb - macro recorded on 10/03/11 by wmatus

' ******************************************************************************

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

Dim PartNum As String

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)

    PartNum = VBA.Left(swComp.Name2, InStr(swComp.Name2, "_") - 1)

    If IsNumeric(PartNum) Then

        If Len(PartNum) < 5 Then

            ' 4 digit part number so just add two zeros in front of number

            sArray(i) = "00" + swComp.Name2

        Else

            sArray(i) = swComp.Name2

        End If

    Else

        ' not part number but still need to add component name to array

        sArray(i) = swComp.Name2

    End If

    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

    Debug.Print components(iArray(i)).Name2 & " - " & components(iArray(i + 1)).Name2

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

Next i

End Sub