Satish Lele
satish.lele@gmail.com


Program Design
defun: Defines a function
(defun symbol_name (arguments / local_variables) expressions …..)
The defun function defines a function with the name symbol_name

Argument-list: (arguments / local variables). Argument is variable passed to the function. Local variables confine these variables to the function, and lose their value once function is over. Variables left out in this list become global variables.
The use of local variables ensures that the variables in your functions are unaffected by the surrounding application and that your variables do not remain available after the calling function has completed its task.
Many user-defined functions are used as utility functions within larger applications. User-defined functions also typically contain a number of variables whose values and use are specific to that function.
The danger in using global variables, instead of local variables, is you may inadvertently modify them outside of the function they were declared in and intended for. This can lead to unpredictable behavior, and it can be very difficult to identify the source of this type of problem.
Another advantage of using local variables is that AutoCAD can recycle the memory space used by these variables, whereas global variables keep accumulating within AutoCAD memory space.

Functions with Arguments: The symbols to use as arguments are defined in the argument list before the local variables. Arguments are treated as a special type of local variable; argument variables are not available outside the function. You cannot define a function with multiple arguments of the same name.

C:XXX Functions: If an AutoLISP function is defined with a name of the form C:xxx, it can be issued at the AutoCAD Command prompt in the same manner as a built-in AutoCAD command. You can use this feature to add new commands to AutoCAD or to redefine existing commands.
To use functions as AutoCAD commands, be sure they adhere to the following rules:
The function name must use the form C:XXX (upper- or lowercase characters). C:XXX functions can be used to override built-in AutoCAD commands. The function must be defined with no arguments. However, local variables are permitted and it is a good programming practice to use them.

Program Design: These are basic steps in design. This four step process can be applied to every program. If you design your prompts and prompt sequences to match closely to those of AutoCAD’s, your program will seem more familiar and therefore will be easier to use.
Establish the name of the program or function
Obtain information by prompting the user
Process the information
Produce the output

It is often helpful to think how you would accomplish your programming task using AutoCAD, without the aid of AutoLISP. Then think how AutoLISP can automate the process. The pseudocode, written in plain language can help in describing the task. It is written with the code, preceded by ;. Anything written after ; up to the end of the line is considered as a comment and neglected by AutoLISP. It often helps to insert comments into a program as a means of giving a plain language description of the code. Excessive use of comments, can cause slower loading of the program.
You can split the functions into smaller functions and call them within the other. You can create a master function, which calls and executes other functions. This modular approach to programming has several advantages. All the variables used in other functions are left as global, and are designated as local in master function. Each function exists independently and can be used by other programs. This reduces the amount of overall code needed to write the full program. You can more easily locate bugs because you will be able to trace them to one function or another. Because smaller groups of code are more manageable, they make problem solving tasks easier. Breaking programs into modules helps to clarify your code. You can use upper case and lower case letters in case of variables as these are not case sensitive. It is case sensitive only in case of strings. “Yes” is different from ”yes”.

Arguments / Local Variables: The variables used in the programs can appear in argument list or as local variables. In order to conserve memory, it is good practice to keep the variable names as short of possible. Whenever possible, use the variable names again and again to conserve memory. Redefine functions and variables to nil.

exit: Forces the current application to quit. (exit)
If exit is called, it returns the error message quit/exit abort and returns to the AutoCAD Command prompt.

quit: Forces the current application to quit. (quit)
If quit is called, it returns the error message quit/exit abort and returns to the AutoCAD Command prompt.

load: Loads an AutoLISP file. (load filename [onfailure])
The filename argument is a string that represents the file name. If the filename argument does not specify a file extension, .lsp is assumed. If the load function fails, it returns the value of the onfailure argument. However, if onfailure is not provided, a load failure causes an AutoLISP error. If the operation is successful, load returns the value of the last expression in the file. The filename can include a directory prefix, as in "/function/test1". On DOS systems, a drive letter is also permitted. A forward slash (/) or two backslashes (\\) are valid directory delimiters. If you don't include a directory prefix in the filename string, load searches the AutoCAD library path for the specified file. If the file is found anywhere on this path, load then loads the file. If the onfailure argument is a valid AutoLISP function, it is evaluated. In most cases, the onfailure argument should be a string or an atom. This allows an AutoLISP application calling load to take alternative action upon failure.
backBack top