- •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
(setq cent (osnap cpt1 "center"))
Here the point picked previously as the point on the circle cpt1 is used in conjunction with the osnap function to obtain the center point of the circle. The syntax for osnap is:
(osnap [point value][osnap mode])
The osnap function acts in the same way as the osnap overrides. If you use the center osnap override and pick a point on the circle, you get the center of the circle. Likewise, the osnap function takes a point value and applies an osnap mode to it to obtain a point. In this case, osnap applies the center override to the point cpt1 which is located on the circle. This gives us the center of the circle which is assigned to the symbol cent.
The next expression obtains the circle's radius
(setq rad1 (distance cpt1 cent))
The distance function is used to get the distance from cpt1, the point located on the circle, to cent, the center of the circle. This value is assigned to the symbol rad1.
Finding Points Using Trigonometry
At this point, we have all the known points we can obtain without utilizing some math. ultimately, we want to find the intersection point between the circle and the cut axis. By using the basic trigonometric functions, we can derive the relationship between the sides of triangle. In particular, we want to look for triangles that contain right angles. If we analyze the known elements to our problem, we can see that two triangles can be used to find one intersection on the circle (See figure 6.14)
Figure 6.14: Triangles used to find the intersection
128
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
In our analysis, we see that we can find a point along the cut axis that describes the corner of a right triangle. To find this point, we only need an angle and the length of the hypotenuse of the triangle. We can then apply one of the basic trigonometric functions shown in figure 6.15 to our problem.
Figure 6.15: Basic trigonometric functions
The sine function is the best match to information we have.
sine(angle) = opposite side / hypotenuse
This formula has to be modified using some basic algebra to suite our needs:
opposite side = hypotenuse * sine (angle)
Before we can use the sine function, we need to find the angle formed by points lpt1, lpt2 and cent (see figure 6.16).
Figure 6.16: Angle needed to before the sine function can be used.
129
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
The following function does this for us:
(setq ang1 (- (angle lpt1 cent) (angle lpt1 lpt2)))
The first of these three functions finds the angle described by points lpt1 and lpt1. The second expression finds the angle from lpt1 to the center of the circle. The third line finds the difference between these two angles to give us the angle of the triangle we need (see figure 6.17).
Figure 6.17: The sine expression written to accommodate the known elements.
We also need the length of the hypotenuse of the triangle. this can be gotten by finding the distance between lpt1 and the center of the triangle:
(setq dst1 (distance lpt1 cent))
The length of the hypotenuse is saved as the variable dst1. We can now apply our angle and hypotenuse to the formula:
opposite side = hypotenuse * sine (angle) becomes
(setq dst2 (* dst1 (sin ang1)))
Now we have the length of the side of the triangle but we need to know the direction of that side in order to find the corner point of the triangle. We already know that the direction is at a right angle to the cut axis. therefore, we can determine the right angle to the cut axis by adding the cut axis angle to 1.57 radians (90 degrees)(see figure 6.18). The following expression does this for us:
(setq ang2 (- (angle lpt1 lpt2) 1.57))
130
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Figure 6.18: Finding the angle of the opposite side of the triangle.
We are now able to place the missing corner of the triangle using the polar function (see figure 6.19).
(setq wkpt (polar cent ang2 dst2))
Figure 6.19: Finding the workpoint wkpt.
131
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
We assign the corner point location to the variable wkpt. We're still not finished with our little math exercise, however. We still need to find the intersection of the cut axis to the circle. Looking at our problem solving sketch, we can see that yet another triangle can be used to solve our problem. We know that the intersection lies along the cut axis. We can describe a triangle whose corner is defined by the intersection of the circle and the cut axis (see figure 6.20).
Figure 6.20: The triangle describing one intersection point.
We also already know two of the sides of this new triangle. One is the radius of the circle stored as the variable rad1. The other is the side of the triangle we used earlier stored as the variable dst2. The most direct way to find the intersection is to apply the Pythagorean Theorem shown in figure 6.21.
Figure 6.21: The Pythagorean Theorem
132
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Again we must apply algebra to derive a formula to suite our needs. The formula:
c2 = a2 - b2
becomes the expression:
(setq cord (sqrt(-(* rad1 rad1)(* dst2 dst2))))
We assign the distance value gotten from the Pythagorean Theorem to the variable cord. Using the Polar function, we can now find one intersection point between the circle and the cut axis:
(setq cpt2 (polar wkpt (angle lpt2 lpt1) cord))
In this expression, we find one intersection by applying the angle described by lpt1 and lpt2 and the distance described by cord to the polar functions (see figure 6.22).
Figure 6.22: Finding the location of an intersection point.
133
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Since the two intersection points are symmetric about point wkpt, the second intersection point is found by reversing the direction of the angle in previous expression:
(setq cpt3 (polar wkpt (angle lpt1 lpt2) cord))
Finally, we can get AutoCAD to do the actual work of cutting the circle:
(command "erase" cpt1 "" "arc" "c" cent cpt2 cpt3 "arc" "c" cent cpt3 cpt2
)
)
Actually, we don't really cut the circle. Instead, the circle is erased entirely and replaced with two arcs (see figure 6.23).
Figure 6.23: Drawing the new circle.
134
Copyright © 2001 George Omura,,World rights reserved