By Wayne Brill
This VB.NET example is a Visual Studio 2012 project that connects to Inventor from out of process. It has one button on a form. When the button is clicked it prompts you to select a DrawingCurveSegment. When you select an edge in one of the views it finds the browser node for the part or assembly and expands it. The part or assembly is also selected.
This Post also selects a node: Get BrowserNode of an occurrence
Before running the code:
After the code was run. Notice the browser is expanded and the part is selected.
To test have a drawing open that has views that reference an assembly or part. This example would need to be updated to handle every possible view that could exist.
Here is the pertinent code:
Public Sub browserSelect()
Dim odoc As Inventor.Document
odoc = m_InvApp.ActiveDocument
Dim oSelectSet As Inventor.SelectSet
oSelectSet = odoc.SelectSet
' Get the selected curve from the select set.
Dim oDrawCurveSeg As DrawingCurveSegment
oDrawCurveSeg = m_InvApp.CommandManager.Pick _
(Inventor.SelectionFilterEnum.
kDrawingCurveSegmentFilter,
"Select a drawingCurve")
' Get the parent DrawingCurve
Dim oDrawCurve As DrawingCurve
oDrawCurve = oDrawCurveSeg.Parent
' Get the model geometry this curve represents.
Dim oModelGeom As Object
oModelGeom = oDrawCurve.ModelGeometry
' Check to see if the returned object supports
' the ContainingOccurrence property.
Dim oOccurrence As ComponentOccurrence
'oOccurrence = oModelGeom.ContainingOccurrence
Dim oModelGeometry =
oDrawCurveSeg.Parent.ModelGeometry
Dim oCompDef As ComponentDefinition
oCompDef =
oModelGeometry.parent.componentdefinition
partNameToTest = oCompDef.Document.DisplayName
Dim intLength As Integer
intLength = partNameToTest.Length()
' Going to use this to test to see if it is
' the right node Need to remove the extension
' and the dot
partNameToTest = partNameToTest.Remove _
(intLength - 4)
Try
oOccurrence = oModelGeom.ContainingOccurrence
' Get the drawing view
Dim oDrwView As DrawingView
oDrwView = oDrawCurve.Parent
'Get the browser node definition of the
' drawing view
Dim oNativeBrowserNodeDef As _
NativeBrowserNodeDefinition
oNativeBrowserNodeDef =
odoc.BrowserPanes. _
GetNativeBrowserNodeDefinition(oDrwView)
' Testing - Get an error eNotImplemented
'Dim oNativeBrowserNodeDef2 As NativeBrowserNodeDefinition
'oNativeBrowserNodeDef2 = _
'odoc.BrowserPanes.GetNativeBrowserNodeDefinition(oOccurrence)
'Get the top browser node
Dim oTopBrowserNode As BrowserNode
oTopBrowserNode =
m_InvApp.ActiveDocument.BrowserPanes. _
ActivePane.TopNode
' Get the browser node of the view using the
' browser node definition
Dim oNode As BrowserNode
oNode = oTopBrowserNode.AllReferencedNodes _
(oNativeBrowserNodeDef).Item(1)
If oNode.BrowserNodes.Count > 0 Then
Call recurseNodes(oNode)
bNodeFound = False
End If
'END WB added
Catch ex As Exception
MsgBox("Problem getting part " & partNameToTest)
Exit Sub
End Try
End Sub
Sub recurseNodes(node As BrowserNode)
Dim strNodeLabel As String
strNodeLabel =
node.BrowserNodeDefinition.Label
'See if the node label contains
' the name of the part
If InStr(strNodeLabel,
partNameToTest) = 1 Then
'Expand the node of the part
node.Expanded = True
'Select the node of the part
node.DoSelect()
' make boolean flag true
'so Exit For will be called
bNodeFound = True
End If
Dim bn As BrowserNode
For Each bn In
node.BrowserNodes
If bNodeFound = False Then
'The base view can have
' other views under it
' - skip them
If Not TypeName(bn.NativeObject) _
= "DrawingView" Then
Call recurseNodes(bn)
Else
'Skipping the DrawingViews
' under the Base View
Continue For
End If
Else
'Node has been found so
'exit the loop
Exit For
End If
Next
End Sub
Project file: Download BrowserSelectTest