Author Topic: Rename layer and add suffix  (Read 4555 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
Rename layer and add suffix
« on: February 04, 2020, 03:03:48 PM »
Lee Mac assisted me with creating a layer from another and adding the suffix -56.

So my layers are as such
S-BF = Record issue layer, Bylayer color
S-BF-NEW = New Construction Layer, color RED
S-BF-FUTURE = Future Construction Layer, Color 15
S-BF-56 = Secondary Revision/construction Layer, color 56

So the progression would be...
if objects are on layer S-BF-NEW, or any layer were "-NEW" is is the suffix
based on user selected button move objects to the layer associated with that button... Could be "FUTURE" , "56" or Record issue layer


As it stands now the code below will create the desired layers but it keeps the "-N" as part of the name, resulting in "S-BF-N-56" rather than S-BF-56.

It also does find the entities on the old layer but does not put them on the newly created layer.

Code - Auto/Visual Lisp: [Select]
  1.  (defun c:foo ( / e i l n s x )
  2.  (defun *error* (msg)
  3.     (if osm
  4.       (setvar 'osmode osm)
  5.     )
  6.     (if (not (member msg '("Function cancelled" "quit / exit abort")))
  7.       (princ (strcat "\nError: " msg))
  8.     )
  9.     (princ)
  10.   )
  11.   (if (setq s (ssget "_x" '((8 . "*-NEW") (62 . 256))))
  12.        (repeat (setq i (sslength s))
  13.            (setq x (entget (ssname s (setq i (1- i))))
  14.                  l (assoc 8 x)
  15.                  n (strcat (cdr l) "-56");;I'm pretty sure the naming issue is here. Have tried cadr and various others. Maybe it's a subst "-56" for "NEW" ???
  16.            )
  17.            (if (not (tblsearch "layer" n))
  18.                (progn
  19.                    (setq e (entget (tblobjname "layer" (cdr l))))
  20.                                             (entmake (subst (cons 2 n) (assoc 2 e) (subst '(62 . 56) (assoc 62 e) e)))
  21.                                 )
  22.            )
  23.            (entmod (subst (cons 8 n) 1 x))
  24.          
  25.        )
  26.    )
  27.     (prompt "\nAll objects have been moved to secondary construction layer, Color 56")
  28.  (princ)
  29. )
 
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

Dlanor

  • Bull Frog
  • Posts: 263
Re: Rename layer and add suffix
« Reply #1 on: February 04, 2020, 05:12:09 PM »
Try this. It will replace "-NEW" with "-56" or "NEW" with "56" (the second uses the or in the comments). I also changed the previous line so that l is now the layer name instead of the dotted pair.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo ( / e i l n s x )
  2.   (defun *error* (msg)
  3.     (if osm (setvar 'osmode osm))
  4.     (if (not (member msg '("Function cancelled" "quit / exit abort"))) (princ (strcat "\nError: " msg)))
  5.     (princ)
  6.   )
  7.   (if (setq s (ssget "_x" '((8 . "*-NEW") (62 . 256))))
  8.     (repeat (setq i (sslength s))
  9.       (setq x (entget (ssname s (setq i (1- i))))
  10.             l (cdr (assoc 8 x))
  11.             n (strcat (substr l 1 (- (strlen l) 4)) "-56") ;; or (strcat (substr l 1 (- (strlen l) 3)) "56")
  12.       )
  13.       (if (not (tblsearch "layer" n))
  14.         (progn
  15.           (setq e (entget (tblobjname "layer" (cdr l))))
  16.           (entmake (subst (cons 2 n) (assoc 2 e) (subst '(62 . 56) (assoc 62 e) e)))
  17.         )
  18.       )
  19.       (entmod (subst (cons 8 n) 1 x))
  20.     )
  21.   )
  22.   (prompt "\nAll objects have been moved to secondary construction layer, Color 56")
  23. )
  24.  

(subst) only works on lists, not strings

jlogan02

  • Bull Frog
  • Posts: 327
Re: Rename layer and add suffix
« Reply #2 on: February 04, 2020, 07:30:05 PM »
Thanks for taking a shot at it Dlanor.
It works for the current layer only. Will not work on multiple RED objects on multiple layers with the "*-New" suffix. And only gets a positive result at the console, not in AutoCad.

When I say positive results at the console I mean the
Code - Auto/Visual Lisp: [Select]
will return the number of entities that are part of the selection set. But the
Code - Auto/Visual Lisp: [Select]
  1. (setq x (entget...
only changes the name of the current layer that meets the selection set criteria.

Still have the issue of nothing moving to the -56 layer. But, why would they move when there isn't actually a layer to move to?

I get your approach using Substr and Strlen. Had to do a little reading for it to makes sense to my wee brain. Well mostly. I played around enough with changing the integers to see I get different results. Obviously I could. For my part I'm focusing on that part as it's obviously where the naming and layer creation issue is. I'm still so new to it all that progress is minimal.

I'll worry about the objects getting put on the "*-56" layer once I figure out the above.


J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

Dlanor

  • Bull Frog
  • Posts: 263
Re: Rename layer and add suffix
« Reply #3 on: February 04, 2020, 08:23:03 PM »
Sorry, I missed some bits later in the code. Try this

Code - Auto/Visual Lisp: [Select]
  1.     (defun c:foo ( / e i l n s x )
  2.       (defun *error* (msg)
  3.         (if osm (setvar 'osmode osm))
  4.         (if (not (member msg '("Function cancelled" "quit / exit abort"))) (princ (strcat "\nError: " msg)))
  5.         (princ)
  6.       )
  7.       (if (setq s (ssget "_x" '((8 . "*-NEW") (62 . 256))))
  8.         (repeat (setq i (sslength s))
  9.           (setq x (entget (ssname s (setq i (1- i))))
  10.                 l (cdr (assoc 8 x))
  11.                 n (strcat (substr l 1 (- (strlen l) 4)) "-56") ;; or (strcat (substr l 1 (- (strlen l) 3)) "56")
  12.           )
  13.           (if (not (tblsearch "layer" n))
  14.             (progn
  15.               (setq e (entget (tblobjname "layer" l)))
  16.               (entmake (subst (cons 2 n) (assoc 2 e) (subst '(62 . 56) (assoc 62 e) e)))
  17.             )
  18.           )
  19.           (entmod (subst (cons 8 n) (assoc 8 x) x))
  20.         )
  21.       )
  22.       (prompt "\nAll objects have been moved to secondary construction layer, Color 56")
  23.     (princ)
  24.     )
  25.  
  26.  

jlogan02

  • Bull Frog
  • Posts: 327
Re: Rename layer and add suffix
« Reply #4 on: February 05, 2020, 11:57:49 AM »
That's the ticket! Thanks Dlanor.

Remind me how this last line reads again. I never can remember the second x and it's function.

Code - Auto/Visual Lisp: [Select]
  1. (entmod (subst (cons 8 n) (assoc 8 x) x))
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

JohnK

  • Administrator
  • Seagull
  • Posts: 10627
Re: Rename layer and add suffix
« Reply #5 on: February 05, 2020, 02:16:43 PM »
That's the ticket! Thanks Dlanor.

Remind me how this last line reads again. I never can remember the second x and it's function.

Code - Auto/Visual Lisp: [Select]
  1. (entmod (subst (cons 8 n) (assoc 8 x) x))

Click on the SUBST function in your post and you will see.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

jlogan02

  • Bull Frog
  • Posts: 327
Re: Rename layer and add suffix
« Reply #6 on: February 05, 2020, 05:18:02 PM »
Check my thinking here...

The progression of layers is
S-* = Record issue layer, Bylayer color
S-*-NEW = New Construction Layer, color RED
S-*-FUTURE = Future Construction Layer, Color 15
S-*-56 = Secondary Revision/construction Layer, color 56

So the progression would be...
if objects are on layer S-BF-NEW, or any layer were "-NEW" is the suffix
based on user selected button move objects to the layer associated with that button... Could be "FUTURE" , "56" or Record issue layer

Am I thinking correctly that in order to move back to the record issue layer, "S-*" from any of the other layers, I would need to use something like a
Code - Auto/Visual Lisp: [Select]
in place of
Code - Auto/Visual Lisp: [Select]
  1. (setq x (entget (ssname s (setq i (1- i))))
  2.                 l (cdr (assoc 8 x))
  3.                 n (strcat (substr l 1 (- (strlen l) 4)) " ")

I haven't had any success strip the "-56" Or "New" suffix and sending objects back to their record issue state with
Code - Auto/Visual Lisp: [Select]
  1. (substr...

The closest I get is S-BF- if my layer is S-BF-NEW. Can't seem to eliminate that last dash.

Code - Auto/Visual Lisp: [Select]
  1.  (setq x (entget (ssname s (setq i (1- i))))
  2.                 l (cdr (assoc 8 x))
  3.                 n (strcat (substr l 1 (- (strlen l) 3)) "")

Code - Auto/Visual Lisp: [Select]
  1.   (defun c:NEWToBy ( / e i l n s x )
  2.       (defun *error* (msg)
  3.         (if osm (setvar 'osmode osm))
  4.         (if (not (member msg '("Function cancelled" "quit / exit abort"))) (princ (strcat "\nError: " msg)))
  5.         (princ)
  6.       )
  7.       (if (setq s (ssget "_x" '((8 . "*-NEW") (62 . 256))))
  8.         (repeat (setq i (sslength s))
  9.           (setq x (entget (ssname s (setq i (1- i))))
  10.                 l (cdr (assoc 8 x))
  11.                 n (strcat (substr l 1 (- (strlen l) 3)) "")
  12.           )
  13.           (if (not (tblsearch "layer" n))
  14.             (progn
  15.               (setq e (entget (tblobjname "layer" l)))
  16.               (entmake (subst (cons 2 n) (assoc 2 e) (subst '(62 . 256) (assoc 62 e) e)))
  17.             )
  18.           )
  19.           (entmod (subst (cons 8 n) (assoc 8 x) x))
  20.         )
  21.       )
  22.       (prompt "\nAll objects have been moved to secondary construction layer, Bylayer")
  23.     (princ)
  24.     )
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

kumarip

  • Mosquito
  • Posts: 1
Re: Rename layer and add suffix
« Reply #7 on: February 06, 2020, 12:30:07 AM »
Good Information.

Dlanor

  • Bull Frog
  • Posts: 263
Re: Rename layer and add suffix
« Reply #8 on: February 06, 2020, 03:06:25 AM »
Check my thinking here...

The progression of layers is
S-* = Record issue layer, Bylayer color
S-*-NEW = New Construction Layer, color RED
S-*-FUTURE = Future Construction Layer, Color 15
S-*-56 = Secondary Revision/construction Layer, color 56

This is OK for objects but will throw an error with layers as you cannot set a layer colour to bylayer, and the code involves creating layers

So the progression would be...
if objects are on layer S-BF-NEW, or any layer were "-NEW" is the suffix
based on user selected button move objects to the layer associated with that button... Could be "FUTURE" , "56" or Record issue layer

Am I thinking correctly that in order to move back to the record issue layer, "S-*" from any of the other layers, I would need to use something like a
Code - Auto/Visual Lisp: [Select]
in place of
Code - Auto/Visual Lisp: [Select]
  1. (setq x (entget (ssname s (setq i (1- i))))
  2.                 l (cdr (assoc 8 x))
  3.                 n (strcat (substr l 1 (- (strlen l) 4)) " ")

I haven't had any success strip the "-56" Or "New" suffix and sending objects back to their record issue state with
Code - Auto/Visual Lisp: [Select]
  1. (substr...

The closest I get is S-BF- if my layer is S-BF-NEW. Can't seem to eliminate that last dash.

Code - Auto/Visual Lisp: [Select]
  1.  (setq x (entget (ssname s (setq i (1- i))))
  2.                 l (cdr (assoc 8 x))
  3.                 n (strcat (substr l 1 (- (strlen l) 3)) "")


If suffix is "-NEW" and

Code - Auto/Visual Lisp: [Select]
  1. (setq n (strcat (substr lyrname 1 (- (strlen lyrname) 4)) "-56"))

replaces "-NEW" with "-56" then

Code - Auto/Visual Lisp: [Select]
  1. (setq n (substr lyrname 1 (- (strlen lyrname)  (strlen old_suffix)))

Will get you the base layer name and

Code - Auto/Visual Lisp: [Select]
  1. (setq n (strcat (substr lyrname 1 (- (strlen lyrname) (strlen old_suffix)) new_suffix))

will replace the old suffix with the new suffix. To reset to base layer name the new_suffix should be a zero length string (""). I think (strlen "") will return zero, if moving from base to a suffix, but i'm on LT at the moment so can't test. Tested and works

« Last Edit: February 06, 2020, 07:00:18 AM by Dlanor »

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Rename layer and add suffix
« Reply #9 on: February 06, 2020, 08:46:16 AM »
FWIW the *error* function is not required either AND it should be localized!
Code - Auto/Visual Lisp: [Select]
  1. (defun c:newtoby (/ *error* e i l n s x)
  2. ;;;  (defun *error* (msg)
  3. ;;;    (if      osm
  4. ;;;      (setvar 'osmode osm)
  5. ;;;    )
  6. ;;;    (if      (not (member msg '("Function cancelled" "quit / exit abort")))
  7. ;;;      (princ (strcat "\nError: " msg))
  8. ;;;    )
  9. ;;;    (princ)
  10. ;;;  )
  11.   (if (setq s (ssget "_x" '((8 . "*-NEW") (62 . 256))))
  12.     (repeat (setq i (sslength s))
  13.       (setq x (entget (ssname s (setq i (1- i))))
  14.             l (cdr (assoc 8 x))
  15.             n (strcat (substr l 1 (- (strlen l) 3)) "")
  16.       )
  17.       (if (not (tblsearch "layer" n))
  18.         (progn (setq e (entget (tblobjname "layer" l)))
  19.                (entmake (subst (cons 2 n) (assoc 2 e) (subst '(62 . 256) (assoc 62 e) e)))
  20.         )
  21.       )
  22.       (entmod (subst (cons 8 n) (assoc 8 x) x))
  23.     )
  24.   )
  25.   (prompt "\nAll objects have been moved to secondary construction layer, Bylayer")
  26.   (princ)
  27. )

Here is another way to remove the suffix:
Code - Auto/Visual Lisp: [Select]
  1. (setq lname "S-YourLayerName-NEW")
  2. ;; Remove -NEW note .. will remove -NEW-NEW-NEW at the end as well
  3. (vl-string-right-trim "-NEW" lname)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

jlogan02

  • Bull Frog
  • Posts: 327
Re: Rename layer and add suffix
« Reply #10 on: February 06, 2020, 11:41:28 AM »
Exactly!!! That was the other issue I was seeing.

This is currently how I do it with a block library I've built.

  • Block drawn on 0 Layer/color white
  • When inserted it automatically goes to the "NEW" layer
  • User selects the button they need to move the object to "-56" or "Record Issue" Layers

This is for department who already have built all of their "Standard" layers into their template and they don't want to automate the process
for literally 100s of layers (well...think 100 hundred record issue layers).

They have 3 layers for each Record Issue layer.
New
56
Future

They literally have all of these layers already in the template with no filtering, customization, or anything. Can you say scroll scroll scroll your way?

They would rather require their users do it all manually. I'm trying to provide an example based on their current setup to convince them that automation saves them time and money. They either refuse to see it or as I suspect have, "My Way or the Highway" mentality. I've been cadding for 30 years and have never hear of anyone saying they want to do it manually. My approach has always been, minimize the number of layers in the layer dialog. The layers that are actively in the drawing should be the only layers in the layer dialog.

It's like talking to myself. I've convinced myself that my approach with this code is wrong and that I need to make them understand the benefits of automating their use of layers.


This is OK for objects but will throw an error with layers as you cannot set a layer colour to bylayer, and the code involves creating layers



If suffix is "-NEW" and

Code - Auto/Visual Lisp: [Select]
  1. (setq n (strcat (substr lyrname 1 (- (strlen lyrname) 4)) "-56"))

replaces "-NEW" with "-56" then

Code - Auto/Visual Lisp: [Select]
  1. (setq n (substr lyrname 1 (- (strlen lyrname)  (strlen old_suffix)))

Will get you the base layer name and

Code - Auto/Visual Lisp: [Select]
  1. (setq n (strcat (substr lyrname 1 (- (strlen lyrname) (strlen old_suffix)) new_suffix))

will replace the old suffix with the new suffix. To reset to base layer name the new_suffix should be a zero length string (""). I think (strlen "") will return zero, if moving from base to a suffix, but i'm on LT at the moment so can't test. Tested and works
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

jlogan02

  • Bull Frog
  • Posts: 327
Re: Rename layer and add suffix
« Reply #11 on: February 06, 2020, 12:00:51 PM »
FWIW the *error* function is not required either AND it should be localized!

Here is another way to remove the suffix:
Code - Auto/Visual Lisp: [Select]
  1. (setq lname "S-YourLayerName-NEW")
  2. ;; Remove -NEW note .. will remove -NEW-NEW-NEW at the end as well
  3. (vl-string-right-trim "-NEW" lname)

Hey...I was looking at

Code - Auto/Visual Lisp: [Select]

this right before the end of the day yesterday.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Rename layer and add suffix
« Reply #12 on: February 06, 2020, 01:07:40 PM »
Here is another way to remove the suffix:
Code - Auto/Visual Lisp: [Select]
  1. (setq lname "S-YourLayerName-NEW")
  2. ;; Remove -NEW note .. will remove -NEW-NEW-NEW at the end as well
  3. (vl-string-right-trim "-NEW" lname)

Be careful with that approach Ron -
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-string-right-trim "-NEW" "S-YOURLAYERNAME-NEW")
  2. "S-YOURLAYERNAM"

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Rename layer and add suffix
« Reply #13 on: February 06, 2020, 02:48:57 PM »
Here is another way to remove the suffix:
Code - Auto/Visual Lisp: [Select]
  1. (setq lname "S-YourLayerName-NEW")
  2. ;; Remove -NEW note .. will remove -NEW-NEW-NEW at the end as well
  3. (vl-string-right-trim "-NEW" lname)

Be careful with that approach Ron -
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-string-right-trim "-NEW" "S-YOURLAYERNAME-NEW")
  2. "S-YOURLAYERNAM"

That's right .. knew there was something quirky  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

jlogan02

  • Bull Frog
  • Posts: 327
Re: Rename layer and add suffix
« Reply #14 on: February 10, 2020, 11:23:23 AM »
I got this one to work.
Code - Auto/Visual Lisp: [Select]
  1. (setq n (strcat (substr lyrname 1 (- (strlen lyrname) 4)) "-56"))

The other two it's kicking a bad arguement type: strigp nil
Code - Auto/Visual Lisp: [Select]
  1. (defun c:str56toBy ( / i lyrname n s x )
  2. (if (setq s (ssget "_X" '((8 . "*-56"))))
  3.         (repeat (setq i (sslength s))
  4.         (setq x (entget (ssname s (setq i (1- i))))
  5.               lyrname (cdr (assoc 8 x))
  6.                                   n (substr lyrname 1 (- (strlen lyrname)  (strlen old_suffix))
  7.                                 )
  8.             (if (not (tblsearch "layer" n))
  9.         (progn (setq e (entget (tblobjname "layer" lyrname)))
  10.                (entmake (subst (cons 2 n) (assoc 2 e) (subst '(62 . 256) (assoc 62 e) e)))
  11.         )
  12.       )
  13.       (entmod (subst (cons 8 n) (assoc 8 x) x))
  14.     )
  15.   )
  16.   (prompt "\nAll objects have been moved to secondary construction layer, Bylayer")
  17.   (princ)
  18. )

tried to create a
Code - Auto/Visual Lisp: [Select]
  1. (setq old_suffix (assoc 8 layername))..
and many other combos. No luck.

Or is it that I'm missing something later in the code?
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Rename layer and add suffix
« Reply #15 on: February 10, 2020, 12:53:17 PM »
That code you posted is formatted incorrectly and missing a paren. Also 'old_suffix' is never defined which is why you're getting the stringp nil error. And when you create the new layer with the old layer properties no need to try and set the layer color to 256?

Here's a quick stab at what I think you're trying to accomplish.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:str56toby (/ e i lyrname n old_suffix s x)
  2.   (setq old_suffix "-56")
  3.   (if (setq s (ssget "_X" (list (cons 8 (strcat "*" old_suffix)))))
  4.     (repeat (setq i (sslength s))
  5.       (setq x       (entget (ssname s (setq i (1- i))))
  6.             lyrname (cdr (assoc 8 x))
  7.             n       (substr lyrname 1 (- (strlen lyrname) (strlen old_suffix)))
  8.       )
  9.       (if (not (tblsearch "layer" n))
  10.         (progn (setq e (entget (tblobjname "layer" lyrname)))
  11.                (entmake (subst (cons 2 n) (assoc 2 e) e))
  12.         )
  13.       )
  14.       ;; (entmod (subst (cons 8 n) (assoc 8 x) x))
  15.       ;; Put on new layer then force to color bylayer
  16.       (entmod (append x (list (cons 8 n)) '((62 . 256))))
  17.     )
  18.     (prompt "\nAll objects have been moved to secondary construction layer, Bylayer")
  19.   )
  20.   (princ)
  21. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

jlogan02

  • Bull Frog
  • Posts: 327
Re: Rename layer and add suffix
« Reply #16 on: February 11, 2020, 10:48:23 AM »
Thanks Ron,

Made some minor tweaks for two additional layer control routines. Now I have a suite of layer controls!!

Thanks for all the help folks.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Rename layer and add suffix
« Reply #17 on: February 11, 2020, 02:49:38 PM »
Thanks Ron,

Made some minor tweaks for two additional layer control routines. Now I have a suite of layer controls!!

Thanks for all the help folks.
Glad to help  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC