Author Topic: Help - Replace text lisp  (Read 1079 times)

0 Members and 1 Guest are viewing this topic.

PM

  • Guest
Help - Replace text lisp
« on: January 20, 2022, 03:40:19 AM »
Hi i am writing a lisp to replace and delete characters for mathimatic function mtext,text

I have a function like this

Code - Auto/Visual Lisp: [Select]
  1. r1 = 1/2 x (4.80 + 4.18 + 3.30 ) = 6.14 m.
  2. E1 = \U+221A 6.14 x (6.14 - 4.80) x (6.14 - 4.18) x (6.14 - 3.30) = 6.77 sq.m.
  3. --------------------------------------------------------------------------------------------------
  4. r2 = 1/2 x (1.41 + 10.45 + 9.05 ) = 10.45 µ.
  5. E2 = \U+221A 10.45 x (10.45 - 1.41) x (10.45 - 10.45) x (10.45 - 9.05) = 0.10 sq.m.
  6.  
  7.  

And i want to convert it to this

Code - Auto/Visual Lisp: [Select]
  1.  
  2. 1.      DELETE EVERYTHING BEFORE AND AFTER  "="
  3. 2.      Add ()  to the text after sqrt
  4. 3.      Explode MTEXT to text
  5.  
  6.  
  7. 1/2 * (4.80 + 4.18 + 3.30 )
  8. sqrt ( 6.14 * (6.14 - 4.80) * (6.14 - 4.18) * (6.14 - 3.30))
  9. -------------------------------------------------------------------------
  10. 1/2 * (1.41 + 10.45 + 9.05 )
  11. sqrt (10.45 * (10.45 - 1.41) * (10.45 - 10.45) * (10.45 - 9.05))
  12.  
  13.  
  14.  


I use this code toy replace "x" to "*"  but i  need help to

1)  replace "\U+221A" to "sqrt"
2) Add ()  to the text after sqrt
3)  delete everything before and after  "=" (must be E1,E2,....,En =   or r1,r2,...,rn =   or  = (a number) m.  or  = (a number) sq.m.   )
4) Explode MTEXT to text

Code - Auto/Visual Lisp: [Select]
  1. (defun c:rex (/ f00 e)
  2.   (defun f00 (string)
  3.     (vl-string-translate "*x" "x*" string)
  4.       )  ;; I try to add \U+221A and sqrt  but become a mess
  5.   (if (and (setq e (car (entsel "\Pick text: ")))
  6.            (setq e (vlax-ename->vla-object e))
  7.            (vlax-property-available-p e 'textstring)
  8.       )
  9.   )
  10.   (princ)
  11. )
  12.  

Thanks
« Last Edit: January 20, 2022, 03:48:49 AM by PM »

PM

  • Guest
Re: Help - Replace text lisp
« Reply #1 on: January 20, 2022, 01:37:34 PM »
Any ideas ?

Thanks

apricot125

  • Mosquito
  • Posts: 13
Re: Help - Replace text lisp
« Reply #2 on: January 20, 2022, 10:34:48 PM »
DELETE EVERYTHING BEFORE AND AFTER  "="
Code: [Select]
(defun str->lst ()
  (defun range (i j)
    (if (= i j) (list i)
        (cons i (range (1+ i) j))
    )
  )
  (setq s1 "r1 = 1/2 x (4.80 + 4.18 + 3.30 ) = 6.14 m.")
  (setq lst (apply 'list (mapcar '(lambda (x) (substr s1 x 1)) (range 1 (strlen s1)))))
  (setq lst (reverse (cdr (member "=" (reverse (cdr (member "=" lst)))))))
  (setq s1 (apply 'strcat lst))
)

PM

  • Guest
Re: Help - Replace text lisp
« Reply #3 on: January 21, 2022, 01:58:19 AM »
Hi apricot125 .Thanks for the replay. The problem is that the mathimatic fuction is not all the time the same. The numbers in the parenthesis will change and the numbers or letters before and after. Thats why i ask to delete every thing before firt "=" and  after last "="

Code: [Select]
r1 = 1/2 x (4.80 + 4.18 + 3.30 ) = 6.14 m.

Thanks

PM

  • Guest
Re: Help - Replace text lisp
« Reply #4 on: January 21, 2022, 12:16:30 PM »
how to add  replace "\U+221A" to "sqrt" and  "sqrt"  to "\U+221A" ?

Code - Auto/Visual Lisp: [Select]
  1.     (defun c:rex (/ f00 e)
  2.       (defun f00 (string)
  3.         (vl-string-translate "*x" "x*" string)
  4.           )  ;; I try to add \U+221A and sqrt  but become a mess
  5.       (if (and (setq e (car (entsel "\Pick text: ")))
  6.                (setq e (vlax-ename->vla-object e))
  7.                (vlax-property-available-p e 'textstring)
  8.           )
  9.         (vla-put-textstring e (f00 (vla-get-textstring e)))
  10.       )
  11.       (princ)
  12.     )
  13.      
  14.  

Thanks

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Help - Replace text lisp
« Reply #5 on: January 21, 2022, 01:21:04 PM »
Here is an example for you to use and to learn from for the other criteria you are working on.
Code - Auto/Visual Lisp: [Select]
  1. (setq str "r1 = 1/2 x (4.80 + 4.18 + 3.30 ) = 6.14 m.")
  2.  
  3. (setq pos (vl-string-search "=" str))
  4. (setq str (substr str (+ pos 2)))
  5.  
  6. (setq pos (vl-string-search "=" str))
  7. (setq str (substr str 1 pos))
  8.  
  9. (vl-string-trim " " str)
  10.  

PM

  • Guest
Re: Help - Replace text lisp
« Reply #6 on: January 21, 2022, 01:38:44 PM »
Hi Tharwat. This code is for a specific type. As i said the r1 must be r2,r3,...,rn .....any letter or number, and the fanction is not the same so i want to search for the firse "=" and selete everything before and search for the last "=" and delete everything after

Thanks

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Help - Replace text lisp
« Reply #7 on: January 21, 2022, 01:47:38 PM »
The above string was just an example for you and you can have it with any compound string as long as you have the equal symbol '=' on the two sides of the string.

You are here in programming forum and you can't work as normal drafter by habit so you might do in AutoCAD so you need to think more deeply in what you are working on to achieve your goal correctly otherwise that would be a matter of wasting time for you.