This blog post includes sample code using which we can use the Navisworks .NET API to focus on selected items and hide all remaining model items.
The code first creates a collection of the model items that will be hidden and does the same for items that should be made visible. While iterating through each of the selected items, it adds the selected model items parents (Ancestors) and children (Descendants) to the visible items collection. It then add the siblings of the visible items to the hidden collection so that they are not displayed and since this might include the selected items too, it removes these items from the hidden items collection. Finally, it uses the ActiveDocument.Models.SetHidden() method to hide the items that are contained in this list.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
//Add two new namespaces
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;
namespace BasicPlugIn
{
[PluginAttribute("BasicPlugIn.ABasicPlugin",
"ADSK",
ToolTip = "BasicPlugIn.ABasicPlugin tool tip",
DisplayName = "Sample Plugin")]
public class ABasicPlugin : AddInPlugin
{
public override int Execute(params string[] parameters)
{
//Create hidden collection
ModelItemCollection hidden = new ModelItemCollection();
//create a store for the visible items
ModelItemCollection visible = new ModelItemCollection();
//Add all the items that are visible to the visible collection
foreach (ModelItem item in
Autodesk.Navisworks.Api.Application.
ActiveDocument.CurrentSelection.SelectedItems)
{
if (item.AncestorsAndSelf != null)
visible.AddRange(item.AncestorsAndSelf);
if (item.Descendants != null)
visible.AddRange(item.Descendants);
}
//mark as invisible all the siblings of the visible items
foreach (ModelItem toShow in visible)
{
if (toShow.Parent != null)
{
hidden.AddRange(toShow.Parent.Children);
}
}
//remove the visible items from the collection
foreach (ModelItem toShow in visible)
{
hidden.Remove(toShow);
}
//hide the remaining items
Autodesk.Navisworks.Api.Application.
ActiveDocument.Models.SetHidden(hidden, true);
return 0;
}
}
}