Welcome to the seventh tutorial of
Navisworks® API series, this post will be covered how to add custom property to the existing category via
Navisworks® COM (Component Object Model) API. This tutorial is a continuation of the third tutorial of our Navisworks API series, Navisworks API : COM Interface and Adding Custom Property.
So, first things first, setting up Navisworks® Add-Ins project in the visual studio, you can follow
Creating Navisworks Add-Ins tutorial to set up the project. In the previous tutorial we’ve already discussed about the equivalent between Navisworks® .Net API objects and COM API objects. So, I strongly recommend you to go through with the previous tutorial first.
Whenever you are ready, in the
Execute method, create a list of variables :
doc to store the current document,
cdoc for COM State object (the document object),
items for the current selected ModelItems. Next variable is InwOaPath (Path object in COM) which is a ModelItem (.Net API),
ToInwOaPath method to convert a ModelItem to Path(COM object).
InwGUIProertyNode2 (COM object) is equivalent to .Net ModelItem’s PropertyCategoryCollection object and from this object we can collect all the related PropertyCategories data. (figure 1.0)
Next step is to loop through each of the PropertyCategory to check has user created? and is the category’s name is “Premier League” ( “Premier League” Category was created in the previous tutorial). (figure 1.1)
InwGUIPropertyNode2 (PropertyCategoryCollection) object allow to create PropertyCategory by using it’s
SetUserDefined method. The first argument of this method is whether create or overwrite ( “0” is to create new PropertyCategory, “1” is to overwrite existing PropertyCategory). Second and third arguments are for the DisplayName and InternalName. The last one is for PropertyCategory data, here we have called our custom method,
AddNewPropertyToExtgCategory, and provided as an argument. (figure 1.1)
AddNewPropertyToExtgCategory method, the only argument
propertyCategory is to retrieve all the existing PropertyData and the return value is
InwOaPropertyVec which is user created PropertyCategory. We have two variables,
cdoc for COM State and
category for a new PropertyCategory object. (figure 1.2)
So far, the only way to add new PropertyData to existing PropertyCategory is – create a new PropertyCategory with the same name, retrieve all the existing PropertyData and add new PropertyData.
Loop through each Property from the argument (propertyCategory) and create new Property with the existing PropertyData (such as.. InternalName, DisplayName, Value) and add to our newly created PropertyCategory. (figure 1.3)
After retrieving all the existing PropertyData, we create a new Property for our PropertyData. So, as you can see in figure 1.3, property’s Name is “2021 Champions” and Value is “Who Knows!!”. Finally add to the PropertyCategory and return user created PropertyCategory. (figure 1.3)
Well, that’s all for this tutorial, I hope not too confuse you. You can find the complete source code from here, Cheers!
Founder and Developer @twentytwo.space