Jump to content

Recommended Posts

Posted

Hi,

I'm tring to make a VBA code to clear a layers content.

So far i the code below but it is deleting everything from all layers..

Anyone got a sugestion?

Cheers Jeroen

 

Sub ClearLayer()
Dim SSet As AcadSelectionSet
Dim test2 As String

On Error Resume Next 

Set SSet = ThisDrawing.SelectionSets.Add("test2")
SSet.Select acSelectionSetAll

SSet.Erase
SSet.Delete

End Sub

Posted

What do you mean deleting everything from all layers?

 

Do you mean erasing the drawing or resetting the properties?

 

If it is erasing the drawing, then try the "New" command. If it is resetting the properties, then just change the properties to what you want.

 

Could you be any clearer?

 

Thanks.

Posted

The code not only deletes all lines and text from the layer "test2" but also from all othere layers. I'm want the code only to delete al lines and text from the layer "test2".

Posted

A selection set has to be set up to only accept entities on a particular layer.

 

Sub ClearLayerWithFilter()
Dim SSet As AcadSelectionSet
Dim intCode(1) As Integer
Dim varData(1) As Variant

intCode(0) = 8: varData(0) = "test2" 'only select items on layer "test2"
intCode(1) = 67: varData(1) = 0 'only select items in modelspace - error without this filter

On Error Resume Next
ThisDrawing.SelectionSets.Item("test2").Delete
On Error GoTo 0
Set SSet = ThisDrawing.SelectionSets.Add("test2")

SSet.Select acSelectionSetAll, , , intCode, varData
SSet.Erase


End Sub

 

Is the intent to collect all entities of a particular layer in both model- and paperspace? If so, then a different tack may need to be employed. With selection sets, an error is thrown when dealing with both model and paperspace. The second routine does its own iteration to eliminate entities in the entire drawing.

 

Sub ClearLayerWithIteration()
Dim objDataBase As AcadDatabase
Dim objBlock As AcadBlock
Dim ent As AcadEntity
Dim count As Integer
Dim i As Integer
Dim entCollection As Collection
Dim varHandle As Variant

Set entCollection = New Collection

For Each objBlock In ThisDrawing.Blocks
  count = objBlock.count
  For i = 0 To count - 1
     If TypeOf objBlock.Item(i) Is AcadEntity Then
        If objBlock.Item(i).Layer = "test2" Then entCollection.Add (objBlock.Item(i).Handle)
     End If
  Next
  On Error Resume Next
  For Each varHandle In entCollection   
     Set ent = ThisDrawing.HandleToObject(CStr(varHandle))
     ent.Delete
  Next
  On Error GoTo 0

Next
End Sub

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...