.NET

Reply
Active Contributor
tony.tomison.iii
Posts: 32
Registered: ‎05-12-2014
Message 1 of 7 (168 Views)
Accepted Solution

Delete Layer with Objects

168 Views, 6 Replies
07-22-2014 05:53 AM

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

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

 

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 2 of 7 (149 Views)

Re: Delete Layer with Objects

07-22-2014 07:51 AM in reply to: tony.tomison.iii
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...
Active Contributor
tony.tomison.iii
Posts: 32
Registered: ‎05-12-2014
Message 3 of 7 (143 Views)

Re: Delete Layer with Objects

07-22-2014 08:31 AM 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?

*Expert Elite*
_gile
Posts: 2,105
Registered: ‎04-29-2006
Message 4 of 7 (131 Views)

Re: Delete Layer with Objects

07-22-2014 10:38 AM in reply to: tony.tomison.iii

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
Active Contributor
tony.tomison.iii
Posts: 32
Registered: ‎05-12-2014
Message 5 of 7 (120 Views)

Re: Delete Layer with Objects

07-22-2014 12:02 PM 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

 

*Expert Elite*
_gile
Posts: 2,105
Registered: ‎04-29-2006
Message 6 of 7 (108 Views)

Re: Delete Layer with Objects

07-22-2014 01:19 PM in reply to: tony.tomison.iii

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
Active Contributor
tony.tomison.iii
Posts: 32
Registered: ‎05-12-2014
Message 7 of 7 (74 Views)

Re: Delete Layer with Objects

07-23-2014 07:58 AM in reply to: _gile

Thank you for clarifying this, it helped.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.