The Basics in a Nutshell - Part 1
So, you've never programmed in AutoLisp before!
You've tried to decipher some AutoLisp routines but, you are still totally confused!!!
Let's see if we can't help you out.
This tutorial will try and teach you the very basics of AutoLisp programming without overwhelming you with double-gook.
Let's start up with something very simple and that will give you immediate results. Fire up AutoCad and type this at the command prompt:
(alert "Hello and welcome to AutoLISP!")
Now press enter. This should appear on your screen :
Well Done, you've just used AutoLisp to make AutoCAD do something.
As you noticed using the (alert) function results in a dialogue box being displayed on your screen.
Let's try something else. Type this at the command prompt and press enter :
(setq a (getpoint))
Then choose a point anywhere on the screen.
A "list" of numbers, looking something like this, should appear in your command window.
(496.0 555.06 0.0)
This list, believe it or not, contains the x, y and z coordinates of the point you picked.
x = 496.04 y = 555.06 z = 0.0
The AutoLisp coding :
(setq a (getpoint))
Means, in plain English :
Get a point from the user and store the x, y and z values as a list in variable "a".
Did you notice how everything is enclosed within parenthesis?
All AutoLisp functions are surrounded by parenthesis.
As well, AutoLisp allows you to "nest" your functions.
This lets you write a function that evaluates another function.
Just remember, that you must leave the nest with an equal number of parenthesis. Here's an example :
(dosomething (dosomethingelse (andanotherthing)))
You could also write the above statement like this to make it more readable :
(dosomething (dosomethingelse (andanotherthing) ) )
Now you can see why "Lisp" is often known as "Lost in Stupid Parenthesis"
You can also add comments to your coding. Anything preceded with a semicolon is not evaluated by Autolisp and is treated as a comment, much the same way as the REM statement in Basic is used. e.g.
(dosomething (dosomethingelse (andanotherthing) ;This is a comment ) ;This is another comment ) ;and another comment
The statement we wrote earlier, told AutoLisp to get a point from the
user and store the value in variable "a".
Now type this at the command line :
The point list should be returned. So, any time that you would like to inspect a variable, just precede the variable name with "!"
Our getpoint function worked, but it didn't really tell the user what was expected from him by way of input. Try this now :
(setq a ( getpoint "\nChoose a Point : "))
Did you notice how Autolisp now asks you for input (and what type of input is expected.)
Let's write a programme.
Type in each of these lines, one at a time, pressing "Enter" at the end of each line, then choosing a point.
(setq a (getpoint "\nEnter First Point : "))
Press "Enter" then select a point.
(setq b (getpoint "\nEnter Second Point : "))
Again, Press "Enter" then select a second point.
(command "Line" a b "")
Press "Enter" again. A line should be drawn between the two
The (command) function is used to tell AutoCad what you want it to do.
"Line" Draw a Line a From the point stored in variable "a" b To the point stored in variable "b" "" Enter to close the Line command.
Now this is very nice, but do we have to type in all this coding every time we want to use this routine?
In Part 2 we will discuss how to "store" your programmes in a file.