删除图层
 
 

You can erase a layer at any time during a drawing session. You cannot erase the current layer, layer 0, an xref-dependent layer, or a layer that contains objects.

在图形任务期间,随时可以删除图层。但不能删除当前图层、图层 0、依赖于外部参照的图层和包含对象的图层。

To erase a layer, use the Erase method. It is recommended to use the Purge function to verify that the layer can be purged, along with verifying that it is not layer 0, Defpoints, or the current layer.

若要删除一个图层,请使用 Erase 方法。推荐使用 Purge 函数检验图层是否可以被清除,它会一起检验它不是图层 0、Defpoints 或当前图层。

NoteLayers referenced by block definitions, along with the special layer named DEFPOINTS, cannot be deleted even if they do not contain visible objects.

注意对于被块定义参照的图层以及特殊的图层 DEFPOINTS,即使其中没有包含可见对象也不能被删除。 

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("EraseLayer")> _
Public Sub EraseLayer()
  '' 获得当前文档和数据库   Get the current document and database
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  ''启动一个事务   Start a transaction
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
 
      '' 以只读方式打开图层表   Open the Layer table for read
      Dim acLyrTbl As LayerTable
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                   OpenMode.ForRead)
 
      Dim sLayerName As String = "ABC"
 
      If acLyrTbl.Has(sLayerName) = True Then
          ''检查图层是否可以被安全的删除   Check to see if it is safe to erase layer
          Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
          acObjIdColl.Add(acLyrTbl(sLayerName))
          acCurDb.Purge(acObjIdColl)
 
          If acObjIdColl.Count > 0 Then
              Dim acLyrTblRec As LayerTableRecord
              acLyrTblRec = acTrans.GetObject(acObjIdColl(0), OpenMode.ForWrite)
 
              Try
                  '' 删除未引用的图层     Erase the unreferenced layer
                  acLyrTblRec.Erase(True)
 
                  ''保存更改并销毁事务   Save the changes and dispose of the transaction
                  acTrans.Commit()
              Catch Ex As Autodesk.AutoCAD.Runtime.Exception
                  '' 图层不能被删除    Layer could not be deleted
                  Application.ShowAlertDialog("Error:\n" + Ex.Message)
              End Try
          End If
      End If
  End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("EraseLayer")]
public static void EraseLayer()
{
  // 获得当前文档和数据库   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 Layer table for read
      LayerTable acLyrTbl;
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                   OpenMode.ForRead) as LayerTable;
 
      string sLayerName = "ABC";
 
      if (acLyrTbl.Has(sLayerName) == true)
      {
          // Check to see if it is safe to erase layer
          ObjectIdCollection acObjIdColl = new ObjectIdCollection();
          acObjIdColl.Add(acLyrTbl[sLayerName]);
          acCurDb.Purge(acObjIdColl);
 
          if (acObjIdColl.Count > 0)
          {
              LayerTableRecord acLyrTblRec;
              acLyrTblRec = acTrans.GetObject(acObjIdColl[0],
                                              OpenMode.ForWrite) as LayerTableRecord;
 
              try
              {
                  // 删除未引用的图层     Erase the unreferenced layer
                  acLyrTblRec.Erase(true);
 
                  // Save the changes and dispose of the transaction
                  acTrans.Commit();
              }
              catch (Autodesk.AutoCAD.Runtime.Exception Ex)
              {
                  // 图层不能被删除    Layer could not be deleted
                  Application.ShowAlertDialog("Error:\n" + Ex.Message);
              }
          }
      }
  }
}
VBA/ActiveX 代码参考