By Adam Nagy
If you'd like to add user data to the selected item or change its existing data, then you need to populate a property vector with all the properties the user data will contain and then add it to the selected item's GUI property node.
In case of modifying user data you need to recreate the use data with the required modifications and then overwrite the existing one. If first parameter of SetUserDefined() is 0, then a new User Data will be created, otherwise an exisiting one will be overwritten.
If you want to remove a single item in a user data, then you just have to omit it when you recreate the property vector, but if you want to remove the whole user data property collection then you need to call RemoveUserDefined() with the index of the user data you are trying to remove.
Here is a sample code written in .NET using the COM API of Navisworks:
Imports NavisWorks8
Imports NavisworksAPI8
Imports NavisworksAPI8.nwEObjectType
Public Class Form1
Dim nwDoc As New Document
Dim nwState As InwOpState10
' Open a Navisworks document
Private Sub btnOpen_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnOpen.Click
nwDoc.OpenFile( _
"C:\Program Files\Autodesk\Navisworks Manage 2011\api\COM\" + _
"examples\gatehouse.nwd")
nwState = nwDoc.State
nwDoc.Visible = True
End Sub
' Add User Data MyAttributeUserName with two items in it
Private Sub btnAddUserData_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnAddUserData.Click
If nwState.CurrentSelection.Paths.Count <> 1 Then
MsgBox("Select an item first")
End If
Dim nwPath As InwOaPath = _
nwState.CurrentSelection.Paths(1)
Dim nwProps As InwOaPropertyVec = _
nwState.ObjectFactory(eObjectType_nwOaPropertyVec)
Dim nwProp1 As InwOaProperty = _
nwState.ObjectFactory(eObjectType_nwOaProperty)
nwProp1.name = "MyPropertyName1"
nwProp1.value = "MyPropertyValue1"
nwProps.Properties.Add(nwProp1)
Dim nwProp2 As InwOaProperty = _
nwState.ObjectFactory(eObjectType_nwOaProperty)
nwProp2.name = "MyPropertyName2"
nwProp2.value = "MyPropertyValue2"
nwProps.Properties.Add(nwProp2)
Dim nwNode As InwGUIPropertyNode2 = _
nwState.GetGUIPropertyNode(nwPath, True)
nwNode.SetUserDefined(0, "MyAttributeUserName", _
"MyAttributeInternalName", nwProps)
End Sub
' Modify the value of MyPropertyName1 in MyAttributeUserName
Private Sub btnModifyUserDataItem_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnModifyUserDataItem.Click
If nwState.CurrentSelection.Paths.Count <> 1 Then
MsgBox("Select an item first")
End If
Dim nwPath As InwOaPath = _
nwState.CurrentSelection.Paths(1)
Dim nwProps As InwOaPropertyVec = _
nwState.ObjectFactory(eObjectType_nwOaPropertyVec)
Dim nwNode As InwGUIPropertyNode2 = _
nwState.GetGUIPropertyNode(nwPath, True)
Dim index As Integer = 1
For Each nwAtt As InwGUIAttribute2 In nwNode.GUIAttributes()
If Not nwAtt.UserDefined Then Continue For
If Not nwAtt.ClassUserName = "MyAttributeUserName" Then
index += 1
Continue For
End If
For Each nwProp As InwOaProperty In nwAtt.Properties()
Dim nwNewProp As InwOaProperty = _
nwState.ObjectFactory(eObjectType_nwOaProperty)
nwNewProp.name = nwProp.name
nwNewProp.value = nwProp.value
If nwNewProp.name = "MyPropertyName1" Then
nwNewProp.value += "-modified"
End If
nwProps.Properties.Add(nwNewProp)
Next
nwNode.SetUserDefined(index, nwAtt.ClassUserName, _
nwAtt.ClassName, nwProps)
Exit For
Next
End Sub
' Remove one of the items in the user data we previously added
Private Sub btnRemoveUserDataItem_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnRemoveUserDataItem.Click
If nwState.CurrentSelection.Paths.Count <> 1 Then
MsgBox("Select an item first")
End If
Dim nwPath As InwOaPath = _
nwState.CurrentSelection.Paths(1)
Dim nwProps As InwOaPropertyVec = _
nwState.ObjectFactory(eObjectType_nwOaPropertyVec)
Dim nwNode As InwGUIPropertyNode2 = _
nwState.GetGUIPropertyNode(nwPath, True)
Dim index As Integer = 1
For Each nwAtt As InwGUIAttribute2 In nwNode.GUIAttributes()
If Not nwAtt.UserDefined Then Continue For
If Not nwAtt.ClassUserName = "MyAttributeUserName" Then
index += 1
Continue For
End If
For Each nwProp As InwOaProperty In nwAtt.Properties()
If nwProp.name = "MyPropertyName1" Then Continue For
Dim nwNewProp As InwOaProperty = _
nwState.ObjectFactory(eObjectType_nwOaProperty)
nwNewProp.name = nwProp.name
nwNewProp.value = nwProp.value
nwProps.Properties.Add(nwNewProp)
Next
nwNode.SetUserDefined(index, nwAtt.ClassUserName, _
nwAtt.ClassName, nwProps)
Exit For
Next
End Sub
' Remove MyAttributeUserName completely
Private Sub btnRemoveUserData_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnRemoveUserData.Click
If nwState.CurrentSelection.Paths.Count <> 1 Then
MsgBox("Select an item first")
End If
Dim nwPath As InwOaPath = _
nwState.CurrentSelection.Paths(1)
Dim nwProps As InwOaPropertyVec = _
nwState.ObjectFactory(eObjectType_nwOaPropertyVec)
Dim nwNode As InwGUIPropertyNode2 = _
nwState.GetGUIPropertyNode(nwPath, True)
Dim index As Integer = 1
For Each nwAtt As InwGUIAttribute2 In nwNode.GUIAttributes()
If Not nwAtt.UserDefined Then Continue For
If Not nwAtt.ClassUserName = "MyAttributeUserName" Then
index += 1
Continue For
End If
nwNode.RemoveUserDefined(index)
Exit For
Next
End Sub
End Class