Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sort sheets like in assembly

3 REPLIES 3
Reply
Message 1 of 4
rafael.barata
300 Views, 3 Replies

Sort sheets like in assembly

Hey guys,

 

Can you help me creating a rule that will sort every sheet in an .idw with the same order that is in the assembly.

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.

 

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.

 

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. 

 

Hope you can help me. It will save me a lot of time for the pdf to be in the correct paging order.

 

Regards,

RB

Labels (4)
3 REPLIES 3
Message 2 of 4
A.Acheson
in reply to: rafael.barata

Hi @rafael.barata 

 

Can you attach the code your using and your attemp so far at automating this?

 

One method to do this would be to loop through occurrences check document type check if it is an assembly then check sub occurrences.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 4
JelteDeJong
in reply to: rafael.barata

You could try something like this:

 

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

 

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 4 of 4
rafael.barata
in reply to: JelteDeJong

Hi @JelteDeJong ,

 

Thanks for your help. That's exactly what I was looking for.

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. 

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.

 

Here's the final code:

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

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report