Find Node from occurrence

Find Node from occurrence

dialunau
Advocate Advocate
1,051 Views
5 Replies
Message 1 of 6

Find Node from occurrence

dialunau
Advocate
Advocate

Hello everyone, I'm trying to get the node where a certain occurrence is located in the browser so I can expand that node and see the items related to this component, but whenever I try to get the node from the occurrence I'm getting an "unspecified error" message.

 

Do you have any ideas for what is causing this error?

This is my code so far:

 

Sub Main
	Dim oDrawDoc As DrawingDocument = ThisDoc.Document
	Dim oSheet As Sheet = oDrawDoc.ActiveSheet
	Dim oView As DrawingView = oSheet.DrawingViews.Item(1)
	Dim oAssyDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument

	Dim oOcc As ComponentOccurrence = oAssyDoc.ComponentDefinition.Occurrences.Item(2)
	MsgBox(oOcc.Name)
	Dim oBP As BrowserPane = ThisDoc.Document.BrowserPanes.Item("Model")
	Dim oNode As BrowserNode = oBP.GetBrowserNodeFromObject(oOcc)
End Sub

 

0 Likes
1,052 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Is there more than one view of the assembly within the whole drawing document?  If so, there might be multiple instances of that component found within the entire model browser tree of the drawing.  Just one thought.  You may have to get the node for the view first, then iterate down through the BrowserNodes below that point, perhaps using a recursive routine, to identify the one for that specific component.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

dialunau
Advocate
Advocate

I just deleted the rest of them so I can only have view 1 and unfortunately I still have the same result.

0 Likes
Message 4 of 6

A.Acheson
Mentor
Mentor

This example post talks about the same error. It looks like you get the drawing view node then iterate through the nodes to get to the node you need. If you look at the nativeobject you can match the occurrence. Here is a sample I kindly had some help with that showed the methods to use. 

 

 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 5 of 6

dialunau
Advocate
Advocate

Hello,

I tested the code and it works fine. The problem is that it expands all the instances of that occurrence in the browser. I somehow managed to expand only one instance of a specific view. However, this will only work in the specific case that the view I'm looking for is the second iteration of the occurrence instance.

I've been trying to get the exact node of the specific occurrence regardless of the order or the type of the views (namely a section, detail or isometric view) but haven't had luck yet.

Any suggestions?

 

The code looks like this:

 

Sub Main()
		'//// GET THE OCCURRENCE NAME TO FIND THE NODE ////
		Dim oOccName = "Insulation1:1"
oDrawDoc = ThisApplication.ActiveDocument Dim oBP As BrowserPane = oDrawDoc.BrowserPanes.Item("Model") oBP.Activate Dim oTNode As BrowserNode = oBP.TopNode Dim objColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection Dim oSheet As Sheet = ThisApplication.ActiveDocument.ActiveSheet Dim oView As DrawingView = oSheet.DrawingViews.Item(2) Dim oCount As Integer = 0 Call Get_SelectedNodes(oTNode, objColl, oOccName, oView) '//// EXPAND THE SECOND VIEW //// Dim oNode As BrowserNode For Each oNode In objColl oCount = oCount + 1 If oSheet.DrawingViews.Item(oCount) Is oView Then oNode.Expanded = True oNode.DoPreSelect() oNode.DoSelect() Exit For End If Next oNode End Sub Public Sub Get_SelectedNodes(oTNode As BrowserNode, ByRef objColl As ObjectCollection, oOccName As String, oView As DrawingView) Dim oNode As BrowserNode For Each oNode In oTNode.BrowserNodes If oNode.BrowserNodeDefinition.Label = oOccName Then Call objColl.Add(oNode) ElseIf oNode.BrowserNodes.Count > 0 Then Call Get_SelectedNodes(oNode, objColl, oOccName, oView) ElseIf oNode.BrowserNodes.Count < 0 Then End If Next End Sub

 

 

0 Likes
Message 6 of 6

A.Acheson
Mentor
Mentor

It looks like you made good progress. Here is a version that will look for the occurrence in each view of the sheet and expand if found, I added an error catch as there is some exceptions found. 

Sub Main()
		'//// GET THE OCCURRENCE NAME TO FIND THE NODE ////
		Dim oOccName As String = "1237-M-001:1"

		Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
		
		Dim oBP As BrowserPane = oDrawDoc.BrowserPanes.Item("Model")
		oBP.Activate
		
		Dim oTNode As BrowserNode = oBP.TopNode
		
	    Dim objColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
		Dim oSheet As Sheet = ThisApplication.ActiveDocument.ActiveSheet
		
		Dim oCount As Integer = 0
		On Error Resume Next
		For Each oView As DrawingView In oSheet.DrawingViews
	
			'Get the browser node definition of the drawing view
			Dim oNativeBrowserNodeDef As NativeBrowserNodeDefinition 
			Dim oViewNode As BrowserNode
			'Use the try statement or On Error as a child view will not be a Native Browser Definition, 
			'it will be picked up By the recursion Loop later
			oNativeBrowserNodeDef = oDrawDoc.BrowserPanes.GetNativeBrowserNodeDefinition(oView)
			
			'Get the browser node Of the View Using the browser node definition
			oViewNode = oTNode.AllReferencedNodes(oNativeBrowserNodeDef).Item(1)
		
			oViewNode.DoPreSelect() 
			oViewNode.DoSelect()
			'MessageBox.Show(oViewNode.BrowserNodeDefinition.Label, "Drawing View Title")

			oCount = oCount + 1
		
		    Call Get_SelectedNodes(oViewNode , objColl, oOccName, oView)
			
			'//// EXPAND THE SPECIFIC Object ////
				Dim oNode As BrowserNode
				For Each oNode In objColl
					Logger.Info(oNode.FullPath)

						oNode.Expanded = True  
						oNode.DoPreSelect() 
						oNode.DoSelect()
			    Next oNode
			Next
	End Sub

	Public Sub Get_SelectedNodes(oViewNode As BrowserNode, ByRef objColl As ObjectCollection, oOccName As String, oView As DrawingView)
	    Dim oNode As BrowserNode
	    For Each oNode In oViewNode.BrowserNodes
				If oNode.BrowserNodeDefinition.Label = oOccName Then
					Call objColl.Add(oNode)
				ElseIf oNode.BrowserNodes.Count > 0 Then
		            Call Get_SelectedNodes(oNode, objColl, oOccName, oView)
				ElseIf oNode.BrowserNodes.Count < 0 Then
		        End If
		Next
	End Sub

AAcheson_0-1649028144985.png

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes