Changing layers names - autolisp

Changing layers names - autolisp

yu85.info
Collaborator Collaborator
6,553 Views
20 Replies
Message 1 of 21

Changing layers names - autolisp

yu85.info
Collaborator
Collaborator

Hi, I have a drawing (attached DWG). Is there a way to automaticity change the layers name according to this EXCEL file (attached)? from OLD column to NEW column?

Thanl you very much

0 Likes
Accepted solutions (3)
6,554 Views
20 Replies
Replies (20)
Message 2 of 21

ronjonp
Mentor
Mentor

Does not read Excel, but if you structure your data in a list it's very easy.

 

(defun c:foo (/ a l)
  ;; Make your data list like so
  (setq l '(("A" "WATER") ("B" "ELEC") ("C" "GROUND") ("D" "PEOPLE")))
  (vlax-for lyr	(vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
    (if	(setq a (assoc (strcase (vla-get-name lyr)) l))
      (vl-catch-all-apply 'vla-put-name (list lyr (cadr a)))
    )
  )
  (princ)
)

Then if you want to make the list in Excel, it can be formatted to lisp like so:

ronjonp_1-1653598008163.png

 

Copy paste into list 'l' and rock and roll!

 

 

Message 3 of 21

cadffm
Consultant
Consultant
Accepted solution

without Lisp - specially for LT users

 

(just a sample, because the best way use LayMrg instead rename - if the new layer is already present)

Sebastian

0 Likes
Message 4 of 21

yu85.info
Collaborator
Collaborator
Thank you very much. But what do you mean to arrange it in a list? In the
script you wrote?
I don't really need an Excel file. It was just for the example.
0 Likes
Message 5 of 21

cadffm
Consultant
Consultant
Accepted solution

@yu85.info  schrieb:
I don't really need an Excel file. It was just for the example.

 

Okay, then write a simple script like my sample in the xls file

_.rename _layer A

Water

_.rename _layer B

ELEC

_.rename _layer C

GROUND

_.rename _layer D

PEOPLE

 

or look at the STANDARDS command or better LAYTRANS [F1]

 

and for the lisp solution above - see how the OLD and NEW names are arranged - so you can change it if you need

safe the .lsp file

load this .lsp file

start the command foo

You can change the name foo to ehatever you want, c:MyLayTrans instead of c:foo for example

(setq l '(("A" "WATER") ("B" "ELEC") ("C" "GROUND") ("D" "PEOPLE")))

 

Sebastian

Message 6 of 21

Sea-Haven
Mentor
Mentor

An extra alternative is to make a csv file from excel would have oldlayername,newlayername.

 

; rename layers

(defun C:renlay ( / fo ans )
; thanks to Lee-mac for this defun
(defun csv->lst ( str / pos )
(if (setq pos (vl-string-position 44 str))
    (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2))))
    (list str)
    )
)

(setq fo (open (getfiled "Select CSV File" "" "csv" 16) "R"))
(setq dummy (read-line fo))
(while (setq str (read-line fo))
(setq ans (csv->lst  str))
(princ ans)
(setq layold (car ans) laynew (cadr ans))
(if (tblsearch "Layer" laynew)
(alert (strcat "The layer "laynew " already exists so skipping that layer "))
(command "-rename" "layer" layold laynew)
)
)
(close fo)

(princ)
)
(c:renlay)
0 Likes
Message 7 of 21

ronjonp
Mentor
Mentor
Accepted solution

@yu85.info 

The list in the code is formatted as ((OLDNAME NEWNAME)(OLDNAME NEWNAME)...) .. modify it to your needs then run the code. Understand?

 

(setq l '(("A" "WATER") ("B" "ELEC") ("C" "GROUND") ("D" "PEOPLE")))

 

ronjonp_0-1653657618751.png

If you run the code in your sample drawing you will see that it works per your example.

 

Message 8 of 21

yu85.info
Collaborator
Collaborator
Thank you very much for your help. I will try what you said.
0 Likes
Message 9 of 21

ronjonp
Mentor
Mentor

@yu85.info wrote:
Thank you very much for your help. I will try what you said.

Glad to help. Let me know how it goes. 👍

0 Likes
Message 10 of 21

Kent1Cooper
Consultant
Consultant

Or the "plain" way:

 

(setq pairs '(("A" "WHO") ("B" "WHAT") ("C" "WHY")))

 

(defun RLL (pairs) ; = Rename Layers from List

  (foreach pair pairs

    (if (tblsearch "layer" (car pair))

      (command "_.rename" "_layer" (car pair) (cadr pair))

    )

  )

  (princ)

)

 

Interestingly, there's a Layer option in the RENAME command, and also the converse -- a Rename option in the -LAYER command.  I used the former because the latter would take an extra enter "" to conclude the Layer command.

EDIT:  Removed the C: before the RLL function name.

Kent Cooper, AIA
0 Likes
Message 11 of 21

Kent1Cooper
Consultant
Consultant

@cadffm wrote:

.... write a simple script like my sample in the xls file

_.rename _layer A

Water

....


[That will have trouble if any of the old Layer names does not exist in the drawing.]

Kent Cooper, AIA
0 Likes
Message 12 of 21

cadffm
Consultant
Consultant

You answering to me?

🤣


 

Sebastian

0 Likes
Message 13 of 21

yu85.info
Collaborator
Collaborator
Sounds good I will try that also. Thank you very much friend.
0 Likes
Message 14 of 21

ronjonp
Mentor
Mentor

@Kent1Cooper wrote:

Or the "plain" way:

 

(setq pairs '(("A" "WHO") ("B" "WHAT") ("C" "WHY")))

 

(defun C:RLL (pairs) ; = Rename Layers from List

  (foreach pair pairs

    (if (tblsearch "layer" (car pair))

      (command "_.rename" "_layer" (car pair) (cadr pair))

    )

  )

  (princ)

)

 

Interestingly, there's a Layer option in the RENAME command, and also the converse -- a Rename option in the -LAYER command.  I used the former because the latter would take an extra enter "" to conclude the Layer command.


@Kent1Cooper That code won't work with C:.

 

Did you mean?

 

(rll '(("A" "WHO") ("B" "WHAT") ("C" "WHY")))

 

 

0 Likes
Message 15 of 21

Kent1Cooper
Consultant
Consultant

@ronjonp wrote:....

....That code won't work with C:.  ....


You're right.  I first wrote it up with the list of Layer-name pairs built into the command, and the C: was appropriate, but when I decided to pull the list out and make it an argument, so it could be used with different lists, I forgot to pull the C: out of the (defun) line.  I've made the correction there.

Kent Cooper, AIA
0 Likes
Message 16 of 21

JohnC_ISM
Collaborator
Collaborator

i have a lisp and we changed the layer names but only by a little so how do i make it search like *hatch/Hatch

 

 

(defun c:HS nil
  ;; Set pattern scale
  (if (> (getvar 'dimscale) 0)
    (setvar 'hpscale (* 0.05 (getvar 'dimscale)))
  )
  ;; Set pattern name
  (setvar 'hpname "AR-SAND")
  (command "._BHATCH")
  (while (> (getvar 'cmdactive) 0) (command "\\"))
  ;; Hatches to back
  (command "._HATCHTOBACK")
  (command "_.chprop" "_last" "" "_layer" "E-Hatch" "")
  (princ)
)

 

it used to be Ehatch and now its E-Hatch sometimes old drawings are used about 50% of the time so im just tryna make it so the lisp works for everyone regardless of old or new drawing is open.  

0 Likes
Message 17 of 21

Kent1Cooper
Consultant
Consultant

@JohnC_ISM wrote:

.... how do i make it search like *hatch/Hatch

....
  (command "_.chprop" "_last" "" "_layer" "E-Hatch" "")
....

it used to be Ehatch and now its E-Hatch sometimes old drawings are used about 50% of the time so im just tryna make it so the lisp works for everyone regardless of old or new drawing is open.  


It can check through all the possibilities for the Layer name until it finds the one that is in the current drawing [or, the first one it finds, if there are more than one of them]:

(command
  "_.chprop" "_last" "" "_layer"
  (cond ; find a Layer that exists
    ((tblsearch "layer" "E-hatch") "E-hatch")
    ((tblsearch "layer" "Ehatch") "Ehatch")
    ((tblsearch "layer" "Hatch") "Hatch")
    ((tblsearch "layer" "Whatever") "Whatever")
    ("0")
  ); cond
  "" ; conclude CHPROP
)

[That would not be case-sensitive.]

It will stop looking as soon as it finds one of those Layer names exists and uses it.  I added a there-aren't-any fallback to put it on Layer 0, so that it can't cause an error, since that Layer will always exist, but it could be made to just keep it on the current Layer instead.

Kent Cooper, AIA
0 Likes
Message 18 of 21

JohnC_ISM
Collaborator
Collaborator
Either one is fine theyre the same everything just the name changed. It was annoying trying to read a list of Ehatch, Eboundary. So I just changed it so itd slightest bit easier with E-Hatch, E-Boundary and so on. Thanks ill give this a go.
0 Likes
Message 19 of 21

632_23
Community Visitor
Community Visitor

Hello everyone, how can I create an autolisp that will change the color of the layers with the corresponding names A, B, C, D in the drawing without changing the layer name?

0 Likes
Message 20 of 21

cadffm
Consultant
Consultant

Hi,

 

the command to create or edit layers, is LAYER or -LAYER

create a script macro or lisp-command to controle it by commandline.

 

Open F2 to follow the command workflow easier

-Layer<enter>

Co<enter>

5<enter<

A<enter>

Co<enter>

1<enter>

B,C<enter>

Co<enter>

6<enter>

[D-E]<enter>

<enter>

 

[F1]

 

HTH

 

Sebastian

0 Likes