CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

向集合对象添加新成员 (.NET)

2023-1-1 16:38| 发布者: admin| 查看: 707| 评论: 0|来自: AutoCAD

若要向集合添加新成员,请使用该方法。例如,以下代码创建一个新层并将其添加到 Layer 表中:Add

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("AddMyLayer")> _
Public Sub AddMyLayer()
  '' 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()
      '' Returns the layer table for the current database
      Dim acLyrTbl As LayerTable
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                   OpenMode.ForRead)
 
      '' Check to see if MyLayer exists in the Layer table
      If Not acLyrTbl.Has("MyLayer") Then
          '' Open the Layer Table for write
          acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite)
 
          '' Create a new layer table record and name the layer "MyLayer"
          Using acLyrTblRec As LayerTableRecord = New LayerTableRecord
              acLyrTblRec.Name = "MyLayer"
 
              '' Add the new layer table record to the layer table and the transaction
              acLyrTbl.Add(acLyrTblRec)
              acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
          End Using

          '' Commit the changes
          acTrans.Commit()
      End If
 
      '' Dispose of the transaction
  End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("AddMyLayer")]
public static void AddMyLayer()
{
  // Get the current document and database, and start a transaction
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // Returns the layer table for the current database
      LayerTable acLyrTbl;
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                   OpenMode.ForRead) as LayerTable;
 
      // Check to see if MyLayer exists in the Layer table
      if (acLyrTbl.Has("MyLayer") != true)
      {
          // Open the Layer Table for write
          acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite);
 
          // Create a new layer table record and name the layer "MyLayer"
          using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
          {
              acLyrTblRec.Name = "MyLayer";
 
              // Add the new layer table record to the layer table and the transaction
              acLyrTbl.Add(acLyrTblRec);
              acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
          }

          // Commit the changes
          acTrans.Commit();
      }
 
      // Dispose of the transaction
  }
}

VBA/ActiveX 代码参考

Sub AddMyLayer()
    Dim newLayer as AcadLayer
    Set newLayer = ThisDrawing.Layers.Add("MyLayer")
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

UG快捷键鼠标垫

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

GMT+8, 2024-5-7 01:53

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部