Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Batch Rename Blocks

26 REPLIES 26
Reply
Message 1 of 27
mad_monk42
5743 Views, 26 Replies

Batch Rename Blocks

Hi,
Please excuse if this question has already been posted.
(I couldn't find one exactly)

I would like to batch rename all blocks in a drawing to NEWJOB01, NEWJOB02 ,etc.
I can't use wildcards with RENAME command because the old names don't always have a common name.
I prefer to use a lisp routine.

Thanks,

Murtz
26 REPLIES 26
Message 21 of 27
Kent1Cooper
in reply to: Netelaana

Don't you run into an error from trying to rename Layer 0?

 

Some suggestions....  [I changed variable names involving "b" from the Block routine.]

 

(defun C:LN- (/ lay laylist i)
  ; don't need to "nil out" variables in localized list above
  (while (setq lay (tblnext "layer" (not lay)))
    (and
      (zerop (logand 21 (cdr (assoc 70 lay)))); not frozen, locked, or in Xref [don't need 32 bit]
      (setq lay (cdr (assoc 2 lay))); don't need (wcmatch) which was for anonymous Blocks
      (/= lay "0"); can't rename that
      (setq laylist (cons lay laylist)); don't need to check whether already in list [it won't be]
    )
  )
  (setq i 0)
  (foreach lname laylist; a little simpler for a list than (while)
    (setq i (1+ i))
    (command "_.-rename" "_la" lname (strcat "LAYER" (if (< b 10) "0" "") (itoa i)))
  )
  (princ)
)

 

I confess to wondering whether you really want to leave Layers un-renamed  if they happen to be frozen or locked.  You'll have a mishmash of Layer names that I assume you're trying to eliminate with such a routine.

 

Also, would you ever try to run something like this in a drawing in which you have already done so?  That will encounter problems with already-existing Layer names.  That can be accounted for, with some more code, if needed.

Kent Cooper, AIA
Message 22 of 27
Netelaana
in reply to: Kent1Cooper

thanks! yes, i did run into a few errors, but it did the job. Yes, if you run it twice, it gets grumbly but a simple solution is to just change it to rename as "layers" instead of "layer" and then back, and you're good to go. 

 

I ended up wrapping my original hack job into a longer routine as a way of preparing and example dwg that is stripped of any identifying info. It is probably like using a 2x4 to drive finish nails, and also runs into errors, but got the job done. 

 

(defun C:MAKEALLGENERIC ( / blist bl b n)
;; ALL BLOCKS
(setq blist '() bl nil b 0 n "BLOCK")
(while (setq bl (tblnext "block" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(not (wcmatch "`**" (setq bl (cdr (assoc 2 bl)))))
(or (member bl blist) (setq blist (cons bl blist)))
)
)
(while (and (car blist) (setq b (1+ b)))
(command "_.-rename" "_b" (car blist)
(strcat n (if (< b 10) "0" "") (itoa b)))
(setq blist (cdr blist))
)
;; ALL DIMSTYLES
(setq blist '() bl nil b 0 n "DIMS")
(while (setq bl (tblnext "dimstyle" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(not (wcmatch "`**" (setq bl (cdr (assoc 2 bl)))))
(or (member bl blist) (setq blist (cons bl blist)))
)
)
(while (and (car blist) (setq b (1+ b)))
(command "_.-rename" "_d" (car blist)
(strcat n (if (< b 10) "0" "") (itoa b)))
(setq blist (cdr blist))
)
;; ALL TEXTSTYLES
(setq blist '() bl nil b 0 n "TEXT")
(while (setq bl (tblnext "style" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(not (wcmatch "`**" (setq bl (cdr (assoc 2 bl)))))
(or (member bl blist) (setq blist (cons bl blist)))
)
)
(while (and (car blist) (setq b (1+ b)))
(command "_.-rename" "_s" (car blist)
(strcat n (if (< b 10) "0" "") (itoa b)))
(setq blist (cdr blist))
)
;; ALL LAYOUTS
(vl-load-com)
(vlax-for layt (vla-get-layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
    (if (> (vla-get-TabOrder layt) 0)
        (setq layt_lst (cons (cons (vla-get-TabOrder layt) (vla-get-Name layt)) layt_lst))
    )
)
(setq layt_lst (vl-sort layt_lst '(lambda (x y) (< (car x) (car y)))))
(setq name (vl-filename-base (getvar "dwgname"))
      i    0
)
(if (= 1 (length layt_lst))
    (command "_-layout" "_rename" "" name)
    (foreach layt layt_lst
        (command "_-layout" "_rename" (cdr layt) (strcat name " (" (itoa (setq i (1+ i))) ")"))
    )
)
;; ALL LAYERS
(setq blist '() bl nil b 0 n "LAYER")
(while (setq bl (tblnext "layer" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(not (wcmatch "`**" (setq bl (cdr (assoc 2 bl)))))
(or (member bl blist) (setq blist (cons bl blist)))
)
)
(while (and (car blist) (setq b (1+ b)))
(command "_.-rename" "_la" (car blist)
(strcat n (if (< b 10) "0" "") (itoa b)))
(setq blist (cdr blist))
)

;; ALL LINETYPES

;; ALL LINETYPES
(setq blist '() bl nil b 0 n "LINE")
(while (setq bl (tblnext "ltype" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(not (wcmatch "`**" (setq bl (cdr (assoc 2 bl)))))
(or (member bl blist) (setq blist (cons bl blist)))
)
)
(while (and (car blist) (setq b (1+ b)))
(command "_.-rename" "_lt" (car blist)
(strcat n (if (< b 10) "0" "") (itoa b)))
(setq blist (cdr blist))
)


;; ALL TABLESTYLES
(setq blist '() bl nil b 0 n "TABLE")
(while (setq bl (tblnext "Tablestyle" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(not (wcmatch "`**" (setq bl (cdr (assoc 2 bl)))))
(or (member bl blist) (setq blist (cons bl blist)))
)
)
(while (and (car blist) (setq b (1+ b)))
(command "_.-rename" "_t" (car blist)
(strcat n (if (< b 10) "0" "") (itoa b)))
(setq blist (cdr blist))
)
(princ)
)

 

Message 23 of 27
dgoutz
in reply to: Anonymous

This Lisp is great. Thank you. 

Is there an option so that the lisp asks the user to type a desired block name rather than "NEWJOB"

 

Thank you in advance.

Message 24 of 27
Kent1Cooper
in reply to: dgoutz


@dgoutz wrote:

..... 

Is there an option so that the lisp asks the user to type a desired block name rather than "NEWJOB"

....


In case they're not still watching after 12-1/2 years [it's an archived account, and they haven't posted anything since around that time]....

 

Change this line:

 

  (setq blist '() bl nil b 0 n "NEWJOB")

 

to this:

 

  (setq blist '() bl nil b 0 n (getstring "\nBlock base name: "))

Kent Cooper, AIA
Message 25 of 27
dgoutz
in reply to: Kent1Cooper

Worked like magic...

 

Thank you so much for the help.

 

Kudos

Message 26 of 27
Sean_LPA
in reply to: abrusil33

Can you get this to work with xrefs? I would like to rename the reference names with a find and replace option.

Message 27 of 27
john.uhden
in reply to: abrusil33

You seem to be modifying incrementally.

What if you have...

"blabla-REV.A-blabla-OPTION.1-blabla"

But there already is a...

"blabla-REV.B-blabla-OPTION.2-blabla"

And why should the Option identifier change just because there's a revision?

Actually, I would think the Options take precedence over the revision level, so to me it would make more sense being...

"blabla-OPTION.A-blabla-REV.2-blabla"

and

"blabla-OPTION.B-blabla-REV.0-blabla"

John F. Uhden

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost