Delete all objects on a layer in VBA

Delete all objects on a layer in VBA

Anonymous
Not applicable
7,900 Views
6 Replies
Message 1 of 7

Delete all objects on a layer in VBA

Anonymous
Not applicable
HI All,

I've seen the example code in the Developer help.

The example is as follows:

for each obj in ThisDrawing.Groups
obj.Delete
next obj

How can I apply this to all objects on a layer AND how should I declare obj

DIm obj as ??
0 Likes
7,901 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
if obj.layer = "your layer name" then
obj.delete

you can use AcadObject or AcadEntity - not sure which is best

"Mike Rand" wrote in message
news:2ADA336FBC8C45DEC28F45752A59D3BA@in.WebX.maYIadrTaRb...
> HI All,
>
> I've seen the example code in the Developer help.
>
> The example is as follows:
>
> for each obj in ThisDrawing.Groups
> obj.Delete
> next obj
>
> How can I apply this to all objects on a layer AND how should I declare
obj
>
> DIm obj as ??
>
>
>
0 Likes
Message 3 of 7

Anonymous
Not applicable
Mike,

Here is how you would do that.

Joe
--

Public Const LAYER = 8 'by layer

Public Function CreateSelectionSet(SelectionSet As AcadSelectionSet, _
FilterCode As Integer, _
FilterValue As String) As Boolean
Dim iFilterCode(0) As Integer
Dim vFilterValue(0) As Variant

iFilterCode(0) = FilterCode
vFilterValue(0) = FilterValue

SelectionSet.Select acSelectionSetAll, , , iFilterCode, vFilterValue
'SelectionSet.SelectOnScreen intFilterCode, varFilterValue

If SelectionSet.Count Then
CreateSelectionSet = True
End If
End Function

Public Sub DeleteEntitiesOnLayer(LayerName As String)
Dim objSS As AcadSelectionSet
Dim oEntity As AcadEntity
On Error Resume Next
'delete any previous selection set
ThisDrawing.SelectionSets("Entities").Delete
'create our new selection set
Set objSS = ThisDrawing.SelectionSets.Add("Entities")
'get the actual entities for the selection set
CreateSelectionSet objSS, LAYER, LayerName
'iterate through selection set delete each entity
For Each oEntity In objSS
oEntity.Delete
Next oEntity
End Sub

Public Sub Test()
DeleteEntitiesOnLayer "... Your Layer Name Goes Here ..."
End Sub
0 Likes
Message 4 of 7

Anonymous
Not applicable
Hey Joe

Why you have taken this:
Public Const LAYER = 8 'by layer

what is 8??

Sachin
0 Likes
Message 5 of 7

Anonymous
Not applicable
Sachin,
This might get you going

Public Sub eraselayer()
Dim ent As AcadEntity
For Each ent In ThisDrawing.ModelSpace
If ent.Layer = "1ST-PBASE" Then ' put layer name to delete here...
ent.Delete
End If
Next
End Sub


john coon


wrote in message news:5143966@discussion.autodesk.com...
Hey Joe

Why you have taken this:
Public Const LAYER = 8 'by layer

what is 8??

Sachin
0 Likes
Message 6 of 7

Anonymous
Not applicable
Why not use a selectionset and then call the erase method on it?

"John Coon" wrote in message
news:5144339@discussion.autodesk.com...
Sachin,
This might get you going

Public Sub eraselayer()
Dim ent As AcadEntity
For Each ent In ThisDrawing.ModelSpace
If ent.Layer = "1ST-PBASE" Then ' put layer name to delete here...
ent.Delete
End If
Next
End Sub


john coon


wrote in message news:5143966@discussion.autodesk.com...
Hey Joe

Why you have taken this:
Public Const LAYER = 8 'by layer

what is 8??

Sachin
0 Likes
Message 7 of 7

Anonymous
Not applicable
Ent works fast & easy. if I'm writing sometime where I'm going to access a
huge drawing I'll use a selectionset or if it's a routine I plan to add to
my companies routines I would add the selectionset but if it's sometime fast
down and dirty I'll use ent
John Coon

" Nemorarius" wrote in message news:5144326@discussion.autodesk.com...
Why not use a selectionset and then call the erase method on it?

"John Coon" wrote in message
news:5144339@discussion.autodesk.com...
Sachin,
This might get you going

Public Sub eraselayer()
Dim ent As AcadEntity
For Each ent In ThisDrawing.ModelSpace
If ent.Layer = "1ST-PBASE" Then ' put layer name to delete here...
ent.Delete
End If
Next
End Sub


john coon


wrote in message news:5143966@discussion.autodesk.com...
Hey Joe

Why you have taken this:
Public Const LAYER = 8 'by layer

what is 8??

Sachin
0 Likes