| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Lesson 1

Page history last edited by Matthew Haws 2 years, 2 months ago

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

 

(alert)

 

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).

 

And why does it say "nil" afterward? Everything inside parentheses is an AutoLISP expression that returns a value. For example (sin 0) returns 0, (+ 1 1 1) returns 3, and (getvar "clAyER")  or (getvar "clayer") returns the name of the current layer. Some expressions like (prompt) always return "nil" instead of any meaningful information. Notably, the empty (princ) expression returns nothing at all, and this is used at the end of programs (who return their last return value) to keep them from muttering "nil" when they end.

 

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

 

Comments (0)

You don't have permission to comment on this page.