Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

change layer name based on original layer name

I am looking for a script that can change the layer of an entity. It must be checked if the layer name ends with -I -W or -E and change to eg same-layername-W, if the layer does not end with -I, -W or -E the entire layer name must be changed to architectural-W. If a layer does not exist, it must be created and the color of layers ending with -W is always 10. The same button for changing to -I (color 7) and to -E (color 102).

I have found this script to remove the -I, -E of -W at the end but it is just the first step:

(defun c:laag-x ( / enx idx lay sel )
   (if (setq sel (ssget "_:L" '((-4 . "<or")(8 . "*?-I") (8 . "*?-W") (8 . "*?-E") (-4 . "or>") )))

       (repeat (setq idx (sslength sel))
           (setq enx (entget (ssname sel (setq idx (1- idx))))
                 lay (assoc 8 enx)
           (entmod (subst (cons 8 (matchlayer (cdr lay) (substr (cdr lay) 1 (- (strlen (cdr lay)) 2)))) lay enx))
(defun matchlayer ( src new )
   (or (tblsearch "layer" new)
               (cons 2 new)
               (cons 2 src)
               (vl-remove-if '(lambda ( x ) (member (car x) '(-1 5 102 360))) (entget (tblobjname "layer" src)))

I found this script to put the -W after the layername. But this script does not use the result of the first script, or changes the other entities (without -I, -W and -E) at the end to architectural-W:

(defun c:laag-W ( / e i l n s x )
   (if (setq s (ssget "_:L" '((8 . "~*-W"))))
       (repeat (setq i (sslength s))
           (setq x (entget (ssname s (setq i (1- i))))
                 l (assoc 8 x)
                 n (strcat (cdr l) "-W")
           (if (not (tblsearch "layer" n))
                   (setq e (entget (tblobjname "layer" (cdr l))))
                   (entmake (subst (cons 2 n) (assoc 2 e) (subst '(62 . 10) (assoc 62 e) e)))
           (entmod (subst (cons 8 n) l x))


I assume that these two steps can also be done in one go, or that modified entities from the first step (with the shortened layer name) are placed in a new selection set that is used by step 2?
Any thoughts?.


  • Is it possible to create 2 selection sets out of one selection? one set containing all the selected objects on layers ending with -I, -W or -E and one selection set on layers not ending on -I, -W or -E?

  • Hans, it is always a good idea to take code from others and try to learn from it. Most code fragments, approaches to challenges, are more or less the same. However, searching for code that fits your needs exactly will not always work, like in this case. There are multiple possible approaches here, like renaming layers instead of entities layers. And changing layers of entities... Does it also apply for viewports for example?

    John Walker did a great job documenting Lisp and as far as I know it has been reasonably updated for ~30 years by Autodesk. You may want to have them on your Desktop, see LISP-functions, AutoLISP Reference Guide and LISP-explained, AutoLISP Developer's Guide. There is also a cheat sheet with commands grouped - the value of the sheet. Kenny Ramage was always a pleasure for learning basics, combined with practical approaches.

    It may be a good idea to focus on the first part in first instance, the (if ...) part. You can recycle (defun ...) inside another (defun ...). By making it more universal, you can run it inside the routine three times, for -I, -W and -E. Smaller, less complex function, makes writing easier.

    Example more universal

    (defun replace-tail ( old-tail new-tail / )
      (if (setq sel (ssget "X" (list (cons 8 (strcat "*" old-tail)))))

    You can also consider less universal, using a (cond ...) statement, for testing entities layers, -I then this, -W than that, -E something else...

    The (repeat ...) part uses strlen and substr to analyze layer names, entmod does the actual change.

    Just some thoughts...

Sign In or Register to comment.
Origami is the Japanese word for paper folding. ORI means to fold and KAMI means paper and involves the creation of paper forms usually entirely by folding.

Powered by VanillaForums, Designed by Steam