画層を削除する(.NET)

作図セッション中は、いつでも画層を削除できます。現在の画層、画層 0、外部参照に従属する画層、オブジェクトを含む画層は削除できません。

画層を削除するには、Erase メソッドを使用します。Purge 関数を使用して画層を削除できることを確認すると共に、画層 0、Defpoints、現在の画層のいずれでもないことを確認することが推奨されます。

注: 画層名が特別の画層名 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 コード リファレンス

Sub EraseLayer()
    On Error Resume Next
 
    Dim layerObj As AcadLayer
    Set layerObj = ThisDrawing.Layers("ABC")
 
    layerObj.Delete
End Sub