
- •Introduction
- •Who should read this book
- •How This Book Is Organized
- •How to Use This Book
- •Where to Find the LISP Programs
- •CHAPTER 1: Introducing AutoLISP
- •Understanding the Interpreter and Evaluation
- •The Components of an Expression
- •Using Arguments and Expressions
- •Using Variables
- •Understanding Data Types
- •Integers and Real Numbers
- •Strings
- •Lists
- •File Descriptors
- •Object Names
- •Selection Sets
- •Symbols
- •Subrs
- •Atoms
- •Assigning Values to Variables with Setq
- •Preventing Evaluation of Arguments
- •Applying Variables
- •Functions for Assigning Values to Variables
- •Adding Prompts
- •CHAPTER 2: Storing and Running Programs
- •Creating an AutoLISP Program
- •What you Need
- •Creating an AutoLISP File
- •Loading an AutoLISP file
- •Running a Loaded Program
- •Understanding How a Program Works
- •Using AutoCAD Commands in AutoLISP
- •How to Create a Program
- •Local and Global Variables
- •Automatic Loading of Programs
- •Managing Large Acad.lsp files
- •Using AutoLISP in a Menu
- •Using Script Files
- •CHAPTER 3: Organizing a Program
- •Looking at a Programs Design
- •Outlining Your Programming Project
- •Using Functions
- •Adding a Function
- •Reusing Functions
- •Creating an 3D Box program
- •Creating a 3D Wedge Program
- •Making Your Code More Readable
- •Using Prettyprint
- •Using Comments
- •Using Capitals and Lower Case Letters
- •Dynamic Scoping
- •CHAPTER 4: Interacting with the Drawing Editor
- •A Sample Program Using Getdist
- •How to Get Angle Values
- •Using Getangle and Getorient
- •How to Get Text Input
- •Using Getstring
- •Using Getkword
- •How to Get Numeric Values
- •Using Getreal and Getint
- •How to Control User Input
- •Using Initget
- •Prompting for Dissimilar Variable Types
- •Using Multiple Keywords
- •How to Select Groups of Objects
- •Using Ssget
- •A Sample Program Using Ssget
- •CHAPTER 5: Making Decisions with AutoLISP
- •Making Decisions
- •How to Test for Conditions
- •Using the If function
- •How to Make Several Expressions Act like One
- •How to Test Multiple Conditions
- •Using the Cond function
- •How to Repeat parts of a Program
- •Using the While Function
- •Using the Repeat Function
- •Using Test Expressions
- •CHAPTER 6: Working With Geometry
- •How to find Angles and Distances
- •Understanding the Angle, Distance, and Polar Functions
- •Using Trigonometry to Solve a Problem
- •Gathering Information
- •Finding Points Using Trigonometry
- •Functions Useful in Geometric Transformations
- •Trans
- •Atan
- •Inters
- •CHAPTER 7: Working with Text
- •Working With String Data Types
- •Searching for Strings
- •Converting a Number to a String
- •How to read ASCII text files
- •Using a File Import Program
- •Writing ASCII Files to Disk
- •Using a Text Export Program
- •CHAPTER 8: Interacting with AutoLISP
- •Reading and Writing to the Screen
- •Reading the Cursor Dynamically
- •Writing Text to the Status and Menu Areas
- •Calling Menus from AutoLISP
- •Drawing Temporary Images on the Drawing Area
- •Using Defaults in a Program
- •Adding Default Responses to your Program
- •Dealing with Aborted Functions
- •Using the *error* Function
- •Organizing Code to Reduce Errors
- •Debugging Programs
- •Common Programming Errors
- •Using Variables as Debugging Tools
- •CHAPTER 9: Using Lists to store data
- •Getting Data from a List
- •Using Simple Lists for Data Storage
- •Evaluating Data from an Entire List at Once
- •Using Complex Lists to Store Data
- •Using Lists for Comparisons
- •Locating Elements in a List
- •Searching Through Lists
- •Finding the Properties of AutoCAD Objects
- •Using Selection Sets and Object Names
- •Understanding the structure of Property Lists
- •Changing the properties of AutoCAD objects
- •Getting an Object Name and Coordinate Together
- •CHAPTER 10: Editing AutoCAD objects
- •Editing Multiple objects
- •Improving Processing Speed
- •Using Cmdecho to Speed up Your Program
- •Improving Speed Through Direct Database Access
- •Filtering Objects for Specific Properties
- •Filtering a Selection Set
- •Selecting Objects Based on Properties
- •Accessing AutoCAD's System Tables
- •CHAPTER 11: Accessing Complex Objects
- •Accessing Polyline Vertices
- •Defining a New Polyline
- •Drawing the new Polyline
- •Testing for Polyline Types
- •How Arcs are Described in Polylines
- •Accessing Object Handles and Block Attributes
- •Using Object Handles
- •Using Object Handles
- •Extracting Attribute Data
- •Appendix A: Menu Primer
- •Appendix B: Error Messages
- •Appendix C: Group Codes

The ABC’s of AutoLISP by George Omura
The while expression does not always include prompts for user input. Figure 5.12 shows a simple program that inserts a sequence of numbers in increasing value. Each number is increased by one and they are spaces 0.5 units apart. The user is prompted for a starting point and a first and last number. Once the user inputs this information, the program calculates the number to be inserted, inserts the number using the text command, calculates the next number and so on until the last number is reached.
;Program to draw sequential numbers -- Seq.lsp |
|
(defun C:SEQ (/ pt1 currnt last) |
|
(setq pt1 (getpoint "\nPick start point: ")) |
|
(setq currnt (getint "\nEnter first number: ")) |
|
(setq last (getint "\nEnter last number: ")) |
|
(command "text" pt1 "" "" currnt) |
;write first number |
(while (< currnt last) |
;while not last number |
(setq currnt (1+ currnt)) |
;get next number |
(command "text" "@.5<0" "" "" currnt) |
;write value of currnt |
);end while |
|
);end seq |
|
Figure 5.12: Sequential number program
This program expects the current text style to have a height of 0.
Using the Repeat Function
Another function that performs recursions is the repeat function. Repeat works in a similar way to While but instead of using a predicate to determine whether to evaluate its arguments, repeat uses an integer value to determine the number of times to perform an evaluation. The syntax for repeat is:
(repeat [n]
(expression 1)(expression 2) (expression 3) ...
)
The n above is an integer or a symbols representing an integer.
108
Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura
The program in Figure 5.13 shows the sequential number program using repeat instead of while. When run, this program appears to the user to act in the same way as the program that uses while.
;Program to write sequential numbers using Repeat |
|
(defun C:SEQ (/ pt1 currnt last) |
|
(setq pt1 (getpoint "\nPick start point: ")) |
|
(setq currnt (getint "\nEnter first number: ")) |
|
(setq last (getint "\nEnter last number: ")) |
|
(command "text" pt1 "" "" currnt) |
;write first number |
(repeat (- last currnt) |
;repeat last - currnt times |
(setq currnt (1+ currnt)) |
;add 1 to currnt |
(command "text" "@.5<0" "" "" currnt) |
;write value of currnt |
);end repeat |
|
);end seq |
|
|
|
Figure 5.13: Sequential number program using repeat
Using Test Expressions
So far, we have shown you functions that perform evaluations based on the result of some test. In all the examples, we use predicates and logical operators for testing values. While predicates and logical operators are most commonly used for tests, you are not strictly limited to these functions. Any expression that can evaluate to nil can also be used as a test expression. Since virtually all expressions are capable of returning nil, you can use almost any expression as a test expression. The following function demonstrates this point:
(defun MDIST (/ dstlst dst) (setq dstlst '(+ 0))
(while (setq dst (getdist "\nPick distance or Return to exit: ")) (Setq dstlst (append dstlst (list dst)))
(princ (Eval dstlst)) );end while
);end MDIST
109
Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura
This function gives the user a running tally of distances. The user is prompted to pick a distance or press Return to exit. if a point is picked, the user is prompted for a second point. The distance between these two points is displayed on the prompt. The Pick distance prompt appears again and if the user picks another pair of points, the second distance is added to the first and the total distance is displayed. This continues until the user presses return. The following discussion examines how this function works.
As usual, the first line defines the function. The second line creates a variable called dstlst and gives it the list value (+ 0).
(defun MDIST (/ dstlst dst) (setq dstlst '(+ 0))
The next line begins the while portion of the program. Instead of a predicate test, however, this expression uses a setq function.
(while (setq dst (getdist "\nPick point or Return to exit: "))
As long as points are being picked, getdist returns a non-nil value to setq and while repeats the evaluation of its arguments. When the user presses return, getdist returns nil and while quits evaluating its arguments. We see that while is really only concerned with nil and non-nil since the test expression in this example returns a value other than T.
The next few lines append the current distance to the list dstlst then evaluates the list to obtain a total:
(Setq dstlst (append dstlst (list dst)))
(princ (eval dstlst))
The function princ prints the value obtained from (eval dstlst) to the prompt (see Figure 5.14).
;Program to measure non-sequential distances -- Mdist.lsp
(Defun C:MDIST (/ dstlst dst) |
|
(setq dstlst '(+ )) |
;create list with plus |
;while a return is not entered ... |
|
(while (setq dst (getdist "\nPick point or Return to exit: ")) |
|
(Setq dstlst (append dstlst (list dst))) |
;append distance value |
(princ (Eval dstlst)) |
;print value of list |
);end while |
|
);end mdist
Figure 5.14: The Mdist function
110
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Conclusion
You have been introduced to several of the most useful functions available in AutoLISP. You can now begin to create functions and programs that will perform time consuming, repetitive tasks quickly and easily. You can also build-in some intelligence to your programs by using decision making functions. You may want to try your hand at modifying the programs in this chapter. For example, you could try to modify the Mdist function to save the total distance as a global variable you can later recall.
In the next chapter, you will get a brief refresher course in geometry.
111
Copyright © 2001 George Omura,,World rights reserved