PDA

View Full Version : Routine loops itself in AutoCAD 2005



bim3d
2007-02-13, 04:39 PM
I'm using the following LISP code to define a command


(defun c:fix ()
(vl-vbarun "DeleteLayers")
)

It calls the following VBA routine:



Sub DeleteLayers()

Dim strLayers, strLayers1 As String
Dim objLayer As AcadLayer

' Initialize string.
strLayers = ""
strLayers1 = ""

' Step through all layers in the drawing.
For Each objLayer In ThisDrawing.Layers
If ((objLayer.Freeze) = True) Or (objLayer.LayerOn) = False Then
strLayers = objLayer.Name

strLayers1 = strLayers1 & objLayer.Name & vbCrLf

ThisDrawing.SendCommand "-laydel" & vbCr & "NAME" & vbCr & strLayers & vbCr & vbCr & "YES" & vbCr & vbCr
End If
Next

' Display the deleted layers to the user.
MsgBox "Layers deleted in current drawing: " & vbCrLf & vbCrLf & _
strLayers1

End Sub


This was running fine in AutoCAD 2007. But in AutoCAD 2005, the VBA routine keeps looping itself over and over again and won't return the command prompt. Any help or insights would be greatly appreciated.

Ed Jobe
2007-02-13, 06:45 PM
Whenever you use SendCommand you have to make sure that you use the right syntax. Did the command syntax change from 2005 to 2007? Also, I don't recommend using SendCommand in the first place, especially since you already have a pointer to the layer object. Just use objLayer.Delete.

bim3d
2007-02-13, 08:39 PM
Whenever you use SendCommand you have to make sure that you use the right syntax. Did the command syntax change from 2005 to 2007?

I fixed the command syntax in AutoCAD 2005, but still having the same problem. What's puzzling is, the VBA routine runs fine in the IDE and only having trouble when called by the LISP command.


Also, I don't recommend using SendCommand in the first place, especially since you already have a pointer to the layer object. Just use objLayer.Delete.

I'm using the Express "LAYDEL" command because "objLayer.Delete" would not work, it always complains being "referenced".

I basically need to build a routine to delete seleted LAYERS with entities on them. Any hints or directions are helpful.

Thanks.

Ed Jobe
2007-02-13, 08:46 PM
For each layer you want to delete, create a filtered selection set and delete all the objects in the ss. Does laydel work if a block references the layer?

bim3d
2007-02-14, 04:43 PM
For each layer you want to delete, create a filtered selection set and delete all the objects in the ss. Does laydel work if a block references the layer?

Thanks again, Ed. "LAYDEL" does work with block-referenced layers. For now, I just have to run my routine in AutoCAD 2007 to get the job done. Thansks for the help.