Welcome to the world of MicroStation Python! One of the common tasks when working with MicroStation is processing selected elements. In this wiki, we will walk through a Python script that demonstrates how to manage selected elements in MicroStation.
Let us dive into processing selected elements in MicroStation, checking if elements are selected, iterating through them, and performing actions on each. Refer to the Python Manager wiki to create and load a python project. Name your project as "SelectionSet.py" and save it to your preferred directory. Open the project in the editor for writing your Python script.
The script begins by importing several modules offering functions and classes to interact with MicroStation and to create geometric elements.
from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import *
Process Selected Elements
The ProcessSelectedElements() function is designed to manage and process selected elements. Let us walk through the steps.
def ProcessSelectedElements (): eh = ElementHandle() selSetMgr = SelectionSetManager.GetManager() elmTypeString = WString() # Check of Selection Set is active if False == selSetMgr.IsActive(): print("No elements selected...") # Process selected elements for i in range(selSetMgr.NumSelected()): # Get Element Handle of selected element ret = selSetMgr.GetElement(i, eh) if BentleyStatus.eSUCCESS == ret: # Do Something eh.GetHandler().GetTypeName (elmTypeString, 100) # Empty Selection Set selSetMgr.EmptyAll()
Putting it all Together: The main Function
The main function initializes the active model and calls the function to process selected elements.
Here is the complete script.
from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * # Get Level Name of Level ID passed as argument def GetLevelName(lvlId): global ACTIVEMODEL levelCache = ACTIVEMODEL.GetDgnFile().GetLevelCache() it = iter(levelCache) for level in it: levelId = level.GetLevelId() if lvlId == levelId: return level.GetName() # Process Selected Elements def ProcessSelectedElements(): elmTypeString = WString() eh = ElementHandle() selSetMgr = SelectionSetManager.GetManager() # Check of Selection Set is active if False == selSetMgr.IsActive(): print("No elements selected...") # Process selected elements for i in range(selSetMgr.NumSelected()): # Get Element Handle of selected element ret = selSetMgr.GetElement(i, eh) if BentleyStatus.eSUCCESS == ret: eh.GetHandler().GetTypeName (elmTypeString, 100) msElement = MSElement() msElement = eh.GetElement() levelId = msElement.ehdr.level levelName = GetLevelName(levelId) print("***") print(f"Element Description: {elmTypeString}") print(f"Element Type: {eh.ElementType}, Element ID: {eh.ElementId}") print(f"Level Name: {levelName}, Level ID: {levelId}") # Empty Selection Set selSetMgr.EmptyAll() # Main function def main(): global ACTIVEMODEL ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef ProcessSelectedElements() # Main if __name__ == "__main__": print("********** Selection Set **********") main()
Load the project "SelectionSet.py" from the Python Manager dialog, select some elements from the active model and Run/Execute the python script.
Selected element information (Description, Type, Id, Level Name, Level Id), is displayed in the MicroStation Text window.
This script provides a basic framework for handling selected elements in MicroStation using Python. Feel free to customize the script to suit your specific needs and integrate it into your workflow.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs