Each individual line of text is a distinct object when using single-line text. To create a single-line text object, you create an instance of a DBText object and then add it to either the block table record that represents Model or Paper space. When you create a new instance of a DBText object, you do not pass the constructor any parameters.
使用单行文字时,每个单行文字都是一个不同的对象。要创建单行文字对象,请创建一个 DBText 对象的实例,然后把它添加到代表模型或图纸空间的块表记录中的一个中。当你创建一个新的 DBText 对象的实例时,不要为构造函数传递任何参数。
The following example creates a single-line text object in Model space at the coordinate (2, 2, 0) with a height of 0.5 and the text string "Hello, World.".
本例在模型空间中的 (2,2,0) 坐标处用0.5的高和文字字符串"Hello, World."创建一个单选文字对象。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("CreateText")> _
Public Sub CreateText()
'' 获得当前文档和数据库 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 single-line text object
Dim acText As DBText = New DBText()
acText.SetDatabaseDefaults()
acText.Position = New Point3d(2, 2, 0)
acText.Height = 0.5
acText.TextString = "Hello, World."
acBlkTblRec.AppendEntity(acText)
acTrans.AddNewlyCreatedDBObject(acText, 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("CreateText")]
public static void CreateText()
{
// 获得当前文档和数据库 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 single-line text object
DBText acText = new DBText();
acText.SetDatabaseDefaults();
acText.Position = new Point3d(2, 2, 0);
acText.Height = 0.5;
acText.TextString = "Hello, World.";
acBlkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
// Save the changes and dispose of the transaction
acTrans.Commit();
}
}