Script to create a Text element with text "Line 1".
Set properties Level, Color, Weight, Style=0.
Set text style "MyCustomTextStyle" if the text style is present in the DGN File.
Note: To create a Text Style, refer to wiki Create Text Style.
from MSPyBentley import * from MSPyECObjects import * from MSPyBentleyGeom import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * def create_text (): # Get active model and file ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgnModel = ACTIVEMODEL.GetDgnModel() dgnFile = dgnModel.GetDgnFile() # Get UOR per master unit for coordinate scaling modelInfo = dgnModel.GetModelInfo() __, uorPerMast = modelInfo.StorageUnit.ConvertDistanceFrom(modelInfo.UorPerStorage, modelInfo.MasterUnit) # Set base point (example: (0,0,0)) basePoint = DPoint3d(0., 0., 0.) basePoint.Scale(uorPerMast) # Try to get the text style "MyCustomTextStyle" textStyle = DgnTextStyle.GetByName("MyCustomTextStyle", dgnFile) # Create text block properties textBlockProps = TextBlockProperties.Create(dgnModel) paraProps = ParagraphProperties.Create(dgnModel) # Use text style if found, else use default font/size if textStyle: runProps = RunProperties.Create(textStyle, dgnModel) else: font = DgnFontManager.GetDefaultTrueTypeFont() size = DPoint2d(1000.0, 1000.0) runProps = RunProperties.Create(font, size, dgnModel) # Create the text block and set content textBlock = TextBlock(textBlockProps, paraProps, runProps, dgnModel) textBlock.AppendText("Line 1") textBlock.SetUserOrigin(basePoint) # Create the text element eeh = EditElementHandle() status = TextElemHandler.CreateElement(eeh, None, textBlock) if status == TextBlockToElementResult.eTEXTBLOCK_TO_ELEMENT_RESULT_Success: # Set properties: Level, Color, Weight, Style=0 propSetter = ElementPropertiesSetter() # Set Level to the first available level levelCache = dgnFile.GetLevelCache() levelIter = iter(levelCache) levelId = next(levelIter).GetLevelId() propSetter.SetLevel(levelId) propSetter.SetColor(2) # Example color index, change as needed propSetter.SetWeight(2) # Example weight, change as needed propSetter.SetLinestyle(0, None) propSetter.Apply(eeh) # Add to model eeh.AddToModel() else: MessageCenter.ShowErrorMessage("Failed to create text element.", "", False) def main (): create_text () if __name__ == "__main__": main ()
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs