I'm struggling to create a custom enumeration. I know I've to create a new DataType which should be referenced to the attribute. But all my trials are not working. The created DataType-Node seems to be working as it is showing up in the DataTypes->Enumeration section along with all the correct enum strings.
Here is the code I tried (only for testing):
Code: Select all
// Create a Folder
CreateObjectSettings folderSettings = new CreateObjectSettings()
{
ParentNodeId = ObjectIds.ObjectsFolder,
ReferenceTypeId = ReferenceTypeIds.Organizes,
RequestedNodeId = new NodeId("Test", InstanceNamespaceIndex),
BrowseName = new QualifiedName("Test", InstanceNamespaceIndex),
TypeDefinitionId = ObjectTypeIds.FolderType
};
CreateObject(Server.DefaultRequestContext, folderSettings);
//Create an object
CreateObjectSettings objectSettings = new CreateObjectSettings()
{
ParentNodeId = new NodeId("Test", InstanceNamespaceIndex),
ReferenceTypeId = ReferenceTypeIds.Organizes,
RequestedNodeId = new NodeId("TestObject", InstanceNamespaceIndex),
BrowseName = new QualifiedName("TestObject", InstanceNamespaceIndex),
DisplayName = new LocalizedText("TestObject"),
TypeDefinitionId = ObjectTypeIds.BaseObjectType
};
CreateObject(Server.DefaultRequestContext, objectSettings);
//Create a DataType for custom enumeration
CreateDataTypeSettings dataTypeSettings = new CreateDataTypeSettings()
{
ReferenceTypeId = ReferenceTypeIds.HasSubtype,
DisplayName = new LocalizedText("TestEnumeration"),
BrowseName = new QualifiedName("TestEnumeration", InstanceNamespaceIndex),
RequestedNodeId = new NodeId("TestEnumeration", InstanceNamespaceIndex),
SuperTypeId = DataTypeIds.Enumeration
};
CreateDataTypeNode(Server.DefaultRequestContext, dataTypeSettings);
//Create the enum values
ExtensionObjectCollection enums = new ExtensionObjectCollection();
EnumValueType enumValueType = new EnumValueType()
{
Value = 0,
DisplayName = "EnumString1"
};
enums.Add(new ExtensionObject(enumValueType));
enumValueType = new EnumValueType()
{
Value = 1,
DisplayName = "EnumString2"
};
enums.Add(new ExtensionObject(enumValueType));
//Create a variant for enumString variable
Variant variant = new Variant(enums);
//Create an EnumStrings variable
CreateVariableSettings enumSettings = new CreateVariableSettings()
{
DataType = DataTypeIds.EnumValueType,
AccessLevel = AccessLevels.CurrentReadOrWrite,
ReferenceTypeId = ReferenceTypeIds.HasOrderedComponent,
DisplayName = new LocalizedText("EnumStrings"),
BrowseName = new QualifiedName("TestEnumeration.EnumStrings", InstanceNamespaceIndex),
RequestedNodeId = new NodeId("TestEnumeration.EnumStrings", InstanceNamespaceIndex),
ParentNodeId = new NodeId("TestEnumeration", InstanceNamespaceIndex),
TypeDefinitionId = VariableTypeIds.PropertyType,
ValueRank = 1,
Value = variant
};
CreateVariable(Server.DefaultRequestContext, enumSettings);
//Create a default value
Variant defaultValue = new Variant((uint)0);
//Create a variable for the TestObject
CreateVariableSettings variableSettings = new CreateVariableSettings()
{
AccessLevel = AccessLevels.CurrentReadOrWrite,
ReferenceTypeId = ReferenceTypeIds.HasComponent,
DisplayName = new LocalizedText("testEnumVariable"),
BrowseName = new QualifiedName("TestObject.testEnumVariable", InstanceNamespaceIndex),
RequestedNodeId = new NodeId("TestObject.testEnumVariable", InstanceNamespaceIndex),
ParentNodeId = new NodeId("TestObject", InstanceNamespaceIndex),
DataType = dataTypeSettings.RequestedNodeId,
Value = defaultValue
};
CreateVariable(Server.DefaultRequestContext, variableSettings);
Did I miss something to set or to create?
Or what is the correct way to create a custom enumeration and use it within an attribute?