How to retrieve the "Phase Created Name" property from the selection tree using the Navisworks API

How to retrieve the "Phase Created Name" property from the selection tree using the Navisworks API

VanQuyet.Doan
Enthusiast Enthusiast
553 Views
4 Replies
Message 1 of 5

How to retrieve the "Phase Created Name" property from the selection tree using the Navisworks API

VanQuyet.Doan
Enthusiast
Enthusiast

Phase Created.png

0 Likes
Accepted solutions (1)
554 Views
4 Replies
Replies (4)
Message 2 of 5

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @VanQuyet.Doan ,

 

Please take a look at this below link
https://forums.autodesk.com/t5/navisworks-api/how-to-get-all-properties-under-a-propertycategory-fro... 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 3 of 5

VanQuyet.Doan
Enthusiast
Enthusiast

I made a loop through the objects and select the first one, but it throws the error "object reference not set to an instance of an object". Can you help me test the code?

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;

namespace NavisworkApiSample

{
    public class B1 : AddInPlugin
    {
        public override int Execute(params string[] parameters)
        {
            // current document
            Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
            // display message
            StringBuilder message = new StringBuilder();
            //Create a new search
            Search s = new Search();

            //set the selection to everything
            s.Selection.SelectAll();

            //Add a search condition
            s.SearchConditions.Add(SearchCondition.HasCategoryByName(PropertyCategoryNames.Geometry));

            ////get the resulting collection by applying this search
            //ModelItemCollection searchResults = s.FindAll(Autodesk.Navisworks.Api.Application.ActiveDocument, true);

            ModelItem item = s.FindFirst(Autodesk.Navisworks.Api.Application.ActiveDocument, false);

            if (item != null)
            {
                // make selection
                doc.CurrentSelection.Add(item);
                // get modelitem's Element category by display name method
                PropertyCategory elementCategory = item.PropertyCategories.
                    FindCategoryByDisplayName("Element");
                // all properties of Element category
                DataPropertyCollection dataProperties = elementCategory.Properties;
                // display properties count 
                message.Append(String.Format("[{0}] ModelItem's Element Category has {1} Properties.\n",
                    Id.ToString(), dataProperties.Count));
                // index
                int index = 1;
                // iterate properties
                foreach (DataProperty dp in elementCategory.Properties)
                {
                    // append to display "Property Display Name & Property Value(includes DataType)"
                    message.Append(String.Format("{0}. {1} => {2}\n", index, dp.DisplayName, dp.Value));
                    // index increment
                    index += 1;
                }
                // display message
                MessageBox.Show(message.ToString(), "Element Category");
            }
            return 0;
        }

    }

}
0 Likes
Message 4 of 5

naveen.kumar.t
Autodesk Support
Autodesk Support
Accepted solution

Hi @VanQuyet.Doan ,

 

I think you are facing an issue on this line

PropertyCategory elementCategory = item.PropertyCategories. FindCategoryByDisplayName("Element");

Please check whether the PropertyCategory with the display name "Element" is present in the PropertyCategories or not.

I need some more details to analyze this issue further.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 5 of 5

VanQuyet.Doan
Enthusiast
Enthusiast

hi @naveen.kumar.t ,
Thank you for your response, it completely solved the problem I was having. The PropertyCategory named "Element" was not present in the PropertyCategories collection. Since then, I have corrected the search condition.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;

namespace NavisworkApiSample

{
    public class SearchModelItemAndCollectProperties : AddInPlugin
    {
        public override int Execute(params string[] parameters)
        {
            // current document
            Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
            // display message
            StringBuilder message = new StringBuilder();

            //Create a new search
            Search search = new Search();

            //set the selection to everything
            search.Selection.SelectAll();

            //Add a search condition
            search.SearchConditions.Add(SearchCondition.HasPropertyByDisplayName("Element", "Id"));

            ModelItem item = search.FindFirst(Autodesk.Navisworks.Api.Application.ActiveDocument, false);

            if (item != null)
            {
                // make selection
                doc.CurrentSelection.Add(item);
                // get modelitem's Element category by display name method
                PropertyCategory elementCategory = item.PropertyCategories.
                    FindCategoryByDisplayName("Element");
                // all properties of Element category
                DataPropertyCollection dataProperties = elementCategory.Properties;
                // index
                int index = 1;
                // iterate properties
                foreach (DataProperty dp in elementCategory.Properties)
                {
                    // append to display "Property Display Name & Property Value(includes DataType)"
                    message.Append(String.Format("{0}. {1} => {2}\n", index, dp.DisplayName, dp.Value));
                    // index increment
                    index += 1;
                }
                // display message
                MessageBox.Show(message.ToString(), "Element Category");
            }
            return 0;
        }

    }

}
0 Likes