Parse BrowserNodes for a specified node and perform UserInterface commands

By Xiaodong Liang

Issue

How can I select programmatically a node in the browser and perform a command as if it was performed from the UserInterface?

Solution
The following DevNote exposes some useful methods to manipulate the Inventor native BrowserPanes for PartDocuments, Assemblies and Drawings.

  • The “GetDocumentBrowserPane(oDoc As Document) As BrowserPane” method will return the main BrowserPane object independently of the type of the active document. From this object you are able to access the “TopNode” property and then all the children nodes present in the browser.

  • The “GetBrowserNodeByFullPath(FullPath As String) As BrowserNode” method will return the BrowserNode object that corresponds to the fullpath you provide as input. ‘Nothing’ is retuned if the path doesn’t exist.

  • The “GetSelectedNodesFullPath” method will print in the VBA immediate window the list of the currently selected nodes in the browser.

  • At last, the “TestSelectNode” method shows an example on how to use these previous methods to select a node in the DrawingBrowser and calls the “Get Model Sketches” command to make the assembly sketches visible in the drawing.

Note: the API is able to select only one node at a time, i.e. calling “oNode.DoSelect” on a second will unselect the first one. If you want to perform the same command on several nodes, you will need to execute it several times, one for each node.

Here are the samples. The code can be cut & paste directly in VBA:

‘Get the Main BrowserPane for any type of document
Public Function GetDocumentBrowserPane(oDoc As Document) As BrowserPane

    Dim oModelBP As BrowserPane
    
    ‘Get the Model BrowserPane for Part
    If (TypeOf oDoc Is PartDocument) Then
        Set oModelBP = oDoc.BrowserPanes.Item("PmDefault")
    End If
    
    ‘Get the Model BrowserPane for Assembly
    If (TypeOf oDoc Is AssemblyDocument) Then
        Set oModelBP = oDoc.BrowserPanes.Item("AmBrowserArrangement")
    End If
    
    ‘Get the Model BrowserPane for Drawing
    If (TypeOf oDoc Is DrawingDocument) Then
        Set oModelBP = oDoc.BrowserPanes.Item("DlHierarchy")
    End If
    
    Set GetDocumentBrowserPane = oModelBP

End Function

Public Function GetBrowserNodeByFullPathRecursive(oBrowserNode As BrowserNode, FullPath As String) As BrowserNode

    If (oBrowserNode.FullPath = FullPath) Then
        Set GetBrowserNodeByFullPathRecursive = oBrowserNode
        Exit Function
    End If

    Dim oBrowserNodeIterator As BrowserNode
    For Each oBrowserNodeIterator In oBrowserNode.BrowserNodes
    
        Dim oResultNode As BrowserNode
        Set oResultNode = GetBrowserNodeByFullPathRecursive(oBrowserNodeIterator, FullPath)
        
        If Not oResultNode Is Nothing Then
            Set GetBrowserNodeByFullPathRecursive = oResultNode
            Exit Function
        End If
    
    Next
    
End Function

Public Function GetBrowserNodeByFullPath(FullPath As String) As BrowserNode

    Dim oModelBP As BrowserPane
    Set oModelBP = GetDocumentBrowserPane(ThisApplication.ActiveDocument)
    
    Set GetBrowserNodeByFullPath = GetBrowserNodeByFullPathRecursive(oModelBP.TopNode, FullPath)
    
End Function

Public Sub GetSelectedNodesFullPathRecursive(oBrowserNode As BrowserNode)

    If (oBrowserNode.Selected = True) Then
        Debug.Print " – " + oBrowserNode.FullPath
    End If

    Dim oBrowserNodeIterator As BrowserNode
    For Each oBrowserNodeIterator In oBrowserNode.BrowserNodes
        Call GetSelectedNodesFullPathRecursive(oBrowserNodeIterator)
    Next

End Sub

Public Sub GetSelectedNodesFullPath()

    ‘Get the Model BrowserPane
    Dim oModelBP As BrowserPane
    Set oModelBP = GetDocumentBrowserPane(ThisApplication.ActiveDocument)

    Call GetSelectedNodesFullPathRecursive(oModelBP.TopNode)

End Sub

Public Sub TestSelectNode()

    Dim oBrowserNode As BrowserNode
    Set oBrowserNode = GetBrowserNodeByFullPath("Assembly1:Sheet:1:VIEW1:Assembly1.iam:Assembly1.iam")

    ‘Ensures Node is expanded, some commands don’t work otherwise
    oBrowserNode.Expanded = True
    oBrowserNode.DoSelect
    
    ‘Call command that is not available in API
    Dim InternalCommandName As String
    
    InternalCommandName = "DrawingGetModelSketchesCtxCmd"
    ThisApplication.CommandManager.ControlDefinitions.Item(InternalCommandName).Execute

End Sub

 

VB.NET Code

Public Function GetDocumentBrowserPane(ByVal oDoc As Document) As BrowserPane        Dim oModelBP As BrowserPane = Nothing             'Get the Model BrowserPane for Part        If (TypeOf oDoc Is PartDocument) Then            oModelBP = oDoc.BrowserPanes.item("PmDefault")        End If             'Get the Model BrowserPane for Assembly        If (TypeOf oDoc Is AssemblyDocument) Then            oModelBP = oDoc.BrowserPanes.item("AmBrowserArrangement")        End If             'Get the Model BrowserPane for Drawing        If (TypeOf oDoc Is DrawingDocument) Then            oModelBP = oDoc.BrowserPanes.item("DlHierarchy")        End If             GetDocumentBrowserPane = oModelBP    End Function         Public Function GetBrowserNodeByFullPathRecursive(ByVal oBrowserNode As BrowserNode, ByVal FullPath As String) As BrowserNode             If (oBrowserNode.FullPath = FullPath) Then            GetBrowserNodeByFullPathRecursive = oBrowserNode            Exit Function        End If        Dim oBrowserNodeIterator As BrowserNode        Dim oResultNode As BrowserNode = Nothing        For Each oBrowserNodeIterator In oBrowserNode.BrowserNodes                      oResultNode = GetBrowserNodeByFullPathRecursive(oBrowserNodeIterator, FullPath)                 If Not oResultNode Is Nothing Then                GetBrowserNodeByFullPathRecursive =                                        oResultNode                Exit Function            End If             Next             Return oResultNode    End Function         Public Function GetBrowserNodeByFullPath(ByVal FullPath As String) As BrowserNode             Dim m_inventorApp As               Inventor.Application = Nothing        ' Try to get an active instance of Inventor        Try            m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")        Catch ex As Exception        End Try        ' If not active, create a new Inventor session        If m_inventorApp Is Nothing Then            Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")            m_inventorApp = System.Activator.CreateInstance(inventorAppType)        End If             Dim oModelBP As BrowserPane        oModelBP = GetDocumentBrowserPane(m_inventorApp.ActiveDocument)             GetBrowserNodeByFullPath = GetBrowserNodeByFullPathRecursive(oModelBP.TopNode, FullPath)         End Function    Public Sub GetSelectedNodesFullPathRecursive(           ByVal oBrowserNode As BrowserNode)            If (oBrowserNode.Selected = True) Then            Debug.Print(" - " + oBrowserNode.FullPath)        End If        Dim oBrowserNodeIterator As BrowserNode        For Each oBrowserNodeIterator In oBrowserNode.BrowserNodes            GetSelectedNodesFullPathRecursive(oBrowserNodeIterator)        Next        End Sub         Public Sub GetSelectedNodesFullPath()        Dim m_inventorApp As Inventor.Application = Nothing        ' Try to get an active instance of Inventor        Try            m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")        Catch ex As Exception        End Try        ' If not active, create a new Inventor session        If m_inventorApp Is Nothing Then            Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")            m_inventorApp = System.Activator.CreateInstance(inventorAppType)        End If            
'Get the Model BrowserPane        Dim oModelBP As BrowserPane        oModelBP = GetDocumentBrowserPane(m_inventorApp.ActiveDocument)        GetSelectedNodesFullPathRecursive(oModelBP.TopNode)    End Sub         Public Sub TestSelectNode()             Dim m_inventorApp As Inventor.Application = Nothing        ' Try to get an active instance of Inventor        Try            m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")        Catch ex As Exception        End Try        ' If not active, create a new Inventor session        If m_inventorApp Is Nothing Then            Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")            m_inventorApp = System.Activator.CreateInstance(inventorAppType)        End If             Dim oBrowserNode As BrowserNode        oBrowserNode = GetBrowserNodeByFullPath("Assembly1:Sheet:1:VIEW1:Assembly1.iam:Assembly1.iam")        'Ensures Node is expanded, some commands don't work otherwise        oBrowserNode.Expanded = True        oBrowserNode.DoSelect()             'Call command that is not available in API        Dim InternalCommandName As String             InternalCommandName = "DrawingGetModelSketchesCtxCmd"        m_inventorApp.CommandManager.ControlDefinitions.Item(InternalCommandName).Execute()    End Sub

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading