TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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.
-
Did this but is not fool proof. 8-)
;; 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)
)
-
Bored and can't sleep...
(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
-
Good one Alan. I was taking the short cut when I penned my version. 8-)
-
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.
-
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 :-(
-
I ended up not going to bed until after 4.
I have so many nights like that :-(
mojo sent
-
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).
-
I ended up not going to bed until after 4.
I have so many nights like that :-(
mojo sent
:-D
-
:-D
wut
-
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.
-
Thanks for the 9...I mean 2 replies. :) only joking.< ... >.
:-D
-
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:
-
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.
-
No code was necessary: You can use wildcards in the rename dialog. ;)
-
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,
-
Very quickly written and untested, but may help:
-
Lee Mac,
Works great, thanks much.
Remarrk
-
Good stuff, happy to help :-)