Creating Attributes

Creating Attributes

xaotix
Contributor Contributor
1,605 Views
2 Replies
Message 1 of 3

Creating Attributes

xaotix
Contributor
Contributor

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);
        }
    }
}

 

0 Likes
1,606 Views
2 Replies
Replies (2)
Message 2 of 3

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @xaotix ,


Please take a look at this discussion

https://forums.autodesk.com/t5/navisworks-api-forum/naviswroks-2024-vs-2025-speed-problem/td-p/13294... 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 3

mikhail_khrushchev
Explorer
Explorer
        public static void TransactionWrap(TransactionDelegate delegat)
        {
            Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
            using (var tr = new Transaction(oDoc, "Custom_Transaction"))
            {
                delegat.Invoke();
                tr.Commit();
            }
        }

One thing you miss - transactions. 

In your case each model item will make it's own transaction - and this why it so slow. 

 

You could wrap it into Autodesk.Navisworks.Api.Transaction . In example your code shall be in delegate.