You can justify single-line text horizontally and vertically. Left alignment is the default. To set the horizontal and vertical alignment options, use the HorizontalMode and VerticalMode properties.
可以在水平和垂直方向对齐单行文字。左对齐是默认选项。要设置水平和垂直对齐选项,请使用 HorizontalMode 和 VerticalMode 属性。
Normally when a text object is closed, the position and alignment points of the text object are adjusted according to its justification and text style. However, the alignment of an in memory text object will not automatically be updated. Call the AdjustAlignment method to update the alignment of the text object based on its current property values.
通常当文字对象被关闭时,文字对象的定位和对齐点会被调整,以适用它的对齐和文字样式。然而,在内存中的文字对象的对齐将不会被自动更新。若要基于文字对象的当前属性值更新它的对齐特性可以调用它的 AdjustAlignment 方法。
The following example creates a single-line text (DBText) object and a point (DBPoint) object. The point object is set to the text alignment point, and is changed to a red crosshair so that it is visible. The text alignment is changed and a message box is displayed so that the macro execution is halted. This allows you to see the impact of changing the text alignment.
本例创建一个单行文本(DBText)对象和一个点(DBPoint)对象。点对象将设置到文字的对齐点,然后将点样式修改成红色的十字光标形状以使点可见。文字对齐将被修改,并显示消息框以终止宏的执行。这样用户就可以查看修改文字对齐的影响。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("TextAlignment")> _
Public Sub TextAlignment()
'' 获得当前文档和数据库 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)
Dim textString(0 To 2) As String
textString(0) = "Left"
textString(1) = "Center"
textString(2) = "Right"
Dim textAlign(0 To 2) As Integer
textAlign(0) = TextHorizontalMode.TextLeft
textAlign(1) = TextHorizontalMode.TextCenter
textAlign(2) = TextHorizontalMode.TextRight
Dim acPtIns As Point3d = New Point3d(3, 3, 0)
Dim acPtAlign As Point3d = New Point3d(3, 3, 0)
Dim nCnt As Integer = 0
For Each strVal As String In textString
''创建单行文本对象 Create a single-line text object
Dim acText As DBText = New DBText()
acText.SetDatabaseDefaults()
acText.Position = acPtIns
acText.Height = 0.5
acText.TextString = strVal
'' 设置文字的对齐方式 Set the alignment for the text
acText.HorizontalMode = textAlign(nCnt)
If acText.HorizontalMode <> TextHorizontalMode.TextLeft Then
acText.AlignmentPoint = acPtAlign
End If
acBlkTblRec.AppendEntity(acText)
acTrans.AddNewlyCreatedDBObject(acText, True)
'' 创建一个经过文字对齐点的点对象 Create a point over the alignment point of the text
Dim acPoint As DBPoint = New DBPoint(acPtAlign)
acPoint.SetDatabaseDefaults()
acPoint.ColorIndex = 1
acBlkTblRec.AppendEntity(acPoint)
acTrans.AddNewlyCreatedDBObject(acPoint, True)
'' 调整插入和对齐点 Adjust the insertion and alignment points
acPtIns = New Point3d(acPtIns.X, acPtIns.Y + 3, 0)
acPtAlign = acPtIns
nCnt = nCnt + 1
Next
'' 设置点样式 Set the point style to crosshair
Application.SetSystemVariable("PDMODE", 2)
''保存更改并销毁事务 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("TextAlignment")]
public static void TextAlignment()
{
// 获得当前文档和数据库 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;
string[] textString = new string[3];
textString[0] = "Left";
textString[1] = "Center";
textString[2] = "Right";
int[] textAlign = new int[3];
textAlign[0] = (int)TextHorizontalMode.TextLeft;
textAlign[1] = (int)TextHorizontalMode.TextCenter;
textAlign[2] = (int)TextHorizontalMode.TextRight;
Point3d acPtIns = new Point3d(3, 3, 0);
Point3d acPtAlign = new Point3d(3, 3, 0);
int nCnt = 0;
foreach (string strVal in textString)
{
//创建单行文本对象 Create a single-line text object
DBText acText = new DBText();
acText.SetDatabaseDefaults();
acText.Position = acPtIns;
acText.Height = 0.5;
acText.TextString = strVal;
// 设置文字的对齐方式 Set the alignment for the text
acText.HorizontalMode = (TextHorizontalMode)textAlign[nCnt];
if (acText.HorizontalMode != TextHorizontalMode.TextLeft)
{
acText.AlignmentPoint = acPtAlign;
}
acBlkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
// 创建一个经过文字对齐点的点对象 Create a point over the alignment point of the text
DBPoint acPoint = new DBPoint(acPtAlign);
acPoint.SetDatabaseDefaults();
acPoint.ColorIndex = 1;
acBlkTblRec.AppendEntity(acPoint);
acTrans.AddNewlyCreatedDBObject(acPoint, true);
// 调整插入和对齐点 Adjust the insertion and alignment points
acPtIns = new Point3d(acPtIns.X, acPtIns.Y + 3, 0);
acPtAlign = acPtIns;
nCnt = nCnt + 1;
}
// 设置点样式 Set the point style to crosshair
Application.SetSystemVariable("PDMODE", 2);
// Save the changes and dispose of the transaction
acTrans.Commit();
}
}