Hi guys,
Based on the tip from *Paul Turvill : http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Delete-a-Layer-with-Laydel/td-p/21742... I made this routine:
(defun c:DeleteLayerTavail ()
(setq ss (ssget "x" '((8 . "Tx-Travail"))))
(command "_.erase" ss "")
(princ)
)
The routine works great but I notice that if I don’t have this layer, it will return an error. Could someone update this to just ignore if the drawing don’t have the layer Tx-Travail?
I need this because I will use this routine together with another routines and in some cases I will not have the layer specified.
Thanks for help!
Solved! Go to Solution.
Solved by pbejse. Go to Solution.
@msarqui wrote:Hi guys,
Based on the tip from *Paul Turvill : http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Delete-a-Layer-with-Laydel/td-p/21742... I made this routine:
(defun c:DeleteLayerTavail ()
(setq ss (ssget "x" '((8 . "Tx-Travail"))))
(command "_.erase" ss "")
(princ)
)
The routine works great but I notice that if I don’t have this layer, it will return an error. Could someone update this to just ignore if the drawing don’t have the layer Tx-Travail?
I need this because I will use this routine together with another routines and in some cases I will not have the layer specified.
Thanks for help!
(defun c:DeleteLayerTavail () (if (setq ss (ssget "x" '((8 . "Tx-Travail")))) (command "_.erase" ss "")) (princ) )
or
(defun DeleteLayer (lay) (cond ((and (tblsearch "LAYER" lay) (setq ss (ssget "x" (list (cons 8 lay)))) (repeat (setq i (sslength ss)) (entdel (ssname ss (setq i (1- i))))) ))) (princ) )
(deleteLayer "layername")
Why it does not work if the layer is not in the current space.
Can be set to erase the layer in both spaces like the LAYDEL command?
Thanks!
@msarqui wrote:Why it does not work if the layer is not in the current space.
Can be set to erase the layer in both spaces like the LAYDEL command?
Thanks!
Try this instead
(defun DeleteLayer (lay) (vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) (vlax-for i (vla-get-block layout) (if (eq (vla-get-layer i) lay) (vla-delete i))) ) )
Hello pbejse
I start to understand and do some basic routines but my knowledge is still very small. So when I see these VLA things I do not understand at all. This is like chinese language to me 😞
So, I tried this :
(defun c:DeleteLayer (lay)
(vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for Tx-Travail (vla-get-block layout)
(if (eq (vla-get-layer Tx-Travail) lay)
(vla-delete Tx-Travail)))
)
)
And I got this error: Command: too few arguments
Thanks for your time.
@msarqui wrote:Hello @pbejse
I start to understand and do some basic routines but my knowledge is still very small. So when I see these VLA things I do not understand at all. This is like chinese language to me 😞
So, I tried this :
And I got this error: Command: too few arguments
Thanks for your time.
msarqui
To use snippet i posted
(DeleteLayer "Tx-Travail")
It is a generic routine that accepts a layer name as argument
If you need to a command to work for a specific layer
(defun c:DeleteLayerTavail ()(DeleteLayer "Tx-Travail"))
Also take note that the layer name is case sensitive: in that case we can modify the routine to
(defun DeleteLayer (lay) (vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) (vlax-for i (vla-get-block layout) (if (eq (strcase (vla-get-layer i)) (strcase lay)) (vla-delete i))) ) )
Better yet , to use the routine for one or more layer names
(defun DeleteLayer (lst) (vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) (vlax-for i (vla-get-block layout) (if (member (strcase (vla-get-layer i)) (mapcar 'strcase lst)) (vla-delete i))) ) )
(DeleteLayer '("Tx-Travail" "otherLayer" "AnotherLayer"))
Now, going back to your original post
(defun c:DeleteLayerTavail ()(DeleteLayer '("Tx-Travail")))
HTH
This two works great and realy fast:
(defun c:dlt1 ()(DeleteLayer "Tx-Travail"))
(defun DeleteLayer (lay)
(vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for i (vla-get-block layout)
(if (eq (strcase (vla-get-layer i))
(strcase lay))
(vla-delete i)))
)
)
;------------------------------------------------------------------------;
(defun c:dlt2 ()(DeleteLayer "Tx-Travail"))
(defun DeleteLayer (lay)
(vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for i (vla-get-block layout)
(if (eq (vla-get-layer i) lay)
(vla-delete i)))
)
)
;------------------------------------------------------------------------;
But this one with the list returns "nil" and do nothing:
(defun c:dlt3 ()(DeleteLayer '("Tx-Travail" "Tx-1" "Tx-2" "Tx-3")))
(defun DeleteLayer (lst)
(vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for i (vla-get-block layout)
(if (member (strcase (vla-get-layer i))
(mapcar 'strcase lst))
(vla-delete i)))
)
)
;------------------------------------------------------------------------;
Regards,
@msarqui wrote:
But this one with the list returns "nil" and do nothing:
(defun c:dlt3 ()(DeleteLayer '("Tx-Travail" "Tx-1" "Tx-2" "Tx-3")))
(defun DeleteLayer (lst)
(vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for i (vla-get-block layout)
(if (member (strcase (vla-get-layer i))
(mapcar 'strcase lst))
(vla-delete i)))
)
);------------------------------------------------------------------------;
Regards,
Works on my end , its either there are no objects on the listed layer or the layer doesnt exist. or worse the entities are on a locked layer.
What you can do is add a sub to unlock all layers then invoke the command
(defun DeleteLayer (lst / n) (setq n 0) (vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) (vlax-for i (vla-get-block layout) (if (member (strcase (vla-get-layer i)) (mapcar 'strcase lst)) (progn (vla-delete i) (setq n (1+ n))) ) ) ) (princ (strcat "\n" (itoa n) " Objects deleted")) (princ) )
HTH
I got this error: Command: Automation Error. On locked layer
So I made this:
(defun DeleteLayer (lst / n)
(command "-layer" "s" "0" "u" "*" "on" "*" "t" "*" "")
(setq n 0)
(vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for i (vla-get-block layout)
(if (member (strcase (vla-get-layer i)) (mapcar 'strcase lst))
(progn (vla-delete i)
(setq n (1+ n)))
)
)
)
(princ (strcat "\n" (itoa n) " Objects deleted"))
(princ)
)
And now it works. 🙂
@msarqui wrote:I got this error: Command: Automation Error. On locked layer
So I made this:
(defun DeleteLayer (lst / n)
(command "-layer" "s" "0" "u" "*" "on" "*" "t" "*" "")
.....And now it works. 🙂
Good for you msarqui 🙂
"i'll leave the "unlocking" to the user"
BTW: (command "_layerp");;<-----