Author Topic: Help including mtext in change text routine  (Read 672 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7023
Re: Help including mtext in change text routine
« Reply #15 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 :)

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7023
Re: Help including mtext in change text routine
« Reply #16 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.  

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

notredave

  • Newt
  • Posts: 104
Re: Help including mtext in change text routine
« Reply #17 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

ronjonp

  • Needs a day job
  • Posts: 7023
Re: Help including mtext in change text routine
« Reply #18 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?

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

notredave

  • Newt
  • Posts: 104
Re: Help including mtext in change text routine
« Reply #19 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

ronjonp

  • Needs a day job
  • Posts: 7023
Re: Help including mtext in change text routine
« Reply #20 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. )
« Last Edit: July 30, 2019, 11:41:50 AM by ronjonp »

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

notredave

  • Newt
  • Posts: 104
Re: Help including mtext in change text routine
« Reply #21 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

ronjonp

  • Needs a day job
  • Posts: 7023
Re: Help including mtext in change text routine
« Reply #22 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. )


« Last Edit: July 30, 2019, 12:00:08 PM by ronjonp »

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

notredave

  • Newt
  • Posts: 104
Re: Help including mtext in change text routine
« Reply #23 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?

notredave

  • Newt
  • Posts: 104
Re: Help including mtext in change text routine
« Reply #24 on: July 30, 2019, 12:20:35 PM »
See gif attached.

ronjonp

  • Needs a day job
  • Posts: 7023
Re: Help including mtext in change text routine
« Reply #25 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
« Last Edit: July 30, 2019, 01:06:24 PM by ronjonp »

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

notredave

  • Newt
  • Posts: 104
Re: Help including mtext in change text routine
« Reply #26 on: July 30, 2019, 01:16:35 PM »
Thank you very much for your personal time on this. I appreciate you.

David

ronjonp

  • Needs a day job
  • Posts: 7023
Re: Help including mtext in change text routine
« Reply #27 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! :)

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

roy_043

  • Water Moccasin
  • Posts: 1713
  • BricsCAD 18
Re: Help including mtext in change text routine
« Reply #28 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
)

ronjonp

  • Needs a day job
  • Posts: 7023
Re: Help including mtext in change text routine
« Reply #29 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*

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC