Rename Multiple Blocks Based on a List

Rename Multiple Blocks Based on a List

don_scribner
Contributor Contributor
1,717 Views
9 Replies
Message 1 of 10

Rename Multiple Blocks Based on a List

don_scribner
Contributor
Contributor

Good Morning All,

 

Does anybody have a lisp that will look at a block name in a list and if it exists, rename that block to a new name?  Company was bought out and the new regime changed all of our part numbers.  I have dozens of templates and hundreds of block in each.  I have many of the blocks in a list as shown below.  So if the list could look at the first part number "F102" and if it existed, rename to "300275" I would be a happy camper. 

 

("F102 " "300275")
("F102-P " "300275-P")
("F102-S " "300275-S")
("F103 " "306259")
("F103-P " "306259-P")
("F103-S " "306259-S")
("F104 " "300274")
("F104-P " "300274-P")
("F104-S " "300274-S")

 

I  . . .  borrowed a lisp that I found on line but I have never been good at error trapping.  Can somebody set me straight?     Don

 

(defun c:RMB ( / lstBlocks)
;Rename Multiple Blocks
;Prep
(setq lstBlocks '(
;("old_name" "new_name")

("F102 " "300275")
("F102-P " "300275-P")
("F102-S " "300275-S")
("F103 " "306259")

))
;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

0 Likes
Accepted solutions (1)
1,718 Views
9 Replies
Replies (9)
Message 2 of 10

Kent1Cooper
Consultant
Consultant

Does that routine work if all the Block names exist, and you just need to add the checking for that?  If so, maybe [untested]:

....

(foreach blk lstBlocks

  (if (and (tblsearch "block" (car blk)) (not (tblsearch "block" (cadr blk))))
    (command "-RENAME" "b" (car blk) (cadr blk)); then

  ); if
); foreach

....

It checks not only that the old Block name exists, but also that the new one does not exist, because you can't Rename a Block to a name that's already in use.

Kent Cooper, AIA
0 Likes
Message 3 of 10

don_scribner
Contributor
Contributor
No, it doesn't even do that. . .
0 Likes
Message 4 of 10

paullimapa
Mentor
Mentor

also make sure list of blocks don't contain spaces after the block name...try this:

; RMB renames old block to new block name per given list
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/rename-multiple-blocks-based-on-a-list/m-p/11727959#M443191
(defun c:RMB ( / lstBlocks)
;Rename Multiple Blocks
;Prep
(setq lstBlocks '(
;("old_name" "new_name") make sure no spaces after block name

("F102" "300275")
("F102-P" "300275-P")
("F102-S" "300275-S")
("F103" "306259")

))
;Begin work
(setvar 'CMDECHO 0)
(foreach blk lstBlocks
(if(and
     (tblsearch "BLOCK" (car blk)) ; chk if old block name exists
     (not(tblsearch "BLOCK" (cadr blk))) ; chk if new block name exists
   )
 (progn
   (command "-RENAME" "_B" (car blk) (cadr blk))
   (princ
    (strcat"\nOld Block Name: [" (car blk) "] successfully renamed to\nNew Block Name: [" (cadr blk) "].")
   )
 ) ; progn
 (princ
   (strcat"\nOld Block Name: [" (car blk) "] NOT renamed because it does NOT exists or\nNew Block Name: [" (cadr blk) "] already exists in current drawing.")
 ) ; else
) ; if
);foreach
(setvar 'CMDECHO 1)
;Finish up
(prompt "\nBlock Re-Naming Complete.")
(princ)
);defun

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 10

pbejse
Mentor
Mentor
Accepted solution

@don_scribner wrote:

...

("F102 " "300275")
("F102-P " "300275-P")
("F102-S " "300275-S")
("F103 " "306259")
("F103-P " "306259-P")
...


Better yet, use an external source for the old-new layer names. that way you can add more layer names.

(defun c:RMB ( / ListOfBlocksSource source linesOnSource p old new)
;Rename Multiple Blocks
(if
   (setq ListOfBlocksSource (getfiled "List of New block names" (getvar 'dwgprefix) "csv" 16 ))
  	(progn
	  (setq source (open ListOfBlocksSource "r"))
	  (While (setq linesOnSource (read-line source))
	    	(if (and
			(setq p (vl-string-position 44 linesOnSource))
			(tblsearch "block" (setq old (substr linesOnSource 1 p)))
			(not (tblsearch "block" (setq New (substr linesOnSource (+ p 2)))))
		  	)
		  (command "-RENAME" "b" old New)
		  )
	    )
	  (close source)
	  (prompt "\nBlock Re-Naming process complete.")
	  )
  )
  (princ)
  )

command: RMB

pbejse_0-1675488529889.png

Block Re-Naming process complete

 

If the location and name of the data source is always on the same folder.

Change this
 (setq ListOfBlocksSource (getfiled "List of New block names" (getvar 'dwgprefix) "csv" 16 ))
to
 (setq ListOfBlocksSource "c:\\Data\\New Regime.csv")

HTH

 

Refer to attached csv file (New Regime.csv)

0 Likes
Message 6 of 10

don_scribner
Contributor
Contributor

Thank you all for helping with my issues.  I really appreciate the time spent. This last iteration has worked very well. 

0 Likes
Message 7 of 10

thecocuk07
Participant
Participant

hello  

 

How do we do it if there is spaces?

0 Likes
Message 8 of 10

Sea-Haven
Mentor
Mentor

@thecocuk07 A block name can have spaces. Personally try to avoid. Is what your trying to achieve not working so provide a sample dwg and text in the dwg with Old & New names. 

0 Likes
Message 9 of 10

thecocuk07
Participant
Participant

Before I answer you. I reviewed the problem again. I had a problem with the csv file I used. I fixed it now and the problem is solved. Lisp works perfectly. Thank you for your interest.

0 Likes
Message 10 of 10

Sea-Haven
Mentor
Mentor

No worries express thanks to @pbejse he did the most work.

0 Likes