iLogic alpha sort components

iLogic alpha sort components

Log0ut
Enthusiast Enthusiast
1,089 Views
2 Replies
Message 1 of 3

iLogic alpha sort components

Log0ut
Enthusiast
Enthusiast

Inventor gives a error when using the Alpha Sort command, if there are folders in assembly.

https://knowledge.autodesk.com/support/inventor/troubleshooting/caas/sfdcarticles/sfdcarticles/Inven...

 

But can iLogic solve this problem, whitout deleting folder?

In my case, i only need to sort parts, that are not in the folders

0 Likes
Accepted solutions (1)
1,090 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor
Accepted solution

This works for me.

Public Class ThisRule

    Sub Main()

        Dim doc As AssemblyDocument = ThisDoc.Document
        Dim topNode As BrowserNode = doc.BrowserPanes.Item("Model").TopNode
        Dim nodesList As List(Of BrowserNode) = New List(Of BrowserNode)()
        Dim orginNode As BrowserNode = Nothing

        For Each node As BrowserNode In topNode.BrowserNodes
            If (node.BrowserNodeDefinition.Label.Equals("Origin")) Then
                orginNode = node
            End If
            If (node.NativeObject IsNot Nothing) Then
                If (node.NativeObject.Type = ObjectTypeEnum.kComponentOccurrenceObject) Then
                    nodesList.Add(node)
                End If
            End If
        Next

        Dim nodeArray() = nodesList.ToArray()

        Array.Sort(nodeArray, New AlphanumReverseComparator())

        Dim transAction As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisDoc.Document, "Alphanum sort")
        For Each node As BrowserNode In nodeArray
            doc.BrowserPanes.Item("Model").Reorder(orginNode, False, node)
        Next
        transAction.End()
    End Sub


End Class

Public Class AlphanumReverseComparator
    Implements IComparer
    ' Taken from:
    ' https://www.dotnetperls.com/alphanumeric-sorting-vbnet 

    Public Function Compare(ByVal x As Object,
                            ByVal y As Object) As Integer Implements IComparer.Compare

        ' [1] Validate the arguments.
        Dim s1 As String = y.BrowserNodeDefinition.Label
        If s1 = Nothing Then
            Return 0
        End If

        Dim s2 As String = x.BrowserNodeDefinition.Label
        If s2 = Nothing Then
            Return 0
        End If

        Dim len1 As Integer = s1.Length
        Dim len2 As Integer = s2.Length
        Dim marker1 As Integer = 0
        Dim marker2 As Integer = 0

        ' [2] Loop over both Strings.
        While marker1 < len1 And marker2 < len2

            ' [3] Get Chars.
            Dim ch1 As Char = s1(marker1)
            Dim ch2 As Char = s2(marker2)

            Dim space1(len1) As Char
            Dim loc1 As Integer = 0
            Dim space2(len2) As Char
            Dim loc2 As Integer = 0

            ' [4] Collect digits for String one.
            Do
                space1(loc1) = ch1
                loc1 += 1
                marker1 += 1

                If marker1 < len1 Then
                    ch1 = s1(marker1)
                Else
                    Exit Do
                End If
            Loop While Char.IsDigit(ch1) = Char.IsDigit(space1(0))

            ' [5] Collect digits for String two.
            Do
                space2(loc2) = ch2
                loc2 += 1
                marker2 += 1

                If marker2 < len2 Then
                    ch2 = s2(marker2)
                Else
                    Exit Do
                End If
            Loop While Char.IsDigit(ch2) = Char.IsDigit(space2(0))

            ' [6] Convert to Strings.
            Dim str1 = New String(space1)
            Dim str2 = New String(space2)

            ' [7] Parse Strings into Integers.
            Dim result As Integer
            If Char.IsDigit(space1(0)) And Char.IsDigit(space2(0)) Then
                Dim thisNumericChunk = Integer.Parse(str1)
                Dim thatNumericChunk = Integer.Parse(str2)
                result = thisNumericChunk.CompareTo(thatNumericChunk)
            Else
                result = str1.CompareTo(str2)
            End If

            ' [8] Return result if not equal.
            If Not result = 0 Then
                Return result
            End If
        End While

        ' [9] Compare lengths.
        Return len1 - len2
    End Function
End Class

This will only work on a english Inventor version. When you use a different language then translate the strings "Model" and "Origin" to your language.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 3

engilic
Advocate
Advocate

Why Autodesk does not implement this method instead of doing a simple but not very smart "just compare text" method, so we do not have xxx:10 in front of xxx:2?

0 Likes