AutoLISP.7: Defining our own functions
Print This Post
![]() |
Author | Orhan Toker |
| Profession | Architect M.Sc. | |
| Autodesk Authorized Consultant | ||
| Database Specialist | ||
All AutoLISP versions
Hello,
In this lesson, we will learn how to define our own functions in AutoLISP projects. You might have noticed in our early lessons that, we have already defined functions in out applications and examples. However, what I would like to explain to you is to make you to apprehend the importance of defining functions and to make this work at a level enough to make professional applications. In this lesson, you will also find application techniques for using functions efficiently.
In AutoLISP, we define our own functions by (defun) function. Once we define our own function, we can easily reach this function that we defined, through VisualLISP console or through our own projects. Functions that we defined become functions similar to AutoLISP functions. Don’t forget that we can define our own commands by using (defun) function.
Creating a new function (defun function name (arguments / local variables))
(defun) function creates functions or commands by combining a group of equations and AutoLISP functions so that they can run one after another. Defun function needs at least 3 number of arguments. First one of them is the name of the new function that we are defining.
Note: Although new program environments enable the use of foreign language characters, do not use them while naming your functions. This may significantly restrict the use of it. Besides, you should give logical names to your functions. Best example is (defun), which is the abbreviation of “DEfine FUNciton” words. All of the programming languages have their own naming logic. Due to the fact that, LISP language is an artificial intelligence programming language, function names are reminiscent of their use. So, you should also pay attention to this.
Second argument of Defun function is the list of arguments to be called upon the execution of the function. This may be empty or may include more than one argument. Third and the last argument is the list of local variables which will be used in the function. Now, let’s define a simple function:
$_(defun hello_world () (princ "\n hello world!"))
hello _ world
Now on, our function is defined. It can be used as shown below:
$_(hello_world)
hello world!
In order to define commands:
$_(defun c:md () (hello_world))
c:md
We created the md command which calls the hello world function.
Definition and usage of functions with arguments
In AutoLISP, you can define functions which are called by arguments. Despite the resident AutoLISP functions, you cannot make definition of functions with optional arguments. Thus, while calling the function, you have to pass all of the arguments.
>_$ (defun squareof (number) (* number number))
SQUAREOF
_$ (squareof 3)
9
In the example shown above, we defined and tried a function that calculates the square of a number. squareof function that we defined uses number argument and gives the multiplication of it with itself as a return value. We can call this function inside another function.
(defun c:take_square_of ()
(setq a (getreal "\nEnter the number, square of which you would like to calculate:))
(princ "\nSquare of the number you entered is:")
(squareof a)
(princ)
)
As you can see, we can call the squareof function, which we have defined, inside the take_square_of command.
Local variables that are used inside the functions
Variables like cycle counters, user entered values like point and distance etc. Are used inside all of the functions, but they have no use outside the function. In order for the function you are defining to learn the variables that will be used inside the function, you have to declare them. By this way, you can use the same variables inside another function and prevent confusions.
However, danger of defining the variables that you use inside your functions is that; if you don’t declare these variables as local variables, they are assumed to be global variables. If you forget this and use these variables in another part of your application, then you face errors that are very difficult to find. In order to avoid this problem, declare the local variables and be sure about it. Let’s correct the above defined function and define variable a as local variable.
(defun c: take_square_of (/ a)
(setq a (getreal "\ nEnter the number, square of which you would like to calculate:))
(princ "\ nSquare of the number you entered is:")
(squareof a)
(princ)
)
By this way, there is no problem for you even if the variable a is used in another place. Another advantage of using local variables is not to allocate too much memory that is used by the editor.
Changing AutoCAD commands (redefining)
Even though it is a method that I have never used, you can change the AutoCAD commands according to your own demands by using (defun) function, which means that you can redefine them.
_$ (defun c:LINE()
(_> (princ "\n Is it better for you to used PLINE?")
(_> (command "._LINE")
(_> )
C:LINE
By this way, we redefined AutoCAD LINE command. Now,
_$ (command "undefine" "line")
nil
By using this small piece of code, we have put AutoCAD LINE command out-of-use and expressed that from now on our own function will be used. TO return to the old case:
$_(command "redefine" "line")
As I have mentioned, I suggest you to do this redefinition only in case it is necessary to do it.
S::STARTUP function
By using S::STARTUP function, you can define operations that you want to be do automatically before the beginning of the drawing. In order for this function to be automatically loaded when the AutoCAD drawing is opened, you have to write it inside “ACAD.LSP” “ACADDOC.LSP” or the relevant MNL file. In my example, I will edit the acad.lsp file: (You can find this file under the support folder. For AutoCAD 2006, \support\acad2006.lsp). Open the file and append the following code. Please read Erhan’s “Loading AutoLISP applications automatically“ article for detailed information.
(defun open_layers()
(command "._layer" "_new" "TAL_DUVAR" "")
(command "._layer" "_new" "TAL_DOSEME" "" )
(command "._layer" "_new" "TAL_3D" "" )
)
(defun-q S::STARTUP()
(open_layers)
)
If you append this code at the end of acad.lsp file, then all the operations you insert into the S::STARTUP function are automatically executed. In this example, we automatically opened layers. That’s all for this lesson.
See you.
Technorati Tags: AutoLISP, defun, define function
Bu yazinin Turkce’sini okumak icin basiniz.
Similar Posts
Rate this article:


May 22nd, 2007 18:43
How to load lsp, fas & vlx in acadLT?
Text disapper or become hidden in zom in?