CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

练习:创建新命令 (.NET)

2023-1-1 17:08| 发布者: admin| 查看: 833| 评论: 0|来自: AutoCAD

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

定义创建新 MText 对象的新命令

  1. 在“解决方案资源管理器”中,根据您创建的项目类型双击“类 1.vb”或“类 1.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();
              }
          }
      }
    }

路过

雷人

握手

鲜花

鸡蛋

最新评论

 VBA & VB.NET开发基础与实例教程

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-6 20:38

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部