マルチ テキストを作成する(.NET)

マルチ テキスト オブジェクトを作成するには、まず MText オブジェクトのインスタンスを作成し、そのインスタンスをモデル空間またはペーパー空間を表すブロック テーブル レコードに追加します。MText オブジェクト コンストラクタはパラメータを使用しません。MText オブジェクトのインスタンスが作成されたら、プロパティを使用して、そのインスタンスに文字列、挿入点、および幅などの値を割り当てることができます。変更できるその他のプロパティの中には、オブジェクトの文字の高さ、位置合わせ、回転角度、および文字スタイルに影響を及ぼしたり、選択した文字に文字の書式設定を適用したりするものがあります。

マルチ テキスト オブジェクトを作成する

次のコード例では、MText オブジェクトをモデル空間の座標(2, 2, 0)の位置に作成します。

C# の例

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
        using (MText acMText = new MText())
        {
            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();
    }
}