<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Sort sheets like in assembly in Inventor Programming Forum</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-forum/sort-sheets-like-in-assembly/m-p/12089689#M21753</link>
    <description>&lt;P&gt;You could try something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="visual-basic"&gt;Sub Main()

    Dim doc As DrawingDocument = ThisDoc.Document
    Dim sheet As Sheet = doc.ActiveSheet
    Dim view As DrawingView = sheet.DrawingViews.Item(1)
    Dim refDoc As AssemblyDocument = view.ReferencedDocumentDescriptor.ReferencedDocument

    Dim names As New List(Of String)
    Dim browser As BrowserPane = refDoc.BrowserPanes.Item("Model")
    Dim node As BrowserNode = browser.TopNode

    names.Add(GetNodeName(node))
    RetriefNames(node, names)

    Dim sheets As New Dictionary(Of String, BrowserNode)
    Dim drawingBrowser As BrowserPane = doc.BrowserPanes.Item("Model")

    For Each node1 As BrowserNode In drawingBrowser.TopNode.BrowserNodes
        sheets.Add(GetNodeName(node1), node1)
    Next

    Dim previousNode = drawingBrowser.TopNode.BrowserNodes.Item(1)
    For Each name As String In names
        Dim foundSheets = sheets.Where(Function(s) s.Key.StartsWith(name))
        If (foundSheets.Any()) Then
            Dim foundSheetNode = foundSheets.First().Value

            drawingBrowser.Reorder(previousNode, False, foundSheetNode)
            previousNode = foundSheetNode

        End If
    Next
End Sub

Public Sub RetriefNames(mainNode As BrowserNode, names As List(Of String))
    For Each node As BrowserNode In mainNode.BrowserNodes
        Try
			' this if statement can be slow on large assemblies.
			' but will also check patterens.
            If (Not TypeOf node.NativeObject Is ComponentOccurrence And
                Not TypeOf node.NativeObject Is CircularOccurrencePattern And
                Not TypeOf node.NativeObject Is RectangularOccurrencePattern And
                Not TypeOf node.NativeObject is OccurrencePatternElement) Then Continue For
            ' this is faster ;-)
			' If (Not TypeOf node.NativeObject Is  ComponentOccurrence) Then Continue For

            names.Add(GetNodeName(node))
            RetriefNames(node, names)
        Catch ex As Exception
        End Try
    Next
End Sub

Public Function GetNodeName(node As BrowserNode)
    Dim nameParts = node.BrowserNodeDefinition.Label.Split(":")
    Return nameParts(0)
End Function&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 09 Jul 2023 22:02:10 GMT</pubDate>
    <dc:creator>JelteDeJong</dc:creator>
    <dc:date>2023-07-09T22:02:10Z</dc:date>
    <item>
      <title>Sort sheets like in assembly</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/sort-sheets-like-in-assembly/m-p/12071964#M21751</link>
      <description>&lt;P&gt;Hey guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you help me creating a rule that will sort every sheet in an .idw with the same order that is in the assembly.&lt;/P&gt;&lt;P&gt;I already have a rule to write the part number of the part/assy as the sheet name and another to sort sheets by name. The problem (one of them) is when the assy name is alphabetically greater than the parts inside of it. This way, assy's sheet appears after its parts sheets and them I have to re sort them manually.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a multi sheet drawing that contains every part in an assembly, having that assembly and drawing (.idw) the same name. The objective is to get the order of every file (.ipt and .iam) contained in the main assembly and, for the same part number in the .idw, sort it likewise.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There's the possibility that the same part is used in different assemblies, so the respective drawing only appears the first time. I placed some prints of the assembly browser and the idw browser sorted by sheet name and sorted like the objective of this rule.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope you can help me. It will save me a lot of time for the pdf to be in the correct paging order.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;RB&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2023 14:15:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/sort-sheets-like-in-assembly/m-p/12071964#M21751</guid>
      <dc:creator>rafael.barata</dc:creator>
      <dc:date>2023-06-30T14:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: Sort sheets like in assembly</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/sort-sheets-like-in-assembly/m-p/12088471#M21752</link>
      <description>&lt;P&gt;Hi &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5584730"&gt;@rafael.barata&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you attach the code your using and your attemp so far at automating this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One method to do this would be to loop through occurrences check document type check if it is an assembly then check sub occurrences.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jul 2023 19:54:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/sort-sheets-like-in-assembly/m-p/12088471#M21752</guid>
      <dc:creator>A.Acheson</dc:creator>
      <dc:date>2023-07-08T19:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Sort sheets like in assembly</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/sort-sheets-like-in-assembly/m-p/12089689#M21753</link>
      <description>&lt;P&gt;You could try something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="visual-basic"&gt;Sub Main()

    Dim doc As DrawingDocument = ThisDoc.Document
    Dim sheet As Sheet = doc.ActiveSheet
    Dim view As DrawingView = sheet.DrawingViews.Item(1)
    Dim refDoc As AssemblyDocument = view.ReferencedDocumentDescriptor.ReferencedDocument

    Dim names As New List(Of String)
    Dim browser As BrowserPane = refDoc.BrowserPanes.Item("Model")
    Dim node As BrowserNode = browser.TopNode

    names.Add(GetNodeName(node))
    RetriefNames(node, names)

    Dim sheets As New Dictionary(Of String, BrowserNode)
    Dim drawingBrowser As BrowserPane = doc.BrowserPanes.Item("Model")

    For Each node1 As BrowserNode In drawingBrowser.TopNode.BrowserNodes
        sheets.Add(GetNodeName(node1), node1)
    Next

    Dim previousNode = drawingBrowser.TopNode.BrowserNodes.Item(1)
    For Each name As String In names
        Dim foundSheets = sheets.Where(Function(s) s.Key.StartsWith(name))
        If (foundSheets.Any()) Then
            Dim foundSheetNode = foundSheets.First().Value

            drawingBrowser.Reorder(previousNode, False, foundSheetNode)
            previousNode = foundSheetNode

        End If
    Next
End Sub

Public Sub RetriefNames(mainNode As BrowserNode, names As List(Of String))
    For Each node As BrowserNode In mainNode.BrowserNodes
        Try
			' this if statement can be slow on large assemblies.
			' but will also check patterens.
            If (Not TypeOf node.NativeObject Is ComponentOccurrence And
                Not TypeOf node.NativeObject Is CircularOccurrencePattern And
                Not TypeOf node.NativeObject Is RectangularOccurrencePattern And
                Not TypeOf node.NativeObject is OccurrencePatternElement) Then Continue For
            ' this is faster ;-)
			' If (Not TypeOf node.NativeObject Is  ComponentOccurrence) Then Continue For

            names.Add(GetNodeName(node))
            RetriefNames(node, names)
        Catch ex As Exception
        End Try
    Next
End Sub

Public Function GetNodeName(node As BrowserNode)
    Dim nameParts = node.BrowserNodeDefinition.Label.Split(":")
    Return nameParts(0)
End Function&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2023 22:02:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/sort-sheets-like-in-assembly/m-p/12089689#M21753</guid>
      <dc:creator>JelteDeJong</dc:creator>
      <dc:date>2023-07-09T22:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Sort sheets like in assembly</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/sort-sheets-like-in-assembly/m-p/12091941#M21754</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5011186"&gt;@JelteDeJong&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help. That's exactly what I was looking for.&lt;/P&gt;&lt;P&gt;I just had to make one little adjustment (line 56), cause it failled when I have more than one instance of a part inside the assembly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Made another change to force the drawing, with the same name as the idw, to become at the top of the sheetlist but that's a personal fining.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the final code:&lt;/P&gt;&lt;LI-CODE lang="visual-basic"&gt;Sub Main()

	If ThisDoc.PathAndFileName = "" Then
		MessageBox.Show("Save file before running this macro!", "Warning")
		Exit Sub
	End If	

    Dim doc As DrawingDocument = ThisDoc.Document
	Dim index As Integer=0
	For Each oSheet As Sheet In doc.Sheets
		index = index + 1
		If ThisDoc.FileName = Left(oSheet.Name, InStrRev(oSheet.Name, "_") -1) Then Exit For
	Next
	Dim sheet As Sheet = doc.Sheets(index)
    Dim view As DrawingView = sheet.DrawingViews.Item(1)
    Dim refDoc As AssemblyDocument = view.ReferencedDocumentDescriptor.ReferencedDocument

    Dim names As New List(Of String)
    Dim browser As BrowserPane = refDoc.BrowserPanes.Item("Model")
    Dim node As BrowserNode = browser.TopNode

    names.Add(GetNodeName(node))
    RetriefNames(node, names)

    Dim sheets As New Dictionary(Of String, BrowserNode)
    Dim drawingBrowser As BrowserPane = doc.BrowserPanes.Item("Model")
   	For Each node1 As BrowserNode In drawingBrowser.TopNode.BrowserNodes
        sheets.Add(GetNodeName(node1), node1)
    Next
	
    Dim previousNode = drawingBrowser.TopNode.BrowserNodes.Item(1)
    For Each name As String In names
        Dim foundSheets = sheets.Where(Function(s) s.Key.StartsWith(name))
        If (foundSheets.Any()) Then
            Dim foundSheetNode = foundSheets.First().Value

            drawingBrowser.Reorder(previousNode, False, foundSheetNode)
            previousNode = foundSheetNode

        End If
    Next
End Sub

Public Sub RetriefNames(mainNode As BrowserNode, names As List(Of String))
	For Each node As BrowserNode In mainNode.BrowserNodes
        Try
			' this if statement can be slow on large assemblies.
			' but will also check patterens.
            If (Not TypeOf node.NativeObject Is ComponentOccurrence And
                Not TypeOf node.NativeObject Is CircularOccurrencePattern And
                Not TypeOf node.NativeObject Is RectangularOccurrencePattern And
                Not TypeOf node.NativeObject Is OccurrencePatternElement) Then Continue For
            ' this is faster ;-)
			' If (Not TypeOf node.NativeObject Is  ComponentOccurrence) Then Continue For

			If Not names.Contains(GetNodeName(node)) Then 
			   	names.Add(GetNodeName(node))
            	RetriefNames(node, names)
			End If
        Catch ex As Exception
        End Try
    Next
End Sub

Public Function GetNodeName(node As BrowserNode)
    Dim nameParts = node.BrowserNodeDefinition.Label.Split(":")
    Return nameParts(0)
End Function&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2023 18:47:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/sort-sheets-like-in-assembly/m-p/12091941#M21754</guid>
      <dc:creator>rafael.barata</dc:creator>
      <dc:date>2023-07-10T18:47:56Z</dc:date>
    </item>
  </channel>
</rss>

