How to collect Parent item (as Type Element) and iterate the children
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to extract the parent items like the below image from the selection like a filter and add them to the modelitemcollection list then iterate the children's item collect in another modelitemcollection
this is the code I'm trying to explore but it can count the items too
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.ComApi;
using Autodesk.Navisworks.Api.Interop.ComApi;
using Autodesk.Navisworks.Api.Plugins;
using Microsoft.SqlServer.Server;
using System;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
namespace PropertyIntegrator
{
[Plugin("PCCheck", "NavisManager", DisplayName = "PCCheck", ToolTip = "Export selected items with their properties and values")]
public class AddPropertyPlugin : AddInPlugin
{
public override int Execute(params string[] parameters)
{
// Get the active document
Document activeDocument = Autodesk.Navisworks.Api.Application.ActiveDocument;
// Ensure a valid document and a valid selection
if (activeDocument == null || activeDocument.CurrentSelection.SelectedItems.Count == 0)
{
MessageBox.Show("No selection found. Please select an item and try again.", "Error");
return -1;
}
try
{
ModelItemCollection all = new ModelItemCollection();
ModelItemCollection parentItems = new ModelItemCollection();
ModelItemCollection childItems = new ModelItemCollection();
// Traverse all descendants
foreach (var item in activeDocument.CurrentSelection.SelectedItems)
{
parentItems.Add(item.Parent);
}
// Count the number of parents and children
int parentCount = parentItems.Count;
// Show message with counts
MessageBox.Show($"Number of Parents: {parentCount}", "Hierarchy Count");
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}", "Error");
}
return 0;
}
}
}