Python Assistant Prompts: Create Text Style


 

Prompt Used:

Can you write a script which defines a new text style with specific font, height, 
width, color, and spacing parameters?

 

 

Python Assistant Generated Code:

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


def NewTextStyle():
    # Set your desired parameters here
    style_name = "MyCustomTextStyle"
    font_name = "Arial"
    height_master = 2.5      # Height in master units
    width_master = 2.5       # Width in master units
    color_index = 3          # Index from color table
    char_spacing_master = 0.1  # Character spacing in master units

    # Get active model and file
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    dgnModel = ACTIVEMODEL.GetDgnModel()
    dgnFile = dgnModel.GetDgnFile()

    # Get UORs per master unit for scaling
    modelInfo = dgnModel.GetModelInfo()
    __, uorPerMast = modelInfo.StorageUnit.ConvertDistanceFrom(modelInfo.UorPerStorage, modelInfo.MasterUnit)

    # Find the font by name
    font = DgnFontManager.FindSystemFont(font_name, DgnFontFilter.eTrueType)
    if font is None:
        MessageCenter.ShowErrorMessage(f"Font '{font_name}' not found.", "", False)
        raise Exception(f"Font '{font_name}' not found.")

    # Create the text style
    textStyle = DgnTextStyle(style_name, dgnFile)
    if not textStyle:
        MessageCenter.ShowErrorMessage("Failed to create text style.", "", False)
        raise Exception("Failed to create text style.")

    # Set properties (scale to UORs where required)
    # Use SetFontProperty instead of SetFont
    textStyle.SetFontProperty(TextStyleProperty.eTextStyle_Font, font)
    textStyle.SetDoubleProperty(TextStyleProperty.eTextStyle_Height, height_master * uorPerMast)
    textStyle.SetDoubleProperty(TextStyleProperty.eTextStyle_Width, width_master * uorPerMast)
    textStyle.SetUInt32Property(TextStyleProperty.eTextStyle_Color, color_index)
    textStyle.SetBoolProperty(TextStyleProperty.eTextStyle_ColorFlag, True)
    textStyle.SetDoubleProperty(TextStyleProperty.eTextStyle_InterCharSpacing, 
char_spacing_master) # Add the style to the file result = textStyle.Add(dgnFile) if result == BentleyStatus.eSUCCESS: MessageCenter.ShowInfoMessage(f"Text style '{style_name}' created successfully.", "", False) else: MessageCenter.ShowErrorMessage(f"Failed to add text style '{style_name}'.", "", False) def main(): NewTextStyle () if __name__ == "__main__": main()

 

 

Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs