Author Topic: Rename Blocks:Replace XYZ with ABC in all block names  (Read 8580 times)

0 Members and 1 Guest are viewing this topic.

Oak3s

  • Guest
Rename Blocks:Replace XYZ with ABC in all block names
« 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.


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #1 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)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #2 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
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #3 on: March 26, 2010, 08:44:36 AM »
Good one Alan. I was taking the short cut when I penned my version. 8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #4 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.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #5 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  :-(

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #6 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
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #7 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).
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Oak3s

  • Guest
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #10 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #11 on: March 26, 2010, 09:23:18 PM »
Thanks for the 9...I mean 2 replies. :) only joking.< ... >.

 :-D
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #12 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:
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Oak3s

  • Guest
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #13 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.

Crank

  • Water Moccasin
  • Posts: 1503
Re: Rename Blocks:Replace XYZ with ABC in all block names
« Reply #14 on: March 28, 2010, 05:10:51 PM »
No code was necessary: You can use wildcards in the rename dialog. ;)
Vault Professional 2023     +     AEC Collection