MicroStation Python: Selection Set Manager


 

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.

 

Manage Selected Elements in MicroStation with Python

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.

 

Import Modules

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.

  1. Initialization: The script starts by initializing an ElementHandle object and obtaining the SelectionSetManager instance.
  2. Check Selection Set: Before processing, the script checks if the selection set is active using selSetMgr.IsActive(). If no elements are selected, it prints a message and exits.
  3. Iterate Through Selected Elements: If elements are selected, the script iterates through each selected element using a for loop. The selSetMgr.GetElement(i, eh) method retrieves the element handle for each selected element.
  4. Process Each Element: Within the loop, the script checks if the element retrieval was successful using BentleyStatus.eSUCCESS. If successful, you can add your custom processing logic where the comment # Do Something is placed.
  5. Empty Selection Set: After processing all selected elements, the script clears the selection set using selSetMgr.EmptyAll().

 

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

 

Run/Execute project

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