Delete Layer with Objects

Delete Layer with Objects

TTIII
Enthusiast Enthusiast
5,369 Views
8 Replies
Message 1 of 9

Delete Layer with Objects

TTIII
Enthusiast
Enthusiast

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

0 Likes
Accepted solutions (1)
5,370 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
you can have a look in below link. hopefully it will serve your purpose

http://exchange.autodesk.com/autocad/enu/online-help/browse#WS1a9193826455f5ff2566ffd511ff6f8c7ca-3c...
0 Likes
Message 3 of 9

TTIII
Enthusiast
Enthusiast

I'm tinkering with that, but I cant quite seem to remove the objects from the ObjectIdCollection.

Is that what I'm looking for?

0 Likes
Message 4 of 9

_gile
Consultant
Consultant
Accepted solution

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;
                }
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 9

TTIII
Enthusiast
Enthusiast

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

 

0 Likes
Message 6 of 9

_gile
Consultant
Consultant

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

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 9

TTIII
Enthusiast
Enthusiast

Thank you for clarifying this, it helped.

0 Likes
Message 8 of 9

Haider_of_Sweden
Collaborator
Collaborator

How do you run this code?

It's not LSP, so what extension should I use?

0 Likes
Message 9 of 9

Anonymous
Not applicable

This code is for VB.NET. you build the DLL and load it into AutoCAD by using 'NETLOAD'  command

0 Likes