Da Ballz Posted January 14, 2020 Posted January 14, 2020 Hello gang, I'm looking for a simple lisp to replace "ABCD" with "EFGH" where "ABCD" resides as a value in multiple different attributes of a block. In other words, there are attributes with different tag names but have the same attribute value "ABCD", and I want to find all instances of these values and replace them without user input in a lisp I can run as a batch process on multiple drawings. Thanks in advance for any assistance. Quote
Emmanuel Delay Posted January 14, 2020 Posted January 14, 2020 (edited) Command ASR EDIT: I added ASRM function, that substitutes multiple instances of the "to be replaced string" (vl-load-com) ;; Get Attribute Values - Lee Mac ;; Returns an association list of attributes present in the supplied block. ;; blk - [ent] Block (Insert) Entity Name ;; Returns: [lst] Association list of ((<tag> . <value>) ... ) ;; http://www.lee-mac.com/attributefunctions.html#algetattributevaluerc (defun LM:getattributevalues ( blk / enx ) (if (and (setq blk (entnext blk)) (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk)))))) (cons (cons (cdr (assoc 2 enx)) (cdr (assoc 1 (reverse enx))) ) (LM:getattributevalues blk) ) ) ) ;; Set Attribute Value - Lee Mac ;; Sets the value of the first attribute with the given tag found within the block, if present. ;; blk - [vla] VLA Block Reference Object ;; tag - [str] Attribute TagString ;; val - [str] Attribute Value ;; Returns: [str] Attribute value if successful, else nil. (defun LM:vl-setattributevalue ( blk tag val ) (setq tag (strcase tag)) (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (progn (vla-put-textstring att val) val) ) ) (vlax-invoke blk 'getattributes) ) ) ;;;;;;;;;;;;; ;; ASR for Attribute String Replace (defun c:ASR ( / from to ss i atts blk a tag val newval) (setq from (getstring "\nString to be replaced: ")) (setq to (getstring "\nString to replace to: ")) ;;(setq from "AZERTY") ;;(setq to "QUERTY") (setq ss (ssget (list (cons 0 "INSERT") (cons 66 1)))) (setq i 0) (repeat (sslength ss) (setq blk (ssname ss i)) (setq atts (LM:getattributevalues blk)) (foreach a atts (setq tag (car a)) (setq val (cdr a)) (if (vl-string-search from val ) (progn (setq newval (vl-string-subst to from val)) ;; substitute the attribute (LM:vl-setattributevalue (vlax-ename->vla-object blk) tag newval) )) ) (setq i (+ i 1)) ) ) ;; ASRM for Attribute String Replace Multiple. ;; Same as ASR, except it changes multiple instances of the the "from" to "to" (defun c:ASRM ( / from to ss i atts blk a tag val newval) (setq from (getstring "\nString to be replaced: ")) (setq to (getstring "\nString to replace to: ")) ;;(setq from "AZERTY") ;;(setq to "QUERTY") (setq ss (ssget (list (cons 0 "INSERT") (cons 66 1)))) (setq i 0) (repeat (sslength ss) (setq blk (ssname ss i)) (setq atts (LM:getattributevalues blk)) (foreach a atts (setq tag (car a)) (setq val (cdr a)) (while (vl-string-search from val ) (progn (setq newval (vl-string-subst to from val)) ;; substitute the attribute (LM:vl-setattributevalue (vlax-ename->vla-object blk) tag newval) (setq val newval) )) ) (setq i (+ i 1)) ) ) Edited August 9, 2022 by Emmanuel Delay Quote
BIGAL Posted January 14, 2020 Posted January 14, 2020 (edited) Agree with Lee great answer, for current drawing no code required just use FIND. Edited January 14, 2020 by BIGAL Quote
Da Ballz Posted January 15, 2020 Author Posted January 15, 2020 Thank you all for the replies. The above solutions by Lee Mac would work, as the string I want to replace is found as a value within multiple block attributes, each with different tag names, but the text string to change is always the same value. So I think that part is already solved, easily. I want to be able to do this as a batch process on multiple drawings (no user input or interface required) where I can hard code in the text string changes or run them in a script. Quote
Da Ballz Posted January 15, 2020 Author Posted January 15, 2020 That said, could I just hard code it in? ;; ASR for Attribute String Replace (defun c:ASR ( / from to ss i atts blk a tag val newval) (setq from "AZERTY") (setq to "QUERTY") Quote
BIGAL Posted January 15, 2020 Posted January 15, 2020 You will need to make sure that you have the lisp loaded can do this via Appload and add to startup suite. Your script would be something like this, not tested. Open dwg1 (c:asr) close "Y" Open dwg2 (c:asr) close "Y" Quote
Emmanuel Delay Posted January 16, 2020 Posted January 16, 2020 On 1/15/2020 at 2:49 AM, Da Ballz said: That said, could I just hard code it in? ;; ASR for Attribute String Replace (defun c:ASR ( / from to ss i atts blk a tag val newval) (setq from "AZERTY") (setq to "QUERTY") With my code, yes, you can hard code it Quote
BIGAL Posted January 16, 2020 Posted January 16, 2020 Like Emmanuel if preloaded. (defun c:ASR (from to / ss i atts blk a tag val newval) remove (setq from "AZERTY") remove (setq to "QUERTY") open dwg1 (c:asr "AZERTY" "QUERTY") close Y open dwg2 (c:asr "AZERTY" "QUERTY") close Y open dwg3 (c:asr "AZERTY" "QUERTY") close Y 1 Quote
Da Ballz Posted January 16, 2020 Author Posted January 16, 2020 Thanks guys, this makes sense. I will create a script, call Emmanuel's lisp routine, then run the script code for each replacement I need. Perfect. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.