18. MicroStation Python:获取图形元素


 

欢迎来到MicroStation Python世界!使用MicroStation时的常见任务之一是搜索特定的元素类型。本文将指导您完成一个Python脚本,该脚本演示如何在MicroStation中搜索特定类型的元素。

让我们深入探讨如何处理模型中的所有图形元素,并仅过滤出与特定元素类型匹配的元素。请参阅Python管理器文章以创建和加载Python项目。将您的项目命名为GetGraphicElements.py并将其保存到任意文件夹下。在编辑器中打开该项目以编写Python脚本。 

导入模块

该脚本首先导入几个模块,这些模块提供了与MicroStation交互以及获取元素属性的函数和类。

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

获取图形元素

selectElementsByType()函数接受元素类型列表作为输入,并在当前模型中选择这些类型的元素。以下是它的功能分解:

  1. 获取DGN模型:使用ACTIVEMODEL.GetDgnModel()检索DGN模型
  2. 获取所有图形元素:使用dgnModel.GetGraphicElements()从模型中提取所有图形元素
  3. 初始化:使用SelectionSetManager.GetManager()初始化选择集管理器并用selSetManager.EmptyAll()清除选择集中的所有现有元素
  4. 遍历元素:对于每个图形元素,检查元素是否可见以及是否为图形类型
  5. 检查元素类型:如果元素类型与其中一个输入类型匹配,则会将该元素添加到选择集中
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函数

main函数指定要搜索的元素类型,并使用这些类型调用selectElementsByType()函数。在此示例中,它搜索线元素、线串元素和形元素。

下面是完整的脚本:

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

运行/执行项目

从Python管理器对话框中选择项目GetGraphicElements.py并运行/执行Python脚本。

当前模型中存在的线、线串和形元素将被添加到选择集中。

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

祝您编程愉快!