Assembly Sort
Assembly Sort
Anyone have a macro that will sort the components of a feature manager tree of an assembly alphanumeric?
Funny you ask, there was some activity on my solution at the SOLIDWORKS forum yesterday.
The code below should do the trick.
Go to full postThe code below should do the trick.
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
- Stefan Sterk
- Posts: 39
- Joined: Tue Aug 10, 2021 2:40 am
- x 53
- x 81
Re: Assembly Sort
Funny you ask, there was some activity on my solution at the SOLIDWORKS forum yesterday.
The code below should do the trick.
The code below should do the trick.
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
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!
It sorts everything in the feature manager, subs included (but not the components of the subs, as expected).
Thank you!
Re: Assembly Sort
Can you help me understand the first line:
-Thank You
Code: Select all
Declare PtrSafe Function StrCmpLogicalW Lib "shlwapi" (ByVal s1 As String, ByVal s2 As String) As Integer
-Thank You
- Stefan Sterk
- Posts: 39
- Joined: Tue Aug 10, 2021 2:40 am
- x 53
- x 81
Re: Assembly Sort
Hi Loeb,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
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.
So the following names part1, part2 and part10 will be sorted like;Compares two Unicode strings. Digits in the strings are considered as numerical content rather than text. This test is not case-sensitive.
part1
part2
part10
Instead of (with the default VBA StrComp function)
part1
part10
part2
Re: Assembly Sort
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
I get it. Thank you for the explanation. I have to wonder what other libraries I don't know about.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?