Script\macro that will delete layers

Script\macro that will delete layers

will.wydock
Advocate Advocate
3,867 Views
10 Replies
Message 1 of 11

Script\macro that will delete layers

will.wydock
Advocate
Advocate

Hi,

 

I am working on a script that I will eventually turn into a macro for my cui. The purpose of the script is to delete certains layers that are in an Architect's Revit export that my engineers never need to see in the files that they use for xrefs. I know that laydel can delete the layer but it will not cycle through if the layers do not exist. Is it possible to do a if layer exists delete it  for a macro/script? If not, how would I handle it within a lisp? see below for the script.

 

 

Thanks

-LAYDEL
n
a-anno-keyn
n
a-anno-dims
n
a-wall-patt
n
g-anno-note
n
g-anno-symb
n
a-wall-iden
n
g-anno-text
n
a-anno-symb

y
-layer
c
8
**

setbylayer
all

y
y
-overkill
all

d
-purge
all

n
0 Likes
3,868 Views
10 Replies
Replies (10)
Message 2 of 11

Ranjit_Singh
Advisor
Advisor

Give this a try. Something very basic and can be improved.

(defun c:custlaydel (/ laylst laycollobj)
  (vl-load-com)
  (setq laycollobj (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
  (vlax-map-collection laycollobj
                       '(lambda (x) (setq laylst (append (list (vla-get-name x)) laylst)))
  ) ;_ end vlax-map-collection
  (mapcar '(lambda (x)
             (if (member x laylst)
               (command "._-laydel" "n" x "" "")
             ) ;_ end if
           ) ;_ end lambda
          '("a-anno-keyn"       "a-anno-dims"       "a-wall-patt"
            "g-anno-note"       "g-anno-symb"       "a-wall-iden"
            "g-anno-text"       "a-anno-symb"
           )
  ) ;_ end mapcar
) ;_ end defun
0 Likes
Message 3 of 11

Anonymous
Not applicable

Thanks, but when I tried using the routine you sent, I get the response (nil nil nil nil nil nil nil nil).

0 Likes
Message 4 of 11

Ranjit_Singh
Advisor
Advisor

Just add a (princ) at the end and you will not get the echo. The routine works though, right?
 

 (defun c:custlaydel (/ laylst laycollobj)
  (vl-load-com)
  (setq laycollobj (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
  (vlax-map-collection laycollobj
                       '(lambda (x) (setq laylst (append (list (vla-get-name x)) laylst)))
  ) ;_ end vlax-map-collection
  (mapcar '(lambda (x)
             (if (member x laylst)
               (command-s "._-laydel" "n" x "" "y")
             ) ;_ end if
           ) ;_ end lambda
          '("a-anno-keyn"       "a-anno-dims"       "a-wall-patt"
            "g-anno-note"       "g-anno-symb"       "a-wall-iden"
            "g-anno-text"       "a-anno-symb"
           )
  ) ;_ end mapcar
(princ)
) ;_ end defun

There was an error. I fixed it. "y" in the command-s 

0 Likes
Message 5 of 11

Anonymous
Not applicable

I am no longer getting the nils, however it is not deleting any layers. The file I am testing it on has most of those layers.

0 Likes
Message 6 of 11

Ranjit_Singh
Advisor
Advisor

Try the updated routine. I edited it. This was the correction (command-s "._-laydel" "n" x "" "y")

 

(defun c:custlaydel (/ laylst laycollobj)
  (vl-load-com)
  (setq laycollobj (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
  (vlax-map-collection laycollobj
                       '(lambda (x) (setq laylst (append (list (vla-get-name x)) laylst)))
  ) ;_ end vlax-map-collection
  (mapcar '(lambda (x)
             (if (member x laylst)
               (command-s "._-laydel" "n" x "" "y")
             ) ;_ end if
           ) ;_ end lambda
          '("a-anno-keyn"       "a-anno-dims"       "a-wall-patt"
            "g-anno-note"       "g-anno-symb"       "a-wall-iden"
            "g-anno-text"       "a-anno-symb"
           )
  ) ;_ end mapcar
(princ)
) ;_ end defun
0 Likes
Message 7 of 11

hmsilva
Mentor
Mentor

Hi Ranjit.Singh
We don't need to get Acad Object, get Active Document, get Layer collection, create a list with all layer nemes in Layer collection, test if layer exist with 'member' that will return a list and if many layers will be slow... testing with vl-position It would be faster...

 

I would suggest something like this:

(untested)

 

(defun c:demo nil
    (foreach lay '("a-anno-keyn" "a-anno-dims" "a-wall-patt" "g-anno-note" "g-anno-symb" "a-wall-iden" "g-anno-text" "a-anno-symb")
        (if (tblobjname "layer" lay)
            (command "_.-laydel" "_N" lay "" "_Y")
        )
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 8 of 11

Ranjit_Singh
Advisor
Advisor

I agree. Using tblobjname is much quicker and concise. I will have to update a few of my routines now 🙂

Message 9 of 11

pramodjoseph
Community Visitor
Community Visitor

Dear all, I need a simple script or lisp file to delete a particular layer called 'M-Rev cloud' . and before deleting the layer I need to change the current layer to '0'.  Thank you.


@Ranjit_Singh wrote:

I agree. Using tblobjname is much quicker and concise. I will have to update a few of my routines now 🙂


 

0 Likes
Message 10 of 11

will.wydock
Advocate
Advocate
-layer;set;0;;-laydel;n;M-Rev cloud;;y;
0 Likes
Message 11 of 11

Anonymous
Not applicable

This script works perfectly for finding the listed layers and removing them all. Unfortunately I want to delete a handful of layers but I wont always know the full name of the layer. So instead of changing the script for every file I work on, is there a script to delete multiple layers containing the word "CHANGE"? I know the word "CHANGE" will be common for all projects I work on.

ex of Layers. "CHANGES KIT"

                       "CHANGES"

                        "CHANGES_520"

Also if possible how would I auto run the .lsp for all drawings in my drawing package. Instead of typing appload.... on every drawing individually?

0 Likes