Sunday, December 28, 2014

AutoLISP Tutorial for AutoCAD Users: Lesson 1

You are at home in AutoCAD, and you would like to harness the power of AutoLISP. What a perfect fit! Make yourself at home.

AutoLISP is for AutoCAD

The good news is that, like you, AutoLISP is right at home in AutoCAD. In fact, for this tutorial, you need to have AutoCAD running.

The more you know AutoCAD, the more you have in common with AutoLISP. Once you get a few preliminaries out of the way and begin to speak the language, you will be creating meaningful commands or applications right away.

AutoLISP is in AutoCAD

AutoLISP is in AutoCAD, right at home with you. You will write your first AutoLISP without leaving this home.

You need to have the command area showing as in the picture below.

Hello, world!

With AutoLISP, it doesn't take a bunch of preliminaries to say "Hello, world! This is my first AutoLISP program."

Type your first AutoLISP program in the command area. Don't forget the parentheses; they tell AutoCAD that everything inside is an AutoLISP expression:

(alert "Hello world! This is my first AutoLISP program.")
Look at that! You got a pretty message box with no more effort than that. This may turn out to be easier than you feared.

The (alert) function is great for presenting a show-stopping AutoCAD message in the middle of a program. If you are curious-minded, you may have tried typing

If you did, your program crashed. And AutoCAD very politely said:

; error: too few arguments
That is AutoCAD's way of giving you a clue that you didn't use the alert function right. You didn't give (alert) anything to say. Or in programming lingo, you didn't supply a string ("some text in quotation marks") argument. Since (alert) couldn't say anything, AutoCAD tried to be helpful by telling you why your program crashed.

Hello, world, again.

If you don't need to stop the show, you can say "Hello world" like this:

(prompt "\nHello world, again.")
Hmm. That isn't as impressive. All it does is put "Hello world" on the command line. But that is how you will probably want to say things most often in your programs.

But what is the "\n" for? "\n" is a new line character. Other special characters are "\r" (carriage return without a new line), "\t" (tab), "\\" (backslash), and "\"" (quotation mark).

Turbo-charged Hello, world

Let's finish this lesson by using (alert) to put up a little more complicated message:

(alert "Lesson 1: Hello, world\n\n\t(alert) shows a box\n\t(prompt), (princ), (print), and (prin1) don't.")
Note that inside the quotation marks, AutoCAD ignored all the parentheses. Inside quotation marks (strings), everything is ignored except the special characters mentioned above, and of course any quotation marks

Tip: Forgetting that quotation marks end strings is one of the most common programming errors. Bad example: (alert "A yard is 36" long.") Good example: (alert "A yard is 36\" long.")

Other ways to say Hello

Other ways to simply say "Hello world" are by using the (princ), (print), and (prin1) functions. You can read about their nuances in the AutoCAD Help AutoLISP Reference chapter. Functions that get user input like (getstring), (getreal), (getint), (getpoint), (getdist), and (getkword) also allow you to supply a string (text) prompt as an argument. Example: (getint "\nNumber of objects: ").

Lesson 2

No comments: