.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Delete Layer with Objects

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
TTIII
4610 Views, 8 Replies

Delete Layer with Objects

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

8 REPLIES 8
Message 2 of 9
mzakiralam
in reply to: TTIII

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...
Message 3 of 9
TTIII
in reply to: mzakiralam

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

Is that what I'm looking for?

Message 4 of 9
_gile
in reply to: TTIII

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
in reply to: _gile

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

 

Message 6 of 9
_gile
in reply to: TTIII

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
in reply to: _gile

Thank you for clarifying this, it helped.

Message 8 of 9
Haider_of_Sweden
in reply to: TTIII

How do you run this code?

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

Message 9 of 9

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost