Rename suffix based on list value

Rename suffix based on list value

yann_fradier
Participant Participant
2,624 Views
28 Replies
Message 1 of 29

Rename suffix based on list value

yann_fradier
Participant
Participant

Hello everyone and thank you for your help,

I try to adapt the lsp evoked on the forum for my case but that exceeds my competence in this field ^^.

 

 

I would like an lsp to rename the suffixes of the layers according to the name of each of them

 

Example: 

 

Suffixe "02-SURFACE"

Layer list: [ZRAEX;ZEX;ZDECAP]

 

Example after lsp: 02-SURFACE-ZRAEX

 

Suffixe "03-CADASTRE"

Layer list: [PARCELLE]

 

Example after lsp: 03-CADASTRE-PARCELLE

 

Suffixe "04-topo"

Layer list: [BT,HT,POINT]

 

Example after lsp: 04-TOPO-BT

 

 

0 Likes
2,625 Views
28 Replies
Replies (28)
Message 2 of 29

ВeekeeCZ
Consultant
Consultant

Welcome to the forums!

 

First, post the lisp you're referring to. Also post a dwg with these layers to test.

Secondly, "renaming" is a 1 to 1 relation. So having a list of suffices, are you actually wanting to CREATE new layers according to that list?

0 Likes
Message 3 of 29

yann_fradier
Participant
Participant

Thanks for your help

 

Here is a dwg type for which I want depending on the type of entity to add a suffix as mentioned before

0 Likes
Message 4 of 29

yann_fradier
Participant
Participant

I don't want to create a new layer but rename the existing one

 

Here is the final result:

 

yann_fradier_0-1681983688114.png

 

0 Likes
Message 5 of 29

komondormrex
Mentor
Mentor

hey,

check this

 

 

 

(defun add_layer_prefix  (layer_list prefix)
	(vlax-map-collection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) 
		'(lambda (layer)
			(if (member (vla-get-name layer) layer_list)
				(vla-put-name layer (strcat prefix "-"(vla-get-name layer)))
			)
		)
	)
	(princ)
)

;to run type in command line (add_layer_prefix '("ZRAEX" "ZEX" "ZDECAP") "02-SURFACE") <Enter>

 

0 Likes
Message 6 of 29

yann_fradier
Participant
Participant

Thanks for coming back,
this lisp does not launch although I call it with APPLOAD

0 Likes
Message 7 of 29

komondormrex
Mentor
Mentor
Accepted solution

it is a function. should run typed in command line (add_layer_prefix '("ZRAEX" "ZEX" "ZDECAP") "02-SURFACE") <Enter>

Message 8 of 29

yann_fradier
Participant
Participant

The command line works perfectly! thank you for your help on my subject and the speed of response!
Best wishes 🙂

0 Likes
Message 9 of 29

yann_fradier
Participant
Participant
Hello everyone,
and thank you again for your help on this subject a few months ago.

I would like to reopen the subject because I would like to make a modification to this routine.

Would it be possible to modify it so that I can give it several dates and so that it renames the original layers as many times as there are dates? 

The previous lisp is very suitable but must be executed several times.

I wish that it can manage the renaming of layers in the same way but as many times as on different dates.
Expected result in attachment with the previous lisp executed several times at different dates ;)
Thank you in advance for your help !

 

0 Likes
Message 10 of 29

komondormrex
Mentor
Mentor

hello,

how do you want to pass various dates to the program?

0 Likes
Message 11 of 29

yann_fradier
Participant
Participant

by indicating a list of dates for example

0 Likes
Message 12 of 29

komondormrex
Mentor
Mentor

you mean like that

 

Suffixe "02-SURFACE"

Layer list: [ZRAEX;ZEX;ZDECAP]

Date: [11-17-2023] - one date, not a list

 

Example after lsp: 02-SURFACE-11-17-2023-ZRAEX

0 Likes
Message 13 of 29

yann_fradier
Participant
Participant

Exactly that could answer my request in this form

0 Likes
Message 14 of 29

komondormrex
Mentor
Mentor

check if that suits you

 

(defun add_layer_prefix  (layer_list prefix date / new_layer_name)
	(vlax-map-collection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) 
		'(lambda (layer)
				(if (and
				 		(member (vla-get-name layer) layer_list)
						(setq new_layer_name (strcat prefix "-" date "-" (vla-get-name layer)))
						(vl-catch-all-error-p 
						 	(vl-catch-all-apply 'vla-item (list (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
								  			    new_layer_name
										      )
							)
						)
				    )
						(vla-put-name layer new_layer_name)
						(if new_layer_name (alert (strcat "Layer \"" new_layer_name "\" already exists")))
					)
				)
			)
		(princ)
	)

;to run type in command line (add_layer_prefix '("ZRAEX" "ZEX" "ZDECAP") "02-SURFACE" "17-11-2023") <Enter>

 

0 Likes
Message 15 of 29

yann_fradier
Participant
Participant
Thanks for your help ! I can't use the script.
Would it be possible to provide me with the example with at least two dates?
I don't particularly want to use the command line but rather by offering a list of dates.
Each layer will therefore be duplicated as many times as the dates

 

0 Likes
Message 16 of 29

komondormrex
Mentor
Mentor

you mean not renaming a particular layer but creating a few with different dates from the list?

0 Likes
Message 17 of 29

yann_fradier
Participant
Participant
Exactly yes,
I would even go for something simpler in the sense that if I have 1 layer in my DWG I indicate 5 dates by a list the program creates a dataset of 5 layers renamed with these same dates in prefix

 

0 Likes
Message 18 of 29

komondormrex
Mentor
Mentor
Accepted solution

check this. same command line thou. 

 

(defun add_layer_prefix  (layer_list prefix date_list / new_layer_name layer_collection layer_name renamed)
	(vlax-map-collection (setq layer_collection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
		'(lambda (layer)
			(if (member (setq layer_name (vla-get-name layer)) layer_list)
				(progn
					(setq renamed nil)
					(foreach date date_list
						(if (and
								(setq new_layer_name (strcat prefix "-" date "-" layer_name))
								(vl-catch-all-error-p 
									(vl-catch-all-apply 'vla-item (list (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
												  						new_layer_name
													  			  )
									)
								)
							)
								(if (not renamed) 
									(setq renamed (not (vla-put-name layer new_layer_name)))
									(vla-add layer_collection new_layer_name)
								)
								(alert (strcat "Layer \"" new_layer_name "\" already exists"))
						)
			  		)
				)
		  	)
		)
	)
	(princ)
)

;to run type in command line (add_layer_prefix '("ZRAEX" "ZEX" "ZDECAP") "02-SURFACE" '("17-11-2023" "17-10-2023" "17-09-2023")) <Enter>

 

 

Message 19 of 29

yann_fradier
Participant
Participant

It's perfect, the program works wonderfully!
Second times you change my daily life XD

Thanks !!

0 Likes
Message 20 of 29

yann_fradier
Participant
Participant

Sorry but I have one more modification but by creating a new layer we do not keep the graphic properties of the source layer.
Do you have a solution to keep these settings?

See attached image (the layers created are all without style)

 

Thank you again for your help

0 Likes