Author Topic: Calculation lisp -help  (Read 1245 times)

0 Members and 1 Guest are viewing this topic.

PM

• Guest
Calculation lisp -help
« 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

JohnK

• Seagull
• Posts: 10377
Re: Calculation lisp -help
« Reply #1 on: January 15, 2022, 05:05:46 PM »
Wouldn't you just use a table for that need?
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

PM

• Guest
Re: Calculation lisp -help
« Reply #2 on: January 15, 2022, 05:32:38 PM »
i was thinking if i can do this without use a table, but a single text

BIGAL

• Swamp Rat
• Posts: 1300
• 40 + years of using Autocad
Re: Calculation lisp -help
« Reply #3 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
« Last Edit: January 15, 2022, 06:07:02 PM by BIGAL »

PM

• Guest
Re: Calculation lisp -help
« Reply #4 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

BIGAL

• Swamp Rat
• Posts: 1300
• 40 + years of using Autocad
Re: Calculation lisp -help
« Reply #5 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

PM

• Guest
Re: Calculation lisp -help
« Reply #6 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

It's Alive!

• Retired
• Needs a day job
• Posts: 8186
• AKA Daniel
Re: Calculation lisp -help
« Reply #7 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
Retired

BIGAL

• Swamp Rat
• Posts: 1300
• 40 + years of using Autocad
Re: Calculation lisp -help
« Reply #8 on: January 16, 2022, 11:50:57 PM »
Still need to convert x to * the others are ok / - +

BIGAL

• Swamp Rat
• Posts: 1300
• 40 + years of using Autocad
Re: Calculation lisp -help
« Reply #9 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")))

BIGAL

• Swamp Rat
• Posts: 1300
• 40 + years of using Autocad
Re: Calculation lisp -help
« Reply #10 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)`