Author Topic: Place prefix in front Layer name  (Read 10138 times)

0 Members and 1 Guest are viewing this topic.

Fabricio28

  • Swamp Rat
  • Posts: 670
Place prefix in front Layer name
« on: September 25, 2013, 02:25:04 PM »
Hi all,

I'm looking for a routine will place a Prefix (-) in front of a layers names by selected objects.
e.g
Layer Name: STREET
Layer Name: - STREET

Anybody Can help me please?

Kind Regards

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Place prefix in front Layer name
« Reply #1 on: September 25, 2013, 02:45:10 PM »
I have no idea where I got this.  I had to go to the Lisp Trunk in attic to get it because I really do not use it much more.
I think I got it from Cadalyst.  :|  It was probably authored by a Swamp Denizen anyway.  :lmao:

Code: [Select]
;;; LAYER-NAME-CHANGE.LISP
;;; CHANGES LAYER NAME BY ADDING A PREFIX OR/AND SUFFIX

(defun C:ChgLayName (/ acadDocument theLayers layName pre)
  (vl-load-com)
    (vl-cmdf ".undo" "begin")
  (setq p_fix (getstring "\nEnter Layer Prefix : ")
s_fix (getstring "\nEnter Layer Suffix : ")
  )
  (setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
  (setq theLayers (vla-get-layers acadDocument))
  (vlax-map-collection theLayers 'layer-mod)
  (vl-cmdf ".undo" "end")
  (princ)
) ;defun

(defun layer-mod (theLayer)
  (setq layName (vlax-get-property theLayer 'Name))
  (if (not (member layName '("0" "Defpoints" "XREF")))
    (vla-put-Name thelayer (strcat p_fix layName s_fix))
  ) ;if
) ;defun

FWIW:  I was just recently shown that Rename command can utilize wild cards to add or strip prefixes or suffixes from layer names.
Thanks TedG
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Place prefix in front Layer name
« Reply #2 on: September 25, 2013, 02:59:15 PM »
You could also use something like this:

Code: [Select]
(defun c:foo (/ e el l pre)
  (if (and (setq e (car (entsel))) (setq pre (getstring t "\nEnter prefix: ")))
    (progn (setq l (cdr (assoc 8 (entget e))))
      (setq el (entget (tblobjname "layer" l)))
      (if (wcmatch (strcase l) (strcat (strcase pre) "*"))
        (princ "\nLayer already has that prefix...")
        (entmod (subst (cons 2 (strcat pre l)) (assoc 2 el) el))
      )
    )
  )
  (princ)
)
« Last Edit: September 25, 2013, 03:15:23 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Place prefix in front Layer name
« Reply #3 on: September 25, 2013, 03:04:27 PM »
Thank you for the quick replay guys,

@Krushert
Thank for sharing the code, but isn't exacatly I was looking for.
Regards

You could also use something like this:

It is perfect, ronjonp!
Is it possible add multiple section in the code, please?

Best Regards



ronjonp

  • Needs a day job
  • Posts: 7527
Re: Place prefix in front Layer name
« Reply #4 on: September 25, 2013, 03:19:41 PM »
Here you go:
Code: [Select]
(defun c:foo (/ _ss->list e el l pre ss)
  (defun _ss->list (ss / n out)
    (repeat (setq n (sslength ss)) (setq out (cons (ssname ss (setq n (1- n))) out)))
  )
  (if (and (setq pre (getstring t "\nEnter prefix: ")) (setq ss (ssget)))
    (foreach e (_ss->list ss)
      (setq l (cdr (assoc 8 (entget e))))
      (setq el (entget (tblobjname "layer" l)))
      (if (wcmatch (strcase l) (strcat (strcase pre) "*"))
(princ "\nLayer already has that prefix...")
(entmod (subst (cons 2 (strcat pre l)) (assoc 2 el) el))
      )
    )
  )
  (princ)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Place prefix in front Layer name
« Reply #5 on: September 25, 2013, 03:36:44 PM »
You're awesome!!!
Thank you very much.

Fabricio

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Place prefix in front Layer name
« Reply #6 on: September 25, 2013, 08:45:40 PM »
Glad to help. :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Place prefix in front Layer name
« Reply #7 on: October 07, 2013, 07:55:54 AM »
I thought I did something wierd here. I used the FOO command; and the entity i selected was renamed to the new layer, but so did all the other objects on that layer. Am I doing something wrong?

I have layer 1 and select an object. Then would assign a prefix line demo_; and finally look like in the layer name as demo_1?

Thanks
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Place prefix in front Layer name
« Reply #8 on: October 07, 2013, 09:11:27 AM »
Sorry about that .. the routine above just renames the layer. If you want the object to be moved to the new layer use the code below.


Code: [Select]
(defun c:foo (/ _ss->list e el l pre ss)
  (defun _ss->list (ss / n out)
    (repeat (setq n (sslength ss)) (setq out (cons (ssname ss (setq n (1- n))) out)))
  )
  (if (and (setq pre (getstring t "\nEnter prefix: ")) (setq ss (ssget)))
    (progn (foreach e (_ss->list ss)
     (setq l (cdr (assoc 8 (entget e))))
     (setq el (entget (tblobjname "layer" l)))
     (if (wcmatch (strcase l) (strcat (strcase pre) "*"))
       (progn (princ "\nObject already has that layer prefix..."))
       (progn (if (not (tblobjname "layer" (strcat pre l)))
(entmakex (subst (cons 2 (strcat pre l)) (assoc 2 el) el))
      )
      (entmod (subst (cons 8 (strcat pre l)) (assoc 8 (entget e)) (entget e)))
       )
     )
   )
    )
  )
  (princ)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Place prefix in front Layer name
« Reply #9 on: October 07, 2013, 09:23:02 AM »
whoot! Again you dah man!
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Place prefix in front Layer name
« Reply #10 on: October 07, 2013, 09:35:31 AM »
 :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

engtechy

  • Guest
Re: Place prefix in front Layer name
« Reply #11 on: August 18, 2016, 04:44:48 PM »
I realize this is an old post. I know enough about lisp to be dangerous.... how would I modify that last routine to add a suffix instead of the prefix?

Thanks.

rkmcswain

  • Swamp Rat
  • Posts: 978
Re: Place prefix in front Layer name
« Reply #12 on: August 18, 2016, 04:55:59 PM »
I realize this is an old post. I know enough about lisp to be dangerous.... how would I modify that last routine to add a suffix instead of the prefix?


Try this:

Code: [Select]
(defun c:foo (/ _ss->list e el l pre ss)
  (defun _ss->list (ss / n out)
    (repeat (setq n (sslength ss)) (setq out (cons (ssname ss (setq n (1- n))) out)))
  )
  (if (and (setq pre (getstring t "\nEnter suffix: ")) (setq ss (ssget)))
    (foreach e (_ss->list ss)
      (setq l (cdr (assoc 8 (entget e))))
      (setq el (entget (tblobjname "layer" l)))
      (if (wcmatch (strcase l) (strcat "*" (strcase pre)))
(princ "\nLayer already has that suffix...")
(entmod (subst (cons 2 (strcat l pre)) (assoc 2 el) el))
      )
    )
  )
  (princ)
)

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Place prefix in front Layer name
« Reply #13 on: November 08, 2016, 01:43:08 PM »
Here you go:
Code: [Select]
(defun c:foo (/ _ss->list e el l pre ss)
  (defun _ss->list (ss / n out)
    (repeat (setq n (sslength ss)) (setq out (cons (ssname ss (setq n (1- n))) out)))
  )
  (if (and (setq pre (getstring t "\nEnter prefix: ")) (setq ss (ssget)))
    (foreach e (_ss->list ss)
      (setq l (cdr (assoc 8 (entget e))))
      (setq el (entget (tblobjname "layer" l)))
      (if (wcmatch (strcase l) (strcat (strcase pre) "*"))
(princ "\nLayer already has that prefix...")
(entmod (subst (cons 2 (strcat pre l)) (assoc 2 el) el))
      )
    )
  )
  (princ)
)


Hi guys,


That code is amazing, I've been using a lot ...

I'm using that code to add - as a prefix of my layers.

I'd like to make a little change at the code.
Remove - of all selected layers.


Thanks in advance

ribarm

  • Gator
  • Posts: 3265
  • Marko Ribar, architect
Re: Place prefix in front Layer name
« Reply #14 on: November 08, 2016, 01:59:35 PM »
Hi guys,


That code is amazing, I've been using a lot ...

I'm using that code to add - as a prefix of my layers.

I'd like to make a little change at the code.
Remove - of all selected layers.


Thanks in advance


If I understood correctly and if the code passed your tests... (untested)

Code: [Select]
(defun c:foo ( / _ss->list f-MR e el l ll ss )
  (defun _ss->list ( ss / n out )
    (repeat (setq n (sslength ss)) (setq out (cons (ssname ss (setq n (1- n))) out)))
  )
  (defun f-MR ( d s / d1 dl k ss c pl z l )
    (while (and (setq d1 (substr d 1 1)) (/= d1 ""))
      (setq d (substr d 2))
      (setq dl (cons d1 dl))
    )
    (foreach d1 dl
      (setq k -1 ss s)
      (while (and (setq c (substr ss 1 1)) (/= c ""))
        (setq ss (substr ss 2))
        (setq k (1+ k))
        (if (= c d1)
          (setq pl (cons k pl))
        )
      )
    )
    (if pl
      (progn
        (setq pl (vl-sort pl '<))
        (foreach p pl
          (if (null z)
            (setq z 1)
          )
          (setq l (cons (substr s z (1+ (- p z))) l))
          (setq z (+ p 2))
        )
        (setq l (cons (substr s z) l))
        (vl-remove "" (reverse l))
      )
      s
    )
  )
  (if (setq ss (ssget))
    (foreach e (_ss->list ss)
      (setq l (cdr (assoc 8 (entget e))))
      (setq el (entget (tblobjname "layer" l)))
      (if (vl-string-search "-" l)
        (progn
          (setq ll (f-MR "-" l))
          (setq ll (apply 'strcat ll))
          (entmod (subst (cons 2 ll) (assoc 2 el) el))
        )
      )
    )
  )
  (princ)
)

Regards...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Place prefix in front Layer name
« Reply #15 on: November 08, 2016, 02:23:01 PM »
Hi guys,


That code is amazing, I've been using a lot ...

I'm using that code to add - as a prefix of my layers.

I'd like to make a little change at the code.
Remove - of all selected layers.


Thanks in advance


If I understood correctly and if the code passed your tests... (untested)

Code: [Select]
(defun c:foo ( / _ss->list f-MR e el l ll ss )
  (defun _ss->list ( ss / n out )
    (repeat (setq n (sslength ss)) (setq out (cons (ssname ss (setq n (1- n))) out)))
  )
  (defun f-MR ( d s / d1 dl k ss c pl z l )
    (while (and (setq d1 (substr d 1 1)) (/= d1 ""))
      (setq d (substr d 2))
      (setq dl (cons d1 dl))
    )
    (foreach d1 dl
      (setq k -1 ss s)
      (while (and (setq c (substr ss 1 1)) (/= c ""))
        (setq ss (substr ss 2))
        (setq k (1+ k))
        (if (= c d1)
          (setq pl (cons k pl))
        )
      )
    )
    (if pl
      (progn
        (setq pl (vl-sort pl '<))
        (foreach p pl
          (if (null z)
            (setq z 1)
          )
          (setq l (cons (substr s z (1+ (- p z))) l))
          (setq z (+ p 2))
        )
        (setq l (cons (substr s z) l))
        (vl-remove "" (reverse l))
      )
      s
    )
  )
  (if (setq ss (ssget))
    (foreach e (_ss->list ss)
      (setq l (cdr (assoc 8 (entget e))))
      (setq el (entget (tblobjname "layer" l)))
      (if (vl-string-search "-" l)
        (progn
          (setq ll (f-MR "-" l))
          (setq ll (apply 'strcat ll))
          (entmod (subst (cons 2 ll) (assoc 2 el) el))
        )
      )
    )
  )
  (princ)
)

Regards...

Thank you very much!!!

Worked perfect!!!

Regards

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Place prefix in front Layer name
« Reply #16 on: November 08, 2016, 03:11:50 PM »
This request inspired me to create this new program: Layer Prefix/Suffix

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Place prefix in front Layer name
« Reply #17 on: November 09, 2016, 05:06:46 AM »
This request inspired me to create this new program: Layer Prefix/Suffix

Hi my old friend Lee Mac

Your web site is amazing!!

I'm happy with my request had inspired you create a new code.  :-D

Thank you very much!

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Place prefix in front Layer name
« Reply #18 on: November 09, 2016, 07:59:08 AM »
Thank you for your compliments for my site - I'm pleased that you like the new program.  :-)

Lee

M. Khateeb

  • Mosquito
  • Posts: 3
Re: Place prefix in front Layer name
« Reply #19 on: December 07, 2016, 11:24:17 AM »
Hello,
Can this, please, be modified to have a dialogue box with a list of the suffixes for each layer I pick. and away to add or remove them.
I attached a dialogue box to explain what I mean. (was modified by photo editor to match the situation I am explaining)
For example.
I have a layer named C1650.
and the "codes" or suffixes to be added or removed, are always numbers with "_" before each number, but could be anything for general use.
In a certain situation, the layer is C1650_0
When I pick the layer, the "0" should appear in the list of suffixes in the dialogue box, I can remove it by pressing "Del" or add another code, like 1;
then the layer should be renamed to
C1650_0_1

I appreciate any help.
Lee's code for layer prefix/suffix is great, Thank you very much for your efforts.
But I need a friendly way to do this since I have a lot of such cases....
Thank you
M.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Place prefix in front Layer name
« Reply #20 on: December 07, 2016, 12:17:42 PM »
suffixes to be added or removed, are always numbers with "_" before each number, but could be anything for general use.
If "anything" applies and the layername is "C1650_0" the list of suffixes must be:
Code: [Select]
1650_0
650_0
50_0
0_0
_0
0
?

M. Khateeb

  • Mosquito
  • Posts: 3
Re: Place prefix in front Layer name
« Reply #21 on: December 08, 2016, 01:58:48 AM »
The layer name is constant, the list of suffixes is managed.
For instance:

C1650_0_1_2_3    (list of suffixes : 0 1 2 3 )

When you remove 3, the result is C1650_0_1_2  and the list now : 0 1 2

What I meant by "anything" is the suffix could be anything like : C1650_XX_YY_ZZ  and when you remove ZZ, you stay with C1650_XX_YY

Thanks for your concern.
       

danallen

  • Guest
Re: Place prefix in front Layer name
« Reply #22 on: December 08, 2016, 04:42:30 PM »
M. Khateeb,

Sounds like this should be a new discussion topic since it is not adding prefixes.

Are you looking for renaming only one layer at a time? My pseudo code of my pseudo understanding of your request is:
run program
a) get layer name, or perhaps run on current layer
b) parse layer name into list of strings using "_" as a separator (see https://www.theswamp.org/index.php?topic=32845.0)
c) use dialog with check boxes, see image below, from doslib using the dos_checklist function. https://wiki.mcneel.com/doslib/home
d) recombine string based on selection, and rename layer

Dan
« Last Edit: December 12, 2016, 09:41:39 PM by dan allen »

GDF

  • Water Moccasin
  • Posts: 2081
Re: Place prefix in front Layer name
« Reply #23 on: December 08, 2016, 05:04:48 PM »
Nicely done LeeMac and Ronjonp
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Place prefix in front Layer name
« Reply #24 on: December 08, 2016, 05:07:47 PM »
Nicely done LeeMac and Ronjonp
Thanks  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

M. Khateeb

  • Mosquito
  • Posts: 3
Re: Place prefix in front Layer name
« Reply #25 on: December 11, 2016, 07:18:55 AM »
Sorry for putting it in the wrong thread, but I was led here while I was looking for prefix and suffix adding and removing.
The other thread is very nice. With a string parser, it would be perfect, combining all the work done and getting what I want.
Thanks a lot.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Place prefix in front Layer name
« Reply #26 on: December 11, 2016, 05:28:19 PM »
Nicely done LeeMac and Ronjonp

Thanks Gary.  :-)

danallen

  • Guest
Re: Place prefix in front Layer name
« Reply #27 on: December 12, 2016, 06:09:04 PM »
No problem, feel free to start a new thread and post your code draft if you need input/help.

-Dan

Sorry for putting it in the wrong thread, but I was led here while I was looking for prefix and suffix adding and removing.
The other thread is very nice. With a string parser, it would be perfect, combining all the work done and getting what I want.
Thanks a lot.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Place prefix in front Layer name
« Reply #28 on: December 12, 2016, 06:15:08 PM »
Hi Dan,
Just a quick question: where did you got that dialog from?
I mean I was about to ask in the DCL section is this thing possible: list_box with toggle buttons inside (just like the picture you uploaded).
Not sure if this is doable with pure DCL.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

danallen

  • Guest
Re: Place prefix in front Layer name
« Reply #29 on: December 12, 2016, 09:40:18 PM »
The dialog, specifically dos_checklist, is from doslib, https://wiki.mcneel.com/doslib/home, a free add-on that has been around for years, and hopefully many more. The pros here tend to create their own, I don't know if pure DCL can do it.


Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Place prefix in front Layer name
« Reply #30 on: December 13, 2016, 05:51:51 AM »
The dialog, specifically dos_checklist, is from doslib, https://wiki.mcneel.com/doslib/home, a free add-on that has been around for years, and hopefully many more. The pros here tend to create their own, I don't know if pure DCL can do it.
Thanks, Dan!
 :-)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg