iLogic: Expand "Origin" in the Model Tree

iLogic: Expand "Origin" in the Model Tree

amarcoc
Advocate Advocate
1,688 Views
18 Replies
Message 1 of 19

iLogic: Expand "Origin" in the Model Tree

amarcoc
Advocate
Advocate

Hi.

 

I didn't find a way to expand the "Origin" folder of the currently selected component. Could this be done with iLogic?

 

Screenshot_1.png

 

Thanks in advance.

0 Likes
Accepted solutions (2)
1,689 Views
18 Replies
Replies (18)
Message 2 of 19

WCrihfield
Mentor
Mentor

Hi @amarcoc.  Here is an iLogic rule you may be able to use for that task.  It worked OK in a couple of my own tests.  The selected component likely needs to be a 'top level' component though, because I'm using its 'Parent' to get the active assembly document and its browser pane to work with.

Sub Main
	Dim oComp As ComponentOccurrence = PickComponent
	If IsNothing(oComp) Then Exit Sub
	Dim oADoc As AssemblyDocument = oComp.Parent.Document 'or .FactoryDocument
	Dim oModelPane As BrowserPane = oADoc.BrowserPanes.Item("Model") 'AmBrowserArrangement
	Dim oCompNode As BrowserNode = oModelPane.GetBrowserNodeFromObject(oComp)
	For Each oNode As BrowserNode In oCompNode.BrowserNodes
		If oNode.BrowserNodeDefinition.Label = "Origin" Then
			If Not oNode.Expanded Then oNode.Expanded = True
		End If
	Next
End Sub

Function PickComponent(Optional oPrompt As String = vbNullString) As ComponentOccurrence
	If oPrompt = "" Then oPrompt = "Select a Component."
	oObj = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, oPrompt)
	If IsNothing(oObj) OrElse (TypeOf oObj Is ComponentOccurrence = False) Then Return Nothing
	Dim oOcc As ComponentOccurrence = oObj
	Return oOcc
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 19

amarcoc
Advocate
Advocate

Thank you. Almost there 🙂

 

In fact, the component may not be on the top-level, so the code doesn't work yet.

 

What I am trying to do, is to replace the "Find in Browser" option (with an F shortcut to run it) with a function that will find the component in browser and automatically expand the "Origin" folder. This is the code I have to find in browser and expand the main component folder:

 

ThisApplication.CommandManager.ControlDefinitions.Item("AppFindInBrowserCtxCmd").Execute
ThisApplication.CommandManager.ControlDefinitions.Item("AppBrowserExpandFolderCmd").Execute

 

After running this code, the component is already selected and expanded. Just need to expand the "Origin" folder.

 

I do this because I create constraints to the planes a lot, and I want to skip the manual expanding folders process to get to the planes.

0 Likes
Message 4 of 19

WCrihfield
Mentor
Mentor

Just so you know then...that 'Origins' node, may look like a BrowserFolder, but apparently it is not recognized that way when trying to find it by code.  I know because I tried looking for it that way when I made the earlier code, but it found zero BrowserFolders under the node of the component (BrowserNode.BrowserFolders.Count = 0).  That's why I had to loop through the BrowserNode.BrowserNodes and find it by its 'Label' instead.

Also, you may be able to keep climbing up the 'Parent' ladder from the component, using error management technique like Try...Catch block, until it doesn't return anything anymore in an attempt to find the top level assembly, if the component is not a top level component.  Just a thought.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 19

Curtis_Waguespack
Consultant
Consultant

Hi @amarcoc 

 

If you're wanting to preselect the items then you could use something like this. I don't think the find in browser and expand folder command manager lines are needed.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim sSet As SelectSet = ThisDoc.Document.SelectSet
If sSet.Count = 0 Then Return 'exit rule
Dim oOcc As ComponentOccurrence = sSet.Item(1)
oOcc.Parent.Document.BrowserPanes.Item("Model") _
.GetBrowserNodeFromObject(oOcc).BrowserNodes.Item("Origin").Expanded = True

.  

EESignature

Message 6 of 19

amarcoc
Advocate
Advocate

Thanks.

 

That worked to expand the Origin 🙂

 

But if the part is a "Sheet Metal", I need to expand the "Folded Model" first. The following code didn't work yet. Any ideas?

 

 

'On Error Resume Next

ThisApplication.CommandManager.ControlDefinitions.Item("AppFindInBrowserCtxCmd").Execute
ThisApplication.CommandManager.ControlDefinitions.Item("AppBrowserExpandFolderCmd").Execute

Dim sSet As SelectSet = ThisDoc.Document.SelectSet
If sSet.Count = 0 Then Return 'exit rule
Dim oOcc As ComponentOccurrence = sSet.Item(1)

'Code for Sheet Metal
'It expands "Folded Model" but not the "Origin"
oOcc.Parent.Document.BrowserPanes.Item("Model").GetBrowserNodeFromObject(oOcc).BrowserNodes.Item("Folded Model").Expanded = True
oOcc.Parent.Document.BrowserPanes.Item("Folded Model").GetBrowserNodeFromObject(oOcc).BrowserNodes.Item("Origin").Expanded = True

'Code for everything else
oOcc.Parent.Document.BrowserPanes.Item("Model").GetBrowserNodeFromObject(oOcc).BrowserNodes.Item("Origin").Expanded = True

 

 

 

EDIT: found this forum topic: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/expanding-the-origin-of-a-selected-c...

 

I customized the code and got it working like this: 

 

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument

Dim sSet As SelectSet = ThisDoc.Document.SelectSet
If sSet.Count = 0 Then Return

Dim oOcc As ComponentOccurrence = sSet.Item(1)

Dim oPane As BrowserPane = oDoc.BrowserPanes.Activepane
		
Dim oCompo As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc)

Dim oNode As BrowserNode
For Each oNode In oCompo.BrowserNodes	
	If oNode.FullPath.Contains("Origin") Then
		oNode.Expanded = True
	Else If oNode.FullPath.Contains("Folded Model") Then
		Dim oNodeSheet As BrowserNodeDefinition
		oNodeSheet = oNode.BrowserNodeDefinition
		' Get the work plane browser node.
		Dim oWorkPlaneNode As BrowserNode
			oWorkPlaneNode = oCompo.AllReferencedNodes(oNodeSheet).Item(1)
		Dim oNode2 As BrowserNode
		For Each oNode2 In oWorkPlaneNode.BrowserNodes	
			If oNode2.FullPath.Contains("Origin") Then
				oNode2.Expanded = True
			End If
		Next
	End If
Next

ThisApplication.CommandManager.ControlDefinitions.Item("AppFindInBrowserCtxCmd").Execute
Message 7 of 19

WCrihfield
Mentor
Mentor
Accepted solution

Hi @amarcoc.  In order to effectively use that 'find in browser' command, you need to have a single model entity selected (a single valid object within the Document.SelectSet), before executing that command.  So, just as @Curtis_Waguespack stated, since we are doing that same action by API code, executing that command is not necessary anymore, so you can get rid of that command from the end of your code, because it is not accomplishing anything.

 

Also, here is just another example of an iLogic rule to expand the 'Origin' node, when the selected component may or may not be a sheet metal part.  I chose to create handy little custom Function just for finding a BrowserNode by its 'Label' under a given BrowserNode, even if it may be multiple levels under that node.  It will simply return the 'first' BrowserNode that it finds matching the specified label under the specified BrowserNode.  The main code first tries to find the node for the 'Folded Model'.  If found, it will then try to find the 'Origin' node under that.  If the 'Folded Model' node was not found, it then just tries to find the 'Origin' node under the main node.  Once it has the 'Origin' node, it sets it to expanded.  Once it does that, even if the component node was not expanded at all, this will immediately expand all 3 nodes (component node, folded model node, & origin node) at once.

Sub Main
	oDoc = ThisDoc.Document
	oSS = oDoc.SelectSet
	If oSS.Count = 0 Then
		MsgBox("A single component must be pre-selected before running this rule.", vbCritical, "")
		Exit Sub
	End If
	Dim oOcc As ComponentOccurrence
	If TypeOf oSS.Item(1) Is ComponentOccurrence Then
		oOcc = oSS.Item(1)
	Else
		MsgBox("A single component must be pre-selected before running this rule.", vbCritical, "")
		Exit Sub
	End If
	Dim oPane As BrowserPane = oDoc.BrowserPanes.Item("AmBrowserArrangement") 'using its 'InternalName'
	Dim oOccNode As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc)
	Dim oFMNode As BrowserNode = GetBrowserNodeByLabel(oOccNode, "Folded Model")
	Dim oOriginNode As BrowserNode = Nothing
	If IsNothing(oFMNode) Then
		oOriginNode = GetBrowserNodeByLabel(oOccNode, "Origin")
	Else
		oOriginNode = GetBrowserNodeByLabel(oFMNode, "Origin")
	End If
	If IsNothing(oOriginNode) Then
		Exit Sub
	Else
		oOriginNode.Expanded = True
	End If
End Sub

Function GetBrowserNodeByLabel(oStartBNode As BrowserNode, oLabel As String) As BrowserNode
	If IsNothing(oStartBNode) Then Return Nothing
	'look at this level completely first
	For Each oNode As BrowserNode In oStartBNode.BrowserNodes
		If oNode.BrowserNodeDefinition.Label = oLabel Then Return oNode
	Next
	'if nothing returned, now try stepping down into lower levels
	For Each oNode As BrowserNode In oStartBNode.BrowserNodes
		If oNode.BrowserNodes.Count > 0 Then GetBrowserNodeByLabel(oNode, oLabel)
	Next
	Return Nothing
End Function

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 19

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @amarcoc 

 

I think this should work for you. There might be another "special" nested browser node situation, but I can't remember what that would be right now ( Frame Generator parts maybe?).

 

In any case this version works with pre-selecting standard files and sheet metal files.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim sSet As SelectSet = ThisDoc.Document.SelectSet
If sSet.Count = 0 Then Return 'exit rule
Dim oOcc As ComponentOccurrence = sSet.Item(1)

Try: oOcc.Parent.Document.BrowserPanes.Item("Model") _
.GetBrowserNodeFromObject(oOcc).BrowserNodes.Item("Origin").Expanded = True
Catch: End Try

Try: oOcc.Parent.Document.BrowserPanes.Item("Model") _
.GetBrowserNodeFromObject(oOcc).BrowserNodes.Item("Folded Model") _
.BrowserNodes.Item("Origin").Expanded = True
Catch: End Try

 

EESignature

Message 9 of 19

amarcoc
Advocate
Advocate

Thank you both.

 

Both solutions works 🙂

0 Likes
Message 10 of 19

tgregory3G7FA
Advocate
Advocate

Is it possible to get any of these routines to work for multiple selected components?

They only work on one component at a time.  It'd be great to be able to select two (or more) components and expand both their Origin Folders at once.

It would make it so much easier when constraining two components to each others origin planes.

 

27 versions of Inventor and they still make it so hard to work with Origin Planes.  Ugh.

0 Likes
Message 11 of 19

WCrihfield
Mentor
Mentor

Hi @tgregory3G7FA.  You can give this version a try.

oDoc = ThisDoc.Document
oSS = oDoc.SelectSet : If oSS.Count = 0 Then Return
oMP = oDoc.BrowserPanes.Item("Model")
For Each oObj In oSS
	If TypeOf oObj Is ComponentOccurrence Then
		Dim oOcc As ComponentOccurrence = oObj
		oNodes = oMP.GetBrowserNodeFromObject(oOcc).BrowserNodes
		Try : oNodes.Item("Origin").Expanded = True: Catch : End Try
		Try : oNodes.Item("Folded Model").BrowserNodes.Item("Origin").Expanded = True: Catch : End Try
	End If
Next

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 19

tgregory3G7FA
Advocate
Advocate

Brilliant!

Works on multiple components and sheet metal.

Thanks.

0 Likes
Message 13 of 19

emanuel.c
Collaborator
Collaborator

Thank you gentlemen! I can't believe I've used Inventor for years, hating having to "Find in Browser" -> + + till you get to Origin.... Finally, a shortcut for that. Also, the option to use iLogic buttons in the ribbon is downright awesome.

0 Likes
Message 14 of 19

emanuel.c
Collaborator
Collaborator

@WCrihfield 

Thank you for the code which works beautifully.

I would like to expand the main assembly origin also. Have tried a few things, but can't manage to get it to work.

Can you help me with this?

Thank you!

 

0 Likes
Message 15 of 19

tgregory3G7FA
Advocate
Advocate

Try this.  It expands the main assembly origin. And it'll expand any sub assemblies if selected too.

 

'Expand Assembly Origin Folder
Dim oDoc As Document
oDoc = ThisApplication.ActiveEditDocument

Dim oPane As BrowserPane
oPane = oDoc.BrowserPanes.ActivePane

Dim oDocNode As BrowserNode
oDocNode = oPane.TopNode

Dim oNode As BrowserNode
For Each oNode In oDocNode.BrowserNodes	
	If oNode.FullPath.Contains("Origin") Then
	oNode.Expanded = True
	End If
Next

'Expand Selected Component(s) Origin Folder
oDoc = ThisDoc.Document
oSS = oDoc.SelectSet : If oSS.Count = 0 Then Return
oMP = oDoc.BrowserPanes.Item("Model")
For Each oObj In oSS
	If TypeOf oObj Is ComponentOccurrence Then
		Dim oOcc As ComponentOccurrence = oObj
		oNodes = oMP.GetBrowserNodeFromObject(oOcc).BrowserNodes
		Try : oNodes.Item("Origin").Expanded = True: Catch : End Try
		Try : oNodes.Item("Folded Model").BrowserNodes.Item("Origin").Expanded = True: Catch : End Try
	End If	
	
Next

 

Message 16 of 19

WCrihfield
Mentor
Mentor

Hi @emanuel.c.  Here is an example iLogic rule you can review for expanding the Origin folder of any type of document (including sheet metal part).  It uses a separate custom Function to get the Model pane, which is good in all languages, due to its use of their internal names, instead of local English names.

Sub Main
	Dim oDoc As Document = ThisDoc.Document
	Dim oModelPane As BrowserPane = GetModelBrowserPane(oDoc)
	Try : oModelPane.TopNode.BrowserNodes.Item("Origin").Expanded = True: Catch : End Try
	Try : oModelPane.TopNode.BrowserNodes.Item("Folded Model").BrowserNodes.Item("Origin").Expanded = True : Catch : End Try
End Sub

Function GetModelBrowserPane(oDoc As Inventor.Document) As Inventor.BrowserPane
	For Each oPane As Inventor.BrowserPane In oDoc.BrowserPanes
		If oPane.BuiltIn And oPane.TreeBrowser And _
			(oPane.InternalName = "AmBrowserArrangement" Or _
			oPane.InternalName = "DlHierarchy" Or _
			oPane.InternalName = "PmDefault") Then Return oPane
	Next : Return Nothing
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 17 of 19

emanuel.c
Collaborator
Collaborator

Thank you to both, it works beautifully. Since I frequently need both the selected part(s) and the main assembly Origins expanded it's nice to do it with one key stroke.

 

Sub Main

oDoc = ThisDoc.Document
Dim oModelPane As BrowserPane = GetModelBrowserPane(oDoc)
Try : oModelPane.TopNode.BrowserNodes.Item("Origin").Expanded = True: Catch : End Try
Try : oModelPane.TopNode.BrowserNodes.Item("Folded Model").BrowserNodes.Item("Origin").Expanded = True : Catch : End Try

oSS = oDoc.SelectSet : If oSS.Count = 0 Then Return
oMP = oDoc.BrowserPanes.Item("Model")
For Each oObj In oSS
	If TypeOf oObj Is ComponentOccurrence Then
		Dim oOcc As ComponentOccurrence = oObj
		oNodes = oMP.GetBrowserNodeFromObject(oOcc).BrowserNodes
		Try : oNodes.Item("Origin").Expanded = True: Catch : End Try
		Try : oNodes.Item("Folded Model").BrowserNodes.Item("Origin").Expanded = True: Catch : End Try
	End If
Next
End Sub

Function GetModelBrowserPane(oDoc As Inventor.Document) As Inventor.BrowserPane
	For Each oPane As Inventor.BrowserPane In oDoc.BrowserPanes
		If oPane.BuiltIn And oPane.TreeBrowser And _
			(oPane.InternalName = "AmBrowserArrangement" Or _
			oPane.InternalName = "DlHierarchy" Or _
			oPane.InternalName = "PmDefault") Then Return oPane
	Next : Return Nothing
End Function 

 

Message 18 of 19

WCrihfield
Mentor
Mentor

While the topic is still fresh, I went ahead and transformed the two main tasks out into their own separate routines, to make the more transferable into other projects.  And in the process, I just integrated the separate routine for getting the Model BrowserPane into the new routine for expanding the main document's origin node.  Although, I guess that maybe the words "Origin" & "Folded Model" may be different in other languages, so it may still not be 100% universal for all languages.  I attached the code in a text file.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 19 of 19

emanuel.c
Collaborator
Collaborator

Beautiful work! It's probably simple / short code to you, but can't tell you how grateful I am to be able to do this with one keystroke 😁

0 Likes