You can create a multiline text object by first creating an instance of a MText object and then adding it to a block table record that represents Model or Paper space. The MText object constructor does 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 other values using its properties. Other properties that you can change affect the object’s text height, justification, rotation angle, and text style, or apply character formatting to selected characters
用户可以首先创建一个 MText 对象的实例,然后将它添加到代表模型空间或图纸空间的块表记录中去。MText 对象的构造函数不能带任何参数。MText 对象的实例被创建后,就可以利用它的属性指定它的文本字符串,插入点和宽度以及另外值中的一个。你可以修改另外的属性来影响对象的文字高度、对齐、旋转角度和文字样式,以及应用字符格式到选择的字符。
The following example creates an MText object in Model space, at the coordinate (2, 2, 0).
以下代码在模型空间中的 (2,2,0) 坐标处创建 MText 对象。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("CreateMText")> _
Public Sub CreateMText()
'' 获得当前文档和数据库 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 Model space 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.SetDatabaseDefaults()
acMText.Location = New Point3d(2, 2, 0)
acMText.Width = 4
acMText.Contents = "This is a text string for the MText object."
acBlkTblRec.AppendEntity(acMText)
acTrans.AddNewlyCreatedDBObject(acMText, True)
''保存更改并销毁事务 Save the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("CreateMText")]
public static void CreateMText()
{
// 获得当前文档和数据库 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
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 创建多行文本对象 Create a multiline text object
MText acMText = new MText();
acMText.SetDatabaseDefaults();
acMText.Location = new Point3d(2, 2, 0);
acMText.Width = 4;
acMText.Contents = "This is a text string for the MText object.";
acBlkTblRec.AppendEntity(acMText);
acTrans.AddNewlyCreatedDBObject(acMText, true);
// Save the changes and dispose of the transaction
acTrans.Commit();
}
}