MicroStation Python: Create Item Type


 

Welcome to the world of MicroStation Python! This guide will walk you through the process of creating an Item Type definition. An Item Type is a user-defined set of properties that describe an object or element within a model. After defining an Item Type, you can link it to elements and update their property values.

 

Create Item Type

To get started, refer to the Python Manager wiki for instructions on creating and loading a new Python project. Name your project "Create_ItemType.py" and save it in a convenient location. Once created, open the project in your preferred code editor to begin writing your Python script.

 

We will create an Item Type with the structure shown in the image below.

 

Here is the complete script.

 

from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *


# Globals
g_itemtype_lib_name = "Project_001"
g_itemtype_name = "RoomDetails"


# Create Item Type Library function
def CreateItemTypeLibrary ():
    global g_itemTypeLib
    global g_customStructType
    global g_myItemTypeName

    dgn_file = ISessionMgr.GetActiveDgnFile()

    # Check/Create Item Type Library "Project_001"
    if (None != ItemTypeLibrary.FindByName(g_itemtype_lib_name, dgn_file)):
        print (f"Item Type Library [{g_itemtype_lib_name}] already exists...")
        return

    g_itemTypeLib = ItemTypeLibrary(g_itemtype_lib_name, dgn_file, False)

    if None == g_itemTypeLib:
        print(f"{g_itemtype_lib_name}, Item Type Library could not be created...")
        return


    # Check/Create Item Type "RoomDetails"
    if(None != g_itemTypeLib.GetItemTypeByName(g_itemtype_name)):
        print (f"Item Type [{g_itemtype_name}] already exists...")
        return

    myItemType  = g_itemTypeLib.AddItemType(g_itemtype_name, False)

    if None == myItemType:
        print(f"{g_itemtype_name}, Item Type could not be created...")
        return


    # Create Properties
    if (myItemType.GetPropertyCount() == 0):
        # ID property
        id_property = myItemType.AddProperty('ID')
        id_property.SetType(CustomProperty.Type1.eInteger)

        # Description property
        desc_property = myItemType.AddProperty('Description')
        desc_property.SetType(CustomProperty.Type1.eString)

        # Temperature property
        temp_property = myItemType.AddProperty('Temperature')
        temp_property.SetType(CustomProperty.Type1.eDouble)
        temp_unit = DgnECUnit.FromID("DEGREE_CELSIUS")
        #print(temp_unit.GetECUnitName())
        temp_property.SetUnits(temp_unit)

        # Humidity property
        humi_property = myItemType.AddProperty('Humidity')
        humi_property.SetType(CustomProperty.Type1.eDouble)
        humi_unit = DgnECUnit.FromID("PERCENT_PERCENT")
        #print(humi_unit.GetECUnitName())
        humi_property.SetUnits(humi_unit)

        # Date property
        date_property = myItemType.AddProperty('Date')
        date_property.SetType(CustomProperty.Type1.eDateTime)

        # Occupied property
        occu_property = myItemType.AddProperty('Occupied')
        occu_property.SetType(CustomProperty.Type1.eBoolean)

        g_itemTypeLib.Write()

        print (f'Item Type Library [{g_itemtype_lib_name}], 
Item Type [{g_itemtype_name}] and Properties created successfully...'
) return # Main function def main(): global ACTIVEMODEL global dgn_model global dgn_file global uor # Globals ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgn_model = ACTIVEMODEL.GetDgnModel() dgn_file = dgn_model.GetDgnFile() model_info = dgn_model.GetModelInfo() uor = model_info.GetUorPerStorage() CreateItemTypeLibrary() PyCadInputQueue.SendKeyin("DIALOG ITEMTYPE") # main if __name__ == "__main__": print("***** Item Type Create *****") main()

 

Run/Execute project

Load the project “Create_ItemType.py” from the Python Manager dialog and Run/Execute the python script.

 

Item Type is created as illustrated below.

 

Happy coding!

 

Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs