Welcome to the world of MicroStation Python! In this wiki, we will explore how to create a Text and Text Node element in MicroStation using Python. Additionally, we will apply existing Text Style to the element from the active DGN file.
Let us dive into creating a Text and Text Node elements using Python. Refer to the Python Manager wiki to create and load a python project. Name your project as "CreateTextElements.py" and save it to your preferred directory. Open the project in the editor for writing your Python script.
The script begins by importing several modules offering functions and classes to interact with MicroStation and to create geometric elements.
from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import *
The createTextElement() function takes base point and text style as parameters and creates a text element.
The function begins by creating a TextBlock() object, which includes initializing TextBlock, Paragraph and Run properties. These properties define the structure and style of the text block.
Next, required text is added to the text block using the tb.AppendText() method. In this case, the text "Heading Text" is appended. The Origin of the text block is then set using the tb.SetUserOrigin() method, which positions the text at the specified base point.
A text element is created using the TextElemHandler.CreateElement() method. This method takes an EditElementHandle and the TextBlock as parameters. The function includes error checking to ensure that the text element is successfully created.
The created text element is added to the DGN model using the t_eeh.AddToModel() method.
If all steps are successful, the function returns True, indicating that the text element was created and added to the model successfully.
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
The createTextNodeElement() function creates a text node element. It follows a similar process to creating a single text element but includes additional steps to handle multiple lines of text.
Next the required text is added to the text block using the tb.AppendText() method. After each line of text, a line break is added using the tb.AppendLineBreak() method. This process is repeated for each line of text to be included in the text node element. The Origin of the text block is then set using the tb.SetUserOrigin() method, which positions the text node at the specified base point.
If all steps are successful, the function returns True, indicating that the text node element was created and added to the model successfully.
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
The main function initializes the global variables and calls the functions to create the text and text node elements.
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 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()
Select project "CreateTextElements.py" from the Python Manager dialog and Run/Execute the python script.
Voila! A Text and a Text Node element with the required Text Style is successfully created and added to your active MicroStation model.
Extend the above code to add other text properties. Refer to the delivered examples and documentation.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs