MicroStation Python: Create Text Fields


WIP (Work In Progress)

 

Welcome to the world of MicroStation Python! In this wiki, we will explore how to create and place Text Fields attached as Item Type to an Element in MicroStation using Python.

 

Here is the complete script.

 

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


# Create Text Block Function
def CreateTextBlock():
    global dgn_file
    global dgn_model

    txt_style = DgnTextStyle.GetSettings(dgn_file)
    txt_block = TextBlock(txt_style, dgn_model)

    return txt_block


# Create Fields Function
def CreateField(eeh):
    global dgn_model
    global uor

    # DGN EC Instance
    itemHost = CustomItemHost(eeh, False)
    dgnecinstance_0 = itemHost.GetCustomItem("Project_001", "RoomDetails")
    dgn_element_ecinstance = dgnecinstance_0.GetAsElementInstance()

    # Text Fields
    iec_instance = IECInstance.CreateCopyThroughSerialization(dgnecinstance_0)
    text_field_0 = TextField.CreateForElement(dgn_element_ecinstance, "Description", 
                                              iec_instance, dgn_model)
    text_field_1 = TextField.CreateForElement(dgn_element_ecinstance, "ID", 
                                              iec_instance, dgn_model)    
    text_field_2 = TextField.CreateForElement(dgn_element_ecinstance, "Temperature", 
                                              iec_instance, dgn_model)
    text_field_3 = TextField.CreateForElement(dgn_element_ecinstance, "Humidity", 
                                              iec_instance, dgn_model)

    # Create Text Block
    text_block = CreateTextBlock ()

    # Description Field
    text_block.AppendField(text_field_0)
    text_block.AppendText(" - ")
    # ID Field
    text_block.AppendField(text_field_1)
    text_block.AppendLineBreak()

    # Temperature Field
    text_block.AppendText("T: ")
    text_block.AppendField(text_field_2)
    text_block.AppendLineBreak()
    # Humidity Field
    text_block.AppendText("H: ")
    text_block.AppendField(text_field_3)
    text_block.AppendLineBreak()    

    # Text Origin
    curve = ICurvePathQuery.ElementToCurveVector(eeh)    
    if curve.IsClosedPath():
        text_org = DPoint3d()
        # text_org = centriod
        isSuccess, text_org, normal, area = curve.CentroidNormalArea()
    else:
        text_org = DPoint3d(0, 0, 0)
    text_block.SetUserOrigin(text_org)

    # Text Rotation
    rmat = RotMatrix ()
    rmat.InitIdentity()
    text_block.SetOrientation(rmat)

    # Create Text Element
    t_eeh = EditElementHandle()
    status = TextElemHandler.CreateElement(t_eeh, None, text_block)
    if (status != TextBlockToElementResult.eTEXTBLOCK_TO_ELEMENT_RESULT_Success):
        print("Text Field creation failed...")
        return False

    # Add Text Element to Model
    if (eSUCCESS != t_eeh.AddToModel ()):
        print("Could not add Text Field element to model...")
        return False

    return True


# Main function
def main():
    global dgn_model
    global dgn_file
    global uor

    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    dgn_model = ACTIVEMODEL.GetDgnModel()
    dgn_file = dgn_model.GetDgnFile()
    model_info = dgn_model.GetModelInfo()
    uor = model_info.GetUorPerStorage()
    
    # Get Element by ID
    elementId = 1684
    eeh = EditElementHandle(elementId, dgn_model)

    # Create Fields
    CreateField(eeh)


# main
if __name__ == "__main__":
    main()

 

Run/Execute project

Load the project "CreateTextField.py" from the Python Manager dialog and Run/Execute the python script.

Text Fields are created and placed, from the Item Types attached to the Shape Element.

 

 

 

Run the script using the attached DGN file to verify its functionality.

 

File:

Building_IT.dgn

 

Video:

 

 

Happy coding!