.NET

Reply
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 1 of 12 (189 Views)

Delete Layer

189 Views, 11 Replies
03-01-2006 08:34 AM
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
Distinguished Contributor
Mikko
Posts: 212
Registered: ‎10-03-2003
Message 2 of 12 (189 Views)

Re: Delete Layer

03-01-2006 09:35 AM in reply to: fmarcelino
I believe you need to delete the entities on the layer first, then delete the layer
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 3 of 12 (189 Views)

Re: Delete Layer

03-01-2006 09:37 AM in reply to: fmarcelino
I already know that. I want to know how to do it.
Distinguished Contributor
Mikko
Posts: 212
Registered: ‎10-03-2003
Message 4 of 12 (189 Views)

Re: Delete Layer

03-01-2006 10:15 AM 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
Distinguished Contributor
RonnieWilkins
Posts: 454
Registered: ‎04-07-2003
Message 5 of 12 (189 Views)

Re: Delete Layer

03-01-2006 07:47 PM 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.
AMS CAD + CAFM Solutions
Sr. Solutions Developer
Autodesk Certified Instructor: Inventor
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 6 of 12 (189 Views)

Re: Delete Layer

03-02-2006 04:36 AM in reply to: fmarcelino
Thanks rwilkins,

it works great. Thank you very much.


Regards,
Filipe Marcelino
*Bobby C. Jones
Message 7 of 12 (189 Views)

Re: Delete Layer

03-02-2006 09:48 AM 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
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 8 of 12 (189 Views)

Re: Delete Layer

03-02-2006 09:50 AM in reply to: fmarcelino
could explain that better, Please?
Distinguished Contributor
RonnieWilkins
Posts: 454
Registered: ‎04-07-2003
Message 9 of 12 (189 Views)

Re: Delete Layer

03-02-2006 09:51 AM 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.
AMS CAD + CAFM Solutions
Sr. Solutions Developer
Autodesk Certified Instructor: Inventor
*Bobby C. Jones
Message 10 of 12 (189 Views)

Re: Delete Layer

03-02-2006 09:58 AM 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.
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.