1 行文字を左右および上下に位置合わせすることができます。既定では、左寄せに設定されています。左右および上下の位置合わせオプションを設定するには、HorizontalMode および VerticalMode プロパティを使用します。
通常、文字オブジェクトを閉じると、文字オブジェクトの位置と位置合わせ点は、その位置合わせと文字スタイルに従って調整されます。しかし、メモリ内の文字オブジェクトの位置合わせは自動的に更新されません。現在のプロパティ値に基づいて文字オブジェクトの位置合わせを更新するには、AdjustAlignment メソッドを呼び出します。
次の例では、1 行文字(DBText)オブジェクトと点(DBPoint)オブジェクトを作成します。点オブジェクトを文字の位置合わせ点に設定し、見やすくなるよう赤の十字形に変更します。文字の位置合わせを変更し、マクロの実行が中止されるようにメッセージ ボックスを表示します。このようにすることで、文字の位置合わせの変更による影響を確認することができます。
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
using (DBText acText = new DBText())
{
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
using (DBPoint acPoint = new DBPoint(acPtAlign))
{
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();
}
}