Page 1 of 1

Assembly Sort

Posted: Thu Jan 09, 2025 2:54 pm
by Bryan O
Anyone have a macro that will sort the components of a feature manager tree of an assembly alphanumeric?

Re: Assembly Sort

Posted: Thu Jan 09, 2025 3:09 pm
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

Re: Assembly Sort

Posted: Thu Jan 09, 2025 3:57 pm
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!

Re: Assembly Sort

Posted: Thu Jan 09, 2025 11:16 pm
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

Re: Assembly Sort

Posted: Fri Jan 10, 2025 2:28 am
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

Re: Assembly Sort

Posted: Fri Jan 10, 2025 9:35 am
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!

Re: Assembly Sort

Posted: Sat Jan 11, 2025 12:37 am
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.