LISP routine for duplicating multiple layers at once

LISP routine for duplicating multiple layers at once

edrewsSBTV9
Explorer Explorer
1,762 Views
10 Replies
Message 1 of 11

LISP routine for duplicating multiple layers at once

edrewsSBTV9
Explorer
Explorer

I am looking for a LISP routine that takes all layers in a drawing and duplicates them, while adding a suffix to the layer name.

 

For example, start with the following layers:

LayerA

LayerB

LayerC

 

End up with duplicated layers, adding the suffix "- Car":

LayerA

LayerA - Car

LayerB

LayerB - Car

LayerC 

LayerC - Car

0 Likes
Accepted solutions (1)
1,763 Views
10 Replies
Replies (10)
Message 2 of 11

devitg
Advisor
Advisor

Hi   @edrewsSBTV9 For better understanding, and maybe get further help, please upload such sample.dwg

0 Likes
Message 3 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

Try this:

(defun C:LDNS ; = Layer Duplicate with Name Suffix [" - Car" built in]
  (/ lay laylist ldata lname)
  (while (setq lay (tblnext "layer" (not lay)))
    (setq lname (cdr (assoc 2 lay)))
    (if (not (wcmatch lname "0,Defpoints"))
      (setq laylist (cons lname laylist)); put in list of names
    ); if
  ); while
  (foreach lname laylist
    (setq ldata (entget (tblobjname "layer" lname)))
    (entmake (subst (cons 2 (strcat lname " - Car")) (assoc 2 ldata) ldata))
  ); foreach
  (princ)
); defun

All the duplicated Layers get the color, linetype, etc. of the source Layers.  If that's not what you want, it could be made to do it in some different way.

It could be made to ask for the suffix, instead of having it built in.

I assumed you would not want to duplicate "0" or "Defpoints", but they could be included.

Kent Cooper, AIA
Message 4 of 11

ronjonp
Advisor
Advisor

@Kent1Cooper  FWIW I'd also exclude xref layers ( not that entmake is going to error on it ) as well as layers that already have the suffix in the event the user runs the code more than once. Without it you'd have a bunch of stuttering -car -car -car layers. 🍻

(wcmatch lname "0,Defpoints,*|*,* - Car")

Message 5 of 11

Kent1Cooper
Consultant
Consultant

@ronjonp wrote:

....

(wcmatch lname "0,Defpoints,*|*,* - Car")


Yes, good idea.

Kent Cooper, AIA
0 Likes
Message 6 of 11

edrewsSBTV9
Explorer
Explorer

Thank you!

0 Likes
Message 7 of 11

edrewsSBTV9
Explorer
Explorer

That works, Thank you!

0 Likes
Message 8 of 11

bguyB9HE7
Observer
Observer

Hello! This is an amazing lsp. Is there a way to do it for layers that are selected instead of all? Tried to see if I could edit it myself but was unsuccessful.

 

Thank you!

 

Brandon

0 Likes
Message 9 of 11

Kent1Cooper
Consultant
Consultant

@bguyB9HE7 wrote:

... for layers that are selected instead of all? ....


Try this:

(defun C:LDNS ; = Layer Duplicate with Name Suffix [" - Car" built in]
  (/ lay laylist ldata lname)
  (prompt "\nTo duplicate Layer(s) of selected object(s) with suffix added to name(s),")
  (if (setq ss (ssget "_:L"))
    (progn ; then
      (repeat (setq n (sslength ss))
        (setq lname (cdr (assoc 8 (entget (ssname ss (setq n (1- n)))))))
        (if
          (and
            (not (member lname laylist)); not already in list
            (not (wcmatch lname "* - Car")); doesn't already have suffix
          ); and
          (setq laylist (cons lname laylist)); then -- put in list of names
        ); if
      ); repeat
      (foreach lname laylist
        (setq ldata (entget (tblobjname "layer" lname)))
        (entmake (subst (cons 2 (strcat lname " - Car")) (assoc 2 ldata) ldata))
      ); foreach
    ); progn
  ); if
  (princ)
); defun

I assume you will similarly replace the suffix with your own,  It could also be made to ask for that each time, rather than have it built in.

Kent Cooper, AIA
0 Likes
Message 10 of 11

Sea-Haven
Mentor
Mentor

Lee-mac has a double List box function so you could have all your layers in the left side and select which ones you need to change, building a list of layer names in right side. Needs to have the add suffix part added, ran out of time.

 

 

0 Likes
Message 11 of 11

bguyB9HE7
Observer
Observer

Wow thank you so much for the help and replying!! Will give this one a go.

0 Likes