- •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
Introduction
One of AutoCAD's greatest assets is its adaptability. You can control just about every aspect of AutoCAD's operations from the appearance of its drawing editor to its variety of menus. A key element of this adaptability is its built-in programming language, AutoLISP. With AutoLISP, you can virtually write your own commands or redefine others.
You can think of AutoLISP as a very sophisticated macro-building facility. (Simple macros are like scripts that automate repetitive keystrokes.) You don't need to be a programmer to use AutoLISP. In fact, AutoLISP is designed so that everyday users of AutoCAD can start to use it after a minimum of training. This book makes AutoLISP accessible to AutoCAD users who are looking for a way to enhance and extend their use of AutoCAD.
1
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
Who should read this book
This book introduces nonprogrammers to the use of AutoLISP. If you are an intermediate level AutoCAD user, interested in learning about this powerful tool, then this is the book for you. If you are just beginning to learn AutoCAD, then you should probably become a bit more familiar with AutoCAD before attempting to learn AutoLISP. This book assumes that you have at least an intermediate level of expertise with AutoCAD and are acquainted with simple Windows operations.
How This Book Is Organized
The book is divided into 11 chapters. The first three chapters give you an introduction to programming in AutoLISP. The Chapter 1 introduces you to AutoLISP by showing you how to use it directly from the AutoCAD command prompt. The Chapter 2 shows you how to create and save programs in a file. Chapter 3 discusses ways of organizing your programming projects and how to manage your computers' memory.
The next four chapters show you how to use AutoLISP to do a variety of editing tasks. Chapter 4 discusses the functions that allow you to ask the user for input. Chapter 5 explains how to build decision-making capabilities into your programs. Chapter 6 shows you how to deal with geometric problems using AutoCAD. Chapter 7 discusses the manipulation of text.
The last four chapters show you how AutoCAD and AutoLISP interact. In Chapter 8, you will see how you can control many facets of AutoCAD through AutoLISP. Chapter 9 delves into lists, a fundamental component of all AutoLISP programs. Chapter 10 shows you ways of modifying AutoCAD objects by directly accessing the AutoCAD drawing database. And finally, Chapter 11 looks at ways to dig deeper into the drawing database to get information on complex drawing objects like polylines and block attributes.
In addition, five appendices are included as reference material to the book. In the original version of this book, these appendices contained the resources indicated by their title. In this electronic version, these appendices offer directions on how to find information in the AutoCAD help system. The first three show you how to find information on the AutoCAD menu structure, AutoLISP error messages, and AutoCAD group codes. The fourth appendix describes how to find informaton on the standard AutoCAD dimension variables and system variables. The fifth appendix describes how to find informaton on the Table group codes.
2
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
How to Use This Book
Each chapter offers exercises and sample programs that demonstrate some general concept regarding AutoLISP. Through these exercises, the book shows you how programs develop from ideas into finished, running programs. Also, the information you learn in one chapter will build on what you learned in the previous chapter. This way, your knowledge of AutoLISP will be integrated and cohesive, rather than fragmented. For this reason, the best way to use this book is to read each chapter in order and do all of the exercises. Since the topics are oriented toward accomplishing tasks rather than simply focusing on individual functions, you will have a good grasp of how to use AutoLISP in real world situations by the end of this book.
Where to Find the LISP Programs
As you read the chapters and do the exercise, you will be asked to enter program code into a file. If you are in a hurry, you can cut and paste the code directly from the chapter you are reading. This will save a good deal of time, but make sure you study the code that you cut and paste.
This book was originally published in 1990 by Sybex Inc. It has been reproduced here in an electronic format by
the Author for the benefit of Mastering AutoCAD readers everywhere. Enjoy....
Copyright © 2001 George Omura,,World rights reserved. No part of this publication may be stored in a retieval system, transmitted, or reproduced in any way, including but not limited to photocopy, photograph, magnetic or other record, without the prior agreement and written permission of the author.
3
Copyright © 2001 George Omura,,World rights reserved
The ABC’s of AutoLISP by George Omura
4
Copyright © 2001 George Omura,,World rights reserved