演習: 新規コマンドを作成する(.NET)

プロジェクトを作成し、必要なライブラリ参照を追加したので、次にコマンドを作成します。このコマンドは、新しいマルチライン テキスト(MText)オブジェクトをモデル空間に作成します。これらの概念については後の章で詳しく説明します。

新しい MText オブジェクトを作成する新しいコマンドを定義するには

  1. ソリューション エクスプローラで、作成したプロジェクトに応じて Class1.vb または Class1.cs をダブルクリックします。

    次のように、コード ウィンドウに Class1 モジュールが開きます。

    VB.NET

    Public Class Class1
     
    End Class

    C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace MyFirstProject1
    {
      public class Class1
      {
      }
    }
  2. コード ウィンドウでコードを次のように変更します。

    VB.NET

    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.DatabaseServices
         
    Public Class Class1
      <CommandMethod("AdskGreeting")> _
      Public Sub AdskGreeting()
          '' Get the current document and database, and start a transaction
          Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
          Dim acCurDb As Database = acDoc.Database
     
          Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
              '' Open the Block table record 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)
     
              '' Creates a new MText object and assigns it a location,
              '' text value and text style
              Using objText As MText = New MText
     
                  '' Specify the insertion point of the MText object
                  objText.Location = New Autodesk.AutoCAD.Geometry.Point3d(2, 2, 0)
     
                  '' Set the text string for the MText object
                  objText.Contents = "Greetings, Welcome to AutoCAD .NET"
     
                  '' Set the text style for the MText object
                  objText.TextStyleId = acCurDb.Textstyle
     
                  '' Appends the new MText object to model space
                  acBlkTblRec.AppendEntity(objText)
     
                  '' Appends to new MText object to the active transaction
                  acTrans.AddNewlyCreatedDBObject(objText, True)
              End Using
    
              '' Saves the changes to the database and closes the transaction
              acTrans.Commit()
          End Using
      End Sub
    End Class

    C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
     
    [assembly: CommandClass(typeof(MyFirstProject1.Class1))]
     
    namespace MyFirstProject1
    {
      public class Class1
      {
          [CommandMethod("AdskGreeting")]
          public void AdskGreeting()
          {
              // Get the current document and database, and start a transaction
              Document acDoc = Application.DocumentManager.MdiActiveDocument;
              Database acCurDb = acDoc.Database;
     
              // Starts a new transaction with the Transaction Manager
              using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
              {
                  // Open the Block table record 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;
     
                  /* Creates a new MText object and assigns it a location,
                  text value and text style */
                  using (MText objText = new MText())
                  {
     
                      // Specify the insertion point of the MText object
                      objText.Location = new Autodesk.AutoCAD.Geometry.Point3d(2, 2, 0);
     
                      // Set the text string for the MText object
                      objText.Contents = "Greetings, Welcome to AutoCAD .NET";
     
                      // Set the text style for the MText object
                      objText.TextStyleId = acCurDb.Textstyle;
     
                      // Appends the new MText object to model space
                     acBlkTblRec.AppendEntity(objText);
     
                      // Appends to new MText object to the active transaction
                      acTrans.AddNewlyCreatedDBObject(objText, true);
                  }
    
                  // Saves the changes to the database and closes the transaction
                  acTrans.Commit();
              }
          }
      }
    }