Search code examples
c#.netautodeskautodesk-navisworks

c# NavisWorks ModelItem isReadOnly() always true


I try to add some new property in existing category for selected NavisWorks ModelItem`s

There is not so many example over network, and it base on same COM approach. However there special method to add property available in API. Only issue that objects is locked. Is there any way to unlock it?

    using ANA = Autodesk.Navisworks.Api;
...
    private void addProperty(string category, string prop, string value)
    {
        var oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
        ModelItemCollection selectionModelItems = new ModelItemCollection();
        ANA.Application.ActiveDocument.CurrentSelection.SelectedItems.CopyTo(selectionModelItems);
        //Clear the current selection
        ANA.Application.ActiveDocument.CurrentSelection.Clear();
        try
        {
            foreach (ModelItem m in selectionModelItems)
            {
                foreach (PropertyCategory p in m.PropertyCategories)
                {
                    if (p.DisplayName != category) continue;
                    var property = new DataProperty(prop, prop, new VariantData(value));
                    p.Properties.Add(property);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

result of execution:

Navisworks Category of ItemModel is ReadOnly, how to modify it?


Solution

  • Properties and categories which are created by Navisworks are read-only. You can not add or modify properties or categories which are created by Navisworks.

    You can only create or add user-defined properties - using COM. See:

    Here is a code snippet (copied from xiaodong.liang forum post mentioned above) which shows how to add a user-defined property using COM:

    private void addProperty() {
    
      ComApi.InwOpState10 state;
    
      state = ComApiBridge.ComApiBridge.State;
    
      ModelItemCollection modelItemCollectionIn = new ModelItemCollection(Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.SelectedItems);
    
      ComApi.InwOpSelection comSelectionOut =
    
        ComApiBridge.ComApiBridge.ToInwOpSelection(modelItemCollectionIn);
    
      ComApi.InwSelectionPathsColl oPaths = comSelectionOut.Paths();
    
      ComApi.InwOaPath3 oPath = (ComApi.InwOaPath3) oPaths.Last();
    
      ComApi.InwGUIPropertyNode2 propn = (ComApi.InwGUIPropertyNode2) state.GetGUIPropertyNode(oPath, true);
    
      ComApi.InwOaPropertyVec newPvec = (ComApi.InwOaPropertyVec) state.ObjectFactory(Autodesk.Navisworks.Api.Interop.ComApi.nwEObjectType.eObjectType_nwOaPropertyVec, null, null);
    
      ComApi.InwOaProperty newP = (ComApi.InwOaProperty) state.ObjectFactory(Autodesk.Navisworks.Api.Interop.ComApi.nwEObjectType.eObjectType_nwOaProperty, null, null);
    
      newP.name = "LXD_Property_Name";
    
      newP.UserName = "LXD_Property_UserName";
    
      newP.value = "LXD_Property_Value";
    
      newPvec.Properties().Add(newP);
    
      propn.SetUserDefined(0, "LXD_PropertyTab_Name", "LXD_PropertyTab_InteralName", newPvec);
    
    }