Item Types CRUD Operations with Native, COM and Managed APIs
Published in
4 min readOct 22, 2019
This blog demonstrates how to use Item Types native, COM and Managed API’s for following operations:
- Create Item type Library, Item Type and Property.
- Read default value of already available property.
- Update the default value of already available property.
- Attach item type to an element.
- Detach item from an element.
- Delete item type property, item type and item type library.
Create Item type Library, Item Type and Property
- Native API
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();
- COM
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
- Managed
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();
Read default value of already available property
- Native API
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();
- COM
Dim libraries As ItemTypeLibraries
Dim itemTypeFurnitureLib As ItemTypeLibrary
Dim itemTypeSofa As itemtype
Dim itemTypeProperty As itemTypeProperty
Dim defaultValue As StringSet libraries = New ItemTypeLibraries
Set itemTypeFurnitureLib = libraries.FindByName(“Imported Furniture”)
Set itemTypeSofa = itemTypeFurnitureLib.GetItemTypeByName(“Sofa”)
Set itemTypeProperty = itemTypeSofa.GetPropertyByName(“Type”)
defaultValue = itemTypeProperty.GetDefaultValue
- Managed
ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.FindByName(“Imported Furniture”, Session.Instance.GetActiveDgnFile());
ItemType itemType = itemTypeLibrary.GetItemTypeByName(“Sofa”);
CustomProperty customProperty = itemType.GetPropertyByName(“Type”);
string defaultValue = customProperty.DefaultValue.ToString();
Update the default value of already available property
- Native API
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();
- COM
Dim libraries As ItemTypeLibraries
Dim itemTypeFurnitureLib As ItemTypeLibrary
Dim itemTypeSofa As itemtype
Dim itemTypeProperty As itemTypePropertySet 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
- Managed
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();
Attach Item Type to an Element
- Native API
//Get the ElementHandle eh for the element on which Item Type will be attached
EditElementHandle eeh(eh.GetElementRef());
Bentley::DgnPlatform::CustomItemHost itemHost(eeh, false);
DgnECInstancePtr instancePtr = itemHost.ApplyCustomItem(*sofaItemTypePtr);
instancePtr->SetValue(L”Type”, ECValue(L”Modifed Sofa”));
instancePtr->WriteChanges();
- COM
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
- Managed
//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();
Detach Item from Element
- Native API
//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(*sofaItemTypePtr);
instancePtr->SetValue(L”Type”, ECValue(L”Modifed Sofa”));
instancePtr->WriteChanges();
- COM
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)
- Managed
//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;
var ecInstanceType = customItems[0];
IECPropertyValue eCProperty = ecInstanceType.GetPropertyValue(“Type”);
ecInstanceType.RemoveValue(eCProperty);
ecInstanceType.Delete();
Detach Item Type Property, Item type and Item Type Library
- Native API
ItemTypeLibraryPtr libPtr = ItemTypeLibrary::FindByName(“Imported Furniture”);
ItemTypeCP sofaItemTypePtr = libPtr.GetItemTypeByName(L”Sofa”);
CustomPropertyCP typePropertyCPtr = sofaItemTypePtr->GetPropertyByName(L”Type”);
sofaItemTypePtr->RemoveProperty(typePropertyCPtr->GetId());
libPtr->RemoveItemType(sofaItemTypePtr->GetId());
libPtr->Delete();
- COM
Dim libraries As ItemTypeLibraries
Dim itemTypeFurnitureLib As ItemTypeLibrary
Dim itemTypeSofa As itemtype
Dim itemTypeProperty As itemTypePropertySet 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
itemTypeFurnitureLib.Write
- Managed
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();
Accreditation
To find this post and other blog posts, Wiki’s, Announcements on the Bentley Communities website, click here.
If you’re interested in speaking with a Bentley expert about making the move to MicroStation CONNECT Edition, please complete this form to have someone contact you to provide advice and assistance.
Thank you for reading!