; Change part of text
; By Alan H info@alanh.com.au july 2019
; will not find hidden characters in middle of mtext
(vl-load-com)
(defun c:chgahtext ( / ans ss told tnew tobj x)
(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Old => New" "Old text " 10 9 "-" "New text"10 9 "_" )))
(setq told (nth 0 ans))
(setq tnew (nth 1 ans))
(setq ss (ssget (list (cons 0 "*text"))))
(repeat (setq x (sslength ss))
(setq tobj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
(vla-put-textstring tobj (vl-string-subst tnew told (vla-get-textstring tobj )))
)
(princ)
)
(alert "To run again type chgahtext")
(c:chgahtext)
(if (= "TEXT" (cdr (assoc 0 (setq e (entget (ssname p l))))))
To:(if
(or
(= "TEXT" (cdr (assoc 0 (setq e (entget (ssname p l))))))
(= "MTEXT" (cdr (assoc 0 e)))
)
ronjonp,What version of AutoCAD are you on? The find and replace has many more options than this lisp as well as wildcard searches. It will work on many more objects as well. ( MLEADERS ATTRIBUTES DIMENSIONS etc.. )
I find typing CT works alot faster. CT is automatically loaded. I don't have to load CT every time. I'm old school, lol. Also, this CT routine works on MTEXT. I just like it a lot better. I'm just curious why it isn't working on that particular instance.
Thank you,
David
ronjonp,Strange .. what are your search strings?
Thank you very much for your efforts. I tried it but I get a spinning windows circle after I type in new text, waiting for it to change. Oh well, don't worry about it but thank you very much for replying.
David
* RING (asterisk space)On the same sample drawing? It works here.
trying to change it to *(asterisk without the space)
That's when I get the windows wheel spinning
This is very strange to me.It's because the trailing spaces get dropped in the getstring input "* ". That's why you got stuck in a loop before.
If I type in to change * R TO R, it works
If i type * (space) to *(no space) it doesn't change it.
But, I will have to say your code is a lot cleaner and shorter than mine. I love it!
Thank you much,
David
See gif attached.It's something in the GETSTRING input/output which I have no control over. It's removing trailing spaces ( same results here ).
Thank you very much for your personal time on this. I appreciate you.Glad to help! :)
David
; (String_Subst "aabbaacc" "aa" "xx") => "xxbbxxcc"
; (String_Subst "aabbaacc" "aa" "xxaa") => "xxaabbxxaacc"
(defun String_Subst (str old new / idx)
(setq idx 0)
(while (setq idx (vl-string-search old str idx))
(setq str (vl-string-subst new old str idx))
(setq idx (+ idx (strlen new)))
)
str
)
@ronjonp:Thanks for the example .. I knew we had chatted about it a while back but could not find the thread. *cheers*
Indeed we have discussed string substitution code before. Problems can occur if the new sub-string contains the old sub-string. Your last version of c:CT will catch some cases but not "apple" => "more apples".
I would use something like this:Code: [Select]; (String_Subst "aabbaacc" "aa" "xx") => "xxbbxxcc"
; (String_Subst "aabbaacc" "aa" "xxaa") => "xxaabbxxaacc"
(defun String_Subst (str old new / idx)
(setq idx 0)
(while (setq idx (vl-string-search old str idx))
(setq str (vl-string-subst new old str idx))
(setq idx (+ idx (strlen new)))
)
str
)