Welcome to the Southern African Autolisp Website.
You are Visitor Number since 19th May 1999.
The 'Eval' Function.
'Eval' is another of the least seldom used, but very powerful, AutoLisp functions.
It is defined in the AutoCad Customization Guide as follows :
Eval - Returns the result of evaluating an AutoLisp expression.
Syntax : (eval expr)
For example, have a look at the following code segment :
(eval (ascii "A"))
Which simply means, Evaluate the expression (ascii "A").
This should return "65", the ASCII character code for "A".
Fine, I understand that, but where would I use it? Here's a simple example.
Say we had a whole lot of distances that we had to measure and then come
up with an accumulative total. We could measure each one, write it down,
and then, manually add them up. Here's a better way.
We are now going to write an AutoLisp routine that allows us to measure
each distance, and then return a total of all the distances. Here we will
use the 'eval' function to perform the final total calculation for us.
Here's the coding :
;Program to measure non-sequential distances
(Defun C:ADIST ( / dst dstlst adist)
;define the function and declare variables
(setq dstlst '(+ ))
;create list with plus
(while
;while a return is not entered
(setq dst (getdist "\nPick point or Return to exit: "))
;get the distance
(setq dstlst (append dstlst (list dst)))
;append the distance to the list
(setq adist (eval dstlst))
;calculate the running total
(setq dst (rtos dst 2 2))
;convert to string
(setq adist (rtos adist 2 2))
;convert to string
(princ (strcat "\nDistance = " dst " Accum Dist = " adist))
;display the distance and the running total
);end while
(prompt (strcat "\nTotal Distance = " adist))
;display the total distance
(princ)
;clean finish
);defun
(princ)
;clean loading
We start of the function by defining a list that only contains the '+' function.
The heart of the routine is nested in a 'while' loop. As long as the user keeps
on adding distances, the routine runs, but as soon as he hits a return, 'while'
evaluates to nil and the loop is ended.
The first line within the loop simply gets the required distance. The second line
appends the distances to the list. Thirdly, we evaluate the list adding all of the
distances together. We then format the distance and the running total and display
them to the user. Once the user has finished selecting the distances, we display
the final total. Simple really!!
Hint - Have you ever thought of building a macro recorder similar to that used
in Excel. By storing the users AutoCAD commands in a list alongwith the AutoLisp
command function, you could use the 'eval' function to 'playback' the list of
commands. For example :
(setq lst '(command "circle" pt2 rad))
(eval lst)
Cheers for now and happy evaluating........
All Tutorials and Code are provided "as-is" for purposes of instruction and
utility and may be used by anyone for any purpose entirely at their own risk.
Please respect the intellectual rights of others.
All material provided here is unsupported and without warranty of any kind.
No responsibility will be taken for any direct or indirect consequences
resulting from or associated with the use of these Tutorials or Code.
Copyright © 1999 by
Kenny Ramage
All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage.
|