Author Topic: Replace two characters in mtext with lisp  (Read 2684 times)

0 Members and 1 Guest are viewing this topic.

mhy3sx

  • Newt
  • Posts: 120
Replace two characters in mtext with lisp
« on: October 03, 2023, 07:15:17 AM »
Hi I have a lot of mtext like this 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 1 and I want to do this 1,2,3,4,5,6,7,8,9,10,11,1. Replace " - "  with ",". How to do this with lisp?

Thanks



dexus

  • Bull Frog
  • Posts: 210
Re: Replace two characters in mtext with lisp
« Reply #1 on: October 03, 2023, 08:15:22 AM »

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Replace two characters in mtext with lisp
« Reply #2 on: October 03, 2023, 12:26:55 PM »
Here is the main part for substituting the string then you can entmod the dxf codes with the updated new string returned.
Code - Auto/Visual Lisp: [Select]
  1. (setq str "1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 1")
  2.  
  3. (while (wcmatch str "* - *")
  4.   (setq str (vl-string-subst "," " - " str))
  5. )
  6.  
Returns: "1,2,3,4,5,6,7,8,9,10,11,1"

mhy3sx

  • Newt
  • Posts: 120
Re: Replace two characters in mtext with lisp
« Reply #3 on: October 04, 2023, 05:47:36 AM »
Hi. Thanks for the reply. I try this but is not working. Any ideas?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ doc)
  2.  (ssget '((0 . "*MTEXT")))
  3.      (vla-put-textstring txt
  4.       (vl-string-subst " - " "," (vla-get-textstring txt)
  5.    (vl-string-search " - " (vla-get-textstring txt))) )
  6. )(princ "\nNo Text selected:")
  7.  )
  8. (princ)    
  9. )
  10.  

mhy3sx

  • Newt
  • Posts: 120
Re: Replace two characters in mtext with lisp
« Reply #4 on: October 05, 2023, 02:32:41 AM »
Hi, I am very close but something is missing. This code works for text and mtext.

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:foo (/ f00 e)
  3.   (defun f00 (string)
  4.     (vl-string-translate " - " "," string)
  5.   )
  6.   (if (and (setq e (car (entsel "\Pick text: ")))
  7.            (setq e (vlax-ename->vla-object e))
  8.            (vlax-property-available-p e 'textstring)
  9.       )
  10.   )
  11.   (princ)
  12. )
  13.  

with this code  get this results

Code: [Select]

1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 1

1,-,2,-,3,-,4,-,5,-,6,-,7,-,8,-,9,-,10,-,11,-,1


if I change
Code - Auto/Visual Lisp: [Select]
  1.     (vl-string-translate " - " "," string)
  2.  
  3. with
  4.  
  5.     (vl-string-translate "* - *" "," string)
  6.  

Code: [Select]
not working

and if I change

Code - Auto/Visual Lisp: [Select]
  1.     (vl-string-translate " - " "," string)
  2.  
  3. with
  4.  
  5.     (vl-string-translate "-" "," string)  <- no space between  -
  6.  

Code: [Select]
1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 1

1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 1

But I want to work with space between - like

Code - Auto/Visual Lisp: [Select]
  1.     (vl-string-translate " - " "," string)
  2.  


How to fix the code ?

Thanks

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Replace two characters in mtext with lisp
« Reply #5 on: October 05, 2023, 04:44:51 AM »
Since you're looking to replace a single character with a sequence of characters, vl-string-translate is not appropriate here; I would instead suggest using vl-string-subst. For this, you may wish to leverage my String Subst wrapper for vl-string-subst, which will replace all occurrences of the string (not just the first occurrence).

Here is an example:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo ( / e )
  2.     (if
  3.         (and
  4.             (setq ent (car (entsel "\nPick text: ")))
  5.             (setq ent (vlax-ename->vla-object ent))
  6.             (vlax-property-available-p ent 'textstring)
  7.         )
  8.         (vla-put-textstring ent (LM:stringsubst " - " "," (vla-get-textstring ent)))
  9.     )
  10.     (princ)
  11. )
  12.  
  13. ;; String Subst  -  Lee Mac
  14. ;; Substitutes a string for all occurrences of another string within a string
  15. ;; new - [str] string to be substituted for 'old'
  16. ;; old - [str] string to be replaced
  17. ;; str - [str] string to be searched
  18.  
  19. (defun LM:stringsubst ( new old str / inc len )
  20.     (setq len (strlen new)
  21.           inc 0
  22.     )
  23.     (while (setq inc (vl-string-search old str inc))
  24.         (setq str (vl-string-subst new old str inc)
  25.               inc (+ inc len)
  26.         )
  27.     )
  28.     str
  29. )

mhy3sx

  • Newt
  • Posts: 120
Re: Replace two characters in mtext with lisp
« Reply #6 on: October 05, 2023, 04:58:36 AM »
Thanks Lee Mac. To work I did a little change to your code

I replace  this

Code: [Select]
       
(vla-put-textstring ent (LM:stringsubst " - " "," (vla-get-textstring ent)))

with

Code: [Select]
(vla-put-textstring ent (LM:stringsubst "," " - " (vla-get-textstring ent)))

for some reason work, like this .....

Thanks




masao

  • Newt
  • Posts: 97
Re: Replace two characters in mtext with lisp
« Reply #7 on: October 05, 2023, 05:27:10 AM »
Hi I have a lot of mtext like this 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 1 and I want to do this 1,2,3,4,5,6,7,8,9,10,11,1. Replace " - "  with ",". How to do this with lisp?

Thanks

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo(/ oldtxt newtxt oldnum newnum eold ename en ss i)
  2.    (setq ss (ssget '((-4 . "<OR")                            
  3.             (-4 . "<AND") (0 . "TEXT") (-4 . "AND>")    
  4.             (-4 . "<AND") (0 . "MTEXT") (-4 . "AND>")  
  5.             (-4 . "OR>"))))
  6.  
  7. (initget "D P N F S")
  8. (setq change_size (getkword "change_size[,(D)/-(P)/ , (N)/ - (F)/space delete(S)]<,(D)>:"))
  9. (if (= change_size nil) (setq change_size "D"))
  10.  
  11. (setq change_size (strcase change_size))
  12.  
  13.  
  14. ((= change_size "D");if 1 - 2 - 3 change to 1 , 2 , 3 use
  15.  
  16.  (setq oldtxt "-")
  17.  (setq newtxt ",")
  18.  
  19. )
  20.  
  21. ((= change_size "P");if 1 , 2 , 3 change to 1 - 2 - 3 use
  22.  
  23.  (setq oldtxt ",")
  24.  (setq newtxt "-")
  25.  
  26. )
  27.  
  28. ((= change_size "N");if 1-2-3 change to 1 , 2 , 3 use
  29.  
  30.  (setq oldtxt "-")
  31.  (setq newtxt (strcat " " "," " "))
  32.  
  33. )
  34.  
  35. ((= change_size "F");if 1,2,3 change to 1 - 2 - 3 use
  36.  
  37.  (setq oldtxt ",")
  38.  (setq newtxt (strcat " " "-" " "))
  39.  
  40. )
  41.  
  42. ((= change_size "S");if 1 - 2 - 3 or 1 , 2 , 3 change to 1-2-3 or 1,2,3
  43.  
  44.  (setq oldtxt " ")
  45.  (setq newtxt "")
  46.  
  47. )
  48.  
  49. );cond
  50.  
  51.     (setq i 0)
  52.     (setq oldnum (strlen oldtxt))
  53.     (setq newnum (strlen newtxt))
  54.     (repeat (sslength ss)
  55.     (setq ename (ssname ss i))
  56.     (setq eold (entget ename))
  57.     (setq en (cdr (assoc 0 eold)))
  58.     (setq p 1)
  59.     (setq ent (assoc 1 eold))
  60.     (setq entxt (cdr ent))
  61.     (setq ennum (strlen entxt))
  62.     (setq aa "")
  63.     (while (<= p ennum)
  64.     (setq kk (substr entxt p oldnum))
  65.     (if (= kk oldtxt)
  66.     (progn
  67.     (setq kk newtxt)
  68.     (setq p (- (+ p oldnum) 1))
  69.     )
  70.     (setq kk (substr entxt p 1))
  71.     )
  72.     (setq aa (strcat aa kk))
  73.     (setq p (+ 1 p)))
  74.     (setq aa (cons 1 aa))
  75.     (setq eold (subst aa ent eold))
  76.     (entmod eold)
  77.     (setq i (+ 1 i)))
  78.     )

hi~ it not my code,but you can test it.

if your text: 1 , 2 , 3  or  1, 2,   3 want change to 1 - 2 - 3 can use S delete space then use F.

if your text: 1 , 2 , 3 want change to 1 - 2 - 3 can use P , cant delete space.

have a good day~

kdub:edit change post code type to =cadlisp-7
« Last Edit: October 09, 2023, 04:07:28 PM by kdub_nz »

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Replace two characters in mtext with lisp
« Reply #8 on: October 05, 2023, 06:31:35 AM »
Thanks Lee Mac. To work I did a little change to your code

I replace  this

Code: [Select]
       
(vla-put-textstring ent (LM:stringsubst " - " "," (vla-get-textstring ent)))

with

Code: [Select]
(vla-put-textstring ent (LM:stringsubst "," " - " (vla-get-textstring ent)))

for some reason work, like this .....

Thanks

Thanks; I thought you wanted the reverse - nevertheless, I'm glad you have a working solution.

mhy3sx

  • Newt
  • Posts: 120
Re: Replace two characters in mtext with lisp
« Reply #9 on: October 09, 2023, 04:22:53 AM »
HI Lee Mac. Is it possible to do both  to replace " - "   ->  ","  and from  ","  ->   " - "

Code - Auto/Visual Lisp: [Select]
  1.     (defun c:foo ( / e )
  2.         (if
  3.             (and
  4.                 (setq ent (car (entsel "\nPick text: ")))
  5.                 (setq ent (vlax-ename->vla-object ent))
  6.                 (vlax-property-available-p ent 'textstring)
  7.             )
  8.             (vla-put-textstring ent (LM:stringsubst "," " - " (vla-get-textstring ent)))
  9.         )
  10.         (princ)
  11.     )
  12.      
  13.     ;; String Subst  -  Lee Mac
  14.     ;; Substitutes a string for all occurrences of another string within a string
  15.     ;; new - [str] string to be substituted for 'old'
  16.     ;; old - [str] string to be replaced
  17.     ;; str - [str] string to be searched
  18.      
  19.     (defun LM:stringsubst ( new old str / inc len )
  20.         (setq len (strlen new)
  21.               inc 0
  22.         )
  23.         (while (setq inc (vl-string-search old str inc))
  24.             (setq str (vl-string-subst new old str inc)
  25.                   inc (+ inc len)
  26.             )
  27.         )
  28.         str
  29.     )
  30.  

Thanks

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Replace two characters in mtext with lisp
« Reply #10 on: October 09, 2023, 04:52:19 AM »
Yes, simply change the order of the arguments passed to my LM:stringsubst function.

mhy3sx

  • Newt
  • Posts: 120
Re: Replace two characters in mtext with lisp
« Reply #11 on: October 09, 2023, 04:50:10 PM »
HI Lee Mac , I want to do both  to replace " - "   ->  ","  and from  ","  ->   " - " 

if   " - "   ->  ","
and

if ","  ->   " - " 

with the same command

Thanks

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Replace two characters in mtext with lisp
« Reply #12 on: October 09, 2023, 06:17:11 PM »
The easiest way is likely to use a relatively obscure intermediate character, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo ( / ent )
  2.     (if
  3.         (and
  4.             (setq ent (car (entsel "\nPick text: ")))
  5.             (setq ent (vlax-ename->vla-object ent))
  6.             (vlax-property-available-p ent 'textstring)
  7.         )
  8.         (vla-put-textstring ent
  9.             (LM:stringsubst "," "|"
  10.                 (LM:stringsubst " - " ","
  11.                     (LM:stringsubst "|" " - "
  12.                         (vla-get-textstring ent)
  13.                     )
  14.                 )
  15.             )
  16.         )
  17.     )
  18.     (princ)
  19. )
  20.  
  21. ;; String Subst  -  Lee Mac
  22. ;; Substitutes a string for all occurrences of another string within a string
  23. ;; new - [str] string to be substituted for 'old'
  24. ;; old - [str] string to be replaced
  25. ;; str - [str] string to be searched
  26.  
  27. (defun LM:stringsubst ( new old str / inc len )
  28.     (setq len (strlen new)
  29.           inc 0
  30.     )
  31.     (while (setq inc (vl-string-search old str inc))
  32.         (setq str (vl-string-subst new old str inc)
  33.               inc (+ inc len)
  34.         )
  35.     )
  36.     str
  37. )

mhy3sx

  • Newt
  • Posts: 120
Re: Replace two characters in mtext with lisp
« Reply #13 on: October 10, 2023, 04:52:40 AM »
Hi Lee Mac. What is the deference between

Code - Auto/Visual Lisp: [Select]
  1. (LM:stringsubst "," "|"
  2.                 (LM:stringsubst " - " ","
  3.                     (LM:stringsubst "|" " - "
  4.                         (vla-get-textstring ent)
  5.                     )
  6.                 )
  7.             )
  8.  

and

Code - Auto/Visual Lisp: [Select]
  1.                 (LM:stringsubst "," " - "
  2.                     (LM:stringsubst " - " ","
  3.                         (vla-get-textstring ent)
  4.                     )
  5.  


why you add  "|"  ?


Thanks

dexus

  • Bull Frog
  • Posts: 210
Re: Replace two characters in mtext with lisp
« Reply #14 on: October 10, 2023, 06:25:06 AM »
The second one will switch "-" to "," and then straight back into "-".
So the first function doesn't change the end result, only the second.
So "12-34,56" will become "12-34-56".

Lee's code has a temporary placeholder character so the characters are switched.
So "12-34,56" will become "12,34-56".