MicroStation Python: Detach Item Type


Welcome to the world of MicroStation Python! In our last wiki, we delved into the process of attaching the "RoomDetails" Item Type to an element. This time, we'll take it a step further and guide you through detaching the "RoomDetails" Item Type from an element within MicroStation. Let's get started!

 

Detach Item Type

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

 

The script scans the DGN file for Shape elements and detaches the "RoomDetails" Item Type if it is attached to any of the shape elements.

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"


# Scan Shape Elements Function
def ScanShapeElements():
    global dgn_model

    # Check Project_001 Item Type Library exists
    g_itemTypeLib = ItemTypeLibrary.FindByName(g_itemtype_lib_name, dgn_file)
    if (None == g_itemTypeLib):
        print (f"Item Type Library [{g_itemtype_lib_name}] not found...")
        return

    # Check if RoomDetails Item Type exists
    roomdetItemType = g_itemTypeLib.GetItemTypeByName(g_itemtype_name)
    if(None == roomdetItemType):
        print (f"Item Type [{g_itemtype_name}] not found...")
        return

    # Detach Item Type RoomDetails from Shape elements
    graphicalElements = dgn_model.GetGraphicElements()

    for elmRef in graphicalElements:
        eeh = EditElementHandle(elmRef, dgn_model)
        msElement = eeh.GetElement()
        isGraphics = msElement.ehdr.isGraphics
        isVisible = not(msElement.hdr.dhdr.props.b.invisible)
        eleType = eeh.GetElementType()

        if (isGraphics and isVisible and eleType == 6):
            itemHost = CustomItemHost(eeh, False)
            dgnecinstance = itemHost.GetCustomItem(g_itemtype_lib_name, g_itemtype_name)
            if (None != dgnecinstance):
                dgnecinstance.Delete()
                print (f'Item type detached from element Id:[{eeh.GetElementId()}]...\n')

    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()

    ScanShapeElements()


# main
if __name__ == "__main__":
    print("***** Item Type Detach *****")
    main()

Run/Execute project

Download the DGN file “Building_Detach.dgn”. Load the project “ItemType_DetachItem.py” from the Python Manager dialog and Run/Execute the python script.

 

Item Type “RoomDetails” is detached from all Shape elements.

 

Happy coding!

 

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