练习:创建一个新的命令
 
 

现在,你已经创建了一个工程并添加了必需的库引用,是时候创建命令了。该命令将在模型空间中创建一个新的多行文本(MText)对象。这些和其它概念将在后面的章节中被深入讨论。

定义创建新 多行文本(MText) 对象的新命令。

  1. 在解决方案资源管理器中,根据创建工程的类型双击 Class1.vbClass1.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)
     
              ''创建一个新的 MText 对象并指定一个位置   Creates a new MText object and assigns it a location,
              ''文本值和文字样式 text value and text style
              Dim objText As MText = New MText
     
              ''为 MText 对象设置默认属性   Set the default properties for the MText object
              objText.SetDatabaseDefaults()
     
              '' 指定 MText 对象的插入点  Specify the insertion point of the MText object
              objText.Location = New Autodesk.AutoCAD.Geometry.Point3d(2, 2, 0)
     
              '' 设置 MText 对象要显示的文本字符串   Set the text string for the MText object
              objText.Contents = "Greetings, Welcome to the AutoCAD .NET 开发人员手册"
     
              '' 设置 MText 对象的文字样式   Set the text style for the MText object
              objText.TextStyleId = acCurDb.Textstyle
     
              ''向模型空间添加新的 MText 对象  Appends the new MText object to model space
              acBlkTblRec.AppendEntity(objText)
     
              ''向活动事务添加新的 MText 对象   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()
          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 */
                  MText objText = new MText();
     
                  // Set the default properties for the MText object
                  objText.SetDatabaseDefaults();
     
                  // 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 the 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();
              }
          }
      }
    }