LISP to rename a list of layers

LISP to rename a list of layers

MByrneX6FAZ
Contributor Contributor
736 Views
14 Replies
Message 1 of 15

LISP to rename a list of layers

MByrneX6FAZ
Contributor
Contributor

I often have drawings with a lot of layers that I want to rename. I want to be able to change the entire name, rather than just replace certain characters in the name and I want to be able to do this in mass, not by renaming one layer at a time. Ideally, I want to be able to paste a list of old layer names in the command window (or lisp itself if it is not possible to do so in the command window) and then paste a list of new layer names that the lisp will use to rename the original layers. 

 

Example

Old layer names: SOL-MET-STATION, C-SITE-COLL, AC-01

New layer names: E-COMM-MET, E-COND-BORE, E-LVAC-01-01

 

I came across a few renaming lisps in my google search for existing lisps, but I didn't find one that does this specifically. I think using the "-layer" or "rename" commands in the lisp would be useful, but don't really know much about writing lisps so I was hoping someone could offer a solution.

 

I saw a lisp that structured old and new layer names like this: 

  (renlay "ARSTAIR" "A-Flor-Strs")
  (renlay "ARPARTITION" "A-Flor-Tptn")
  (renlay "ARWOOD" "A-Flor-Wdwk")
  (renlay "ARFURNITURE" "A-Furn")

but I would rather be able to paste in all of the old names at once and then all of the new names at once. However, if that can't be done, I'm okay with a lisp that would use the format above. 

 

Feel free to let me know if there is a better way to do this without a lisp. 

 

0 Likes
Accepted solutions (1)
737 Views
14 Replies
Replies (14)
Message 2 of 15

Kent1Cooper
Consultant
Consultant

Where are the lists of Layer names coming from?  What is their actual format?  Are they by any chance in AutoLisp list-of-text-strings format of this kind?

'("SOL-MET-STATION" "C-SITE-COLL" "AC-01")

That would make the task pretty easy.

If each list is a single text string:

"SOL-MET-STATION, C-SITE-COLL, AC-01"

then it could be broken into separate Layer names around the comma-&-space instances. but it would need to be exactly the same separator between each in both lists.  And if the separator is just a space, you would need to restrict the names to not have any spaces in them.

 

Kent Cooper, AIA
0 Likes
Message 3 of 15

paullimapa
Mentor
Mentor

Perhaps try the attached RenLayer.lsp 

Save this in a folder listed under: Options>Files>Support File Search Path

Load the lisp by entering the following at th command prompt:

(load"RenLayer")

The function requires you to enter two list of layer names each separated by quotes "":

"old layername one, old layername two" "new layername one, newlayername two".

Notice that within each list the layer names are separated by commas ",".

So using your example enter the following at the command line to run:

(renlayer "SOL-MET-STATION, C-SITE-COLL, AC-01" "E-COMM-MET, E-COND-BORE, E-LVAC-01-01")

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 4 of 15

BlackBox_
Advisor
Advisor

@MByrneX6FAZ wrote:

I often have drawings with a lot of layers that I want to rename. I want to be able to change the entire name, rather than just replace certain characters in the name and I want to be able to do this in mass, not by renaming one layer at a time. Ideally, I want to be able to paste a list of old layer names in the command window (or lisp itself if it is not possible to do so in the command window) and then paste a list of new layer names that the lisp will use to rename the original layers. 

 

Example

Old layer names: SOL-MET-STATION, C-SITE-COLL, AC-01

New layer names: E-COMM-MET, E-COND-BORE, E-LVAC-01-01

 

I came across a few renaming lisps in my google search for existing lisps, but I didn't find one that does this specifically. I think using the "-layer" or "rename" commands in the lisp would be useful, but don't really know much about writing lisps so I was hoping someone could offer a solution.

 

I saw a lisp that structured old and new layer names like this: 

  (renlay "ARSTAIR" "A-Flor-Strs")
  (renlay "ARPARTITION" "A-Flor-Tptn")
  (renlay "ARWOOD" "A-Flor-Wdwk")
  (renlay "ARFURNITURE" "A-Furn")

but I would rather be able to paste in all of the old names at once and then all of the new names at once. However, if that can't be done, I'm okay with a lisp that would use the format above. 

 

Feel free to let me know if there is a better way to do this without a lisp. 

 


@MByrneX6FAZ the behavior you're describing is already part of AutoCAD using LAYTRANS Command.

 

If you prefer LISP, here's one way, just modify the list of pairs at top: 

(defun c:RenameLayers (/ *error* layersToRename acDoc e newName oldName)

  ;; (oldName newName)
  (setq layersToRename
         '(
           ("SOL-MET-STATION" "E-COMM-MET")
           ("C-SITE-COLL" "E-COND-BORE")
           ("AC-01" "E-LVAC-01-01")
           ("ARSTAIR" "A-Flor-Strs")
           ("ARPARTITION" "A-Flor-Tptn")
           ("ARWOOD" "A-Flor-Wdwk")
           ("ARFURNITURE" "A-Furn")
          )
  )

  (defun *error* (msg)
    (if acDoc
      (vla-endundomark acDoc)
    )
    (cond ((not msg))                                                   ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
    )
    (princ)
  )

  (vla-startundomark
    (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
  )
  (foreach item layersToRename
    (if (not (tblsearch "layer" (setq newName (cadr item))))
      (if
        (and
          (setq e (tblobjname "layer" (setq oldName (car item))))
          (setq e (entget e))
        )
        (if (entmod (subst (cons 2 newName) (assoc 2 e) e))
          (prompt
            (strcat "\n >> Layer \"" oldName "\" renamed to \"" newName "\" \n")
          )
          (prompt
            (strcat "\n ** Failed to rename layer \"" oldName "\" to \"" newName "\" ** \n")
          )
        )
        (prompt
          (strcat "\n ** Layer \"" oldName "\" not found ** \n")
        )
      )
      (prompt
        (strcat "\n ** Layer \"" newName "\" already exists ** \n")
      )
    )
  )
  (*error* nil)
)

 


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 5 of 15

BlackBox_
Advisor
Advisor

@Kent1Cooper & @paullimapa - man, you guys are fast! Haha


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 6 of 15

paullimapa
Mentor
Mentor

game on...lol


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 15

Sea-Haven
Mentor
Mentor

Another suggestion, if you set up a Excel file with 2 columns Old & new you can read the excel file from CAD, just have the correct Excel file open at time of running the lisp. You have suggestions about how to do the rename above. To make the 1st Excel or a new one you can write to excel all the current layer names into say column 1, again via lisp. An extra is also change color and linetype. Yes have done it but had a txt file with layer  details.

0 Likes
Message 8 of 15

BlackBox_
Advisor
Advisor

You can always 'right click convert Excel to CSV' from File Explorer too... I do this for LISP apps where it's easier to manage the data using XLSx, but CSV is super easy for LISP to handle without the overhead required to work with Excel directly. 

 

Circa 2016:  https://www.theswamp.org/index.php?topic=38330.msg567823#msg567823

 

2016-07-14_16-40-12.png


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 9 of 15

komondormrex
Mentor
Mentor
Accepted solution

@MByrneX6FAZ

check this one

;*******************************************************************************************************************************

(defun string_to_list (input_string delimiter / delimiter_position output_list)
  (while (setq delimiter_position (vl-string-search delimiter input_string 0))
    (setq output_list (append output_list (list (vl-string-trim " " (substr input_string 1 delimiter_position))))
        input_string (substr input_string (+ 2 delimiter_position))
    )
  )
  (append output_list (list (vl-string-trim " " input_string)))
)

;*******************************************************************************************************************************

(defun c:rename_layers (/ from_layers to_layers layer layer_collection)
  (if (and (/= "" (setq from_layers (getstring t "\nPaste layers to rename, comma separated: ")))
       (/= "" (setq to_layers (getstring t "\nPaste layers to rename to, comma separated: ")))
       (setq layer_collection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
    )
    (mapcar '(lambda (from_layer to_layer)
          (if (vl-catch-all-error-p (setq layer (vl-catch-all-apply 'vla-item (list layer_collection from_layer))))
            (progn
              (princ "\n***Layer \"")
              (princ from_layer)
              (princ "\" not found***")
            )
            (progn 
              (vla-put-name layer to_layer)
              (princ "\nLayer \"")
              (princ from_layer)
              (princ "\" renamed to \"")
              (princ to_layer)
              (princ "\"")
            )
          )
         )
         (string_to_list from_layers ",")
         (string_to_list to_layers ",")
    )
  )
  (princ)
)

;*******************************************************************************************************************************

 

Message 10 of 15

MByrneX6FAZ
Contributor
Contributor

What is the command for running this lisp? I loaded it in, but RenLayer isn't a recognized command.

0 Likes
Message 11 of 15

MByrneX6FAZ
Contributor
Contributor

That worked, thank you!

0 Likes
Message 12 of 15

MByrneX6FAZ
Contributor
Contributor

If I were to use LAYTRANS though wouldn't the new layer names have to come from existing layers in another CAD file, rather than a typed list?

0 Likes
Message 13 of 15

paullimapa
Mentor
Mentor

As I stated in my response previously:

So using your example enter the following at the command line to run:

(renlayer "SOL-MET-STATION, C-SITE-COLL, AC-01" "E-COMM-MET, E-COND-BORE, E-LVAC-01-01")

Notice the open parentheses “(“ preceding RenLayer followed by first a list of the original layer names “..,..” and then the second list of new layer names“..,..”and then the close parentheses “)”.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 14 of 15

komondormrex
Mentor
Mentor

🌹

0 Likes
Message 15 of 15

dfinlay
Participant
Participant

Have you considered using Autocad's Rename command? You can rename layers there as well as lots of other things. No lisp required. 

0 Likes