12. MicroStation Python:创建复杂元素


欢迎来到MicroStation Python的世界!之前的文章探讨了如何使用Python在MicroStation中创建基本/简单元素。这一次,我们将深入探讨创建复杂元素。此外,我们将为复杂形状元素设置元素属性(线符),包括填充属性。

在MicroStation中用Python创建复杂元素

让我们深入探讨使用Python创建复杂元素,如复杂链(Complex Chain)和复杂形(Complex Shape)。请参阅Python管理器文章以创建和加载Python项目。将项目命名为CreateComplexElement.py,并将其保存到任一目录中。在编辑器中打开项目以编写Python脚本。

导入模块

该脚本首先导入几个模块,这些模块提供了与MicroStation交互和创建几何元素的函数和类。

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

创建复杂链(Complex Chain)元素

createComplexChainElement()函数创建了一个复杂链元素,并包括错误检查,以确保元素被成功创建并添加到模型中。
它首先使用ChainHeaderHandler.CreateChainHeaderElement()方法创建一个链头。
请注意,对于复杂链元素,第三个参数isClosed设置为False
接下来,将使用LineHandler.CreateLineElement()方法创建多个线元素,并使用ChainHeaderHandler.AddComponentElement()方法将这些线元素添加到链头。
最后,使用ChainHeaderHandler.AddComponentComplete()方法添加所有组件后更新链元素范围。

 def createComplexChainElement():
    global ACTIVEMODEL

    cc_eeh = EditElementHandle()

    NUM_LINES = 3

    lines0 = EditElementHandle()
    lines1 = EditElementHandle()
    lines2 = EditElementHandle()

    Lines = [lines0, lines1, lines2]
    m_points = [[DPoint3d() for i in range(2)] for j in range(NUM_LINES)]

    # Create Lines and add to Chain Header
    ChainHeaderHandler.CreateChainHeaderElement(cc_eeh, None, False, 
                                                 ACTIVEMODEL.Is3d(), ACTIVEMODEL)

    for j in range(0, NUM_LINES):
        if (j == 0):
            m_points[j][0].x = 0; m_points[j][0].y = 0
            m_points[j][1].x = 0; m_points[j][1].y = 5000

        if (j == 1):
            m_points[j][0].x = 0; m_points[j][0].y = 5000
            m_points[j][1].x = 5000; m_points[j][1].y = 5000

        if (j == 2):
            m_points[j][0].x = 5000; m_points[j][0].y = 5000
            m_points[j][1].x = 5000; m_points[j][1].y = 0

        pt1 = DPoint3d(m_points[j][0].x, m_points[j][0].y, m_points[j][0].z)
        pt2 = DPoint3d(m_points[j][1].x, m_points[j][1].y, m_points[j][1].z)

        seg = DSegment3d(pt1, pt2)
        LineHandler.CreateLineElement(Lines[j], None, seg, 
                                       ACTIVEMODEL.Is3d(), ACTIVEMODEL)

        ChainHeaderHandler.AddComponentElement (cc_eeh, Lines[j])

    # Update Complex Chain element range
    ChainHeaderHandler.AddComponentComplete(cc_eeh)

    # Add the Complex Chain element to model
    if BentleyStatus.eSUCCESS != cc_eeh.AddToModel():
        return False

    return True

以下脚本设置了复杂链元素的属性(线符),包括颜色和线宽。

     # Set Element Properties (Symbology) to Element
    color = 100
    weight = 2
    propertiesSetter = ElementPropertiesSetter()
    propertiesSetter.SetColor(color)
    propertiesSetter.SetWeight(weight)
    propertiesSetter.Apply(cc_eeh)

创建复杂形(Complex Shape)元素

createComplexShapeElement()函数创建了一个复杂形元素,并包括错误检查,以确保元素被成功创建并添加到模型中。
它首先使用ChainHeaderHandler.CreateChainHeaderElement()方法创建一个链头。
请注意,对于复杂形元素,第三个参数isClosed设置为True
接下来,使用ArcHandler.CreateArcElement()方法创建弧,用LineStringHandler.CreateLineStringElement()方法创建线串,并使用ChainHeaderHandler.AddComponentElement()方法将弧和线串元素添加到链头。
最后,使用ChainHeaderHandler.AddComponentComplete()方法添加所有组件后更新链元素范围。

 def createComplexShapeElement (basePt):
    global g_1mu
    global ACTIVEMODEL

    a_eeh = EditElementHandle()
    l_eeh = EditElementHandle()
    cs_eeh = EditElementHandle()

    # Create Complex Shape Header Element
    ChainHeaderHandler.CreateChainHeaderElement(cs_eeh, None, True, 
                                                 ACTIVEMODEL.Is3d(), ACTIVEMODEL)  


    # Create Arc Element
    pt1 = DPoint3d()
    pt2 = DPoint3d()
    pt3 = DPoint3d()

    pt1.x = basePt.x
    pt1.y = basePt.y
    pt1.z = 0.0
    pt2.x = g_1mu*1.3
    pt2.y = + g_1mu*0.7
    pt2.z = 0.0
    pt3.x = basePt.x + g_1mu
    pt3.y = basePt.y + g_1mu
    pt3.z = basePt.z

    el3d = DEllipse3d.FromPointsOnArc(pt3, pt2, pt1)
    status = ArcHandler.CreateArcElement(a_eeh, None, el3d, 
                                          ACTIVEMODEL.Is3d(), ACTIVEMODEL)

    if BentleyStatus.eSUCCESS != status:
        return False

    # Add Arc/Ellipse to Chain Header
    ChainHeaderHandler.AddComponentElement (cs_eeh, a_eeh)
    ChainHeaderHandler.AddComponentComplete(cs_eeh)  


    # Create LineString Element
    pt2.x = pt1.x + g_1mu
    pt2.y = pt1.y
    points = DPoint3dArray()
    points.append(pt1)
    points.append(pt2)
    points.append(pt3)

    status = LineStringHandler.CreateLineStringElement(l_eeh, None, points, 
                                                        ACTIVEMODEL.Is3d(), 
                                                        ACTIVEMODEL)

    if BentleyStatus.eSUCCESS != status:
        return False
    
    # Add LineString to Chain Header
    ChainHeaderHandler.AddComponentElement (cs_eeh, l_eeh)
    ChainHeaderHandler.AddComponentComplete(cs_eeh)  

    # Set color, weight to the created element
    color = 100
    weight = 2
    propertiesSetter = ElementPropertiesSetter()
    propertiesSetter.SetColor(color)
    propertiesSetter.SetWeight(weight)
    propertiesSetter.Apply(cs_eeh)

    # Add and Set fill color
    fill_color = 31
    IAreaFillPropertiesEdit = cs_eeh.GetHandler()
    IAreaFillPropertiesEdit.AddSolidFill(cs_eeh, fill_color, True)

    # Add the Complex Shape element to model
    if BentleyStatus.eSUCCESS != cs_eeh.AddToModel():
        return False
    
    return True

以下脚本设置了复杂形元素的属性(线符),包括颜色和线宽。

     # Set color, weight to the created element
    color = 100
    weight = 2
    propertiesSetter = ElementPropertiesSetter()
    propertiesSetter.SetColor(color)
    propertiesSetter.SetWeight(weight)
    propertiesSetter.Apply(cs_eeh)

以下脚本将填充属性设置给封闭的复杂形元素。

     # Add and Set fill color
    fill_color = 31
    IAreaFillPropertiesEdit = cs_eeh.GetHandler()
    IAreaFillPropertiesEdit.AddSolidFill(cs_eeh, fill_color, True)

将所有代码组合在一起:main函数

main函数初始化全局变量并调用函数来创建复杂元素。
以下是完整的脚本。

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


# Create Complex Shape element
def CreateComplexShapeElement (basePt):
    global g_1mu
    global ACTIVEMODEL

    a_eeh = EditElementHandle()
    l_eeh = EditElementHandle()
    cs_eeh = EditElementHandle()

    # Create Complex Shape Header Element
    ChainHeaderHandler.CreateChainHeaderElement(cs_eeh, None, True, 
                                                 ACTIVEMODEL.Is3d(), ACTIVEMODEL)  

    # Create Arc Element
    pt1 = DPoint3d()
    pt2 = DPoint3d()
    pt3 = DPoint3d()

    pt1.x = basePt.x
    pt1.y = basePt.y
    pt1.z = 0.0
    pt2.x = g_1mu*1.3
    pt2.y = + g_1mu*0.7
    pt2.z = 0.0
    pt3.x = basePt.x + g_1mu
    pt3.y = basePt.y + g_1mu
    pt3.z = basePt.z

    el3d = DEllipse3d.FromPointsOnArc(pt3, pt2, pt1)
    status = ArcHandler.CreateArcElement(a_eeh, None, el3d, 
                                          ACTIVEMODEL.Is3d(), ACTIVEMODEL)

    if BentleyStatus.eSUCCESS != status:
        return False

    # Add Arc/Ellipse to Chain Header
    ChainHeaderHandler.AddComponentElement (cs_eeh, a_eeh)
    ChainHeaderHandler.AddComponentComplete(cs_eeh)  


    # Create LineString Element
    pt2.x = pt1.x + g_1mu
    pt2.y = pt1.y
    points = DPoint3dArray()
    points.append(pt1)
    points.append(pt2)
    points.append(pt3)

    status = LineStringHandler.CreateLineStringElement(l_eeh, None, points, 
                                                        ACTIVEMODEL.Is3d(), 
                                                        ACTIVEMODEL)

    if BentleyStatus.eSUCCESS != status:
        return False
    
    # Add LineString to Chain Header
    ChainHeaderHandler.AddComponentElement (cs_eeh, l_eeh)
    ChainHeaderHandler.AddComponentComplete(cs_eeh)  

    # Set color, weight to the created element
    color = 100
    weight = 2
    propertiesSetter = ElementPropertiesSetter()
    propertiesSetter.SetColor(color)
    propertiesSetter.SetWeight(weight)
    propertiesSetter.Apply(cs_eeh)

    # Add and Set fill color
    fill_color = 31
    IAreaFillPropertiesEdit = cs_eeh.GetHandler()
    IAreaFillPropertiesEdit.AddSolidFill(cs_eeh, fill_color, True)

    # Add the Complex Shape element to model
    if BentleyStatus.eSUCCESS != cs_eeh.AddToModel():
        return False
    
    return True


# Create Complex Chain element
def CreateComplexChainElement():
    global ACTIVEMODEL

    cc_eeh = EditElementHandle()

    NUM_LINES = 3

    lines0 = EditElementHandle()
    lines1 = EditElementHandle()
    lines2 = EditElementHandle()

    Lines = [lines0, lines1, lines2]
    m_points = [[DPoint3d() for i in range(2)] for j in range(NUM_LINES)]

    # Create Lines and add to Chain Header
    ChainHeaderHandler.CreateChainHeaderElement(cc_eeh, None, False, 
                                                 ACTIVEMODEL.Is3d(), ACTIVEMODEL)

    for j in range(0, NUM_LINES):
        if (j == 0):
            m_points[j][0].x = 0; m_points[j][0].y = 0
            m_points[j][1].x = 0; m_points[j][1].y = 5000

        if (j == 1):
            m_points[j][0].x = 0; m_points[j][0].y = 5000
            m_points[j][1].x = 5000; m_points[j][1].y = 5000

        if (j == 2):
            m_points[j][0].x = 5000; m_points[j][0].y = 5000
            m_points[j][1].x = 5000; m_points[j][1].y = 0

        pt1 = DPoint3d(m_points[j][0].x, m_points[j][0].y, m_points[j][0].z)
        pt2 = DPoint3d(m_points[j][1].x, m_points[j][1].y, m_points[j][1].z)

        seg = DSegment3d(pt1, pt2)
        LineHandler.CreateLineElement(Lines[j], None, seg, 
                                       ACTIVEMODEL.Is3d(), ACTIVEMODEL)

        ChainHeaderHandler.AddComponentElement (cc_eeh, Lines[j])

    # Update Complex Chain element range
    ChainHeaderHandler.AddComponentComplete(cc_eeh)

    # Set Element Properties (Symbology) to Element
    color = 100
    weight = 2
    propertiesSetter = ElementPropertiesSetter()
    propertiesSetter.SetColor(color)
    propertiesSetter.SetWeight(weight)
    propertiesSetter.Apply(cc_eeh)

    # Add the Complex Chain element to model
    if BentleyStatus.eSUCCESS != cc_eeh.AddToModel():
        return False

    return True


def main():
    # Global Variables
    global g_1mu
    global ACTIVEMODEL

    # Get the active DGN model reference
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    dgnModel = ACTIVEMODEL.GetDgnModel()
    modelInfo = dgnModel.GetModelInfo() 
    g_1mu = modelInfo.GetUorPerStorage()

    # Create Complex Chain element
    if True != CreateComplexChainElement():
        print("Complex Chain creation failed...")

    # Create Complex Shape element
    if True != CreateComplexShapeElement(DPoint3d(g_1mu, 0, 0)):
        print("Complex Shape creation failed...")

    PyCadInputQueue.SendKeyin("FIT VIEW EXTENDED")


#main
if __name__ == "__main__":
    print ("Create Complex Elements...")
    main()

运行/执行项目

从Python管理器对话框中选择项目CreateComplexElement.py,并运行/执行Python脚本。
瞧!已成功创建复杂链和复杂形元素,并将其添加到活动MicroStation模型中。

image

请参阅交付的示例和文档增强上述代码,在MicroStation中添加、注册键入命令来创建上述的复杂链和复杂形。
祝您编码愉快!