在许多工程设计和图纸制作中,文字元素扮演着至关重要的角色。特别是带有上下标的文字,在展示特定信息(如化学式、数学公式)时尤为重要。本文将指导读者如何在MicroStation软件中,通过编程创建带有上下标的文字元素。
文本样式决定了文字元素的样子,Mstn中我们在如下所示的对话框中订制文本样式:
可以看到文字样式包含了很多设置,在Mstn的.Net编程框架中有如下所示的枚举类型:
namespace Bentley.DgnPlatformNET { public enum TextStyleProperty { InvalidProperty = 0, Color = 1, ColorFlag = 2, Font = 3, ShxBigFont = 4, Width = 5, Height = 6, Slant = 7, Underline = 8, Overline = 9, Italics = 10, Bold = 11, Superscript = 12, Subscript = 13, Backgroundstyleflag = 14, Backgroundstyle = 15, Backgroundweight = 16, Backgroundcolor = 17, Backgroundfillcolor = 18, Backgroundborder = 19, Underlinestyle = 20, Underlineweight = 21, Underlinecolor = 22, Underlinestyleflag = 23, UnderlineOffset = 24, Overlinestyle = 25, Overlineweight = 26, Overlinecolor = 27, Overlinestyleflag = 28, OverlineOffset = 29, LineSpacing = 30, FixedSpacing = 31, LineOffset = 32, InterCharSpacing = 33, DgnLineSpacingType = 34, AcadInterCharSpacing = 35, Justification = 36, FullJustification = 38, LineLength = 39, Backwards = 41, Upsidedown = 42, Fractions = 43, Vertical = 45, RightToLeft = 47 } }
Mstn SDK中用类型DgnTextStyle来表示文字样式,在这个类型下有多个名为SetProperty的重载函数,我们调用这些函数可以修改文本样式的设置。 这些函数的第一个参数都是TextStyleProperty类型,通过这个参数可以指定要修改哪个设置。我们将Superscript和Subscript这两个属性设置为True
就可以创建出上下标文字来。如下代码完整演示了如何创建带有上标的文字元素:
DgnModel dgnModel = Session.Instance.GetActiveDgnModel(); DgnFile dgnFile = Session.Instance.GetActiveDgnFile(); DgnTextStyle txtStyle = DgnTextStyle.GetSettings(dgnFile); TextBlock txtBlock = new TextBlock(txtStyle, dgnModel); txtBlock.AppendText("This is a demo Text"); txtStyle.SetProperty(TextStyleProperty.Superscript, true); RunProperties runProp = new RunProperties(txtStyle, dgnModel); txtBlock.SetRunPropertiesForAdd(runProp); txtBlock.AppendText("Superscript"); TextHandlerBase txtHandler = TextHandlerBase.CreateElement(null, txtBlock); txtHandler.AddToModel();