Rename Multiple Blocks

Rename Multiple Blocks

anderson51
Advocate Advocate
15,105 Views
36 Replies
Message 1 of 37

Rename Multiple Blocks

anderson51
Advocate
Advocate

Is there lisp routine that will allow you to rename multiple blocks at once?

 

 

Thanks,

Anderson51

0 Likes
Accepted solutions (1)
15,106 Views
36 Replies
Replies (36)
Message 2 of 37

CodeDing
Advisor
Advisor

@anderson51 ,

 

There sure is:

(defun c:RMB ( / lstBlocks)
  ;Rename Multiple Blocks
  ;Prep
  (setq lstBlocks '(
    ;("old_name" "new_name")
    ("Block A" "Block 1")
    ("BLOCK B" "Block 2")
  ))
  ;Begin work
  (setvar 'CMDECHO 0)
  (foreach blk lstBlocks
    (command "-RENAME" "b" (car blk) (cadr blk))
  );foreach
  (setvar 'CMDECHO 1)
  ;Finish up
  (prompt "\nBlock Re-Naming Complete.")
  (princ)
);defun
Message 3 of 37

JTBWorld
Advisor
Advisor

You may find these code snippets useful https://jtbworld.com/autocad-axblock-lsp and if you find you need further help feel free to use the contact page. JTB Batch Change is another option. 


Jimmy Bergmark
JTB World - Software development and consulting for CAD and license usage reports
https://jtbworld.com

Message 4 of 37

anderson51
Advocate
Advocate

Is there a way to have it pull up in list form, similar to how the rename command works?

 

Block Rename.png

0 Likes
Message 5 of 37

JTBWorld
Advisor
Advisor

Yes it can be done. Either learn to program, hope someone create it for free or pay to get it done. I can help but not for free. 


Jimmy Bergmark
JTB World - Software development and consulting for CAD and license usage reports
https://jtbworld.com

0 Likes
Message 6 of 37

anderson51
Advocate
Advocate

Have a nice day Jimmy!

0 Likes
Message 7 of 37

CodeDing
Advisor
Advisor

@anderson51 ,

 

I don't understand. If you want to rename them via a form, then why not just use the original RENAME command?

I can not think of any effective mechanic / workflow to rename multiple blocks at once via a form that would be more-efficient than the original command..

 

Can you describe what you have in mind that would be faster?

 

Because, we can easily create a form to select multiple blocks at once.. but then what.. loop through each one asking for a new name? that would be essentially the same as the original command.

 

Best,

~DD

0 Likes
Message 8 of 37

hak_vz
Advisor
Advisor
(defun c:renmblocks nil (renmblocks))
(defun renmblocks ( / get-blocks getallblocks nonincluded blocklist dont oldblock newname)

	(defun getblocks (/ adoc name lst)
	  (vlax-for block (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
		(if (and  (equal (vla-get-IsLayout block) :vlax-false)
				  (equal (vla-get-IsXref block) :vlax-false)
				  (/= (substr (vla-get-Name block) 1 1) "*")) 
			 (setq lst (cons (vla-get-Name block) lst))
		  ) 
		) 
	  lst
	)
	(defun getallblocks ( / lst)(vlax-for block (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))(setq lst (cons (vla-get-Name block) lst))) lst)
	(defun nonincluded(a b) (if a (if (not(member (car a) b)) (cons (car a) (nonincluded (cdr a) b)) (nonincluded (cdr a)b) ) ) )
	(setq blocklist (getblocks))
	(setq dont (nonincluded (getallblocks) blocklist))
	(foreach bname blocklist
		(initget 1)
		(setq newname (getstring (strcat"\nNew name for block <" bname ">:" )))
		(setq oldblock (vla-item(vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) bname))
		(if 
			(and
				(> (strlen newname) 0)
				(not (member newname dont))
			)
			(vlax-put oldblock 'name newname)
		)
	)
(princ "\nDone")
)
(princ)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 9 of 37

pbejse
Mentor
Mentor

@JTBWorld wrote:

You may find these code snippets useful https://jtbworld.com/autocad-axblock-lsp 


 

 

;;; By Jimmy Bergmark
;;; Copyright (C) 1997-2016 JTB World Inc., All Rights Reserved

 

Thats is a classic 😊

0 Likes
Message 10 of 37

CodeDing
Advisor
Advisor

@hak_vz ,

 

If OP is NOT trying to rename EVERY block, then I do not think that looping through every block in the drawing and asking for each to be renamed is a practical or efficient method. Again, I believe the original command would accomplish it better.

 

--EDIT--

Also, I do not believe that the (initget 1) flag works on the (getstring ...) method.

 

Best,

~DD

0 Likes
Message 11 of 37

anderson51
Advocate
Advocate

The fact that you have to invoke the command each time slows you down. Also, you have to scroll thru the list each time to find the block you want to rename, depending on the list size, you could be scrolling a while to find the block.

 

It would definitely save time to pick multiple and rename multiple at the same time.

 

Thanks,

 

Anderson51

0 Likes
Message 12 of 37

hak_vz
Advisor
Advisor

@CodeDing wrote:

@hak_vz ,

 

If OP is NOT trying to rename EVERY block, then I do not think that looping through every block in the drawing and asking for each to be renamed is a practical or efficient method. Again, I believe the original command would accomplish it better.


DD I agree with you. Original command is best option, at least from security point of view,

It actually mimics -rename, but then offer names of all blocks that can be modified (removes xrefs and layouts).  

To skip rename one just have to hit <enter>

 

I've jumbled this code in last 20 mins just for fun, so you can find some stupid entries i.e initget on getstring 🙂

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 13 of 37

dmfrazier
Advisor
Advisor

@anderson51 

Just checking: Are you aware of the use of "*" as a wildcard?

0 Likes
Message 14 of 37

ВeekeeCZ
Consultant
Consultant

Would you give us list of examples of block names and how they should be renamed?

0 Likes
Message 15 of 37

Kent1Cooper
Consultant
Consultant

@anderson51 wrote:

....

It would definitely save time to pick multiple and rename multiple at the same time.

....


I'm trying to understand the procedure you envision.  Say you can pick multiple Block names that you want to rename, from a list or dialog box or something.  Then what?  How are you expecting to give them their new names, other than one at a time?

Kent Cooper, AIA
0 Likes
Message 16 of 37

ВeekeeCZ
Consultant
Consultant
Accepted solution

@anderson51 wrote:

The fact that you have to invoke the command each time slows you down....

 


 

Are you aware that you can actually hit the button "Rename To" to quickly rename an item without closing a dialog...

 

Z9E3zK5E_0-1603482316413.png

 

Message 17 of 37

anderson51
Advocate
Advocate

Below is an example list.

 

Say I wanted to rename the following:

 

DS-145-301-a1 to DS-146-302-a8

DS-145-301-a2 to DS-146-302-a9

DS-145-301-a3 to DS-146-302-a10

 

I would have to invoke the command 3 separate times, scroll to the block, and rename accordingly. It would be nice to take care of  it all during the single execution of the command.

 

The existing rename command would work perfectly if you could simply "OK" it without it exiting the command, and moving on to the next block you want to rename....if that makes sense?

 

 

Block Rename.png

Message 18 of 37

anderson51
Advocate
Advocate

WOW.

I was completely unaware that you could simply click "rename to" and move on to the next one. For some reason I was under the impression you had to click OK in order to complete the renaming.

 

Learn something new everyday.

 

Thanks

 

0 Likes
Message 19 of 37

dmfrazier
Advisor
Advisor

In addition to using that handy "Rename to" button, you could also take advantage of using the "*" wildcard character to expedite some of the renaming. It may be necessary to do the renaming in stages, e.g. first change "145-301" to "146-302", followed by the other segments.

0 Likes
Message 20 of 37

anderson51
Advocate
Advocate

I am not sure I am following what you mean, can you give me an example?

 

Thanks

0 Likes