Hello everyone,as we all know in lisp you can use mathematical formulas by first inserting the type of operation and then the parameters:
(* (+ 3 5) 2) = 16
there is a function that can be interpreted, however, the text string:
"(3+5)*2" = 16
?
Please also confirm the absence of a similar function.
Thanks
Hello everyone,
So what info are you asking for?
Even if you remove the quotes it's still not in the proper format to execute. You need to clarify what kind of help you are asking for.
I do not know how to explain better the problem
The user must enter (3 +5) * 2
I am looking for a function that is able to interpret the string as a mathematical formula and then to return the formula result.
You would need to construct a function to parse the string and return an AutoLISP expression.
An example of such a function is used in this program by member HighflyingBird (may require membership), the parser function is attached.
e.g.:
_$ (trans_format "(3+5)*2") (* (+ 3.0 5.0) 2.0)
Lupo,
You're welcome
Did you just want the result (16), or the AutoLISP expression itself?
If you just wanted the result (16), you could use an instance of ScriptControl to evaluate a VBScript expression, for example:
;; Eval VBScript Expression - Lee Mac 2011 ;; Args: expr = string containing expression to evaluate (defun LM:EvalVBExpression ( expr / scr res ) (setq res (vl-catch-all-apply (function (lambda nil (setq scr (vla-getinterfaceobject (vlax-get-acad-object) "ScriptControl")) (vlax-put-property scr 'language "VBScript") (vlax-invoke scr 'eval expr) ) ) ) ) (if scr (vlax-release-object scr)) (if (not (vl-catch-all-error-p res)) res) ) (vl-load-com)
e.g.:
_$ (LM:EvalVBExpression "(3+5)*2") 16
Very good,even better!
I used the above code with (eval):
(setq risult (eval (trans_format "(3+5)*2")))
but the function LM: EvalVBExpression is much cleaner.
thanks.
Excellent, you're welcome
Hello Lee
I'm using the function
(LM:EvalVBExpression "(3+5)*2")
a new computer with Win7 64-bit + AutoCAD MAP3D 2012.
With this configuration, returns nil 😞
The exact same function with WinXP 32-bit + AutoCAD2012 works very well!
Can you tell me why?
You hardly need to reinvent the wheel by fancy string parsing. Since AutoLISP was originally built, there has been a built-in calculator for this type of stuff. It is arx based and must be arxloaded or automatically loaded by using the cal command.
(cal "(3+5)*2")
16
It also has lots of really useful unique functions and keywords that it supports.