Welcome to the world of MicroStation Python! This wiki will walk you through the process of extracting ECProperty, ECPropertyValue from ECInstance attached to an Element. To know more about ECFramework refer to API Presentations in the Python documentation.
Extract ECProperty Data
To get started, refer to the Python Manager wiki for instructions on creating and loading a new Python project. Name your project "ECPropertyData.py" and save it in a convenient location. Once created, open the project in your preferred code editor to begin writing your Python script.
To extract ECProperty data from ECInstance attached to an Element.
Here is the complete script.
from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * # This class has responsibility to serialize property names and values in dictionary class ECInstanceSerializer: def __init__(self, instance): self.m_instance = instance def SerializePropValues (dgnECInstance, collection, ecPropNamValDict): for propertyValue in collection: accessor = propertyValue.GetValueAccessor() accessString = accessor.GetManagedAccessString () value = propertyValue.GetValue() typeAdaptedValue = WString () strAccessString = str(accessString) if value.IsStruct() == False and value.IsArray() == False: if BentleyStatus.eSUCCESS == dgnECInstance.GetValueAsString(typeAdaptedValue,
strAccessString,
False, 0): ecPropNamValDict[str(accessString)] = str(typeAdaptedValue) else: dgnECInstance.GetValue(value, strAccessString) ecPropNamValDict[str(accessString)] = str(value.ToString()) if (propertyValue.HasChildValues ()): ECInstanceSerializer.SerializePropValues (dgnECInstance,
propertyValue.GetChildValues(),
ecPropNamValDict) def SerializeInstance (instance, ecPropNamValDict): ecPropNamValDict['Instance schema'] = str(instance.GetClass().GetSchema().GetName()) ecPropNamValDict['Instance class'] = str(instance.GetClass().GetName()) ecPropNamValDict['Instance ID'] = str(instance.GetInstanceId()) collection = ECValuesCollection.Create (instance) ECInstanceSerializer.SerializePropValues(instance, collection, ecPropNamValDict) def Serialize(self): ecPropNamValDict = dict() ECInstanceSerializer.SerializeInstance(self.m_instance, ecPropNamValDict) return ecPropNamValDict # Get element instance function def ProcessElementInstaces(elementHandle): mgr = DgnECManager.GetManager() query = ECQuery.CreateQuery(eECQUERY_PROCESS_SearchAllClasses) elementScope = FindInstancesScope.CreateScope (elementHandle, FindInstancesScopeOption
(DgnECHostType.eElement)) instanceCollection = mgr.FindInstances (elementScope, query) #Iterate through each EC instance for inst in instanceCollection[0]: serializer = ECInstanceSerializer(inst) ecPropNamValDict = serializer.Serialize() #print (ecPropNamValDict) #print (ecPropNamValDict["ElementDescription"]) for key, value in ecPropNamValDict.items(): print(f"{key} - {value}") # Main function def main(): ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgn_model = ACTIVEMODEL.GetDgnModel() elementId = 1111 eeh = EditElementHandle(elementId, dgn_model) ProcessElementInstaces(eeh) # Main if __name__ == "__main__": print ("***** Element Property Data *****") main()
Load the project “ECPropertyData.py” from the Python Manager dialog and Run/Execute the python script.
Create a new DGN file and draw a Line and Circle element. Retrieve the Element ID of the Line element from the Properties dialog and set it in the code as elementId = 1111, where 1111 is the Element ID.
Running the script displays the ECProperty, ECPropertyValue in the MicroStation Text Window.
Test the output of the Circle element by getting and setting its Element ID.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs