[Click Here for Info on Placing Your Banner Ad Here]
CLICK ON THE IMAGE TO FIND OUT HOW TO PLACE YOUR AD WITH PAUG

Command of the Month: June 1998
Arguing with AutoLISP (Using Arguments with AutoLISP)

AutoLISP is a programming language that is built into AutoCAD and is a sub set of a complex programming language known as CommonLISP or LISP1.5.  Its origin takes us back to the 1950's when a program developer needed a language that could have "Artificial Intelligence".  John. McCarthy developed LISP in the late 50's based on mathematical expression or algebraic LISt Processing (hence the name).  To our dismay, LISP is not an acronym for "Lost in Stupid Parentheses". AutoLISP, even though it is smaller than LISP1.5, is still very powerful and contains over 170 built in functions.  Each function performs a specific task.  In this exercise we are assuming some basic AutoLISP syntax is known.

Arguments

The value passed to a function is called an argument.  An argument can be either literal or a variable.  The function receives these values and uses them in its processing and then returns a result.  The example code is showing the the cosine function with an argument of 45.  The AutoLISP interpreter passes the argument 45 to the cos function for processing.  In response, the cos function calculates and returns the result which the setq function assigns to the variable "x".

Passing Arguments to a function

In order to call and pass arguments to a function, you simply follow the functions name with the arguments you wish to pass.   For example the highlighted expression the getstring function is an argument that is requesting user input.  Once the user gives the requested information to the function, the AutoLISP interpreter passes the argument to the setq function which stores the argument in the variable "mk".

Declaring Global or Local Variables

For user defined functions such as this one called type-1a you have the option to declare local or global variables.  To do this you must first define a function and name it, in this case the argument for the defun function is "type-1a".  After you name your function, you can declare the variables that you would like to remain local.   To do this you you will place a space and then a front slash followed by another space and then the local variable names in the variable declaration area.  By declaring a variable local, the information contained in the variable will remain only in that program.  If variables are not declared they will then become global and the information contained in the variable can be passed onto another LISP routine. 

The Type-1a AutoLISP Routine

The Type-1a LISP routine is a program that based on user input will place dimensional information into an AutoCAD drawing for the fabrication of roof or floor penetration frames that support the deck around HVAC ducts.

After you define the program name and declare any local variables.   The real stuff begins.  First this program is asking the user to enter some information.  Using the getstring, getkword and getint, functions we are passing information from the arguments which are in the form of questions to the user.  This information is passed on to the setq function which places the information into the variables "mk", "type" and "qty".  Next we are verifying and setting up a continuous default based on the result from the users input from the question posed using the getkword and strcat functions and passing the information to the variable "choice".  Here the initget function is used to force the user into answering the functions argument with predictable information.  That is, only JOIST, BEAM or JB will be allowed as user input. Hence artificial intelligence.   Now based on the users response, one of three things will happen.
If the response is JOIST, the argument for the first if function will be satisfied, then the program will issue the number 1 to the variable "d" and the number 4 to the variable "h" which are the arguments to the setq function.
If the response is BEAM, the second conditional if function will be met and the program will issue another question.  The results to argument for the getreal function  will invoke several other function and mathematical equations.  The same type of functions and arguments are used if the response is JB.

In Closing

Function arguments allow the programmer to create and manipulate data.   When you use arguments to a function call you can minimize your code and make it easier to debug.  As you can see, AutoLISP is very versatile and even though it is a subset of CommonLISP or LISP1.5 its uses in AutoCAD are limitless.  The program TYPE-1A has approximately 200 lines of code and will place schematic information into an AutoCAD drawing in about 1-1/2 seconds.



Command of the Month: Main
Command of the Month: 1999 | Command of the Month: 1998 | Command of the Month: 1997
Jan 98 | Feb 98 | Mar 98| Apr 98 | May 98 | Jun 98 | Jul 98 | Aug 98 | Sep 98


If you have any questions, comments, or suggestions about the Command of the Month,
please send e-mail to mail@paug.org.