# TheSwamp

## Code Red => AutoLISP (Vanilla / Visual) => Topic started by: PM on January 15, 2022, 03:09:23 PM

Title: Calculation lisp -help
Post by: PM on January 15, 2022, 03:09:23 PM
Hi is possible to write a lisp to calculate a sigle text (this text must be anythig with numbers )

for examle

Code: [Select]
`E1 = (1/2 x 59.328 x 102.361) + (1/2 x 16.672 x 85.953) = 3752.93 sq.morE= 3036.410 + 716.521 = 3752.93 sq.m`
Select the text

Code: [Select]
`E1 = (1/2 x 59.328 x 102.361) + (1/2 x 16.672 x 85.953) = `
and insert the calculation.

I get the idia by this photo

Thanks
Title: Re: Calculation lisp -help
Post by: JohnK on January 15, 2022, 05:05:46 PM
Wouldn't you just use a table for that need?
Title: Re: Calculation lisp -help
Post by: PM on January 15, 2022, 05:32:38 PM
i was thinking if i can do this without use a table, but a single text
Title: Re: Calculation lisp -help
Post by: BIGAL on January 15, 2022, 05:48:54 PM
There was a post I think over at Forums/autodesk convert formula to lisp, pretty sure for simple x*y x-y x+y x/y that has been done. I think did something

Command Cal
1234*456
562704

Command: CAL
>> Expression: (1/2 * 59.328 * 102.361) + (1/2 * 16.672 * 85.953)

You can save the cal into a variable
Command: (setq m2 (cal "(1/2 * 59.328 * 102.361) + (1/2 * 16.672 * 85.953)"))
3752.94

(setq str "(1/2 * 59.328 * 102.361)+(1/2 * 16.672 * 85.953)")
Command: (vl-cmdf "cal" str)
3752.94091
3752.94091
Title: Re: Calculation lisp -help
Post by: PM on January 15, 2022, 06:04:58 PM
Is it  possible to select the text , if have x instend of  * to understand it and insert a text with the result ?

Thanks
Title: Re: Calculation lisp -help
Post by: BIGAL on January 15, 2022, 06:09:39 PM
Post updated, to allow for a string input.

Yes you will need to replace x with * in the text string when you pick the table text, lee-mac.com/stringsubst.html

Title: Re: Calculation lisp -help
Post by: PM on January 15, 2022, 06:37:14 PM
I try this but is not working

Code - Auto/Visual Lisp: [Select]
1. (defun c:test ( new old str / inc len )
2.     (setq len (strlen new)
3.           inc 0
4.     )
5.     (while (setq inc (vl-string-search old str inc))
6.         (setq str (vl-string-subst new old str inc)
7.               inc (+ inc len)
8.         )
9.     )
10.     str
11.   (vl-cmdf "cal" str)
12. )
13.
14.

thanks
Title: Re: Calculation lisp -help
Post by: It's Alive! on January 15, 2022, 07:05:09 PM
not sure how to do this with lisp, but you can create an expression in fields I.e. %<\AcExpr (2*4)>%
I think LM has lisp tools to work with fields
Title: Re: Calculation lisp -help
Post by: BIGAL on January 16, 2022, 11:50:57 PM
Still need to convert x to * the others are ok / - +
Title: Re: Calculation lisp -help
Post by: BIGAL on January 17, 2022, 12:24:51 AM
The answer is a bit long winded, still need to replace X with *

http://www.theswamp.org/index.php?topic=41681

You need say (setq ans (eval (infix->prefix "2 * 2 * 34")))
Title: Re: Calculation lisp -help
Post by: BIGAL on January 17, 2022, 12:40:42 AM
I had problems but this works

Code: [Select]
`(defun rep ( new old / inc len ) (setq len (strlen new) inc 0) (while (setq inc (vl-string-search old str inc)) (setq str (vl-string-subst new old str inc)) (setq inc (+ inc len)) ))(setq str "2+(3x6/4)")(rep "*" "x")(princ str)(vl-cmdf "cal" str)`