Stratos Posted March 18, 2023 Posted March 18, 2023 Hello, I am looking for an AutoLISP script that deletes all layers that have a name starting by specific strings. For example "wall" or "win" or "slab". It should not be case sensitive and the layers should be deleted even if they have something inside. Could it be an integated function so I can call it multiple times for different strings in the main body of the script? Could anyone help me out? I know I am quite a novice in the AutoLISP area.... Quote
marko_ribar Posted March 18, 2023 Posted March 18, 2023 (edited) Not tested, but it should be something like this : (defun c:dellaysbyprefix ( / lay lays prefix ) (while (setq lay (tblnext "LAYER" (not lay))) (setq lays (cons (cdr (assoc 2 lay)) lays)) ) (setq prefix (strcase (getstring "\nSpecify prefix of layers to remove from DWG : "))) (foreach lay lays (if (wcmatch (strcase lay) (strcat prefix "*")) (progn (vl-cmdf "_.LAYDEL" lay) (while (< 0 (getvar (quote cmdactive))) (vl-cmdf "") ) ) ) ) (princ) ) HTH. M.R. Edited March 18, 2023 by marko_ribar 2 Quote
Stratos Posted March 21, 2023 Author Posted March 21, 2023 @marko_ribar Thank you for your reply. Unfortynately the _.LAYDEL command did not work for me. I found anohter solution on this thread that deletes everything inside the layer and then purges the drawing which deletes these layers. I combined it with your code: (defun delbylayer (layname / *error* adoc umark sset sslen) ; Loads the vl library (vl-load-com) ; Define error handler function (defun *error* (msg) ; Undo previous changes if there is an error (if umark (progn (vla-endundomark adoc)(setq umark nil))) ; If the error is 'Function cancelled' or 'quit / exit abort', do nothing (if (or (= msg "Function cancelled")(= msg "quit / exit abort")) (princ) ; Otherwise, print the error message (princ (strcat "\nError: " msg)) ) ) ; Get the active AutoCAD document (setq adoc (vla-get-activedocument (vlax-get-acad-object))) ; Execute different actions depending on whether a selection set is returned or not (cond ( (setq sset (ssget "_X" (list (cons 8 layname)))) ; Set current layer to "0" and start an undo mark (setvar 'clayer "0") (progn (vla-startundomark adoc)(setq umark T)) ; Delete all the entities on the specified layer and get the count of the deleted entities (setq sslen (length (mapcar 'entdel (setq sslst (vl-remove-if 'listp (mapcar 'cadr (ssnamex sset)) ) ) ) ) ) ; End the undo mark and print the count of deleted entities (progn (vla-endundomark adoc)(setq umark nil)) ;(princ (strcat "\nErased " (itoa sslen) " items.")) ) ) ; Return nil (princ) ) (defun c:dellaysbyprefix ( / lay lays prefix ) ; Loads the vl library (vl-load-com) ; Get the active AutoCAD document (setq adoc (vla-get-activedocument (vlax-get-acad-object))) ; Loop through all layers in the drawing and store their names in a list (while (setq lay (tblnext "LAYER" (not lay))) (setq lays (cons (cdr (assoc 2 lay)) lays)) ) ; Prompt user to input the prefix of layers to be removed (setq prefix (strcase (getstring "\nSpecify prefix of layers to remove from DWG : "))) ; Loop through the list of layer names and delete all layers that start with the specified prefix (foreach lay lays (if (wcmatch (strcase lay) (strcat prefix "*")) (progn ; Print the name of the layer being deleted and call the 'delbylayer' function to delete the layer's contents (princ "\n") (princ lay) (delbylayer lay) ) ) ) (vla-purgeall adoc) ; Return nil (princ) ) Quote
pkenewell Posted March 21, 2023 Posted March 21, 2023 55 minutes ago, Stratos said: @marko_ribar Thank you for your reply. Unfortynately the _.LAYDEL command did not work for me. I found anohter solution on this thread that deletes everything inside the layer and then purges the drawing which deletes these layers. I combined it with your code: (defun delbylayer (layname / *error* adoc umark sset sslen) ; Loads the vl library (vl-load-com) ; Define error handler function (defun *error* (msg) ; Undo previous changes if there is an error (if umark (progn (vla-endundomark adoc)(setq umark nil))) ; If the error is 'Function cancelled' or 'quit / exit abort', do nothing (if (or (= msg "Function cancelled")(= msg "quit / exit abort")) (princ) ; Otherwise, print the error message (princ (strcat "\nError: " msg)) ) ) ; Get the active AutoCAD document (setq adoc (vla-get-activedocument (vlax-get-acad-object))) ; Execute different actions depending on whether a selection set is returned or not (cond ( (setq sset (ssget "_X" (list (cons 8 layname)))) ; Set current layer to "0" and start an undo mark (setvar 'clayer "0") (progn (vla-startundomark adoc)(setq umark T)) ; Delete all the entities on the specified layer and get the count of the deleted entities (setq sslen (length (mapcar 'entdel (setq sslst (vl-remove-if 'listp (mapcar 'cadr (ssnamex sset)) ) ) ) ) ) ; End the undo mark and print the count of deleted entities (progn (vla-endundomark adoc)(setq umark nil)) ;(princ (strcat "\nErased " (itoa sslen) " items.")) ) ) ; Return nil (princ) ) (defun c:dellaysbyprefix ( / lay lays prefix ) ; Loads the vl library (vl-load-com) ; Get the active AutoCAD document (setq adoc (vla-get-activedocument (vlax-get-acad-object))) ; Loop through all layers in the drawing and store their names in a list (while (setq lay (tblnext "LAYER" (not lay))) (setq lays (cons (cdr (assoc 2 lay)) lays)) ) ; Prompt user to input the prefix of layers to be removed (setq prefix (strcase (getstring "\nSpecify prefix of layers to remove from DWG : "))) ; Loop through the list of layer names and delete all layers that start with the specified prefix (foreach lay lays (if (wcmatch (strcase lay) (strcat prefix "*")) (progn ; Print the name of the layer being deleted and call the 'delbylayer' function to delete the layer's contents (princ "\n") (princ lay) (delbylayer lay) ) ) ) (vla-purgeall adoc) ; Return nil (princ) ) You may also want to add (command "._layer" "_U" layname "") before deleting the entities. If the layer is locked, it won't work. Just my $0.02 1 Quote
BIGAL Posted March 22, 2023 Posted March 22, 2023 Laydel unknown command in Bricscad V20 not sure later versions. Quote
Recommended Posts
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.