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