Bilro Logo

Autolisp 2

Home Autocad Tips and Trips Index
 
This is not a book on the how-to of lisp. There are lots of those.
It is only intended as an introduction.

The Development Approach

1. Start simple. 2. Test it. Once you have one bit working, move on to the next bit. Don't try to write the whole program all at once, all in one hit. You will make mistakes, and these are quite tricky to find with Lisp. Make your first lisp routine 4 or 4 lines if you can. Leave the handrail generator for when you have 4 weeks off. Looking for help? Try going Tools/Autolisp/Visual Lisp Editor. Once the Editor is up you can hit help. Someone at Autodesk is a smart one, because he put all the lisp commands under an aphabetical listing in the help section. Which is good if you know the name of the command.

Basics of Variables

What is a variable? Just a name, that you have made up, and you assign a value to. This value might change over usage.

For instance, consider:

(setq the_length 22) The minute Autocad sees this it says "Aha! that variable the_length, it has an integer value of 22!" You may not know what an integer is. It is definitely nothing complicated, all it means is it is a whole number. So for instance 22.24 would not be an integer. Just a trap for young players here....do not use variables like angle or length. These are already reserved for Autocad's own use and causes trouble if you do (Yes...I am guilty!)

How about:

(setq the_layer_name "Fred") Here, the variable is being set to a string (whats a string?-just a bunch of characters) You could change this in the course of the program by going: (setq the_layer_name "Sam")

Naming of Variables

I'm afraid I have been a bad boy as far as consistency is concerned here: I seem to choose whatever takes my fancy at the time. Sometimes I like running all the letters together, for instance thelayername. Other times I go the_layer_name or TheLayerName. Just remember lisp does not care about upper or lower case so just choose the system that suits you best. But remember: One day you might have to read your own program back, and having variables like "e" or "b" or "updown" won't make a lot of sense.

Get a peer review

At one stage I had written a 3D stair generator, which I was intensely proud of. Until I gave it to a friend, who kindly pointed out that it did not compute the stairs he thought it should be able to. He was right. I was wrong. But I went away and corrected it and now it goes nicely. I hope to post this over the Xmas period, along with some other ones. There are good books out there, I bought one (Woops in 1992!) called Maximising Autolisp by Rusty Gesner and Joseph Smith, and I have had good use out of it.