Python

Get EC Data from DGN File

I wrote this article in response to a question posted to the MicroStation Programming web site.

The MicroStation developer asked: How can I create a python script to find the total editing time of a DGN file?

EC Essentials

Read this article about EC Essentials to learn about programming using EC Data.

Implementation

We need a query that interrogates a DGN file. The query will search for EC instance data belonging to the EC schema DgnFileSchema and DgnFileProperties class in that schema.

I borrowed stole some of the code that follows from the examples delivered with MicroStation.

Get EC Schemas

Get a list of available EC Schemas …

def GetStoredSchemaList():
    """
    Get a list of schemas stored in the current DGN file.

    :returns: List of schemas stored in the current DGN file.
    :rtype: list
    """
    #list to collect schema names
    schemas = list()
    #Get dgnEC manager instance
    ec_manager = DgnECManager.GetManager()

    dgn_file = ISessionMgr.GetActiveDgnFile()

    #discover schemas
    infos = SchemaInfoArray()
    ec_manager.DiscoverSchemas(infos, dgn_file,
                                ECSchemaPersistence.eECSCHEMAPERSISTENCE_All,
                                DgnECHostType.eFile)
    print(f"SchemaInfoArray length={len(infos)}")
    info_container = SchemaInfoArray()
    for info in infos:
        info_container.append(info)

        if len(info_container) <= 0:
            continue

    #iterate container
    for info in info_container:
        #locate schema
        schema = ec_manager.LocateSchemaInDgnFile(info, SchemaMatchType.eSCHEMAMATCHTYPE_Exact)
        if schemas is None:
            continue
        #get schema name
        #schemaName = ecSchema.GetFullSchemaName()
        #add to list
        schemas.append(schema)

    return schemas
 

Get EC Schemas

Get a list of available EC Schemas …

In __main__ you can see the schema list …

schemas = GetStoredSchemaList()
print("EC schemas in active DGN file")
for schema in schemas:
    print(f"schema '{schema.GetName()}' ")

Create an EC Query

Get EC instance data …

mgr = DgnECManager.GetManager()
# Prepare EC query and scope:
# query = what to look for
# scope = where to look
# We want to look for a specific EC schema and EC class...
query = ECQuery.CreateQuery(eECQUERY_PROCESS_Empty)
_EC_DGN_FILE_SCHEMA = 'DgnFileSchema'
_EC_DGN_FILE_PROPERTIES_CLASS = 'DgnFileProperties'
query.AddSearchClass(_EC_DGN_FILE_SCHEMA, _EC_DGN_FILE_PROPERTIES_CLASS)
scope = FindInstancesScope.CreateScope(dgn_file, FindInstancesScopeOption(DgnECHostType.eFile)

Get EC Data

Fetch EC instance data selected using the above EC Query …

instance_collection, n_instances = mgr.FindInstances(scope, query)
# Serialize EC data
ec_data_list = []
#inst: MSPyECObjects.DgnECInstance
for inst in instance_collection:
	print(f"inst type={type(inst)}")
	serializer = ECInstanceSerializer(inst)
	ec_data = serializer.Serialize()
	ec_data_list.append(ec_data)

You can display the retrieved data from that list like this …

# This list contains key/value pairs of EC data found in the active DGN file
ec_data_list = extract_ec_data_from_dgnfile()
for data in ec_data_list:
    for o in data:
        #print(type(o), o)
        print(f"prop '{o}' value '{data[o]}'")

You can search that list for a named property like this. We're using the standard Python approach to getting a value from a dictionary by key …

print("Get specified EC properties...")
CREATE_DATE_PROPERTY = 'CreateDate'
create_date = data[CREATE_DATE_PROPERTY]
print(f"{CREATE_DATE_PROPERTY}='{create_date}'")
TOTAL_EDIT_TIME_PROPERTY =  'TotalEditingTime'
total_edit_time = data[TOTAL_EDIT_TIME_PROPERTY]
print(f"{TOTAL_EDIT_TIME_PROPERTY}='{total_edit_time}'")

You will find that code in function extract_ec_data_from_dgnfile() in the sample provided.

Write EC Data to a CSV File

The original question want the EC data written to a CSV file. The following shows how to generate a CSV file using standard Python …



The CSV file looks something like this (many lines removed). The first line is the header …

Property,Value
Instance schema,DgnFileSchema
Instance class,DgnFileProperties
 …
CreateDate,9/25/2024 4:13:41 PM
Editor,MicroStation v25.00.01.62
 …
FileName, …\LA Solutions\WorkSets\Python\dgn\test01.dgn
 …
SaveDate,12/21/2025 10:38:40 AM
 …
TotalEditingTime,2 Days 12 Hours 51 Minutes
 …

Download Get EC Data from DGN File

Download Download la_solutions_total_file_edit_time.zip

The ZIP file contains the main module la_solutions_total_file_edit_time.py and supporting Python files in folder la_solutions.

The above code is available in this MicroStation Python project. Unpack the ZIP archive and copy la_solutions_total_file_edit_time.py and folder la_solutions to a location where MicroStation can find them. A good place to copy it would be \Workspace\Standards\Python. To create the graph, open MicroStation's Python manager and run la_solutions_total_file_edit_time..

Questions

Post questions about MicroStation Python programming to the MicroStation Programming Forum. is a top-level object that lets us create other EC objects to create an application.

Read this article about EC Essentials to learn about programming using EC Data.