MicroStation Python: Get Graphic Elements


 

MicroStation Python: Get Graphic Elements

Welcome to the world of MicroStation Python! One of the common tasks when working with MicroStation is searching for specific element types. In this wiki, we will guide you through a Python script that demonstrates how to search for elements of a particular element type in MicroStation.

 

Get Graphic Elements in MicroStation with Python

Let us dive into processing all graphical elements from a model and filter out only those elements that match specific element types. Refer to the Python Manager wiki to create and load a python project. Name your project as “GetGraphicElements.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 MSPyMstnPlatform import *
from MSPyDgnView import *

 

Get Graphic Elements

The selectElementsByType() function, takes a list of element types as input and selects elements of those types in the active model. Here is a breakdown of what it does:

  1. Get DGN model: Retrieves the DGN model using ACTIVEMODEL.GetDgnModel().
  1. Get all graphical elements: Fetches all graphical elements from the model using dgnModel.GetGraphicElements().
  1. Initialize: Initialize selection set manager using SelectionSetManager.GetManager() and clear any existing elements in the selection set selSetManager.EmptyAll().
  2. Iterate through elements: For each graphical element, it checks if the element is visible and of a graphical type.
  3. Check element type: If the element type matches one of the input types, it adds the element to the selection set.

 

 

def selectElementsByType(inputElementTypes):
    #Get active model
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    dgnModel = ACTIVEMODEL.GetDgnModel()

    #Get all graphical elements from the model
    graphicalElements = dgnModel.GetGraphicElements()
    selSetManager = SelectionSetManager.GetManager()
    selSetManager.EmptyAll()

    for perElementRef in graphicalElements:
        elementId = perElementRef.GetElementId()
        eeh = EditElementHandle(perElementRef, dgnModel)
        eh = ElementHandle(perElementRef)

        msElement = MSElement()
        msElement = eeh.GetElement ()

        isGraphics = msElement.ehdr.isGraphics
        isInvisible = msElement.hdr.dhdr.props.b.invisible

        if (isGraphics and not(isInvisible)):
            eleType = eh.GetElementType()
            if eleType in inputElementTypes:
                #It will select highlight all elements added in selection set
                selSetManager.AddElement(perElementRef,dgnModel)         

 

Putting it all Together: The main Function

The main function specifies the element types to search for and calls the selectElementsByType() function with these types. In this example, it searches for Line elements, Line String elements, and Shape elements.

Here is the complete script.

 

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


# Select elements by Element type
def selectElementsByType(inputElementTypes):
    #Get active model
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    dgnModel = ACTIVEMODEL.GetDgnModel()

    #Get all graphical elements from the model
    graphicalElements = dgnModel.GetGraphicElements()
    selSetManager = SelectionSetManager.GetManager()
    selSetManager.EmptyAll()

    for perElementRef in graphicalElements:
        elementId = perElementRef.GetElementId()
        eeh = EditElementHandle(perElementRef, dgnModel)
        eh = ElementHandle(perElementRef)

        msElement = MSElement()
        msElement = eeh.GetElement ()

        isGraphics = msElement.ehdr.isGraphics
        isInvisible = msElement.hdr.dhdr.props.b.invisible

        if (isGraphics and not(isInvisible)):
            eleType = eh.GetElementType()
            if eleType in inputElementTypes:
                #It will select highlight all elements added in selection set
                selSetManager.AddElement(perElementRef,dgnModel)         


# Main function
def main():
    #highlight all the elements with type 
    #2-D/3-D Line Element (type 3)
    #2-D/3-D Line String Element (type 4)
    #2-D/3-D Shape Element (type 6)
    #2-D/3-D Ellipse Element (type 15)
    #2-D/3-D Arc Element (type 16)
    #2-D/3-D Text Element (type 17)
    inputElementTypes = [3, 4, 6]
    selectElementsByType(inputElementTypes)


# main
if __name__ == "__main__":
    main()

 

Run/Execute project

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

Line, Line String and Shape elements present in the active Model is added to the Selection Set.

 

This script provides a basic framework for searching elements by element type 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