Q: Is it possible to sort the occurrences alphabetically in an assembly browser?
A: The following VBA code illustrates the solution.
Public Sub AlphaSortComponents()
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument
Dim odef As AssemblyComponentDefinition
Set odef = oDoc.ComponentDefinition
Dim strOccs() As String
ReDim strOccs(odef.Occurrences.Count - 1)
' Get names of all occurrences in the assembly.
Dim i As Long
For i = 1 To odef.Occurrences.Count
strOccs(i - 1) = odef.Occurrences(i).Name
Next
' Sort the names alphabetically.
QuickSort strOccs, LBound(strOccs), UBound(strOccs)
On Error Resume Next
Dim oTransaction As Transaction
Set oTransaction = ThisApplication.TransactionManager _
.StartTransaction(oDoc, "Sort Components")
' Get the "model" browser pane
Dim oPane As BrowserPane
Set oPane = oDoc.BrowserPanes.Item("AmBrowserArrangement")
Dim oPreviousOcc As ComponentOccurrence
Set oPreviousOcc = Nothing
Dim j As Long
For j = 1 To odef.Occurrences.Count
Dim oThisOcc As ComponentOccurrence
Set oThisOcc = odef.Occurrences.ItemByName(strOccs(j - 1))
'Ignore pattern elements
If oThisOcc.PatternElement Is Nothing Then
Dim oThisNodeDef As BrowserNodeDefinition
Set oThisNodeDef = oDoc.BrowserPanes _
.GetNativeBrowserNodeDefinition(oThisOcc)
Dim oThisBrowserNode As BrowserNode
Set oThisBrowserNode = oPane.TopNode _
.AllReferencedNodes(oThisNodeDef).Item(1)
If Not oPreviousOcc Is Nothing Then
Dim oPreviousNodeDef As BrowserNodeDefinition
Set oPreviousNodeDef = oDoc.BrowserPanes _
.GetNativeBrowserNodeDefinition(oPreviousOcc)
Dim oPreviousBrowserNode As BrowserNode
Set oPreviousBrowserNode = oPane.TopNode _
.AllReferencedNodes(oPreviousNodeDef).Item(1)
Call oPane.Reorder(oPreviousBrowserNode, _
False, oThisBrowserNode)
If Err.Number Then
oTransaction.Abort
MsgBox "Sorting failed.", vbExclamation
Exit Sub
End If
Else
'Move the first node below origin folder
Dim oFirstBrowserNode As BrowserNode
Dim oTempNode As BrowserNode
For Each oTempNode In oPane.TopNode.BrowserNodes
Dim oNativeObject As Object
Set oNativeObject = oTempNode _
.BrowserNodeDefinition.NativeObject
If Not oNativeObject Is Nothing Then
If TypeOf oNativeObject Is ComponentOccurrence Or _
TypeOf oNativeObject Is OccurrencePattern Then
Set oFirstBrowserNode = oTempNode
Exit For
End If
End If
Next
Call oPane.Reorder(oFirstBrowserNode, True, oThisBrowserNode)
Err.Clear
End If
Set oPreviousOcc = oThisOcc
End If
Next
oTransaction.End
End Sub
Private Sub QuickSort( _
strArray() As String, _
intBottom As Integer, _
intTop As Integer)
Dim strPivot As String, strTemp As String
Dim intBottomTemp As Integer, intTopTemp As Integer
intBottomTemp = intBottom
intTopTemp = intTop
strPivot = strArray((intBottom + intTop) \ 2)
While (intBottomTemp <= intTopTemp)
While (UCase$(strArray(intBottomTemp)) < UCase$(strPivot) _
And intBottomTemp < intTop)
intBottomTemp = intBottomTemp + 1
Wend
While (UCase$(strPivot) < UCase$(strArray(intTopTemp)) _
And intTopTemp > intBottom)
intTopTemp = intTopTemp - 1
Wend
If intBottomTemp < intTopTemp Then
strTemp = strArray(intBottomTemp)
strArray(intBottomTemp) = strArray(intTopTemp)
strArray(intTopTemp) = strTemp
End If
If intBottomTemp <= intTopTemp Then
intBottomTemp = intBottomTemp + 1
intTopTemp = intTopTemp - 1
End If
Wend
'the function calls itself until everything is in good order
If (intBottom < intTopTemp) Then _
QuickSort strArray, intBottom, intTopTemp
If (intBottomTemp < intTop) Then _
QuickSort strArray, intBottomTemp, intTop
End Sub
Thanks to Nathaniel for help!