In the last lesson, you began to learn how to employ AutoLISP to make your AutoCAD drafting work easier. You learned to add two numbers with AutoLISP, along with the other arithmetic functions -, *, and \. You saw how AutoLISP can be employed to perform a calculation in the middle of an AutoCAD command, and you saved the result of calculations using the SetQ function.
Autodesk made AutoLISP so powerful that it can manipulate almost any aspect of the AutoCAD drawing. In this tutorial, you get a taste of the many different kinds of functions AutoLISP offers you for manipulating numbers and words. As we start on our whirlwind tour of five groups of functions, start AutoCAD and type the examples in the text screen at the Command: prompt.
In addition to the four basic arithmetic functions you learned to use last month, AutoLISP has many of the mathematical functions you might expect in a programming language. The list includes trigonometric, logarithmic, logical, and bit manipulation functions; one type of function missing is matrix manipulation. For example, the Min function returns the smallest (minimum) of a list of numbers:
Command: (min 7 3 5 11)
3
To remember the result of this function, use the SetQ function, as follows:
Command: (setq minnbr (min 7 3 5 11))
3
Now each time you want to refer to the minimum value of that series of numbers, you can refer to variable Minnbr. Here's an example of a trig function,sine:
Command: (sin minnbr)
0.14112
Returns the sine of the angle of 3 radians.
Note that you must provide the angle in radians (such as the Minnbr), not degrees. This is an inconvenience, because if you are working with degrees, you must first convert the degrees to radians. Fortunately, AutoLISP can do this for you, so long as you code it correctly. Recall that there are 2*pi (approximately 6.3) radians in 360 degrees. For example, to get the sine of 45 degrees, you have to indulge in some fancy footwork, first:
Command: (sin (* (/ 45 180.0) pi))
0.707107
Here I divided the degrees (45) by 180, then multiplied by pi. Remember from the last tutorial that either the 45 or the 180 needs the decimal ( .0 ) to force division of real numbers, rather than integers. By the way, 'PI' is the only constant predefined by AutoLISP and is equal to 3.1415926. That means you just type PI, instead of 3.1415926 each time you need the value of pi in a function. To see this for yourself, use the exclamation mark at the command prompt:
Command: !pi
3.14159
AutoLISP displays the results to six decimal places, even though it performs calculations to 32-bit accuracy.
Since CAD deals with geometry, AutoLISP has a number of functions for dealing with geometrics. For example, the Distance function is just like the Dist command:
Command: (setq p1 '(1.3 5.7))
(1.3 5.7)
Command: (setq p2 '(7.5 3.1 11))
(7.5 3.1 11)
Command: (distance p1 p2)
6.72309
It returns the 3D distance between two points.
You may have missed that single quote mark in front of the list of x,y,z coordinates, as in: '(1.3 5.7). That tells AutoLISP you are creating a pair (or triple) of coordinates and that it shouldn't try to evaluate the numbers.
Don't use a comma: use a space to separate the values of the coordinates. Note also that if you leave out the z-coordinate, AutoLISP assumes it equals 0.0000. Other geometric functions of interest include the angle from 0 degrees (usually pointing east) to the line defined by P1 and P2:
Command: (angle p1 p2)
5.88611
The result is returned in radians. The intersection of two lines is determined by: Command: (inters pt1 pt2 pt3 pt4) The x,y,z-coordinates that result from applying an object snap mode to a point. The object has to actually exist:
Command: line
From point: !p1
To point: !p2
To point: [Enter]
Command: (osnap p1 "mid")
(4.4 4.4 5.5)
where the "mid" refers to the midpoint object snapmode. Thus, in this function, you are finding the midpoint of the line that starts at P1 (1.3, 5.7). The other geometric functions are Textbox (find the rectangular outline of a line of text) and Polar, returns a 3D point of a specified distance and angle.
You could say that conditional functions are the most important, since they define the existence of a programming language. If you're not sure if it's a programming language or merely a macro language, check for conditionals. The toolbar macros have no conditionals; they are not a true programming language.
It is conditionals that allow a computer program to "think" and make decisions. Conditional functions check if one value is less than, equal, or greater than another value. They check if something is true; they repeat an action until something is false.
For example, if the floor-to-ceiling distance is greater than eight feet (ie 96 inches), then draw 14 steps; else, draw 13 steps. A similar wording is used in condition functions:
Command: (if (> height 96) (setq steps 14) (setq steps 13))
13
Let's break this down to see how the function compares with our statement:
This function equates to the Basic language IF-Then-Else statement ie:
If Height > 96 Then
Let Steps = 14
Else
Let Steps = 13
You can manipulate strings (one or more characters) in AutoLISP but to a lesser extent than numbers. For example, you can find the length of a string as follows:
Command: (strlen "AutoCAD World")
13
The StrLen (short for STRing LENgth) function tells you that "AutoCAD World" has 13 characters in it, counting the space. Notice how "AutoCAD World" is surrounded by quote marks. That tells AutoLISP you are working with a string, not a variable. If you type (strlen AutoCAD World), AutoLISP tries to find the length of the strings held by variables Autocad and World. For example:
Command: (setq autocad "A software package")
A software package"
Command: (setq world "The planet earth")
The planet earth"
Command: (strlen autocad world)
34
Other string functions change all characters to upper or lower case, returns part of a string, finds a pattern in a string, and join two strings together, as follows:
Command: (strcat autocad " used all over " world)
A software package used all over The planet earth"
That's how you create reports, such as "13 steps drawn" by mixing text and variables.
Related to string functions are the conversion functions because some of them convert to and from strings. For example, earlier we showed how to convert 45 degrees to radians. That's fine for decimal degrees, like 45.3711 degrees. But how do you convert 45 degrees, 37 minutes and 11 seconds, which AutoCAD represents as 45d37'11"? That's where a conversion function like Angtof (short for ANGle TO Floating-point) comes in. It converts an angle string to a real number radians:
Command: (angtof "45d37'11\"" 1)
0.796214
Here we've supplied Angtof with the angle in degrees-minutes-seconds format.
Notice how the seconds indicator (") is handled: \". That's so it doesn't get confused with the closing quote mark (") that indicates the end of the string.
However, AutoLISP isn't smart enough to know that, so we tell it by means of the mode number, 1 in this case. This (and some other functions) use the following as mode numbers:
Mode --------- 0 1 2 3 4 |
Meaning |
Example |
Notice the similarity between the mode numbers and the values of system variable AUnits. The coincidence is not accident. When you don't know ahead of time the current setting of units, you make use of this fact by specifying the mode number as a variable, as follows:
Command: (angtof "45d37'11\"" (getvar "aunits"))
0.796214
Here we use GetVar (short for GET VARiable), the AutoLISP function that gets the value of a system variable. We used Getvar to get AUnits, which holds the state of angular display as set by the Units and DdUnits commands.
Other conversion functions convert one unit of measurement into another (via the Cvunit function and the Acad.Unt file), an integer number into a string, a character into its ASCII value (for example, letter A into ASCII 65), and translates (moves) a point from one coordinate system to another.
In this lesson, we got a taste of many of the different kinds of functions AutoLISP offers you for manipulating numbers and text. In the next lesson, we'll continue our tour by looking at the more esoteric functions that AutoLISP manipulate the drawing file.
Comments on this tutorial series? Tell me about it.
Return to home page.