03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-9)

 

1.9、Erase Layers删除图层

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图层或当前图层。

Note: Layers 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 Code Reference

Sub EraseLayer()

    On Error Resume Next

 

    Dim layerObj As AcadLayer

    Set layerObj = ThisDrawing.Layers("ABC")

 

    layerObj.Delete

End Sub

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值