- •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
Functions Useful in Geometric Transformations
The majority of your graphic problems can be solved with the basic trigonometric functons used in the Cutcr program. But AutoLISP provides the tools to solve even th emost arcane trigonometric problems. This section shows the functions you are most likely to use in situations that require geometric manipulations.
Trans
Trans translates a coordinate or displacement from one user coordinate system to another. The first argument is a point of reference. The second argument is a code indicating which coordinate system the point is expressed in. The third argument is a code indicating which coordinate system the point is to be translated to. An optional fourth True/nil argument can be included. If this fourth argument evaluates to True or non-nil, then the first argument will be treated as a displacement rather than a point value. The following are the codes for the second and third arguments.
Code Coordinate System
0World Coordinate System
1Current User Coordinate System
2Coordinate system of the current view plane Trans returns a coordinate or displacement list.
The syntax for Trans is
(trans [coordinage_list] [UCS_code] [optionalT/nil] )
Atan
Atan returns the arctangent in radians of its first argument. If the argument is negative, then the value returned will be negative. If two arguments are supplied, then Atan returns a the arctangent of the first argument divided by the second argument.
The syntax for Atan is
(atan [number] [optional_2nd_number])
Inters
Inters returns a coordinate list of the intersection of two vectors. The first two arguments to inters are the endpoints of one vector while the third and fourth arguments define the other vector. If an
135
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
optional fifth agrument is present and evaluates to nil, then Inters will attempt to locate an intersection point of the two vectors regardless of whether the intersection falls between the specified points or not.
The syntax for Inters is
(inters [point point point point] [optional_T/nil])
Sin
Sine returns the sine of an angle as a real number. The angle must be expressed in radians.
The syntax for Sine is
(sin [angle])
Cos
Cos returns the Cosine of an angle as a real number. The angle must be expressed in radians.
The syntax for Cos is
(cos [angle])
Conclusion
The majority of your graphic problems can be solved using the basic trigonometric functions shown in this sample program. But AutoLISP provides the tools to solve even the most arcane Trigonometric problems. If you find you need to use these math trig functions, consider making liberal use of a sketch pad or for that matter, AutoCAD itself to document your program. You may want to re-use or modify programs such as the previous example and if you don't have some graphic documentation recording how it works, you can have a difficult time understanding why you wrote your program as you did.
136
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
137
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Chapter 7: Working with Text
Introduction
Working With String Data Types
Searching for strings
How to Convert Numbers to Strings and Back
Converting a Number to a String
Converting Other Data Types
How to Read ASCII Text Files
Using a File Import Program
Writing ASCII Files to Disk
Using a Text Export Program
Conclusion
Introduction
If you have ever had to edit large amounts of text in a drawing, you have encountered one of AutoCAD's most frustrating limitations. While AutoCAD's text handling capabilities is one of it's strong points, it still leaves much to be desired where editing text is concerned. Fortunately AutoLISP can be of great help where text is concerned. In this chapter, you will look at the many functions that AutoLISP offers in the way of text or string manipulation. You will also look at how textural information can be store and retrieved from a file on disk and how data can be converted to and from string data types.
Working With String Data Types
In earlier versions of AutoCAD, editing text was a tedious task. You have to use the Change command to select a text line, then press return several times before you can actually make changes to the text. Even then, you would have to re-enter the entire line of text just to change one word. The text editing features of AutoCAD have come a long way and it doesn't take the same painful effort it once did. The following program is a simple line editor which simplifies the task of editing a single line of text. It was designed for the older versions of AutoCAD before the Ddmodify and Ddedit commands were available. While its function may be a bit outdated, it will serve to demonstrate how to handle text in AutoLISP.
138
Copyright © 2001 George Omura,,World rights reserved