I'm looking for a way to enumerate the list of properties defined in an Item Type using VBA. The following code works correctly but requires me to hard-code the property name rather than extract it from the item type definition:
Sub UpdateCellItemType() Dim oItemLibs As ItemTypeLibraries Dim oItemLib As ItemTypelibrary Dim oItem As ItemType Dim oItemPropHandler As ItemTypePropertyHandler Dim cellEl As CellElement Dim oScanCriteria As ElementScanCriteria Dim oScanEnumerator As ElementEnumerator ' get item type definition Set oItemLibs = New ItemTypeLibraries Set oItemLib = oItemLibs.FindByName("Upgraded Tag Sets") Set oItem = oItemLib.GetItemTypeByName("0-TTL-A1-NTA") ' init scanner Set oScanCriteria = New ElementScanCriteria oScanCriteria.ExcludeAllTypes oScanCriteria.IncludeType msdElementTypeCellHeader ' process cells Set oScanEnumerator = ActiveModelReference.Scan(oScanCriteria) Do While oScanEnumerator.MoveNext Set cellEl = oScanEnumerator.Current ' get cell itemType instance Set oItemPropHandler = oItem.FindItem(cellEl) ' update itemtype property If Not (oItemPropHandler Is Nothing) Then oItemPropHandler.SetPropertyValue "Prop Name", "Prop Value" End If Loop ' update all fields CadInputQueue.SendKeyin "FIELD UPDATE ALL" End Sub
Benzi Papo said:but requires me to hard-code the property name rather than extract it from the item type definition:
The following code does not require to know anything about attached ItemType and ItemType properties in advance.
It is proof of concept and I treat it as dirty, badly structured, but it works find and I guess it not hard to understand it. The element is obtained using its ID only for development and testing purpose.
Dim el As Element Set el = ActiveModelReference.GetElementByID(DLongFromLong(12652)) Dim itemProps As Items Set itemProps = el.Items Dim itemTypePropHandler As ItemTypePropertyHandler Do Set itemTypePropHandler = itemProps.Find("*", "*", itemTypePropHandler) If itemTypePropHandler Is Nothing Then Exit Do Dim itemTypeLibraryName As String itemTypeLibraryName = itemTypePropHandler.LibraryName Dim itemTypeLib As ItemTypeLibrary Set itemTypeLib = ActiveDesignFile.ItemTypeLibraries.FindByName(itemTypeLibraryName) Dim itemTypeName As String itemTypeName = itemTypePropHandler.itemTypeName Dim itemTypeDef As ItemType Set itemTypeDef = itemTypeLib.GetItemTypeByName(itemTypeName) Dim propertyDef As ItemTypeProperty Do Set propertyDef = itemTypeDef.Find("*", propertyDef) If propertyDef Is Nothing Then Exit Do Dim propVal As Variant propVal = itemTypePropHandler.GetPropertyValue(propertyDef.PropertyName) Debug.Print "Library: " & itemTypeLibraryName & ", ItemType: " & itemTypeName & ", property " & propertyDef.PropertyName & " = " & CStr(propVal) Loop Loop
Regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Answer Verified By: Benzi Papo
So indeed this was the missing piece. Thanks for the code snippet and also the ElementID suggestion which helps with code testing.
Hi Benzi,
you are welcomed :-)
The code illustrates what I wrote in my first reaction: For an element, it checks what ItemType(s) is attached. Using this information, ItemType definition is read and it is the way how to obtain a list of all ItemType properties. And using the property name, it is possible to ask element data for the property value.