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

Delete Layer

11 REPLIES 11
Reply
Message 1 of 12
fmarcelino
510 Views, 11 Replies

Delete Layer

Hi,

has the topic title says, i'm trying to delete a layer. I achived the following code:

< CommandMethod ("removelayer") > _
Public Shared Function RemoveLayer()
Dim curdb As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim myT As Transaction = curdb.TransactionManager.StartTransaction()
curdb.Clayer = CType(myT.GetObject(curdb.LayerTableId, OpenMode.ForWrite, False), LayerTable).Item("0")
myT.Commit()
myT = curdb.TransactionManager.StartTransaction()
Try
Dim lt As LayerTable = CType(myT.GetObject(curdb.LayerTableId, OpenMode.ForWrite), LayerTable)
For Each objId As ObjectId In lt
Dim layer As LayerTableRecord = CType(myT.GetObject(objId, OpenMode.ForWrite), LayerTableRecord)
If layer.Name = "Layer1" Then
layer.Erase()
Exit For
End If
Next
myT.Commit()
Catch ex As Exception 'Caso ocorra algum erro
myT.Abort()
MessageBox.Show(ex.Message + vbCrLf + vbCrLf + ex.StackTrace, "MyApp", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
myT.Dispose()
End Try
End Function

This works, the layer is removed but all the entities that exists in the layer, like lines, polygons, etc will persist in the document. More, when I run the command "audit" errors are produced indicating that those entities still exist.

How can I remove the layer and all the entities in the layer? It's possible?


Thanks in advance for your attention.
Regards,
Filipe Marcelino
11 REPLIES 11
Message 2 of 12
Mikko
in reply to: fmarcelino

I believe you need to delete the entities on the layer first, then delete the layer
Message 3 of 12
fmarcelino
in reply to: fmarcelino

I already know that. I want to know how to do it.
Message 4 of 12
Mikko
in reply to: fmarcelino

No error handling here. Make sure the layer your deleting is not the active layer.

_
Public Shared Sub DeleteLayer()
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim tm As Transaction = db.TransactionManager.StartTransaction()
Try
Dim res As PromptSelectionResult = ed.SelectAll
If res.Status PromptStatus.OK Then
Exit Sub
End If
Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
Dim tempIdArray() As ObjectId
tempIdArray = SS.GetObjectIds()
Dim tempId As ObjectId
For Each tempId In tempIdArray
Dim Ent As Entity = CType(tm.GetObject(tempId, OpenMode.ForWrite), Entity)
If Ent.Layer = "Layer1" Then
Ent.Erase()
End If
Next
Dim lt As LayerTable = CType(tm.GetObject(db.LayerTableId, OpenMode.ForWrite), LayerTable)
For Each objId As ObjectId In lt
Dim layer As LayerTableRecord = CType(tm.GetObject(objId, OpenMode.ForWrite), LayerTableRecord)
If layer.Name = "Layer1" Then
layer.Erase()
Exit For
End If
Next
tm.Commit()
Catch
Finally
tm.Dispose()
End Try
End Sub
Message 5 of 12
RonnieWilkins
in reply to: fmarcelino

Instead of selecting the entire drawing and then iterating over the objects I'd recommend using a selection filter to only select the items on the layer(s) that you wish to delete.

Private Sub EraseObjectsFromLayer()

Dim DB As AcDb.Database = AcDb.HostApplicationServices.WorkingDatabase
Dim ED As AcEd.Editor = AcadApp.DocumentManager.MdiActiveDocument.Editor
Dim myT As AcDb.Transaction = DB.TransactionManager.StartTransaction
Try
Dim Values() As AcDb.TypedValue = {New AcDb.TypedValue(AcDb.DxfCode.LayerName, "Centerlines")}
Dim SF As New AcEd.SelectionFilter(Values)
Dim Result As AcEd.PromptSelectionResult = ED.SelectAll(SF)

If Result.Status <> AcEd.PromptStatus.OK Then Return
Dim IDArray As AcDb.ObjectId() = Result.Value.GetObjectIds


For Each ID As AcDb.ObjectId In IDArray
Dim Entity As AcDb.Entity = myT.GetObject(ID, AcDb.OpenMode.ForWrite)
Entity.Erase()
Entity.Dispose()
Next

myT.Commit()
Catch ex As Exception
AcadApp.ShowAlertDialog(ex.ToString)
Finally
myT.Dispose()
DB.Dispose()
End Try
End Sub
Ronnie Wilkins, Jr.
Message 6 of 12
fmarcelino
in reply to: fmarcelino

Thanks rwilkins,

it works great. Thank you very much.


Regards,
Filipe Marcelino
Message 7 of 12
Anonymous
in reply to: fmarcelino

Why use a selection set at all? What about entities in block definitions?
--
Bobby C. Jones
http://www.acadx.com

wrote in message news:5099830@discussion.autodesk.com...
Thanks rwilkins,

it works great. Thank you very much.


Regards,
Filipe Marcelino
Message 8 of 12
fmarcelino
in reply to: fmarcelino

could explain that better, Please?
Message 9 of 12
RonnieWilkins
in reply to: fmarcelino

As would I.

Never mind...I see what your saying Bobby.

If any entities within blocks are using the layer you must delete or move them to another layer before you can delete the layer.

If you know that no entities within blocks other than modelspace or paperspace use the layer then the selection set will save you a chunk of time.

I would still recommend the selection set method for deleting items items from model or paper space and then iterate over all remaining blocks.

If you have 10,000 items in model space (not unheard of) iterating over each item would take some time to accomplish. Message was edited by: rwilkins
Ronnie Wilkins, Jr.
Message 10 of 12
Anonymous
in reply to: fmarcelino

Create a block that contains an entity on a layer that you want to delete.
Then run your EraseObjectsFromLayer() procedure.
--
Bobby C. Jones
http://www.acadx.com

wrote in message news:5100257@discussion.autodesk.com...
As would I.
Message 11 of 12
RonnieWilkins
in reply to: fmarcelino

I stuck my foot in mouth. Apologies.
Ronnie Wilkins, Jr.
Message 12 of 12
Anonymous
in reply to: fmarcelino

No apologies needed. I've got a shoestring permanently jammed between a
couple of my wisdom teeth that I've been trying to remove for years 🙂
--
Bobby C. Jones
http://www.acadx.com

wrote in message news:5100296@discussion.autodesk.com...
I stuck my foot in mouth. Apologies.

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