How to collect Parent item (as Type Element) and iterate the children

T52K-BIB
Enthusiast

How to collect Parent item (as Type Element) and iterate the children

T52K-BIB
Enthusiast
Enthusiast

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
api.png

 

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;
        }
    }
}
0 Likes
Reply
184 Views
1 Reply
Reply (1)

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @T52K-BIB ,

 

You can use the SEARCH API for this purpose.
Type.png

In the selection tree:

  • Selecting "Walls" returns a type value of "Category".
  • Selecting "Basic Wall" returns a type value of "Family".
  • Selecting "Wall-Ext_102Bwk-75Ins-100LBlk-12P" returns a type value of "Type".

To access the required type element, you can search for the "Type" property with a display value of "Type". Let’s refer to this as the parent element. Once you retrieve the parent element, you can iterate through its child elements using the following code:

ModelItem parentItem;
ModelItemEnumerableCollection childrenItems = parentItem.Children;


Search API:
https://adndevblog.typepad.com/aec/2012/05/navisworks-net-api-find-item.html
https://adndevblog.typepad.com/aec/2012/07/use-search-api.html
 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes