TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Oak3s on March 25, 2010, 11:01:23 PM

Title: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Oak3s on March 25, 2010, 11:01:23 PM
I just searched the forum but didnt find quite what I was looking for. (doesnt mean its not there) i also just discovered the 'problems and solutions' sticky. I found things that are close but didnt seem close enough.

Rename Blocks: Replace XYZ with ABC in all block names.
Blocks:
Current Block Name             Desired Block Name
XYZ-1                              ABC-1
XYZ-2                              ABC-2
XYZ-3                              ABC-3
etc.                                 etc.

The blocks are currently named in a way that it wouldnt matter if the routine wanted to rename a block where XYZ was at the start, middle, or end of the block name. ex) 123-XYZ to 123-ABC or 123-XYZ-456 to 123-ABC-456

I dont expect anyone to write the code for me, but if you have something already that does the above I would very much appreciate the help.

Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: CAB on March 26, 2010, 12:16:29 AM
Did this but is not fool proof. 8-)
Code: [Select]
;;  By CAB 01.05.09
(defun c:BlockRename2 (/ bp prefix siffix n obj usercmd)
    ;; ignore xref, xref dependent and anonymous
  (defun GetBlkNames (/ data result)
    (while (setq data (tblnext "block" (null data)))
      (if (zerop (logand 21 (cdr (assoc 70 data))))
        (setq result (cons (cdr (assoc 2 data)) result))
      )
    )
    (acad_strlsort result)
  )
 
  (if (and
        (setq bp (getstring t "\nEnter Block name pattern to match: "))
        (setq find (getstring t "\nEnter string to replace: "))
        (setq replace (getstring t "\nEnter replacement string: "))
      )
    (progn
      (setq usercmd (getvar "CMDECHO"))
      (setvar "CMDECHO" 0)
      (command "_undo" "_begin")
      ;;  Walk through the collection
      (foreach n (GetBlkNames)
        (if (and (/=(substr n 1 1)"_") ; ignore blocknames starting with underscore
                 (wcmatch (strcase n) (strcase bp))
            )
          (if (vl-catch-all-error-p
                (vl-catch-all-apply
                  '(lambda ()
                     (if (/= (setq NewName (vl-string-subst replace find n)) n)
                       (progn
                         (command "_.-rename" "_b" n NewName)
                         (princ (strcat "\n" n " --> " NewName))
                       )
                       (princ (strcat "\nSkiped Block " n " *** "))
                     )
                   )
                )
              )
            (prompt (strcat "\nError for block name: " NewName))
          )
        )
      )
      (command "_undo" "_end")
      (setvar "CMDECHO" usercmd)
    )
  )
  (princ)
)
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: alanjt on March 26, 2010, 02:11:34 AM
Bored and can't sleep...

Code: [Select]
(defun c:BlockRenamer (/ b f r n gl bl)
  ;; Alan J. Thompson, 03.26.10
  (vl-load-com)
  (cond
    ((and (/= "" (setq b (getstring t "Block name pattern: ")))
          (/= "" (setq f (getstring t "String to replace: ")))
          (/= "" (setq r (getstring t "Replacement string: ")))
     ) ;_ and
     (or *AcadDoc* (setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
     (vlax-for x (vla-get-blocks *AcadDoc*)
       (if
         (and (not (wcmatch (setq n (vla-get-name x)) "*|*,_*")) (wcmatch (strcase n) (strcase b)))
          (if (vl-catch-all-error-p
                (vl-catch-all-apply (function vla-put-name) (list x (vl-string-subst r f n)))
              ) ;_ vl-catch-all-error-p
            (setq bl (cons n bl))
            (setq gl (cons (cons n (vla-get-name x)) gl))
          ) ;_ if
       ) ;_ if
     ) ;_ vlax-for
     (if (or gl bl)
       (alert
         (strcat
           (if gl
             (strcat "The following blocks were renamed (Old . New):\n\n"
                     (vl-princ-to-string (vl-sort gl (function (lambda (a b) (< (car a) (car b))))))
             ) ;_ strcat
             ""
           ) ;_ if
           (if bl
             (strcat "\n\n\nThe following blocks could NOT be renamed:\n\n"
                     (vl-princ-to-string (vl-sort bl '<))
             ) ;_ strcat
             ""
           ) ;_ if
         ) ;_ strcat
       ) ;_ alert
       (alert "Nothing renamed.")
     ) ;_ if
    )
  ) ;_ cond
  (princ)
) ;_ defun
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: CAB on March 26, 2010, 08:44:36 AM
Good one Alan. I was taking the short cut when I penned my version. 8-)
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: alanjt on March 26, 2010, 11:14:00 AM
Good one Alan. I was taking the short cut when I penned my version. 8-)
Nothing wrong with that. :) I was just killing time. I ended up not going to bed until after 4.
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Lee Mac on March 26, 2010, 11:22:18 AM
Good one Alan. I was taking the short cut when I penned my version. 8-)
Nothing wrong with that. :) I was just killing time. I ended up not going to bed until after 4.

I have so many nights like that  :-(
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: MP on March 26, 2010, 11:36:33 AM
I ended up not going to bed until after 4.

I have so many nights like that  :-(

mojo sent
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: alanjt on March 26, 2010, 11:45:05 AM
Good one Alan. I was taking the short cut when I penned my version. 8-)
Nothing wrong with that. :) I was just killing time. I ended up not going to bed until after 4.

I have so many nights like that  :-(
When I was younger, I'd go 2 days without sleeping. It was horrible at times.

It also didn't help that my little girl was waking me up at 7:30 to go to her birthday party at school (turns 3 on Monday).
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Lee Mac on March 26, 2010, 12:11:51 PM
I ended up not going to bed until after 4.

I have so many nights like that  :-(

mojo sent

 :-D
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: MP on March 26, 2010, 12:31:18 PM
:-D

wut
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Oak3s on March 26, 2010, 09:04:49 PM
Thanks for the 9...I mean 2 replies. :) only joking. My day got a lot busier than I had anticipated. I will take a look next week. Thanks again.
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Kerry on March 26, 2010, 09:23:18 PM
Thanks for the 9...I mean 2 replies. :) only joking.< ... >.

 :-D
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: alanjt on March 26, 2010, 09:28:39 PM
Thanks for the 9...I mean 2 replies. :) only joking.< ... >.

 :-D
It wouldn't be a thread if we didn't stray slightly off topic. :wink:
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Oak3s on March 26, 2010, 09:52:15 PM
I was able to take a look at both of them tonight. Thank you very very much. They both work as needed. I didnt dig into the code yet but plan to. Again, thanks for the time saver.
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Crank on March 28, 2010, 05:10:51 PM
No code was necessary: You can use wildcards in the rename dialog. ;)
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Remarrk on January 19, 2012, 12:54:48 PM
Hello Swamp,

I'm hoping that posting to this old thread helps with the explanation better than I could by starting a new thread.
I have a situation similar to the OP's.
I have created blocks in several master drawings for tool palettes use.
I want to change the convention of the blocks names.
I have, for example:
ISORED90_L1
ISORED90_L2
ISORED90_L3
etc., etc.

I want to change not only the names, but the case of part of the name,
for example:
iso90RED_L1
iso90RED_L2
iso90RED_L3
etc., etc.

I tried the lisp routines without success. It may be my ignorance of the first user entry "Block name pattern:". I don't know what is required there.

Also, I tried the AutoCAD rename with wild cards and all I was able to accomplish was adding my entry to the block name.

Perhaps someone could clear this up for me.

thanks,
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Lee Mac on January 19, 2012, 01:04:41 PM
Very quickly written and untested, but may help:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / name ) (vl-load-com)
  2.         (if (wcmatch (setq name (vla-get-name block)) "ISORED90*")
  3.             (vl-catch-all-apply 'vla-put-name (list block (strcat "iso90RED" (substr name 9))))
  4.         )
  5.     )
  6.     (princ)
  7. )
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Remarrk on January 19, 2012, 07:48:53 PM
Lee Mac,
Works great, thanks much.

Remarrk
Title: Re: Rename Blocks:Replace XYZ with ABC in all block names
Post by: Lee Mac on January 20, 2012, 07:03:41 AM
Good stuff, happy to help  :-)