This blog demonstrates how to use Item Types Native, COM and Managed API's for following operations:
1. Create Item type Library, Item Type and Property
DgnFileP dgnfilePtr = Bentley::MstnPlatform::ISessionMgr::GetActiveDgnFile(); ItemTypeLibraryPtr libPtr = ItemTypeLibrary::Create(L"Imported Furniture", *dgnfilePtr, false); ItemTypeP sofaItemTypePtr = libPtr->AddItemType(L"Sofa", false); CustomPropertyP typeProperty = sofaItemTypePtr->AddProperty(L"Type", false); typeProperty->SetType(Bentley::DgnPlatform::CustomProperty::Type::String); typeProperty->SetDefaultValue(ECValue(L"Decorated")); libPtr->Write();
Dim libraries As ItemTypeLibraries Dim itemTypeFurnitureLib As ItemTypeLibrary Dim itemTypeSofa As itemtype Dim itemTypeProperty As itemTypeProperty Set libraries = New ItemTypeLibraries Set itemTypeFurnitureLib = libraries.CreateLib("Imported Furniture", False) Set itemTypeSofa = itemTypeFurnitureLib.AddItemType("Sofa", False) Set itemTypeProperty = itemTypeSofa.AddProperty("Type", ItemPropertyTypeString) itemTypeProperty.SetDefaultValue ("Decorated") itemTypeFurnitureLib.Write
ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.Create("Imported Furniture", Session.Instance.GetActiveDgnFile()); ItemType itemType = itemTypeLibrary.AddItemType("Sofa"); CustomProperty customProperty = itemType.AddProperty("Type"); customProperty.DefaultValue = "Decorated"; customProperty.Type = CustomProperty.TypeKind.String; itemTypeLibrary.Write();
2. Read default value of already available property
DgnFileP dgnfilePtr = Bentley::MstnPlatform::ISessionMgr::GetActiveDgnFile(); ItemTypeLibraryPtr libPtr = ItemTypeLibrary::FindByName(L"Imported Furniture", *dgnfilePtr); ItemTypeCP sofaItemTypePtr = libPtr->GetItemTypeByName(L"Sofa"); CustomPropertyCP typeProperty = sofaItemTypePtr->GetPropertyByName(L"Type"); ECValue val; typeProperty->GetDefaultValue(val); std::wstring defaultValue = val.GetString();
Dim libraries As ItemTypeLibraries Dim itemTypeFurnitureLib As ItemTypeLibrary Dim itemTypeSofa As itemtype Dim itemTypeProperty As itemTypeProperty Dim defaultValue As String Set libraries = New ItemTypeLibraries Set itemTypeFurnitureLib = libraries.FindByName("Imported Furniture") Set itemTypeSofa = itemTypeFurnitureLib.GetItemTypeByName("Sofa") Set itemTypeProperty = itemTypeSofa.GetPropertyByName("Type") defaultValue = itemTypeProperty.GetDefaultValue
ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.FindByName("Imported Furniture", Session.Instance.GetActiveDgnFile()); ItemType itemType = itemTypeLibrary.GetItemTypeByName("Sofa"); CustomProperty customProperty = itemType.GetPropertyByName("Type"); string defaultValue = customProperty.DefaultValue.ToString();
3. Update the default value of already available property
DgnFileP dgnfilePtr = Bentley::MstnPlatform::ISessionMgr::GetActiveDgnFile(); ItemTypeLibraryPtr libPtr = ItemTypeLibrary::FindByName(L"Imported Furniture", *dgnfilePtr); ItemTypeCP sofaItemTypePtr = libPtr->GetItemTypeByName(L"Sofa"); CustomPropertyCP typePropertyCPtr = sofaItemTypePtr->GetPropertyByName(L"Type"); CustomPropertyP typePropertyPtr = const_cast<CustomPropertyP>(typePropertyCPtr); typePropertyPtr->SetDefaultValue(ECValue(L"Newly Decorated")); libPtr->Write();
Dim libraries As ItemTypeLibraries Dim itemTypeFurnitureLib As ItemTypeLibrary Dim itemTypeSofa As itemtype Dim itemTypeProperty As itemTypeProperty Set libraries = New ItemTypeLibraries Set itemTypeFurnitureLib = libraries.FindByName("Imported Furniture") Set itemTypeSofa = itemTypeFurnitureLib.GetItemTypeByName("Sofa") Set itemTypeProperty = itemTypeSofa.GetPropertyByName("Type") itemTypeProperty.SetDefaultValue ("Newly Decorated") itemTypeFurnitureLib.Write
ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.FindByName("Imported Furniture", Session.Instance.GetActiveDgnFile()); ItemType itemType = itemTypeLibrary.GetItemTypeByName("Sofa"); CustomProperty customProperty = itemType.GetPropertyByName("Type"); string defaultValue = customProperty.DefaultValue.ToString(); customProperty.DefaultValue = "Newly Decorated"; itemTypeLibrary.Write();
4. Attach Item Type to an element
ItemTypeLibraryPtr libPtr = ItemTypeLibrary::FindByName(L"Imported Furniture", *ISessionMgr::GetActiveDgnFile()); ItemTypeCP sofaItemTypeCPtr = libPtr->GetItemTypeByName(L"Sofa"); //Get the ElementHandle eh for the element on which an Item Type will be attached EditElementHandle eeh(eh.GetElementRef()); Bentley::DgnPlatform::CustomItemHost itemHost(eeh, false); DgnECInstancePtr instancePtr = itemHost.ApplyCustomItem(*sofaItemTypeCPtr); instancePtr->SetValue(L"Type", ECValue(L"Modifed Sofa")); instancePtr->WriteChanges();
Dim libraries As ItemTypeLibraries Dim itemTypeFurnitureLib As ItemTypeLibrary Dim itemTypeSofa As itemtype Dim itemPropHandler As ItemTypePropertyHandler Dim lineElement As Element Dim instanceOldValue As Object Dim instanceSetStatus As Boolean instanceSetStatus = False Set libraries = New ItemTypeLibraries Set itemTypeFurnitureLib = libraries.FindByName("Imported Furniture") Set itemTypeSofa = itemTypeFurnitureLib.GetItemTypeByName("Sofa") 'Get the lineElement on to which an Item type will be attached Set itemPropHandler = itemTypeSofa.AttachItem(lineElement) If (itemPropHandler.SetPropertyValue("Type", "Modifed Sofa")) = True Then Debug.Print itemPropHandler.GetPropertyValue("Type") End If
//line is Element object on which an Item Type will be set CustomItemHost customItemHost = new CustomItemHost(line, false); IDgnECInstance ecInstance = customItemHost.ApplyCustomItem(itemType); ecInstance.SetString("Type", "Modified Sofa"); ecInstance.WriteChanges();
5. Read/Update properties of Array, Struct and Array of Struct type
//Set Array Property values instancePtr->SetValue(L"Color", ECValue(L"Red"), 0); instancePtr->SetValue(L"Color", ECValue(L"Green"), 1); //Set Struct Property values instancePtr->SetValue(L"ColorStruct.Prop1", ECValue(L"StructProp1")); instancePtr->SetValue(L"ColorStruct.Prop2", ECValue(10)); //Set Array of Struct Property values ECClassCP structClass = instancePtr->GetClass().GetSchema().GetClassCP(structPropType->GetInternalName()); IECInstancePtr ecInstansePtr = structClass->GetDefaultStandaloneEnabler()->CreateInstance(); ecInstansePtr->SetValue(L"Prop1", ECValue(L"StructArray Prop1")); ecInstansePtr->SetValue(L"Prop2", ECValue(100)); ECValue newStructValue; newStructValue.SetStruct(ecInstansePtr.get()); instancePtr->SetValue(L"ColorStructArray", newStructValue, 0);
//Get Array Property values ECValue propValue; instancePtr->GetValue(propValue, L"Color", 0); WCharCP colorVal0 = propValue.GetString(); instancePtr->GetValue(propValue, L"Color", 1); WCharCP colorVal1 = propValue.GetString(); //Get Struct Property values instancePtr->GetValue(propValue, L"ColorStruct.Prop1"); WCharCP strcutProp1 = propValue.GetString(); instancePtr->GetValue(propValue, L"ColorStruct.Prop2"); int strcutProp2 = propValue.GetInteger(); //Get Array of Struct Property values instancePtr->GetValue(propValue, L"ColorStructArray", 0); IECInstancePtr strValueInstancePtr = propValue.GetStruct(); ECValue structPropValues; strValueInstancePtr->GetValue(structPropValues, L"Prop1"); WCharCP arrayofstructProp1 = structPropValues.GetString(); strValueInstancePtr->GetValue(structPropValues, L"Prop2"); int arrayofstructProp2 = structPropValues.GetInteger();
'Set Array Property values isBoolean = oItemPropHandler.SetPropertyValue("Color[0]", "Red") isBoolean = oItemPropHandler.SetPropertyValue("Color[1]", "Green") 'Set Struct Property values isBoolean = oItemPropHandler.SetPropertyValue("UsingStruct.Prop1", "StructProp1") isBoolean = oItemPropHandler.SetPropertyValue("UsingStruct.Prop2", 10) 'Set Array of Struct Property values isBoolean = oItemPropHandler.SetPropertyValue("ArrayofStruct[0].Prop1", "ArrayofStruct Prop1") isBoolean = oItemPropHandler.SetPropertyValue("ArrayofStruct[0].Prop2", 100)
'Get Array Property values Debug.Print oItemPropHandler.GetPropertyValue("Color[0]") Debug.Print oItemPropHandler.GetPropertyValue("Color[1]") 'Get Struct Property values Debug.Print oItemPropHandler.GetPropertyValue("UsingStruct.Prop1") Debug.Print oItemPropHandler.GetPropertyValue("UsingStruct.Prop2") 'Get Array of Struct Property values Debug.Print oItemPropHandler.GetPropertyValue("ArrayofStruct[0].Prop1") Debug.Print oItemPropHandler.GetPropertyValue("ArrayofStruct[0].Prop2")
//Set Array Property values dgnECInstance.SetAsString("Color[0]", "Red"); dgnECInstance.SetAsString("Color[1]", "Green"); //Set Struct Property values dgnECInstance.SetAsString("UsingStruct.Prop1", "StructProp1"); dgnECInstance.SetInteger("UsingStruct.Prop2", 10); //Set Array of Struct Property values dgnECInstance.SetAsString("ArrayofStruct[0].Prop1", "ArrayofStruct Prop1"); dgnECInstance.SetInteger("ArrayofStruct[0].Prop2", 100);
//Get Array Property values string colorValue0 = dgnECInstance.GetPropertyValue("Color[0]").StringValue; string colorValue1 = dgnECInstance.GetPropertyValue("Color[1]").StringValue; //Get Struct Property values string structProp1 = dgnECInstance.GetPropertyValue("UsingStruct.Prop1").StringValue; int structProp2 = dgnECInstance.GetPropertyValue("UsingStruct.Prop2").IntValue; //Get Array of Struct Property values string arrayofstructProp1 = dgnECInstance.GetPropertyValue("ArrayofStruct[0].Prop1").StringValue; int arrayofstructProp2 = dgnECInstance.GetPropertyValue("ArrayofStruct[0].Prop2").IntValue;
6. Detach item from an element
//Get the ElementHandle eh for the element for which an Item Type will be attached EditElementHandle eeh(eh.GetElementRef()); Bentley::DgnPlatform::CustomItemHost itemHost(eeh, false); DgnECInstancePtr instancePtr = itemHost.GetCustomItem(L"Imported Furniture", L"Sofa"); instancePtr->ScheduleDelete(eeh);
Dim libraries As ItemTypeLibraries Dim itemTypeFurnitureLib As ItemTypeLibrary Dim itemTypeSofa As itemtype Set libraries = New ItemTypeLibraries Set itemTypeFurnitureLib = libraries.FindByName("Imported Furniture") Set itemTypeSofa = itemTypeFurnitureLib.GetItemTypeByName("Sofa") 'lineElement is the Element object from which the Item Type will be detached itemTypeSofa.DetachItem lineElement
//line is the Element object from which the Item Type will be detached CustomItemHost customItemHost = new CustomItemHost(line, false); System.Collections.Generic.IList<IDgnECInstance> customItems = customItemHost.CustomItems; //Detaching first item type from the element var ecInstanceType = customItems[0]; ecInstanceType.ScheduleDelete(line);
7. Delete item type property, item type and item type library
ItemTypeLibraryPtr libPtr = ItemTypeLibrary::FindByName(L"Imported Furniture", *ISessionMgr::GetActiveDgnFile()); ItemTypeCP sofaItemTypeCPtr = libPtr->GetItemTypeByName(L"Sofa"); ItemTypeP sofaItemTypePtr = const_cast<ItemTypeP>(sofaItemTypeCPtr); CustomPropertyCP typePropertyCPtr = sofaItemTypePtr->GetPropertyByName(L"Type"); sofaItemTypePtr->RemoveProperty(typePropertyCPtr->GetId()); libPtr->RemoveItemType(sofaItemTypePtr->GetId()); libPtr->Delete();
Dim libraries As ItemTypeLibraries Dim itemTypeFurnitureLib As ItemTypeLibrary Dim itemTypeSofa As itemtype Dim itemTypeProperty As itemTypeProperty Set libraries = New ItemTypeLibraries Set itemTypeFurnitureLib = libraries.FindByName("Imported Furniture") Set itemTypeSofa = itemTypeFurnitureLib.GetItemTypeByName("Sofa") Set itemTypeProperty = itemTypeSofa.GetPropertyByName("Type") itemTypeSofa.RemoveProperty (itemTypeProperty.GetPropId) itemTypeFurnitureLib.RemoveItemType (itemTypeSofa.GetId) itemTypeFurnitureLib.DeleteLib
ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.Create("Imported Furniture", Session.Instance.GetActiveDgnFile()); ItemType itemType = itemTypeLibrary.AddItemType("Sofa"); CustomProperty customProperty = itemType.AddProperty("Type"); bool isRemoved = itemType.RemoveProperty(customProperty); itemTypeLibrary.RemoveItemType(itemType); itemTypeLibrary.Delete(); itemTypeLibrary.Write();
I got it working, in my case delete itemtype propertiezs was required
How can I save changes to IECInstance as I can se there is IDgnECInstance..WriteChanges();
Hi Jon,
Apologies for the late reply. I have corrected the Detach item from an element example. Now the corrected example will work as expected.
The example Detach item from an element unfortunately doesn't do what it says. It's a duplicate of the example to attach an Item Type instance.