13. MicroStation Python:创建文本元素


欢迎来到MicroStation Python的世界!本文将探索如何使用Python在MicroStation中创建文本和文本节点元素。此外,我们将把现有的文本样式应用于当前DGN文件中的元素。

使用Python在MicroStation中创建文本元素
让我们深入探讨使用Python创建文本和文本节点元素。请参阅Python管理器一文以创建和加载Python项目。将项目命名为CreateTextElements.py,并将其保存到任一目录中。在编辑器中打开项目以编写Python脚本。
导入模块
该脚本首先导入几个模块,这些模块提供了与MicroStation交互和创建几何元素的函数和类。

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

创建文本元素
createTextElement()函数将基点和文本样式作为参数,并创建一个文本元素。
该函数首先创建一个TextBlock对象,其中包括初始化TextBlock、Paragraph和Run属性。这些属性定义了文本块的结构和样式。
接下来,使用tb.AppendText()将所需的文本Heading Text添加到文本块中。然后使用tb.SetUserOrigin()设置文本块的原点,将文本定位在指定的基点。
使用TextElemHandler.CreateElement()方法创建文本元素。此方法将EditElementHandle和TextBlock作为参数。该函数包括错误检查,以确保成功创建文本元素。
使用t_eeh.AddToModel()方法将创建的文本元素添加到DGN模型中。
如果所有步骤都成功,则函数返回True,表示文本元素已成功创建并添加到模型中。

 def createTextElement (basePt, txStyle):
    global ACTIVEMODEL
    global dgnModel
    global loadDgnFile    

    tbProp = TextBlockProperties.Create(dgnModel)
    pProp = ParagraphProperties.Create(dgnModel)
    rProp = RunProperties.Create(txStyle, dgnModel)
    tb = TextBlock(tbProp, pProp, rProp, dgnModel)
    tb.AppendText("Heading Text")
    tb.SetUserOrigin(basePt)

    t_eeh = EditElementHandle()

    status = TextElemHandler.CreateElement(t_eeh, None, tb)
    if TextBlockToElementResult.eTEXTBLOCK_TO_ELEMENT_RESULT_Success != status:
        return False

    if BentleyStatus.eSUCCESS != t_eeh.AddToModel():
        return False    
	
    return True

创建文本节点元素
createTextNodeElement()函数创建一个文本节点元素。它遵循与创建单个文本元素类似的过程,但包括处理多行文本的额外步骤。
接下来,使用tb.AppendText()方法将所需的文本添加到文本块中。在每行文本之后,使用tb.AppendLineBreak()方法添加换行符。对于要包含在文本节点元素中的每一行文本,都会重复此过程。然后使用tb.SetUserOrigin()方法设置文本块的原点,将文本节点定位在指定的基点。
如果所有步骤都成功,则函数返回True,表示文本节点元素已成功创建并添加到模型中。

 def createTextNodeElement (basePt, txStyle):
    global ACTIVEMODEL
    global dgnModel
    global loadDgnFile

    tbProp = TextBlockProperties.Create(dgnModel)
    pProp = ParagraphProperties.Create(dgnModel)
    rProp = RunProperties.Create(txStyle, dgnModel)
    tb = TextBlock(tbProp, pProp, rProp, dgnModel)
    tb.AppendText("First Line")
    tb.AppendLineBreak()
    tb.AppendText("Second Line")
    tb.AppendLineBreak()
    tb.AppendText("Third Line")
    tb.SetUserOrigin(basePt)

    tn_eeh = EditElementHandle()

    status = TextElemHandler.CreateElement(tn_eeh, None, tb)
    if TextBlockToElementResult.eTEXTBLOCK_TO_ELEMENT_RESULT_Success != status:
        return False

    if BentleyStatus.eSUCCESS != tn_eeh.AddToModel():
        return False    
	
    return True

将上面代码组合在一起: main函数

main函数初始化全局变量并调用函数来创建文本和文本节点元素。
下面是完整的脚本。

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


# Create Text Node element function  
def createTextNodeElement (basePt, txStyle):
    global ACTIVEMODEL
    global dgnModel
    global loadDgnFile

    tbProp = TextBlockProperties.Create(dgnModel)
    pProp = ParagraphProperties.Create(dgnModel)
    rProp = RunProperties.Create(txStyle, dgnModel)
    tb = TextBlock(tbProp, pProp, rProp, dgnModel)
    tb.AppendText("First Line")
    tb.AppendLineBreak()
    tb.AppendText("Second Line")
    tb.AppendLineBreak()
    tb.AppendText("Third Line")
    tb.SetUserOrigin(basePt)

    tn_eeh = EditElementHandle()

    status = TextElemHandler.CreateElement(tn_eeh, None, tb)
    if TextBlockToElementResult.eTEXTBLOCK_TO_ELEMENT_RESULT_Success != status:
        return False

    if BentleyStatus.eSUCCESS != tn_eeh.AddToModel():
        return False    
	
    return True    


# Create Text element function
def createTextElement (basePt, txStyle):
    global ACTIVEMODEL
    global dgnModel
    global loadDgnFile    

    tbProp = TextBlockProperties.Create(dgnModel)
    pProp = ParagraphProperties.Create(dgnModel)
    rProp = RunProperties.Create(txStyle, dgnModel)
    tb = TextBlock(tbProp, pProp, rProp, dgnModel)
    tb.AppendText("Heading Text")
    tb.SetUserOrigin(basePt)

    t_eeh = EditElementHandle()

    status = TextElemHandler.CreateElement(t_eeh, None, tb)
    if TextBlockToElementResult.eTEXTBLOCK_TO_ELEMENT_RESULT_Success != status:
        return False

    if BentleyStatus.eSUCCESS != t_eeh.AddToModel():
        return False    
	
    return True


# Main function
def main():
    # Global Variables
    global ACTIVEMODEL
    global dgnModel
    global loadDgnFile

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

    # Create Text element
    txtStyle = DgnTextStyle.GetByName ("MyStyle", loadDgnFile)
    if txtStyle is not None:
        if True != createTextElement(DPoint3d(25 * g_1mu, 100 * g_1mu, 0), 
                                              txtStyle):
            print("Text element creation failed...")
    else:
        print("TextStyle MyStyle not found...")

    # Create Text Node element
    txtStyle = DgnTextStyle.GetByName ("MyStyle1", loadDgnFile)
    if txtStyle is not None:
        if True != createTextNodeElement(DPoint3d(25 * g_1mu, 50 * g_1mu, 0), 
                                                  txtStyle):
            print("Text node element creation failed...")
    else:
        print("TextStyle MyStyle1 not found...")

    PyCadInputQueue.SendKeyin("FIT VIEW EXTENDED; CHOOSE ELEMENT")


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

运行/执行项目

Python管理器对话框中选择项目CreateTextElements.py,并运行/执行Python脚本。
瞧!已成功创建具有所需文本样式的文本和文本节点元素,并将其添加到MicroStation的当前模型中。

image

请参阅交付的示例和文档扩展上述代码以添加其他文本属性。
祝您编码愉快!