- •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 6: Working With Geometry
Introduction
How to Find Angles and Distances
Understanding the Angle, Distance, and Polar Function
Using Trigonometry to Solve a Problem
Gathering Information
Finding Points Using Trigonometry
Conclusion
Introduction
It is inevitable that your work with AutoLISP will involve some geometric manipulations. With the box program in chapter 2, you have already created a program that derives new point locations based on user input. There, you learned how to take coordinate lists apart, then re-assemble them to produce a new coordinate. In this chapter, you will be introduced to other AutoLISP functions that will help you determine locations in your drawings coordinate system and in the process, you will review some basic trigonometry.
How to find Angles and Distances
In chapter 4, you learned how to prompt the user for angles and distances. At time, however, you will want to find angles and distances based on the location of existing point variables rather than relying on user input every time you need to find an angle.
Suppose you want to find a way to break two parallel lines between two points in a manner similar to the standard AutoCAD break command. In addition, you would like this function to join the ends of the two broken portions of each line to form an opening. Figure 6.1 shows a drawing of the process along with a description of what must occur. This drawing can be developed into pseudocode for your program. A function similar to this is commonly used in architectural drawings to place an opening in a wall.
112
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Figure 6.1: Sketch of the parallel line break program
In Chapter 3, we discussed the importance of designing your program to be simple to use. This program is designed
113
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
to obtain the information needed to perform its task using the minimum of user input. Since it is similar to the break command, it also tries to mimic the break program to some degree so the user feels comfortable with it. As you read through this section, pay special attention to the way information is gathered and used to accomplish the final result.
Open an AutoLISP file called Break2.lsp and copy the program in figure 6.2. Open a new AutoCAD file called Chapt6. Draw a line from point 2,4 to 12,4 then offset that line a distance of 0.25 units. Your screen should look like figure 6.3.
;Program |
to break 2 parallel lines -- Break2.lsp |
|
(defun c:break2 (/ pt1 pt2 pt3 pt4 pt0 ang1 dst1) |
|
|
(setvar "osmode" 512) |
;near osnap mode |
|
(setq |
pt1 (getpoint "\nSelect object: ")) |
;get first break point |
(setq |
pt2 (getpoint pt1 "\nEnter second point: ")) |
;get second break point |
(setvar "osmode" 128) |
;perpend osnap mode |
|
(Setq |
pt3 (getpoint pt1 "\nSelect parallel line: "));get 2nd line |
|
(Setvar "osmode" 0) |
;no osnap mode |
|
(setq |
ang1 (angle pt1 pt3)) |
;find angle btwn lines |
(setq |
dst1 (distance pt1 pt3)) |
;find dist. btwn lines |
(setq |
pt4 (polar pt2 ang1 dst1)) |
;derive pt4 on 2nd line |
(command |
|
|
|
"break" pt1 pt2 |
;break 1st line |
|
"break" pt3 pt4 |
;break 2nd line |
|
"line" pt1 pt3 "" |
;close ends of lines |
|
"line" pt2 pt4 "" |
|
) |
|
|
) |
|
|
|
|
|
|
|
|
Figure 6.2: The parallel line break program
114
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Figure 6.3: Two parallel lines drawn
1.Load the Break2.lsp file and enter break2 at the Command prompt.
2.At the prompt:
Select object:
The osnap cursor appears. Pick the lowermost line near coordinate 5,4. 3. At the next prompt:
Enter second point:
Pick the lowermost line again near coordinate 10,4. 4. Finally, at the prompt:
Select parallel line:
115
Copyright © 2001 George Omura,,World rights reserved