
- •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
Chapter 4: Interacting with the Drawing Editor
Introduction
Using Getreal and Getint
Prompting the User for Distances
How to Control User Input
How to Use Getdist
Using Initget
A Sample Program Using Getdist
Prompting for Dissimilar Variable Types
How to Get Angle Values
Using Multiple Keywords
Using Getangle and Getorient
How to Select Groups of Object
How to Get Text Input
Using Ssget
Using Getstring
A Sample Program Using Ssget
Using Getkword
Conclusion
How to Get Numeric Values
Introduction
In the first three chapters of the book, you learned the basics of AutoLISP. You now have a framework within which you can begin to build your programs. We can now discuss each built-in AutoLISP functions without loosing you in AutoLISP nomenclature. In this and subsequent chapters, You will be shown how each of the built-in functions work. Since the AutoLISP interpreter can be used interactively, you can enter the sample expressions shown in this chapter at the AutoCAD command prompt to see first hand how they work.
A key element in a user defined function is how it obtains information from the user. In this chapter, you will look at some built in functions that expedite your programs information gathering. You have already seen the use of two of these functions, Getpoint and Getcorner, in chapter 2 and 3.
70
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Prompting the user for Distances
You will often find the need to prompt the user to obtain a distance value. At times, it is easier for the user to select distances directly from the graphic screen by using the cursor. AutoLISP provides the Getdist function for this purpose.
How to use Getdist
The syntax for Getdist is:
(getdist [optional point value] [optional prompt string])
You can optionally supply one or two arguments to Getdist. These arguments can be either string values which will be used as prompts to user when the program runs, or point values indicating a position from which to measure the distance. Getdist will accept both keyboard input as well as cursor input and it always returns values in real numbers regardless of what unit style is current. This means that if you have your drawing set up using an architectural unit style, Getdist will return a value in decimal units. The following exercises will demonstrate these uses.
There are three ways to use Getdist. First, you can use it to input a distance by picking two points using the cursor. For example:
1.Open a file called Chapt4=. Set the snap mode and dynamic coordinate readout on.
2.Enter the following:
(setq dist1 (getdist "Pick point or enter distance: "))
The prompt line will display:
Pick first point or enter distance:
This prompt is the same as the string value used as the argument to Getdist. At this prompt, you can enter a numeric value in the current unit style or in decimal units or you can pick a beginning point using the cursor.
3. Pick a point at coordinate 3,3 with your cursor. You will get the prompt:
Second point:
and a rubber banding line will appear from the first point selected.
4.Pick a second point at coordinate 8,3 with you cursor, the distance between the two points selected is assigned to the variable dist1.
5.Enter the following:
!dist1
71
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
The distance value stored by dist1 is displayed.
5.0
Using the same expression as above, you can also enter a distance value at the keyboard. 1. Enter the following:
(setq dist1 (getdist "Pick point or enter distance: "))
The prompt line will display:
Pick first point or enter distance:
2.In the previous exercise, you picked a point at this prompt and Getdist responded by asking for a second point. Instead of picking a point, enter the value of 6.5. Once you have done this, 6.5 is assigned to the variable dist1.
3.Enter the following:
!dist1
The distance value stored by dist1 is displayed.
6.5
A third way to use Getpoint is to supply a point variable as an argument.
1. Enter the following at the command prompt:
(setq pt1 (getpoint "Enter a point: "))
When the Enter a point prompt appears, pick a point at the coordinate 3,3. The coordinate 3,3 is assigned to the variable pt1.
2. Enter the following expression:
(setq dist1 (getdist pt1 "Enter a second point: "))
Notice that the variable pt1 is used as an argument to getdist. A rubber-banding line appears from pt1 and the prompt displays the string prompt argument you entered with the expression.
3.Pick a point at the coordinate 9,6. The distance from pt1 to the point 9,6 is assigned to the variable dist1.
4.Enter the following to get the distance stored by dist1:
!dist1
6.7082
As you can see, getdist is quite flexible in the way it will accept input. This flexibility makes it ideal when you need
72
Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura
to get sizes of objects from the user. The user has the flexibility to either enter a size value at the keyboard or visually enter a size by picking points from the drawing area. The rubber-banding line provided by getdist allows the user to visually select a size. Several AutoCAD commands act in a similar manner. For example, the Text command allows you to select a text size either by entering a height or by visually selecting a height using your cursor. Figure 4.1 summarizes the three ways you can use Getdist.
Figure 4.1: Three methods that Getdist accepts for input
73
Copyright © 2001 George Omura,,World rights reserved