- •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
(defun C:SEQ (/ pt1 currnt last)
(setq pt1 |
(getpoint "\nPick start point: ")) |
(setq spc |
(getdist pt1 "\nEnter number spacing: ")) |
(setq currnt |
(getint "\nEnter first number: ")) |
(setq last |
(getint "\nEnter last number: ")) |
(setq stspc |
(rtos spc 2 2)) |
(setq stspc |
(strcat "@" stspc "<0" )) |
(command "text" pt1 "" "" currnt) (repeat (- last currnt)
(setq currnt (1+ currnt))
(command "text" stspc "" "" currnt)
)
)
Figure 7.8: The sequential number program
Converting a Number to a String
Exit AutoCAD and open the AutoLISP file seq.lsp. Make the changes shown in bold face type in figure 7.10. Save and exit the seq.lsp file and return to the file chapt7. Load the C:SEQ program and do the following:
1.Enter seq at the command prompt.
2.At the prompt:
Pick start point:
pick a point at coordinate 1,3.
3. At the prompt:
Enter spacing:
enter .5.
4. At the prompt:
Enter first number:
enter 4.
147
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
5. At the last prompt:
Enter last number:
enter 12.
The numbers 4 through 12 will appear beginning at your selected start point and spaced at 0.5 unit intervals.
The program starts by prompting the user to pick a starting point:
(defun C:SEQ (/ pt1 pt2 currnt last)
(setq pt1 (getpoint "\nPick start point: "))
A new prompt is added that obtains the spacing for the numbers:
(setq spc (getdist pt2 "\nEnter number spacing: "))
The spacing is saved as the symbol spc. The program continues by prompting the user to enter starting and ending value:
(setq currnt (getint "\nEnter first number: "))
(setq last (getint "\nEnter last number: "))
Next, the function rtos is used to convert the value of spc to a string:
(setq stspc (rtos spc 2 2))
the syntax for rtos is:
(rtos [real or integer value][unit style code][precision])
The first argument to rtos is the number being converted. It can be a real or integer. The next argument is the unit style code. Table shows a listing of these codes and their meaning.
Code |
Format |
1 |
Scientific |
2 |
Decimal |
3 |
Feet and decimal inches |
4 |
Feet and inches |
5 |
Fractional units |
148
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
This code determines the style the number will be converted to. For example, if you want a number to be converted to feet and inches, you would use the code 4. The third argument, precision, determines to how many decimal places to convert. In our example, we use the code 2 for unit style and 2 for the number of decimal places to convert to a string.
The next expression combines the converted number with the strings "@" and "<0" to form a string that can be used in with the command function:
(setq stspc (strcat "@" stspc "<0" ))
The next two expressions set up the location of the beginning of the text:
(command "text" pt1 "" "" currnt)
This is done because in the next set of expressions, The string that locates the text, "@distance<0", gives a distance and direction rather than a point. The previous expressions locate a point which will cause the Text command in the next expression to place the text in the proper place:
(repeat (- last currnt)
(setq currnt (1+ currnt))
(command "text" stspc "" "" currnt)
)
)
In the last three expressions, the repeat function is used to issue the text command, enter the number and advance to the next number repeatedly until the last number is in place (see figure 7.9).
Figure 7.9: Writing the numbers into the AutoCAD file
149
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Converting Other Data Types
Before we continue, we should briefly look at several other functions that offer data type conversion. These functions are listed in table .
Function
angtos ascii atoi itoa
chr
Uses
Converts real numbers (radians) into string values. Converts a string into its ASCII character code. Converts a string a string into an integer. Converts an integer to a string.
Converts an integer representing an ASCII character code into a string.
Angtos works in a similar way to rtos. It accepts a unit style code and a precision value. Its syntax is:
(angtos [angle value][unit style code][precision])
All of the other functions listed in table take a single item, the value to be converted, as their argument. For example, to convert an integer into a string, you could use the following expression:
(itoa 55)
The resulting value is "55".
The functions ascii and chr convert ASCII character codes. These are numeric values that represent letters, numbers and symbols. Figure 7.10 shows these codes and their meaning.
150
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Code Meaning |
Code Meaning |
Code |
Meaning |
Code Meaning |
Code Meaning |
Code Meaning |
|||||
07 |
Beep |
46 . |
65 |
A |
84 |
T |
103 |
g |
122 |
z |
|
09 |
Tab |
47 |
/ |
66 |
B |
85 |
U |
104 |
h |
123 |
{ |
10 |
New line |
48 |
0 |
67 |
C |
86 |
V |
105 |
i |
124 |
| |
13 |
Return |
49 |
1 |
68 |
D |
87 |
W |
106 |
j |
125 |
} |
27 |
Escape |
50 |
2 |
69 |
E |
88 |
X |
107 |
k |
126 |
~ |
32 |
Space |
51 |
3 |
70 |
F |
89 |
Y |
108 |
l |
|
|
33 |
! |
52 |
4 |
71 |
G |
90 |
Z |
109 |
m |
|
|
34 |
" |
53 |
5 |
72 |
H |
91 |
[ |
110 |
n |
|
|
35 |
# |
54 |
6 |
73 |
I |
92 |
\ |
111 |
o |
|
|
36 |
$ |
55 |
7 |
74 |
J |
93 |
] |
112 |
p |
|
|
37 |
% |
56 |
8 |
75 |
K |
94 |
^ |
113 |
q |
|
|
38 |
& |
57 |
9 |
76 |
L |
95 |
_ |
114 |
r |
|
|
39 |
' |
58 |
: |
77 |
M |
96 |
` |
115 |
s |
|
|
40 |
( |
59 |
; |
78 |
N |
97 |
a |
116 |
t |
|
|
41 |
) |
60 |
< |
79 |
O |
98 |
b |
117 |
u |
|
|
42 |
* |
61 |
= |
80 |
P |
99 |
c |
118 |
v |
|
|
43 |
+ |
62 |
> |
81 |
Q |
100 |
d |
119 |
w |
|
|
44 |
, |
63 |
? |
82 |
R |
101 |
e |
120 |
x |
|
|
45 |
- |
64 |
@ |
83 |
S |
102 |
f |
121 |
y |
|
|
Figure 7.10: The ASCII character codes
151
Copyright © 2001 George Omura,,World rights reserved