03-07 创建和编辑AutoCAD实体(七) 向图形中添加文字(3)使用多行文字

3、Use MultilineText (MText)使用多行文字(MText命令)

For long, complex entries, createmultiline text (MText). Multiline text fits a specified width but can extendvertically to an indefinite length. You can format individual words orcharacters within the MText.

对于字数较多的复杂的文字实体,就要创建多行文字对象(MText)。多行文字在指定宽度范围下可以垂直延展无限长。我们可以对MText中的单个字或字符单独进行格式设置。

Topics in this section本小节内容:

·        Create Multiline Text创建多行文字

·        Format Multiline Text格式化多行文字

 

3.1、CreateMultiline Text创建多行文字

You can create a multiline text object byfirst creating an instance of a MText object and then adding it to a blocktable record that represents Model or Paper space. The MText object constructordoes not take any parameters. After an instance of an MText object is created,you can then assign it a text string, insertion point, and width among othervalues using its properties. Other properties that you can change affect theobject’s text height, justification, rotation angle, and text style, or applycharacter formatting to selected characters.

创建多行文字的步骤:首先创建一个MText对象的实例,然后将它添加到代表模型空间或图纸空间的块表记录中。MText对象的构造函数没有参数。创建了MText对象的实例后,我们可以使用属性给这个实例赋予一个文字串、设置插入点、宽度及其他属性值。我们能修改的其他属性影响对象的文字高度、排版、旋转角度、文字样式,或者给选定字符设置格式等。

Create a multiline text object 创建一个多行文字对象

The following example creates an MTextobject in Model space, at the coordinate (2, 2, 0).

下面这个例子在模型空间一个MText对象,起点坐标为(2,2,0)。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("CreateMText")>_

Public SubCreateMText()

  '' Get the current document and database

  Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument

  Dim acCurDb As Database = acDoc.Database

 

  '' Start a transaction

  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

 

      '' Open the Block table for read

      Dim acBlkTbl As BlockTable

      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId, _

                                  OpenMode.ForRead)

 

      '' Open the Block table record Modelspace for write

      Dim acBlkTblRec As BlockTableRecord

      acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _

                                     OpenMode.ForWrite)

 

      '' Create a multiline text object

      Dim acMText As MText = New MText()

      acMText.Location = New Point3d(2, 2, 0)

      acMText.Width = 4

      acMText.Contents = "This is a textstring for the MText object."

 

      acBlkTblRec.AppendEntity(acMText)

      acTrans.AddNewlyCreatedDBObject(acMText,True)

 

      '' Save the changes and dispose of thetransaction

      acTrans.Commit()

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

 

[CommandMethod("CreateMText")]

public static voidCreateMText()

{

  // Get the current document and database获取当前文档及数据库

  Document acDoc =Application.DocumentManager.MdiActiveDocument;

  Database acCurDb = acDoc.Database;

 

  // Start a transaction启动事务

  using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())

  {

      // Open the Block table for read以读打开Block

      BlockTable acBlkTbl;

      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,

                                   OpenMode.ForRead) as BlockTable;

 

      // Open the Block table record Modelspace for write以写打开Block表记录Model空间

      BlockTableRecord acBlkTblRec;

      acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],

                                      OpenMode.ForWrite) asBlockTableRecord;

 

      // Create a multiline text object创建多行文字对象

      MText acMText = new MText();//创建实例

      acMText.Location = new Point3d(2, 2, 0);

      // 属性赋值

      acMText.Width = 4;

      acMText.Contents = "This is a textstring for the MText object.";

 

      acBlkTblRec.AppendEntity(acMText);

      acTrans.AddNewlyCreatedDBObject(acMText,true);

 

      // Save the changes and dispose of thetransaction保存修改并关闭事务

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Sub Ch4_CreateMText()

    Dim mtextObj As AcadMText

    Dim insertPoint(0 To 2) As Double

    Dim width As Double

    Dim textString As String

 

    insertPoint(0) = 2

    insertPoint(1) = 2

    insertPoint(2) = 0

    width = 4

    textString = "This is a text stringfor the mtext object."

 

    ' Create a text Object in model space

    Set mtextObj = ThisDrawing.ModelSpace. _

                       AddMText(insertPoint,width, textString)

    ZoomAll

End Sub

 

3.2、FormatMultiline Text格式化多行文字

Newly created multiline text automaticallyassumes the characteristics of the current text style. The default text styleis STANDARD. You can override the default text style by applying formatting toindividual characters and applying properties to the multiline text object. Youalso can indicate formatting or special characters using the methods describedin this section.

新创建的多行文字会自动呈现为当前文字样式的特性。默认文字样式为STANDARD。我们可以通过格式化个别字符及设置多行文字属性来覆盖默认文字样式。我们还可以使用本节介绍的方法标示格式化字符或特殊字符。

Orientation options such as style, justification,width, and rotation affect all text within the multiline text boundary, notspecific words or characters. Use the Attachment property to change the justification of amultiline text object, and the Rotation property to control the angle of rotation.

像样式、排版、宽度及旋转等方向选项会影响多行文字框内的所有文字,而不是单单影响指定单词或字符。使用Attachment属性来修改多行文字对象的排版,使用Rotation属性来控制旋转角度。

The TextStyleId property sets the font and formattingcharacteristics for a multiline text object. As you create multiline text, youcan select which style you want to use from a list of existing styles. When youchange the style of a multiline text object that has character formattingapplied to any portion of the text, the style is applied to the entire object,and some formatting of characters might not be retained. For instance, changingfrom a TrueType style to a style using an SHX font or to another TrueType fontcauses the multiline text to use the new font for the entire object, and anycharacter formatting is lost.

TextStyleId属性用来设置多行文字对象的字体和格式化特性。创建多行文字时,我们可以从现有样式列表中选择我们想用的样式。如果多行文字的的部分字符已经格式化,这时要修改多行文字的样式的话,新样式将应用于整个多行文字对象,已做的字符格式化将不会保留。例如,将TrueType样式修改成使用SHX字体的样式或另外一种TrueType字体样式时,其结果是新样式应用于整个多行文字对象,任何字符格式化都被丢弃。

Formatting options such as underlining,stacked text, or fonts can be applied to individual words or characters withina paragraph. You also can change color, font, and text height. You can changethe spaces between text characters or increase the width of the characters.

像下划线、上下标文字、字体这些格式化选项可以应用于文字段落内的单个单词或字符。我们还可以修改单个单词或字符颜色、字体、文字高度等。我们可以修改字符与字符的间距或者扩大字符的宽度。

Use curly braces ({ }) to apply a formatchange only to the text within the braces. You can nest braces up to eightlevels deep.

使用花括号({})可以只对括号内的文字进行格式修改。花括号的嵌套深度最多为8层。

You also can enter the ASCII equivalentfor control codes within lines or paragraphs to indicate formatting or specialcharacters, such as tolerance or dimensioning symbols.

我们还可以在行内或段落内键入ASCII值控制码来表示格式或特殊字符,像公差符号、尺寸标注符号等。

The following control characters can beused to create the text in the illustration.

下面的控制字符可以用来创建图示那样的文字。

{{\H1.5x; Big text} \A2; overtext\A1;/\A0; under text}

效果图:


 

For more information about formattingmultiline text, see “Format Characters Within Multiline Text” in the AutoCADUser's Guide.

更多关于格式化多行文字的内容,见AutoCAD用户指南的“格式化多行文字内的字符”。

Use control characters to format text 使用控制字符格式化文字

The following example creates and formatsa multiline text object.

下面这个例子创建并格式化一个多行文字对象。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("FormatMText")>_

Public SubFormatMText()

  '' Get the current document and database

  Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument

  Dim acCurDb As Database = acDoc.Database

 

  '' Start a transaction

  Using acTrans As Transaction =acCurDb.TransactionManager.StartTransaction()

 

      '' Open the Block table for read

      Dim acBlkTbl As BlockTable

      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId, _

                                   OpenMode.ForRead)

 

      '' Open the Block table record Modelspace for write

      Dim acBlkTblRec As BlockTableRecord

      acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _

                                      OpenMode.ForWrite)

 

      '' Create a multiline text object

      Dim acMText As MText = New MText()

      acMText.Location = New Point3d(2, 2, 0)

      acMText.Width = 4.5

      acMText.Contents = "{{\H1.5x; Bigtext}\A2; over text\A1;/\A0;under text}"

 

      acBlkTblRec.AppendEntity(acMText)

      acTrans.AddNewlyCreatedDBObject(acMText,True)

 

      '' Save the changes and dispose of thetransaction

      acTrans.Commit()

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.Geometry;

 

[CommandMethod("FormatMText")]

public static voidFormatMText()

{

  // Get the current document and database获取当前文档和数据库

  Document acDoc = Application.DocumentManager.MdiActiveDocument;

  Database acCurDb = acDoc.Database;

 

  // Start a transaction启动事务

  using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())

  {

      // Open the Block table for read以读打开Block

      BlockTable acBlkTbl;

      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,

                                  OpenMode.ForRead) as BlockTable;

 

      // Open the Block table record Modelspace for write以写打开Block表记录Model空间

      BlockTableRecord acBlkTblRec;

      acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],

                                     OpenMode.ForWrite) as BlockTableRecord;

 

      // Create a multiline text object创建多行文字对象

      MText acMText = new MText();

      acMText.Location = new Point3d(2, 2, 0);

      acMText.Width = 4.5;

      acMText.Contents = "{{\\H1.5x; Bigtext}\\A2; over text\\A1;/\\A0;under text}";

 

      acBlkTblRec.AppendEntity(acMText);

      acTrans.AddNewlyCreatedDBObject(acMText,true);

 

      // Save the changes and dispose of thetransaction保存修改,关闭事务

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Sub FormatMText()

    Dim mtextObj As AcadMText

    Dim insertPoint(0 To 2) As Double

    Dim width As Double

    Dim textString As String

 

    insertPoint(0) = 2

    insertPoint(1) = 2

    insertPoint(2) = 0

    width = 4.5

 

    ' Define the ASCII characters for thecontrol characters

    Dim OB As Long  ' Open Bracket  {

    Dim CB As Long  ' Close Bracket }

    Dim BS As Long  ' Back Slash    \

    Dim FS As Long  ' Forward Slash /

    Dim SC As Long  ' Semicolon     ;

    OB = Asc("{")

    CB = Asc("}")

    BS = Asc("\")

    FS = Asc("/")

    SC = Asc(";")

 

    ' Assign the text string the following lineof control

    ' characters and text characters:

    ' {{\H1.5x; Big text}\A2; over text\A1;/\A0;undertext}

 

    textString = Chr(OB) + Chr(OB) + Chr(BS) +"H1.5x" _

    + Chr(SC) + "Big text" + Chr(CB)+ Chr(BS) + "A2" _

    + Chr(SC) + "over text" + Chr(BS)+ "A1" + Chr(SC) _

    + Chr(FS) + Chr(BS) + "A0" +Chr(SC) + "under text" _

    + Chr(CB)

 

    ' Create a text Object in model space

    Set mtextObj = ThisDrawing.ModelSpace. _

                       AddMText(insertPoint,width, textString)

    ZoomAll

End Sub

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值