TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Fabricio28 on September 25, 2013, 02:25:04 PM

Title: Place prefix in front Layer name
Post by: Fabricio28 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
Title: Re: Place prefix in front Layer name
Post by: Krushert 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
Title: Re: Place prefix in front Layer name
Post by: ronjonp 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)
)
Title: Re: Place prefix in front Layer name
Post by: Fabricio28 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


Title: Re: Place prefix in front Layer name
Post by: ronjonp 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)
)
Title: Re: Place prefix in front Layer name
Post by: Fabricio28 on September 25, 2013, 03:36:44 PM
You're awesome!!!
Thank you very much.

Fabricio
Title: Re: Place prefix in front Layer name
Post by: ronjonp on September 25, 2013, 08:45:40 PM
Glad to help. :)
Title: Re: Place prefix in front Layer name
Post by: MSTG007 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
Title: Re: Place prefix in front Layer name
Post by: ronjonp 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)
)
Title: Re: Place prefix in front Layer name
Post by: MSTG007 on October 07, 2013, 09:23:02 AM
whoot! Again you dah man!
Title: Re: Place prefix in front Layer name
Post by: ronjonp on October 07, 2013, 09:35:31 AM
 :)
Title: Re: Place prefix in front Layer name
Post by: engtechy 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.
Title: Re: Place prefix in front Layer name
Post by: rkmcswain 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)
)
Title: Re: Place prefix in front Layer name
Post by: Fabricio28 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
Title: Re: Place prefix in front Layer name
Post by: ribarm 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...
Title: Re: Place prefix in front Layer name
Post by: Fabricio28 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
Title: Re: Place prefix in front Layer name
Post by: Lee Mac on November 08, 2016, 03:11:50 PM
This request inspired me to create this new program: Layer Prefix/Suffix (http://lee-mac.com/pslay.html)
Title: Re: Place prefix in front Layer name
Post by: Fabricio28 on November 09, 2016, 05:06:46 AM
This request inspired me to create this new program: Layer Prefix/Suffix (http://lee-mac.com/pslay.html)

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!
Title: Re: Place prefix in front Layer name
Post by: Lee Mac 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
Title: Re: Place prefix in front Layer name
Post by: M. Khateeb 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.
Title: Re: Place prefix in front Layer name
Post by: roy_043 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
?
Title: Re: Place prefix in front Layer name
Post by: M. Khateeb 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.
       
Title: Re: Place prefix in front Layer name
Post by: danallen 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 (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 (https://wiki.mcneel.com/doslib/home)
d) recombine string based on selection, and rename layer

Dan
Title: Re: Place prefix in front Layer name
Post by: GDF on December 08, 2016, 05:04:48 PM
Nicely done LeeMac and Ronjonp
Title: Re: Place prefix in front Layer name
Post by: ronjonp on December 08, 2016, 05:07:47 PM
Nicely done LeeMac and Ronjonp
Thanks  :)
Title: Re: Place prefix in front Layer name
Post by: M. Khateeb 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.
Title: Re: Place prefix in front Layer name
Post by: Lee Mac on December 11, 2016, 05:28:19 PM
Nicely done LeeMac and Ronjonp

Thanks Gary.  :-)
Title: Re: Place prefix in front Layer name
Post by: danallen 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.
Title: Re: Place prefix in front Layer name
Post by: Grrr1337 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.
Title: Re: Place prefix in front Layer name
Post by: danallen on December 12, 2016, 09:40:18 PM
The dialog, specifically dos_checklist, is from doslib, https://wiki.mcneel.com/doslib/home (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.

Title: Re: Place prefix in front Layer name
Post by: Grrr1337 on December 13, 2016, 05:51:51 AM
The dialog, specifically dos_checklist, is from doslib, https://wiki.mcneel.com/doslib/home (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!
 :-)