MicroStation Python: Fence Manager


 

Welcome to the world of MicroStation Python! One of the common tasks when working with MicroStation is processing elements inside a Fence. In this wiki, we will walk through a Python script that demonstrates how to manage, fence elements in MicroStation.

 

Manage Fence Elements in MicroStation with Python

Let us dive into processing fence elements in MicroStation, 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 “FenceElements.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 Fence Elements

The ProcessFenceElements() function is designed to manage and process fence elements. Let us walk through the steps.

  1. Initialization: The script begins by initializing the necessary instances FenceManager.GetManager(), SelectionSetManager.GetManager() and a WString().
  2. Check if Fence is active: Before processing, the script checks if there is an active fence using fenceMgr.IsFenceActive(). If not, it prints a message and exits the function.
  1. Initialize Fence Parameters: Next the script populates the supplied Fence Params with information for the active fence using fenceMgr.InitFromActiveFence().
  2. Build Element Agenda: The script builds the agenda with elements that meet the fence parameters using fenceMgr.BuildAgenda().
  3. Iterate Through Fence Elements: The script iterates through each element that matches the fence criteria. The eeh = eAgenda.GetEntry(i) method retrieves the edit element handle for each selected element.
  4. Process Each Element: Add your custom processing logic for the element, where the comment # Do Something is placed.

 

def ProcessFenceElements():
    global ACTIVEMODEL

    # Initialize
    fenceMgr = FenceManager.GetManager()
    selSetManager = SelectionSetManager.GetManager()
    elmTypeString = WString()

    # Check of Fence is active
    if False == fenceMgr.IsFenceActive():
        print("No active fence...")
        return

    # InitFromActiveFence: Populate the supplied FenceParams with information 
    #                      for the active fence. 
    fenceParams = FenceParams(ACTIVEMODEL)
    bOverlapMode = fenceMgr.IsOverlapMode()    
    bClipMode = fenceMgr.IsClipMode()
    allowClipFlag = FenceClipMode.eOriginal
    fenceMgr.InitFromActiveFence (fenceParams, bOverlapMode, bClipMode, allowClipFlag)

    # ElementAgenda: A bvector of EditElementHandle entries to be used for 
    #                operating on groups of elements such as SelectionSet 
    #                and Fences. 
    eAgenda = ElementAgenda()
    modelRefList = DgnFile.GetModelRefList (DgnFile.GetMasterFile())
    # BuildAgenda: Collect elements that satisfy the supplied FenceParams/criteria.
    fenceMgr.BuildAgenda(fenceParams, eAgenda, modelRefList, False, False, False)

    # Clear current selection set
    selSetManager.EmptyAll()

    # Process Fence elements
    for i in range(eAgenda.GetCount()):
        # Do Something
        eeh = eAgenda.GetEntry(i)
        eeh.GetHandler().GetTypeName (elmTypeString, 100)

 

Putting it all Together: The main Function

The main function initializes the active model and calls the function to process fence 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 Fence Elements
def ProcessFenceElements():
    global ACTIVEMODEL

    # Initialize
    fenceMgr = FenceManager.GetManager()
    selSetManager = SelectionSetManager.GetManager()
    elmTypeString = WString()

    # Check of Fence is active
    if False == fenceMgr.IsFenceActive():
        print("No active fence...")
        return

    # InitFromActiveFence: Populate the supplied FenceParams with information 
    #                      for the active fence. 
    fenceParams = FenceParams(ACTIVEMODEL)
    bOverlapMode = fenceMgr.IsOverlapMode()    
    bClipMode = fenceMgr.IsClipMode()
    allowClipFlag = FenceClipMode.eOriginal
    fenceMgr.InitFromActiveFence (fenceParams, bOverlapMode, bClipMode, allowClipFlag)

    # ElementAgenda: A bvector of EditElementHandle entries to be used for 
    #                operating on groups of elements such as SelectionSet 
    #                and Fences. 
    eAgenda = ElementAgenda()
    modelRefList = DgnFile.GetModelRefList (DgnFile.GetMasterFile())
    # BuildAgenda: Collect elements that satisfy the supplied FenceParams/criteria.
    fenceMgr.BuildAgenda(fenceParams, eAgenda, modelRefList, False, False, False)

    # Clear current selection set
    selSetManager.EmptyAll()

    # Process Fence elements
    for i in range(eAgenda.GetCount()):
        eeh = eAgenda.GetEntry(i)
        eeh.GetHandler().GetTypeName (elmTypeString, 100)

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

        levelId = msElement.ehdr.level
        levelName = GetLevelName(levelId)

        selSetManager.AddElement(eeh.GetElementRef(),ACTIVEMODEL.GetDgnModel())  

        print("***")
        print(f"Element Description: {elmTypeString}")
        print(f"Element Type: {eeh.GetElementType()}, Element ID: {eeh.GetElementId()}")
        print(f"Level Name: {levelName}, Level ID: {levelId}")


# Main function
def main():
    global ACTIVEMODEL
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef

    ProcessFenceElements()


# Main
if __name__ == "__main__":
    print("********** Fence Elements **********")
    main()

Run/Execute project

Load the project “FenceElements.py” from the Python Manager dialog, place a Fence covering elements from the active model, select the required Fence Type, Mode and Run/Execute the python script.

Elements matching the Fence criteria are highlighted and their information (Description, Type, Id, Level Name, Level Id), is displayed in the MicroStation Text window.

 

 

This script provides a basic framework for handling fence 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