AutoLISP Lesson #4 - PROGRAM FORMAT One of the main differences between programming in AutoLISP and programming in other languages, is AutoLISP is an interpreted language, as I have stated before. That means it will take one line of code at a time and evaluate it by itself. You don't have to write a complete program for AutoLISP to be of value to you. AutoLISP has three different forms a program can take.
Every line of code you write in AutoLISP will be a list. The one thing all lists have in common is that they are all enclosed in parenthesis "()". This is the most important point to get right now. You must put everything in parenthesis, and for every open parenthesis, there must be a closing parenthesis. The Expression Example: (/ 50 25.4) The example above converts 50 in metric to standard units. It can be entered from the command line, or used as part of a larger program. The Function Example: (defun m ( / ) The example above shows the form for defining functions. The list starts with DEFUN as the function doing the work. DEFUN needs three or more arguments. The first argument is the name for the function you are defining, in the example this was "m". The second argument is the parameter list, and in the example there are no parameters. You will notice in the parameter list a slash separator, this is to separate the arguments to the function on the left of the slash, from the local variables on the right side of the slash. The third and all subsequent arguments are expressions for the function to perform when it is run. In the example the function will always return 50 divided by 25.4. The Command Function Example: (defun c:m ( / ) When making the call to this type of function you must call the name just as it's written in the function definition. Example: (c:m) When called from inside the lisp interpreter the command function must be treated as any other function. The real power of the command function comes when calling it from the command line. Any function may be called from the command line, but the command function does not need the parenthesis when calling it from the command line. Example: AutoLISP Programs The first thing we will do is create our header comment so we know what we are working on, and more importantly, when we return later to modify it, we will know what we worked on. The next thing you will need, is the program definition. ;; Title: Program The first thing you will notice in the program above is I have defined a command function. The filename for the lisp should be the same as the command name given.In this case the filename will be PROG.LSP. (defun c:prog ( / ) Now we have an error checking routine, which is quite basic, but will get the job done for now. All it will do is catch the error string passed back from AutoLISP when the error occurs and print the error string on the command line. (defun c:prog ( Param1 Param2 / ) As you can see I have to parameters I'm checking against each other, and I've decided I need to do several things if my cases work out. (defun c:prog ( Param1 Param2 / prog_main check_params stuff_to_do do_other_stuff *error* ) (defun check_params ( Item1 Item2 / ) (defun stuff_to_do ( / ) (defun do_other_stuff ( / ) (defun *ERROR* (ErrStr) (prog_main) ) As you can see the program grows but is less complex because we now have our logic contained within a few lines of code. You can follow the logic without having to understand the details of what the program is doing. The larger a program becomes the more important it is to hide the details, and leave only the logic out front. Lesson 4 Summary... The reasons we need to structure our programs are varied but certain reasons keep coming up again and again, so I will list them here. 1. Areas likely to change. Any area, which seems complex, should probably be moved to its own function where you can control the interface to the function. |
|