Assembly Sort

Library for macros
User avatar
Bryan O
Posts: 115
Joined: Wed Mar 24, 2021 7:34 am
Answers: 1
Location: Lowell MI
x 18
x 37

Assembly Sort

Unread post by Bryan O »

Anyone have a macro that will sort the components of a feature manager tree of an assembly alphanumeric?
by Stefan Sterk » Thu Jan 09, 2025 3:09 pm
Funny you ask, there was some activity on my solution at the SOLIDWORKS forum yesterday.

The code below should do the trick. UU

Code: Select all

Option Explicit
 
Declare PtrSafe Function StrCmpLogicalW Lib "shlwapi" (ByVal s1 As String, ByVal s2 As String) As Integer
 
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swDoc As SldWorks.ModelDoc2
 
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
 
    If swDoc Is Nothing Then End
    If swDoc.GetType <> swDocASSEMBLY Then End
 
    swDoc.FeatureManager.EnableFeatureTree = False
 
    ' Sort assembly components
    SortAssemblyComponents swDoc
 
    swDoc.FeatureManager.EnableFeatureTree = True
 
End Sub
 
Function SortAssemblyComponents(swDoc As SldWorks.ModelDoc2)
    Dim swAsm As SldWorks.AssemblyDoc
    Dim swComp As SldWorks.Component2
    Dim swFeatmgr As SldWorks.FeatureManager
    Dim swConf As SldWorks.Configuration
    Dim swRootComp As SldWorks.Component2
    Dim swFeatFolder As SldWorks.Feature
    Dim vChildComp As Variant
    Dim i As Integer, j As Integer
 
    Set swFeatmgr = swDoc.FeatureManager
 
    Set swAsm = swDoc
    Set swConf = swDoc.GetActiveConfiguration
    Set swRootComp = swConf.GetRootComponent3(True)
 
    vChildComp = swRootComp.GetChildren
 
    For i = 0 To UBound(vChildComp)
        For j = i + 1 To UBound(vChildComp)
                If StrCmpLogicalW(StrConv(vChildComp(i).Name2, vbUnicode), StrConv(vChildComp(j).Name2, vbUnicode)) = 1 Then
                Set swComp = vChildComp(j)
                Set vChildComp(j) = vChildComp(i)
                Set vChildComp(i) = swComp
            End If
        Next j
    Next i
 
    vChildComp(0).Select4 False, Nothing, False
    Set swFeatFolder = swFeatmgr.InsertFeatureTreeFolder2(swFeatureTreeFolderType_e.swFeatureTreeFolder_Containing)
 
    For i = 0 To UBound(vChildComp)
        swAsm.ReorderComponents vChildComp(i), swFeatFolder, swReorderComponentsWhere_e.swReorderComponents_LastInFolder
    Next
 
    swFeatFolder.Select2 False, -1
    swDoc.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed
 
End Function
Go to full post
User avatar
Stefan Sterk
Posts: 39
Joined: Tue Aug 10, 2021 2:40 am
Answers: 4
x 53
x 81

Re: Assembly Sort

Unread post by Stefan Sterk »

Funny you ask, there was some activity on my solution at the SOLIDWORKS forum yesterday.

The code below should do the trick. UU

Code: Select all

Option Explicit
 
Declare PtrSafe Function StrCmpLogicalW Lib "shlwapi" (ByVal s1 As String, ByVal s2 As String) As Integer
 
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swDoc As SldWorks.ModelDoc2
 
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
 
    If swDoc Is Nothing Then End
    If swDoc.GetType <> swDocASSEMBLY Then End
 
    swDoc.FeatureManager.EnableFeatureTree = False
 
    ' Sort assembly components
    SortAssemblyComponents swDoc
 
    swDoc.FeatureManager.EnableFeatureTree = True
 
End Sub
 
Function SortAssemblyComponents(swDoc As SldWorks.ModelDoc2)
    Dim swAsm As SldWorks.AssemblyDoc
    Dim swComp As SldWorks.Component2
    Dim swFeatmgr As SldWorks.FeatureManager
    Dim swConf As SldWorks.Configuration
    Dim swRootComp As SldWorks.Component2
    Dim swFeatFolder As SldWorks.Feature
    Dim vChildComp As Variant
    Dim i As Integer, j As Integer
 
    Set swFeatmgr = swDoc.FeatureManager
 
    Set swAsm = swDoc
    Set swConf = swDoc.GetActiveConfiguration
    Set swRootComp = swConf.GetRootComponent3(True)
 
    vChildComp = swRootComp.GetChildren
 
    For i = 0 To UBound(vChildComp)
        For j = i + 1 To UBound(vChildComp)
                If StrCmpLogicalW(StrConv(vChildComp(i).Name2, vbUnicode), StrConv(vChildComp(j).Name2, vbUnicode)) = 1 Then
                Set swComp = vChildComp(j)
                Set vChildComp(j) = vChildComp(i)
                Set vChildComp(i) = swComp
            End If
        Next j
    Next i
 
    vChildComp(0).Select4 False, Nothing, False
    Set swFeatFolder = swFeatmgr.InsertFeatureTreeFolder2(swFeatureTreeFolderType_e.swFeatureTreeFolder_Containing)
 
    For i = 0 To UBound(vChildComp)
        swAsm.ReorderComponents vChildComp(i), swFeatFolder, swReorderComponentsWhere_e.swReorderComponents_LastInFolder
    Next
 
    swFeatFolder.Select2 False, -1
    swDoc.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed
 
End Function
User avatar
Bryan O
Posts: 115
Joined: Wed Mar 24, 2021 7:34 am
Answers: 1
Location: Lowell MI
x 18
x 37

Re: Assembly Sort

Unread post by Bryan O »

NICE! Works exactly how I hoped!
It sorts everything in the feature manager, subs included (but not the components of the subs, as expected).
Thank you!
User avatar
loeb
Posts: 74
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 40
x 10

Re: Assembly Sort

Unread post by loeb »

Can you help me understand the first line:

Code: Select all

Declare PtrSafe Function StrCmpLogicalW Lib "shlwapi" (ByVal s1 As String, ByVal s2 As String) As Integer

-Thank You
User avatar
Stefan Sterk
Posts: 39
Joined: Tue Aug 10, 2021 2:40 am
Answers: 4
x 53
x 81

Re: Assembly Sort

Unread post by Stefan Sterk »

loeb wrote: Thu Jan 09, 2025 11:16 pm Can you help me understand the first line:

Code: Select all

Declare PtrSafe Function StrCmpLogicalW Lib "shlwapi" (ByVal s1 As String, ByVal s2 As String) As Integer

-Thank You
Hi Loeb,

Just declaring a function from the shlwapi.dll. Having this line makes the code more copy past friendly.
Otherwise you would need to reference the shlwapi.dll library. Which isn't referenced by default. Does this make sense for you?

Description for the StrCmpLogicalW function.
Compares two Unicode strings. Digits in the strings are considered as numerical content rather than text. This test is not case-sensitive.
So the following names part1, part2 and part10 will be sorted like;
part1
part2
part10

Instead of (with the default VBA StrComp function)
part1
part10
part2
User avatar
josh
Posts: 319
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 17
x 23
x 541

Re: Assembly Sort

Unread post by josh »

Shoot... Wish I had known about this a while back. I basically rolled my own version, which was a bit of a pain in the butt. Thanks for the info!
User avatar
loeb
Posts: 74
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 40
x 10

Re: Assembly Sort

Unread post by loeb »

Stefan Sterk wrote: Fri Jan 10, 2025 2:28 am Just declaring a function from the shlwapi.dll. Having this line makes the code more copy past friendly.
Otherwise you would need to reference the shlwapi.dll library. Which isn't referenced by default. Does this make sense for you?
I get it. Thank you for the explanation. I have to wonder what other libraries I don't know about.
Post Reply