Message 1 of 3
Creating Attributes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Is there a better way to create custom attributes on Navisworks 2026?
The code above works fine. But in large models, he takes a lot of time.
In this new version existis something without using the ComAPI bridge?
public static void EditOrCreateProperties(ModelItem modelItem, List<Property> attributes, bool recursive)
{
if(Wait.WasCanceled())
{
return;
}
if (modelItem == null) return;
var tabName = BCfg.Diags.C0000;
CreateTab(modelItem, tabName);
var newProperties = new List<Property>();
newProperties.AddRange(attributes);
tabName = tabName.ToUpper();
InwOpState10 nwState;
nwState = ComApiBridge.State;
// Get the selection in COM
InwOpSelection comSelectionOut = ComApiBridge.ToInwOpSelection(modelItem.ToModelItemCollection());
// Get paths within the selection
InwSelectionPathsColl selectionPaths = comSelectionOut.Paths();
InwOaPath3 selectedPath = (InwOaPath3)selectionPaths.Last();
// Get properties collection of the path
InwGUIPropertyNode2 tabs = (InwGUIPropertyNode2)nwState.GetGUIPropertyNode(selectedPath, false);
// Create new tab
InwOaPropertyVec newTab = (InwOaPropertyVec)nwState.ObjectFactory(nwEObjectType.eObjectType_nwOaPropertyVec, null, null);
bool modify = false;
int tabIndex = 1;
foreach (InwGUIAttribute2 tab in tabs.GUIAttributes())
{
if (Wait.WasCanceled())
{
return;
}
if (!tab.UserDefined) continue;
if (tab.ClassUserName.ToUpper() != tabName.ToUpper())
{
tabIndex++;
continue;
}
// Add existing properties while modifying requested ones
foreach (InwOaProperty prop in tab.Properties())
{
if (Wait.WasCanceled())
{
return;
}
var match = newProperties.Find(x => x.Column == prop.UserName || x.ColumnUpper == prop.name || x.ColumnUpperOriginal == prop.UserName);
InwOaProperty newProp = nwState.ObjectFactory(nwEObjectType.eObjectType_nwOaProperty);
newProp.name = prop.name;
newProp.value = prop.value;
newProp.UserName = prop.UserName;
if (match != null)
{
var value = match.String();
if (newProp.value != value)
{
newProp.value = value;
modify = true;
}
newProperties.Remove(match);
}
newTab.Properties().Add(newProp);
}
// If the property was not found, create a new one
foreach (var prop in newProperties)
{
if (Wait.WasCanceled())
{
return;
}
InwOaProperty newProp = nwState.ObjectFactory(nwEObjectType.eObjectType_nwOaProperty);
newProp.UserName = prop.Column;
newProp.name = prop.ColumnUpper;
newProp.value = prop.String();
newTab.Properties().Add(newProp);
}
if (modify || newProperties.Count > 0)
{
newTab.Properties().Sort();
tabs.SetUserDefined(tabIndex, tab.ClassUserName, tab.ClassName, newTab);
}
break;
}
// Recursively process child elements if requested
if (recursive)
{
foreach (var child in modelItem.Children)
{
if (Wait.WasCanceled())
{
return;
}
EditOrCreateProperties(child, attributes, recursive);
}
}
}