メンバー オブジェクトの Erase メソッドを使用して、コレクション オブジェクトのメンバーを削除できます。たとえば、次のコードは LayerTable オブジェクトから画層 MyLayer を削除します。
図面から画層を削除する前に、安全に削除できることを確認する必要があります。画層またはブロックや文字スタイルなどの他の名前付きオブジェクトを削除できるかどうかを判定するには、Purge メソッドを使用します。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("RemoveMyLayer")]
public static void RemoveMyLayer()
{
// 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)
{
LayerTableRecord acLyrTblRec;
acLyrTblRec = acTrans.GetObject(acLyrTbl["MyLayer"],
OpenMode.ForWrite) as LayerTableRecord;
try
{
acLyrTblRec.Erase();
acDoc.Editor.WriteMessage("\n'MyLayer' was erased");
// Commit the changes
acTrans.Commit();
}
catch
{
acDoc.Editor.WriteMessage("\n'MyLayer' could not be erased");
}
}
else
{
acDoc.Editor.WriteMessage("\n'MyLayer' does not exist");
}
// Dispose of the transaction
}
}
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("RemoveMyLayer")> _
Public Sub RemoveMyLayer()
'' 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 acLyrTbl.Has("MyLayer") = True Then
Dim acLyrTblRec As LayerTableRecord
acLyrTblRec = acTrans.GetObject(acLyrTbl("MyLayer"), _
OpenMode.ForWrite)
Try
acLyrTblRec.Erase()
acDoc.Editor.WriteMessage(vbLf & "'MyLayer' was erased")
'' Commit the changes
acTrans.Commit()
Catch
acDoc.Editor.WriteMessage(vbLf & "'MyLayer' could not be erased")
End Try
Else
acDoc.Editor.WriteMessage(vbLf & "'MyLayer' does not exist")
End If
'' Dispose of the transaction
End Using
End Sub
Sub RemoveMyLayer()
On Error Resume Next
'' Get the layer "MyLayer" from the Layers collection
Dim ABCLayer As AcadLayer
Set ABCLayer = ThisDrawing.Layers.Item("MyLayer")
'' Check for an error, if no error occurs the layer exists
If Err = 0 Then
'' Delete the layer
ABCLayer.Delete
'' Clear the current error
Err.Clear
'' Get the layer again if it is found the layer could not be removed
Set ABCLayer = ThisDrawing.Layers.Item("MyLayer")
'' Check for error, if an error is encountered the layer was removed
If Err <> 0 Then
ThisDrawing.Utility.prompt "'MyLayer' was removed"
Else
ThisDrawing.Utility.prompt "'MyLayer' could not be removed"
End If
Else
ThisDrawing.Utility.prompt "'MyLayer' does not exist"
End If
End Sub
オブジェクトを削除した後は、それ以降にプログラムでそのオブジェクトに再びアクセスしないようにする必要があります。アクセスすると、エラーが発生します。前のサンプルでは、オブジェクトに再びアクセスする前に、オブジェクトが存在するかどうかをテストしています。オブジェクトの削除を要求するときは、Has メソッドを使用してオブジェクトが存在するかどうかを確認するか、Try ステートメントを使用して発生した例外を検出する必要があります。