Lisp to rename layer of a selected object

Lisp to rename layer of a selected object

a.tawkXHKN6
Contributor Contributor
1,549 Views
11 Replies
Message 1 of 12

Lisp to rename layer of a selected object

a.tawkXHKN6
Contributor
Contributor

I need a lisp to rename the layer of a selected object (block, polyline, line...), and keeping all the original layer format (e.g: color, linetype...)

0 Likes
Accepted solutions (1)
1,550 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant
Accepted solution

Something like this?

 

(defun C:RSOL ; = Rename Selected Object's Layer

  (command "_.rename" "_layer"

    (cdr (assoc 8 (entget (car (entsel "\nSelect object to Rename its Layer: ")))))

  ); command

); defun

 

Untested, but that should leave you in the command to give it the new name.  It could be enhanced to ask again if you miss in picking, and so on.

Kent Cooper, AIA
0 Likes
Message 3 of 12

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

....  It could be enhanced to ask again if you miss in picking, and so on.


Another thing to be aware of:  I find that RENAME can be used on a locked Layer.  If, for whatever reason you have locked a given Layer, you would not  want it to be Renamed, the routine could be adjusted to refuse to do that, and if you like, to tell you about it, or if you prefer, ask you whether or not  you want to Rename it.

Kent Cooper, AIA
0 Likes
Message 4 of 12

_Tharwat
Advisor
Advisor

Hint: layer name "0" can not be renamed so the routine will exit with error.

Message 5 of 12

Kent1Cooper
Consultant
Consultant

@_Tharwat wrote:

Hint: layer name "0" can not be renamed so the routine will exit with error.


That would be "0" [the numeral, not the letter], but yes.  I find, unexpectedly, that the DEFPOINTS Layer can  be Renamed, which I can only imagine could cause problems -- if I then draw another Dimension, it creates DEFPOINTS again, so there can be more than one Layer with Dimension DEFinition POINTS on it.  And the Renamed one can then be set to Plot.

 

So if there's any likelihood that the User would select something on either of those two Layers, the routine can be amended to forbid that.

Kent Cooper, AIA
0 Likes
Message 6 of 12

_Tharwat
Advisor
Advisor

@Kent1Cooper wrote:

@_Tharwat wrote:

Hint: layer name "0" can not be renamed so the routine will exit with error.


That would be "0" [the numeral, not the letter],


Seriously ?

How did you know that it is a letter and not the numeral ?

Copy my comment to your Vslide then remove the double quotes to know where your mistake is.

0 Likes
Message 7 of 12

Kent1Cooper
Consultant
Consultant

I assumed because it looks just like a lower-case "o".  You might want to consider changing fonts to one that isn't misleading in that way.

Kent Cooper, AIA
0 Likes
Message 8 of 12

_Tharwat
Advisor
Advisor

If the reader can't differentiate between letters and digits then that is their problem.

The font style that I use is very clear and comfortable for reading.

0 Likes
Message 9 of 12

margonoC4PXL
Explorer
Explorer

; error: syntax error.
When I load this Lisp showing " (LOAD "C:/Users/margono/Downloads/RSOL.lsp") ; error: syntax error " any idea why this happen Sir? 
I use autocad 2025.

0 Likes
Message 10 of 12

BlackBox_
Advisor
Advisor

Another way of doing this, which accounts for layers 0 (zero), Defpoints, among other things:

 

https://apps.autodesk.com/ACD/en/Detail/Index?id=3752103605651655897&appLang=en&os=Win32_64

 

Cheers

 


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

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 11 of 12

Kent1Cooper
Consultant
Consultant

@margonoC4PXL wrote:

; error: syntax error.
 ....any idea why this happen Sir? 


As I said, it was untested.  It was missing the parentheses for the localized variables list, which are needed even if there's nothing in them:
(defun C:RSOL (); = Rename Selected Object's Layer
  (command "_.rename" "_layer"
    (cdr (assoc 8 (entget (car (entsel "\nSelect object to Rename its Layer: ")))))
  ); command
)

I have to assume the OP [who marked it an accepted solution, so they must have gotten it to work] supplied those for themselves.

Kent Cooper, AIA
Message 12 of 12

margonoC4PXL
Explorer
Explorer

Ok, thank you sir already work.
I thought I would just add the name from original but this requires creating the new name.

 

0 Likes