Welcome to the world of MicroStation Python! In our previous wiki, we explored how to create an Item Type definition "RoomDetails". Now, in this wiki, we'll take the next step and learn how to attach or link the "RoomDetails" Item Type to an element within MicroStation.
To get started, refer to the Python Manager wiki for instructions on creating and loading a new Python project. Name your project "ItemType_AttachItem.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 attaches the 'RoomDetails' Item Type to each identified element.
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 # Attach Item Type RoomDetails to 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() # Change color, linestyle, weight to the line element if (isGraphics and isVisible and eleType == 6): itemHost = CustomItemHost(eeh, False) itemHost.GetCustomItem dgnecinstance_existing = itemHost.GetCustomItem(g_itemtype_lib_name, g_itemtype_name) if (None == dgnecinstance_existing): dgnecinstance = itemHost.ApplyCustomItem(roomdetItemType) if None != dgnecinstance: print (f'Item type attached to element Id:[{eeh.GetElementId()}]...\n') else: print (f'Element Id:[{eeh.GetElementId()}] Type:[{eeh.GetElementType()}]
has item type attached...\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 Attach *****") main()
Download the attached DGN file “Building_Attach.dgn”. Load the project “ItemType_AttachItem.py” from the Python Manager dialog and Run/Execute the python script.
Item Type “RoomDetails” is attached to all Shape elements as illustrated below.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs