How to keep the the BrowserNode position of the ClientFeature

By Wayne Brill

Issue

I create a ClientFeature which contains some elements. I then remove the elements in the ClientFeature and add new elements.  But the BrowserNode position of the ClientFeature changes to the node position of the first element. Is there a way to keep the the BrowserNode of the ClientFeature at its original position?

Solution

As of the 2009 release the only way to achieve this is to put the BrowserNode of  the first element right after that of the ClientFeature first and then add the element .

The VB.NET code below shows how this is done. Use the attached part file (link below) with this example. There is a ClientFeature which does not have any elements.  The code will add ExtrudeFeatures(4), ExtrudeFeatures(5) and all ResolveFeatures. First the elements are added to an object array, then moved their node right after the node of ClientFeature one by one, and the elements are added one by one.

Sub CreateClientFeature()
    Dim m_inventorApp As  _
        Inventor.Application = Nothing
    Dim m_quitInventor As Boolean = False
 
    ' 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)
        m_quitInventor = True
    End If
 
    Dim oDoc As PartDocument
    oDoc = m_inventorApp.ActiveDocument
    Dim oDef As PartComponentDefinition
    oDef = oDoc.ComponentDefinition
    Dim oElements() As Object
    ' Check if ClientFeature allready exist  
    If oDef.Features.ClientFeatures.Count > 0 Then
        'ReDim oElements(1 To 1)  
        ReDim oElements(0 To 1)
 
        oElements(0) = oDef.Features. _
                        ExtrudeFeatures.Item(4)
        ReDim Preserve oElements _
                     (0 To UBound(oElements) + 1)
 
        oElements(1) = oDef.Features _
                         .ExtrudeFeatures.Item(5)
        oElements(2) = oDef.Features. _
                         ExtrudeFeatures.Item(6)
        Dim oFeature As PartFeature
        For Each oFeature In oDef.Features
            If TypeOf oFeature Is RevolveFeature Then
                ReDim Preserve _
                  oElements(0 To UBound _
                                 (oElements) + 1)
                oElements(UBound(oElements)) _
                                        = oFeature
            End If
        Next
        'get the current BrowserPane, such as 
        ' in this case, the pane is "PmDefault"     
        Dim oPane As BrowserPane
        oPane = oDoc.BrowserPanes.Item("PmDefault")
        ' Use existing one      
        Dim oClFeat As ClientFeature
        oClFeat = oDoc.ComponentDefinition _
            .Features.ClientFeatures.Item(1)
        ' get the BrowserNodeDefinition 
        ' of ClientFeature       
        Dim oClientBrowserNodeDef As  _
                            BrowserNodeDefinition
        oClientBrowserNodeDef = _
                          oClFeat.BrowserNode _
                           .BrowserNodeDefinition
        ' ClientFeature provides its BrowserNode
        ' directly, however when I used it, 
        ' there is some issue. So I have to 
        ' use its BrowserNodeDefinition to get     
        ' BrowserNode.      
        Dim oClientBrowserNode As BrowserNode
        oClientBrowserNode = oPane.TopNode. _
         AllReferencedNodes _
              (oClientBrowserNodeDef).Item(1)
 
        ' move position of element node one by one 
        ' and Add them one by one   
        Dim oFIndex As Integer
        'For oFIndex = 1 To UBound(oElements, 1)
        For oFIndex = 0 To UBound(oElements, 1)
            Dim oEveryObj As Object
            oEveryObj = oElements(oFIndex)
            ' get the BrowserNodeDefinition 
            ' of the element, because for most 
            ' object, there is no direct way
            ' to get its BrowserNode, we have 
            ' to get its BrowserNodeDefinition 
            ' first by 
            ' GetNativeBrowserNodeDefinition.      
            Dim oElementBrowserNodeDef As  _
                               BrowserNodeDefinition
            oElementBrowserNodeDef = _
                oDoc.BrowserPanes _
                .GetNativeBrowserNodeDefinition _
                                         (oEveryObj)
 
            Dim oElementBrowserNode As BrowserNode
            oElementBrowserNode = oPane.TopNode _
                .AllReferencedNodes _
                (oElementBrowserNodeDef).Item(1)
 
            ' put the element node near after the
            ' node of ClientFeature    
            Call oPane.Reorder _
                (oClientBrowserNode, False, _
                                  oElementBrowserNode)
            'add the element      
            Call oClFeat.Definition. _
                ClientFeatureElements.Add _
                                   (oEveryObj, False)
        Next
    Else
        ' Create a new one    
        Dim begin As Object
        begin = oDef.Features.ExtrudeFeatures.Item(2)
        Dim oCDF As ClientFeatureDefinition
        Dim oClientFeature As ClientFeature
        oCDF = oDef.Features.ClientFeatures _
            .CreateDefinition("TestFeature", _
                        begin, begin, Nothing)
        oClientFeature = oDef.Features _
                        .ClientFeatures.Add _
                     (oCDF, "TestClientFeatureID")
    End If
End Sub
 
Download ClientFeature_BrowserNode

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading