TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: notredave on July 23, 2019, 01:41:34 PM

Title: Help including mtext in change text routine
Post by: notredave on July 23, 2019, 01:41:34 PM
All,

Good afternoon. Could someone please look at attached routine and be able to modify it to include mtext. I love typing ct and picking text to change what I need. It would be nice to select regular and mtext at the same time as drawings have mixed text formats. I would rather use this opposed to using "FIND" to replace mtext characters. Thank you very much in advance.

David
Title: Re: Help including mtext in change text routine
Post by: BIGAL on July 24, 2019, 12:36:57 AM
A full rewrite would be easier there is  (vl-string-subst newstr oldstr string 1) which swaps new for old. The ssget should be (ssget (list (cons 0 "*text"))) so only text or mtext is found. Multi getvals.lsp is a library routine you are welcome to use.

Code: [Select]
; 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)

Title: Re: Help including mtext in change text routine
Post by: notredave on July 24, 2019, 06:28:50 AM
BIGAL,

Thank you very much for the response and I tried your routine and it does work but backwards to what I'm used to. With mine, I type in "ct" which is embedded in my main lisp routine as part of my many other routines and i can do crossing on whole drawing if I wanted to, over lines, attributes, etc and when I hit return, I input old value and new value and it changes applicable text. If there is a way to modify the one I posted to be able to change mtext also, that would be awesome. I don't like to use "find" because you have to make sure you select upper right icon to select part of drawing you want which the "ct" routine does that. I hope I didn't make you mad, not my intention BIGAL.

Thank you,
David
Title: Re: Help including mtext in change text routine
Post by: CAB on July 24, 2019, 07:12:48 PM
Quick fix is to change  chgahtext to ct, both places.Then move this line  (setq ss (ssget (list (cons 0 "*text"))))up to just below the line (defun c:chgahtext  ( / ans ss told tnew tobj x)Give that a try.
Title: Re: Help including mtext in change text routine
Post by: notredave on July 25, 2019, 07:05:46 AM
CAB,

Thank you very much for your input but it's still not what I'm looking for it to do. I would like the lisp routine I attached earlier to be modified by working on mtext also. I would like everything else to stay the same because it works now. I would prefer not to have it load "multi getvals.lsp" also. I'm sorry if I'm sounding like a pain in the you know what. Please try routine I have attached to see what I'm talking about. If modification of text, mtext and attributes all in one would be strong!!

Thank you in advance,
David
Title: Re: Help including mtext in change text routine
Post by: roy_043 on July 25, 2019, 08:52:40 AM
Change (note: 2 lines in the original code):
Code: [Select]
(if (= "TEXT" (cdr (assoc 0 (setq e (entget (ssname p l))))))To:
Code: [Select]
(if
  (or
    (= "TEXT" (cdr (assoc 0 (setq e (entget (ssname p l))))))
    (= "MTEXT" (cdr (assoc 0 e)))
  )
Title: Re: Help including mtext in change text routine
Post by: notredave on July 25, 2019, 09:24:59 AM
roy_043,

Thank you very much trying to help me but now I an receiving ; error: malformed list on input. I am attaching the modified to my original.

Thank you,
David
Title: Re: Help including mtext in change text routine
Post by: notredave on July 25, 2019, 10:53:15 AM
roy_043,

But what you suggested is what I would like. To use my code.

Thank you,
David
Title: Re: Help including mtext in change text routine
Post by: notredave on July 25, 2019, 11:39:01 AM
roy_043,

I got it to work, finally. I want to thank you for being able to edit my original code and make it work. I also want to thank BIGAL and CAB for your inputs also!

P.S. CAB, I use your BreakObjects.lsp, v.2.2 religiously on my wiring diagrams and want to thank you for that!

Thanks again guys,
David
Title: Re: Help including mtext in change text routine
Post by: CAB on July 25, 2019, 12:11:41 PM
Thank you for saying so David.   8)
Title: Re: Help including mtext in change text routine
Post by: BIGAL on July 28, 2019, 11:37:22 PM
Glad to give a starting point, thanks also Cab.
Title: Re: Help including mtext in change text routine
Post by: notredave on July 30, 2019, 09:37:33 AM
Good morning all,

I thought I had my change text lisp nailed down but I ran across this and glad I looked at it because I assumed, which I KNOW I shouldn't do to see it didn't change text. If you can load this lisp and drawing and try to change * RING to *RING (minus space) by doing a crossing on all 3 text lines, it claims it changes 3 lines but it really doesn't.

If I am confusing you guys,
I typed CT, chose all 3 text strings, typed *  (old string)
*(minus space) for new string
Response is "Changed 3 text lines"

But it really doesn't change the text lines. I'm confused. Thank you for taking a look at this at your leisure.

I'm not sure if both files are attaching. If not, I will attach as another post.

Thank you,
David
Title: Re: Help including mtext in change text routine
Post by: notredave on July 30, 2019, 09:38:09 AM
Drawing file
Title: Re: Help including mtext in change text routine
Post by: ronjonp on July 30, 2019, 10:29:26 AM
Why don't you use the built-in find and replace? Part of the answer not looking too close at the code you're using is '*' is a wildcard character. Try replacing " RING" with "RING".
Title: Re: Help including mtext in change text routine
Post by: notredave on July 30, 2019, 10:40:08 AM
ronjonp,

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
Title: Re: Help including mtext in change text routine
Post by: ronjonp on July 30, 2019, 10:46:02 AM
ronjonp,

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
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 understand being old school ( I'm a command line guy ) but sometimes there are just better ways to do things :)
Title: Re: Help including mtext in change text routine
Post by: ronjonp on July 30, 2019, 10:55:05 AM
Consider something like this .. written quickly and not tested.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ct (/ f s new old str)
  2.   (if (and (setq s (ssget ":L" '((0 . "MTEXT,TEXT"))))
  3.            (setq old (getstring "\nOld string: " t))
  4.            (setq new (getstring "\nNew string: " t))
  5.       )
  6.     (foreach o (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
  7.       (setq str (vla-get-textstring o))
  8.       (setq f nil)
  9.       ;; I'm pretty sure this search and replace method has a bug .. Roy?
  10.       (while (vl-string-search old str) (setq str (vl-string-subst new old str)) (setq f t))
  11.       (and f (vla-put-textstring o str))
  12.     )
  13.   )
  14.   (princ)
  15. )
  16.  
Title: Re: Help including mtext in change text routine
Post by: notredave on July 30, 2019, 11:12:39 AM
ronjonp,

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
Title: Re: Help including mtext in change text routine
Post by: ronjonp on July 30, 2019, 11:27:27 AM
ronjonp,

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
Strange .. what are your search strings?
Title: Re: Help including mtext in change text routine
Post by: notredave on July 30, 2019, 11:34:01 AM
* RING (asterisk space)

trying to change it to *(asterisk without the space)

That's when I get the windows wheel spinning
Title: Re: Help including mtext in change text routine
Post by: ronjonp on July 30, 2019, 11:36:31 AM
* RING (asterisk space)

trying to change it to *(asterisk without the space)

That's when I get the windows wheel spinning
On the same sample drawing? It works here.
Try this version .. has a check to make sure we're not stuck in a loop.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ct (/ a i f s new old str)
  2.   (if (and (setq s (ssget ":L" '((0 . "MTEXT,TEXT"))))
  3.            (setq old (getstring "\nOld string: " t))
  4.            (setq new (getstring "\nNew string: " t))
  5.       )
  6.     (foreach o (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
  7.       (setq str (vla-get-textstring o))
  8.       (setq f nil)
  9.       (while (and (setq i (vl-string-search old str)) (not a))
  10.         (setq str (vl-string-subst new old str))
  11.         ;; Make sure we're not stuck in a loop
  12.         (setq a (= (vl-string-search old str) i))
  13.         (setq f t)
  14.       )
  15.       (and f (vla-put-textstring o str))
  16.     )
  17.   )
  18.   (princ)
  19. )
Title: Re: Help including mtext in change text routine
Post by: notredave on July 30, 2019, 11:43:52 AM
This is very strange to me.

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
Title: Re: Help including mtext in change text routine
Post by: ronjonp on July 30, 2019, 11:52:21 AM
This is very strange to me.

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
It's because the trailing spaces get dropped in the getstring input "* ". That's why you got stuck in a loop before.

This version checks for equal new and old strings .. give it a try! :)
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ct (/ a i f s new old str)
  2.   (if (and (setq s (ssget ":L" '((0 . "MTEXT,TEXT"))))
  3.            (setq old (getstring "\nOld string: " t))
  4.            (setq new (getstring "\nNew string: " t))
  5.       )
  6.     (if (= old new)
  7.       (princ (strcat "\nOld '" old "' is same as new '" new "' buh bye!"))
  8.       (foreach o (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
  9.         (setq str (vla-get-textstring o))
  10.         (setq f nil)
  11.         (while (and (setq i (vl-string-search old str)) (not a))
  12.           (setq str (vl-string-subst new old str))
  13.           ;; Make sure we're not stuck in a loop
  14.           (or (setq a (= (vl-string-search old str) i)) (setq f t))
  15.         )
  16.         (and f (vla-put-textstring o str))
  17.       )
  18.     )
  19.   )
  20.   (princ)
  21. )


Title: Re: Help including mtext in change text routine
Post by: notredave on July 30, 2019, 12:06:42 PM
ronjonp,

How do you create a gif file to show you what's it doing to me?
Title: Re: Help including mtext in change text routine
Post by: notredave on July 30, 2019, 12:20:35 PM
See gif attached.
Title: Re: Help including mtext in change text routine
Post by: ronjonp on July 30, 2019, 01:03:10 PM
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 ).

FIND works  :-P
Title: Re: Help including mtext in change text routine
Post by: notredave on July 30, 2019, 01:16:35 PM
Thank you very much for your personal time on this. I appreciate you.

David
Title: Re: Help including mtext in change text routine
Post by: ronjonp on July 30, 2019, 01:48:00 PM
Thank you very much for your personal time on this. I appreciate you.

David
Glad to help! :)
Title: Re: Help including mtext in change text routine
Post by: roy_043 on July 30, 2019, 02:02:02 PM
@ronjonp:
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
)
Title: Re: Help including mtext in change text routine
Post by: ronjonp on July 30, 2019, 02:41:01 PM
@ronjonp:
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
)
Thanks for the example .. I knew we had chatted about it a while back but could not find the thread. *cheers*