Solved! Go to Solution.
Solved by john.uhden. Go to Solution.
I haven't read Mr. Rampage's article, but basically there are two (2) types of functions...
1. That is prefixed with C: to become an AutoLisp command that can typed into the command line and run just like any AutoCAD command,
2. That serves as a home-grown AutoLisp function to accept arguments and perform some specific task. They can be called from within AutoLisp code without the need for the command function. Functions are almost always intended to return a value, just like (+ 1 2) returns 3.
Example:
(defun average (values)
(/ (apply '+ values)(length values))
)
(defun C:Average ( / value values)
(while (setq value (getreal "\nEnter number to average: "))
(setq values (cons value values))
)
(princ (strcat "\nAverage = " (rtos (average values) 2 3)))
(princ)
)
So while the c:average function is a helpful way to take the user's input and call the average function, he can also use it from the command line as in...
Command: (average (list 1.1 3.2 2.6 4.7 pi))
John F. Uhden
@Anonymous.vip wrote:
Hi John,
If I wrote this Average function in one lisp routine, Can I use this in other lisp without writing it in that lisp or I have to copy this in other lisp too? Is it globally used or is local for particular lisp only?
Thanks...
The way that John's post is constructed, average subfunction is GLOBAL, but only on the current drawing session.
(defun average (values)
(/ (apply '+ values)(length values))
)
(defun C:Average ( / value values)
(while (setq value (getreal "\nEnter number to average: "))
(setq values (cons value values))
)
(princ (strcat "\nAverage = " (rtos (average values) 2 3)))
(princ)
)
To declare the function as LOCAL, you must include the function on the argument list after this symbol "/"
(defun C:Average ( / average value values) (defun average (values) (/ (apply '+ values)(length values)) ) (while (setq value (getreal "\nEnter number to average: ")) (setq values (cons value values)) ) (princ (strcat "\nAverage = " (rtos (average values) 2 3))) (princ) )
Makes me wonder if you even read thru the link i posted
Cheers
@Anonymous.vip wrote:
"I just wanted to know can I use this sub functions again in another lisp in another drawing session just by calling "Average"?
Why of course, you can.
Loading Programs Automatically
One way.Say.
(defun C:Average ( / value values)
(or average (load "M:\\InThisForest\\Otherlisp.lsp"))
(while (setq value (getreal "\nEnter number to average: ")) (setq values (cons value values)) ) (princ (strcat "\nAverage = " (rtos (average values) 2 3))) (princ) )
Side note: It's important that you set proper naming convention for your functions, average could be variable
I'll post a link for a good read on naming conventions.
@pbejse's responses are dead on, as usual.
I put functions that are used by other various functions in a, let's say, "BASE.LSP" file which I load within acaddoc.lsp which gets automatically loaded with every drawing new or open. That way they are always available without needing to redefine them. Or just define your functions in acaddoc.lsp.
I lied. What I really do is to compile the "BASE.LSP" file into BASE.FAS so that no one else in a networked environment can alter it. It should be obvious, but then there's a line in acaddoc.lsp that reads...
(load "base.fas")
The only problem with global functions is that they take up a little memory and might not even be used in a drawing session. But unless the functions are collectively voluminous it's not significant.
John F. Uhden
Can't find what you're looking for? Ask the community or share your knowledge.