Get BrowserNode of an occurrence

By Adam Nagy

There is a function for exactly this purpose called GetBrowserNodeFromObject, but unfortunately, it seems that it does not always return the correct BrowserNode object. E.g. in case of an occurrence which is part of a pattern feature, it would return a BrowserNode object which seems like some internal object. And so if you try to call EnsureVisible() on it, then Inventor will throw an error. 

If you run into such a situation then you could find the correct node using the following code: 

public BrowserNode findNode(  BrowserNode node, ComponentOccurrence occ)
{
  // Use try/catch because the node may not have a NativeObject   // and would throw an exception here
  try
  {
    if (node.NativeObject == occ)
      return node;
  }
  catch { }
 
  foreach (BrowserNode subNode in node.BrowserNodes)
  {
    BrowserNode returned = findNode(subNode, occ);
    if (returned != null)
      return returned;
  }
 
  return null;
}
 
private void SelectNode(  AssemblyDocument oDoc, ComponentOccurrence occ)
{
  // Get The Browser Pane by BrowserInternalName
  BrowserPane browserPane =     oDoc.BrowserPanes["AmBrowserArrangement"];
 
  BrowserNode occNode = findNode(browserPane.TopNode, occ);   
 
  // Select node
  occNode.EnsureVisible();
 
  // EnsureVisible seems to highlight it
  // so this is probably not needed
  occNode.DoSelect();
}

Bolts

 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading