
- •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
Figure 11.6: Finding the angle of an arc
Accessing Object Handles and Block Attributes
In the last section, you saw how to extract information from a complex object. You can use the same method to extract information from block attributes. The program you are about to examine uses block attributes as well as object handles and external files to help assign and store names to objects in your drawing. We will look at how entnext can be used to get attribute information from a block and how you can use attributes to permanently store information. You will also explore one possible way of using object handles which are permanent names AutoCAD can give to objects in a drawing.
Using Object Handles
We know that every object is given an object name. This name is the key to accessing the object's record in the drawing database. Unfortunately, this object name changes from editing session to editing session. This means that during one editing session, an object will have the object name <Object name: 600000d4> while in another session the same object will have the name <Object name: 60000012>. For the most part, this may not be of concern to you. But if you want to have a way of permanently identifying objects from one editing session to another, you may find this fact disturbing.
Fortunately, starting with release 10, you can add what is called an object handle to each and every object in your AutoCAD drawing. Handles are added to the object's in a drawing by AutoCAD whenever you turn on the handles function using the handles command. You do not have control over the naming of Objects, AutoCAD automatically assigns an alpha-numeric name to every object in the drawing.
The handent function in conjunction with other functions can be used to obtain an objects handle from the drawing
249
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
database. The handles are added to an object's property list as a group code 5 property sublist. To get an object's handle, you use the usual assoc-entget function combination to extract the sublist.
1.Return to the Chapt11 drawing and enter the following at the command prompt: handles
2.At the prompt:
Handles are disables.
ON/DESTROY:
enter ON. Next enter
(assoc 5(entget(car (entsel))))
3. At the select object prompt, pick the polyline box. You will get a list similar to the following:
(5 . "29")
The second element of the group 5 property is the object handle. Note that the handle is a numeric value in quotes, so it is really a string data type even though it is a number.
Using handles, you could write a simple routine to display an object's handle which you could record somewhere. Then, you could have another program to retrieve an object based on this handle. We've taken this idea a step further and have written a program that allows you to assign any name you like to an object and later select that object by entering the name you have assigned to it. Figure 11.7 shows this program and Figure 11.8 shows a diagram of how it works.
250
Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura
;Function to turn a list into a string---------------------------------------- |
|
(defun ltos (lst / gfile strname) |
|
(setq gfile (open "acad.grp" "w")) |
;open a file on disk |
(prin1 lst gfile) |
;print list to file |
(close gfile) |
;close file |
(setq gfile (open "acad.grp" "r")) |
;open file |
(setq strname (read-line gfile)) |
;read list from file |
(close gfile) |
;close file |
strname |
;return converted list |
) |
|
;Function to obtain name list stored in attribute----------------------------- |
|
(defun getatt (/ nament) |
|
(setq nament (ssname(ssget "X" '((2 . "NAMESTOR")))0)) |
;get attribute block |
(read (cdr (assoc 1(entget (entnext nament))))) |
;get attribute value |
) |
|
;Function to clear stored names-----------------------------------------------
(defun attclr ()
(setq nament (ssname (ssget "X" '((2 . "NAMESTOR")))0)) ;get attrib. block
(setq namevl |
(entget (entnext nament))) |
;get attrib. ent. list |
|
(setq namelt (assoc 1 namevl)) |
;get attrib. value |
||
(entmod (subst (cons 1 |
"()") namelt namevl)) |
;add list to attrib |
|
) |
|
|
|
;Program to assign a name to an entity |
|
||
;----------------------------------------------------------------------------- |
|
|
|
(defun C:NAMER (/ group gname ename sname namevl namelt) |
|
||
(setq ename |
(cdr (assoc 5 (entget (car (entsel "\nPick object: ")))))) |
||
(setq gname |
(list (strcase (getstring "\nEnter name of object: ")))) |
||
(setq group |
(getatt)) |
|
;get names from attrib. |
(setq gname |
(append gname (list ename))) |
;new name + ent. name |
|
(setq group |
(append group (list gname))) |
;add names to list |
|
(setq sname |
(ltos group)) |
;convert list to strng |
|
(setq namevl |
(entget (entnext (ssname (ssget "X" '((2 . "NAMESTOR")))0)))) |
||
(setq namelt (assoc 1 namevl)) |
;get attrib. value |
||
(entmod (subst (cons 1 |
sname) namelt namevl)) |
;add list to attrib |
|
(entupd (cdr (assoc -1 |
namevl))) |
|
|
(princ) |
|
|
|
) |
|
|
|
;Function to select an entity by its name------------------------------------- |
|
||
(defun GETNAME (/ group gname ) |
|
||
(setq gname |
(strcase (getstring "\nEnter name of entity: "))) |
||
(setq group |
(getatt)) |
|
;get names from attrib. |
(handent (cadr (assoc gname group)))
)
Figure 11.7: Program to give names to objects
251
Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura
Figure 11.8: Diagram of a program to name objects
This program makes use of a block attribute as a storage medium for the names you assign to objects. Lets take a closer look.
First, you need to define the attribute used for storage.
1. Exit the Chapt11 file and open an AutoLISP file called Namer.lsp. Copy the program shown in figure 11.7 into your file then save and exit the file.
252
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
2.Return to the Chapt11 file then load Namer.lsp.
3.Enter attdef to start the attribute definition command.
4.Enter the following responses to the attdef prompts:
Attribute modes -- Invisible:N Constant:N Verify:N Preset:N
Enter (ICVP) to change, RETURN when done:~CR
Attribute tag: name
Attribute name: name
Default attribute value: ()
Start point or Align/Center/Fit/Middle/Right/Style: 2,9
Height (2.0): ~CR
Rotation angle <0>: ~CR
5. The word name will appear in at coordinate 2,9. Now issue the block command and enter the following responses to the block prompts:
Block name (or ?): namestor
Insert base point: 2,9
select objects: [pick attribute defined in previous step.]
6. Issue the insert command and enter the following responses to the insert prompts: Block name (or ?): namestor
Insertion point: 2,9
X scale factor <1> / Corner / XYZ: ~CR
Y scale factor (default=X): ~CR
Rotation angle <0>: ~CR
Enter attribute values name <()>: ~CR
You have just defined the attribute within which the program namer will store your object names. Now you are ready to use the program.
253
Copyright © 2001 George Omura,,World rights reserved