Hello,
I found this code sample: http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-DF8A64D3-AE09-4BCE-B9E1-1B642DA4FCFF
However I just want to delete all of the objects on the layer, I dont care what they are I just want the whole layer and everything on it deleted.
Kinda like the LAYDEL express command.
Tony
Solved! Go to Solution.
Solved by _gile. Go to Solution.
I'm tinkering with that, but I cant quite seem to remove the objects from the ObjectIdCollection.
Is that what I'm looking for?
Hi,
This snippet should work.
private string LayerDelete(Database db, string layerName) { if (layerName == "0") return "Layer '0' cannot be deleted."; using (Transaction tr = db.TransactionManager.StartTransaction()) { var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead); if (!layerTable.Has(layerName)) return "Layer '" + layerName + "' not found."; try { var layerId = layerTable[layerName]; if (db.Clayer == layerId) return "Current layer cannot be deleted."; var layer = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForWrite); layer.IsLocked = false; var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); foreach (var btrId in blockTable) { var block = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead); foreach (var entId in block) { var ent = (Entity)tr.GetObject(entId, OpenMode.ForRead); if (ent.Layer == layerName) { ent.UpgradeOpen(); ent.Erase(); } } } layer.Erase(); tr.Commit(); return "Layer '" + layerName + "' have been deleted."; } catch (System.Exception e) { return "Error: " + e.Message; } } }
Thank you.
That is what I needed.
When I converted your code over to .NET you had used var alot, I wasnt quite sure what to do, because the compiler was complaining though I fixed it. (I Think)
Here is my tweaked version. (Changes are in bold.) Also I have Option Explicit On
Tony
DOT NET VERSION
Function LayerDelete(ByVal db As Database, ByVal layerName As String) As String If (layerName = "0") Then Return "Layer '0' cannot be deleted." End If Dim tr As Transaction = db.TransactionManager.StartTransaction Dim layerTable As LayerTable = CType(tr.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable) If Not layerTable.Has(layerName) Then Return ("Layer '" + (layerName + "' not found.")) End If Try Dim layerId As Object = layerTable(layerName) If Application.GetSystemVariable("Clayer") = layerName Then Return "Current layer cannot be deleted." End If Dim layer As LayerTableRecord = CType(tr.GetObject(layerId, OpenMode.ForWrite), LayerTableRecord) layer.IsLocked = False Dim blockTable As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable) For Each btrId As Object In blockTable Dim block As BlockTableRecord = CType(tr.GetObject(btrId, OpenMode.ForRead), BlockTableRecord) For Each entId As Object In block Dim ent As Entity = CType(tr.GetObject(entId, OpenMode.ForRead), Entity) If (ent.Layer = layerName) Then ent.UpgradeOpen() ent.Erase() End If Next Next layer.Erase() tr.Commit() MsgBox("Layer '" + (layerName + "' have been deleted.")) Catch e As System.Exception MsgBox("Error: " + e.Message) End Try End Function
Sorry for the 'var' (type inference) using.
I don't know much about VB but I think the same can be done with option Infer = On (and option Strict = On to insure inference rather than late binding).
To avoid late binding (and unboxing), you'd rather replace the following expressions:
Dim layerId As Object
For Each btrId As Object
and
For Each entId As ObjectId
with strongly typed ones:
Dim layerId As ObjectId
For Each btrId As ObjectId
For Each entId As ObjectId
This code is for VB.NET. you build the DLL and load it into AutoCAD by using 'NETLOAD' command