19. MicroStation Python:选择集管理器


欢迎来到MicroStation Python世界! 使用MicroStation时的常见任务之一是处理选定的元素。本文通过一个Python脚本来演示如何管理MicroStation中的选定元素。

让我们深入探讨在MicroStation中处理选定元素,检查元素是否被选中,迭代它们,并对每个元素执行操作。请参阅Python管理器文章以创建和加载Python项目。将您的项目命名为SelectionSet.py并将其保存到任意文件夹下。在编辑器中打开该项目以编写Python脚本。  

导入模块

该脚本首先导入几个模块,这些模块提供了与MicroStation交互以及操作选择集的函数和类。

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

处理选定元素

ProcessSelectedElements()函数旨在管理和处理选定的元素。让我们分步实现这些功能:

  1. 初始化:脚本首先初始化ElementHandle对象并获取SelectionSetManager实例
  2. 检查选择集:在处理之前,脚本会使用selSetMgr.IsActive()检查选择集是否处于激活状态。如果没有选择任何元素,它将打印一条消息并退出
  3. 遍历选定元素:如果选择了元素,脚本将使用for循环遍历每个选定元素。selSetMgr.GetElement(i,eh)方法检索每个选定元素的元素句柄
  4. 处理每个元素:在循环中,脚本使用BentleyStatus.eSUCCESS检查元素检索是否成功。如果成功,您可以在放置注释#Do Something的位置添加自定义处理逻辑
  5. 清空选择集:处理完所有选定元素后,脚本将使用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()

将以上代码集成在一起:main函数

main函数初始化当前模型并调用ProcessSelectedElements()函数来处理选定的元素。

下面是完整的脚本:

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()
运行/执行项目

从当前模型中选择一些元素,然后从Python管理器对话框中选择项目SelectionSet.py并运行/执行Python脚本。

选定的元素信息(描述、类型、Id、层名称、层Id)将显示在MicroStation文本窗口中。

此脚本提供了一个使用Python处理MicroStation中选定元素的基本框架。您可以自由定制脚本以满足您的特定需求,并将其集成到您的工作流程中。

祝您编程愉快!