Inventor API: Alpha Sort Occurrences in Assembly browser

by Vladimir Ananyev

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!


Comments

2 responses to “Inventor API: Alpha Sort Occurrences in Assembly browser”

  1. Nathaniel Avatar
    Nathaniel

    error when I run this >> Quicksort cannot be found. What is the reference for this?

  2. Nathaniel Avatar
    Nathaniel

    Quick Sort

    Sub QuickSort(SortMe() As String, lowbound As Long, hibound As Long)
    ‘Recursive QuickSort routine for VBA. Sorts an array of strings into ascending order.
    ‘SortMe() is the array of strings to be sorted. lowbound is the index of thefirst
    ‘element in the array (usually 0 or 1). hibound is the index of the lastelement in
    ‘the array.
    Dim low As Long, high As Long, midval As String, temp As String
    low& = lowbound&
    high& = hibound&
    midval$ = SortMe((low + high) / 2)
    While (low lowbound)
    high = high – 1
    Wend
    If (low <= high) Then
    temp = SortMe(low)
    SortMe(low) = SortMe(high)
    SortMe(high) = temp
    low = low + 1
    high = high – 1
    End If
    Wend
    If (lowbound < high) Then
    Call QuickSort(SortMe(), lowbound, high)
    End If
    If (low < hibound) Then
    Call QuickSort(SortMe(), low, hibound)
    End If
    End Sub

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading