2

我尝试在现有类别中为选定的 NavisWorks ModelItem 添加一些新属性

网络上的例子不多,它基于相同的 COM 方法。但是有特殊的方法可以在 API 中添加可用的属性。唯一的问题是对象被锁定。有什么方法可以解锁吗?

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

执行结果:

ItemModel的Navisworks Category是ReadOnly,如何修改?

4

1 回答 1

1

Navisworks 创建的属性和类别是只读的。您不能添加或修改由 Navisworks 创建的属性或类别。

您只能使用 COM 创建或添加用户定义的属性。看:

这是一个代码片段(从上面提到的 xiaodong.liang 论坛帖子中复制),它显示了如何使用 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);

}
于 2021-04-08T14:44:12.780 回答