AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Home. Page II. Page III. Page IV.

The Basics in a Nutshell - Page I.

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 "Yebo Gogo")

Now press enter. This should appear on your screen :

Lisp 16a

Well Done, you've just used AutoLisp to make AutoCad do something.
By the way, most other programming tutorials use "Hello World" as a similar example. But, because this is an African site, I thought that I would use a very well known Zulu phrase instead.

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 treat 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 :

	!a

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 points.
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?

On the next page we will discuss how to "store" your programmes in a file.

Home. Page II. Page III. Page IV.
 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website