Message 1 of 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Solved! Go to Solution.
Solved! Go to Solution.
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...
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;
}
}
}
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.
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;
}
}
}