Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential...

23
Essential AutoLISP®

Transcript of Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential...

Page 1: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Essential AutoLISP®

Page 2: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Springer Science+ Business Media, LLC

Page 3: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

RoyHarkow

Essential AutoLISP®

With a Quick Reference Card and a Diskette

Springer

Page 4: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

RoyHarkow Roy Harkow Associates 40 MacArthur Road Wellesley, MA 02181 USA E-mail: [email protected]

AutoLISP, AutoCAD, and AutoCAD Training Center are registered trademarks of Autodesk, lnc. AutoCAD Development System is a trademark of Autodesk, Inc.

This book is not an Autodesk product and is not warranted by Autodesk.

Library of Congress Cataloging-in-Publication Data Harkow, Roy.

Essential AutoLISP 1 Roy Harkow. p. cm.

Includes bibliographical references and index.

1. AutoLISP (Computer program language) 1. Title. QA76.73.A84H37 1995 620'.0042'028755369-dc20 95-34192

Printed on acid-free paper.

© 1996 Springer Science+Business Media New York Originally published by Springer-Verlag New York, Inc. in 1996 Softcover reprint of the hardcover 1 st edition 1996

AH rights reserved. This work may not be translated or copied in whole or in part without the written permission of the publisher (Springer Science+Business Media, LLC), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed is forbidden. The use of general descriptive names, trade names, trademarks, etc., in this publication, even if the former are not especially identified, is not to be taken as a sign that such names, as understood by the Trade Marks and Merchandise Marks Act, may accordingly be used freely by anyone.

Production coordinated by Impressions and managed by Bill Imbomoni; manufacturing super­vised by Jacqui Ashri. Typeset by Impressions, a division of Edwards Brothers, lnc., Madison, WI.

9 8 7 6 5 4 3 2

ISBN 978-0-387-94571-2 ISBN 978-1-4612-2350-4 (eBook) DOI 10.1007/978-1-4612-2350-4

Page 5: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

"If my words did glow With the gold of sunshine ... "

-Robert Hunter

Page 6: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Contents

The Purpose of Essential AutoLISP and What Makes It Unique xiii

Why Essential AutoLISP Is Special if You're New to Programming xiv Why Essential AutoLISP Is Special if You're New to AutoLISP xv How to Get Started Quickly xv Why Essential AutoLISP Is Special if You're Already Using AutoLISP xv

Acknowledgments xvii

Terms and Conventions Used in Essential AutoLISP xxi

Windows versus DOS xxiv Release 13 versus Release 12 and Prior Releases xx1v

Part I Working with AutoLISP Data 1

1. The AutoLISP World s 1.1 A Short History of Lisp 6

1.1.1 This Book's Approach to Learning AutoLISP 7 1.2 Why AutoLISP? 8

vii

Page 7: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

viii

Contents

1.3 The AutoLISP World: Understanding the Picture Notation Used in This Book 10

1.4 Numbers 12 1.5 Symbols 14

1.5.1 Values of Symbols 16 1.5.2 nil and t 18

1.6 Strings 19 1. 7 Conses 21 1.8 Lists 26 1. 9 Functions 34 1.10 Basic Functions 37 1.11 Important Points to Remember 52 1.12 Labs 54

2. Printed Representation of AutoLISP Objects 59 2.1 Numbers, Strings, Symbols, and Functions 60 2.2 Conses 62 2.3 Lists 64 2.4 Converting Printed Representation to Picture Notation 74 2.5 Background Topic: Rules for Simplifying Lists 83 2.6 Prefix Notation 86 2.7 The DRAW Program 91 2.8 Important Points to Remember 93 2.9 Labs 94

3. Arithmetic Functions 99 3.1 Basic Arithmetic Functions 100 3.2 Number Conversion 109 3.3 Advanced Topic: Demystifying the Bit Manipulation Functions 111 3.4 Important Points to Remember 120 3.5 Labs 121

Part II Understanding How Code Is Executed in AutoLISP

4. Evaluation of AutoLISP Expressions 4.1 How the eval Function Processes Our Code

4.1.1 How eval Handles Lists 4.1.2 Errors Reported by eval 4.1.3 Examples

4.2 The Eval Flowchart Program 4.3 The DRAW Program (Reprise) 4.4 Important Points to Remember 4.5 Labs

123

125 127 128 132 134 144 146 147 148

Page 8: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Contents

5. Writing and Running Functions 153 5.1 Special Operators 154

5.1.1 Using defun to Define Functions 154 5.1.1.1 Theacad.lspFile 170

5.1.2 Using quote to Prevent Evaluation 171 5.1.2.1 Using quote with Symbols 173 5.1.2.2 Using quote with Lists 174 5.1.2.3 Using quote with Other Object Types 176 5.1.2.4 The Quote Character: ' 176 5.1.2.5 The Difference between Data and Code

in AutoLISP 178 5.1.2.6 Guidelines for Using quote 178

5.1.3 A Closer Look at setq 179 5.1.4 How Code Is Executed at the Command: Prompt 180

5.1.4.1 The Read-Eval-Print Loop 182 5.1.4.2 The 1> Prompt 185

5.2 Global and Local Variables 186 5.2.1 Creating Local Variables 187 5.2.2 Using Global Variables 191 5.2.3 Advanced Topic: Scoping of Variables 193 5.2.4 AutoCAD System Global Variables 197 5.2.5 Review Topic: Using Symbols in AutoLISP 199

5.3 Creating and Using AutoCAD Commands in AutoLISP 202 5.3.1 Running AutoCAD Commands in AutoLISP 203 5.3.2 Creating AutoCAD Commands in AutoLISP 208

5.4 Important Points to Remember 213 5.5 Labs 216

Part Ill Programming in AutoLISP 223

6. Control Structure: Producing Powerful Programs 225 6.1 Predicate Functions: Asking Yes/No Questions 226

6.1.1 Equality and Comparison Predicates 231 6.2 Conditional Execution of Code 240 6.3 Iteration Functions: Writing Loops to Repeat Code 250 6.4 Advanced Topic: Writing Recursive Functions 265 6.5 Important Points to Remember 271 6.6 Labs 272

7. Working with Lists 279 7.1 Basic List Functions 280

7 .1.1 List Functions Quiz 280 7.1.2 Quiz Answers 282

ix

Page 9: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

X

Contents

7.2 Association Lists 293 297 303 304

7.3 Advanced Functions: apply, mapcar, and lambda 7.4 Important Points to Remember 7.5 Labs

8. Input/Output 309 8.1 The get Functions: Building an Elegant User Interface 310

8.1.1 Using initget to Fine-Tune the get Functions 321 8.2 Other 110 Functions 327 8.3 File 110 334

8.3.1 The load Function Revisited 341 8.3.1.1 Automating the Edit-Load Cycle 342

8.3.2 Replacing Standard AutoCAD Commands with Our Own Commands 343

8.3.3 Obtaining Filenames via Dialog Boxes 346 8.4 Advanced File Access 350 8.5 Important Points to Remember 355 8.6 Labs 356

9. Expanding our Auto LISP Toolbox 359 9.1 String Functions 360

9.1.1 Wildcards 365 9.2 Conversion Functions 368

9.2.1 String Conversions 368 9.2.2 Number Conversions 371 9.2.3 Angle Conversions 374 9.2.4 Point Conversions 377

9.3 Measurement Functions 378 9.4 Symbol Manipulation Functions 386 9.5 Graphics Functions 397 9.6 Advanced Topic: Display Control and Device Access 399 9.7 Background Topic: Application-Handling Functions 413 9.8 Labs 415

Part IV Programming AutoCAD's Entity Database 419

10. Accessing and Modifying Entities 421 10.1 Understanding the Entity Database 422

1 0.1.1 Complex Entities 424 10.2 Entity Name Functions 427 10.3 Entity Data Functions 433 10.4 Handles 448

Page 10: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Contents

10.5 Advanced Topic: Extended Entity Data (Xdata) 450 10.6 Advanced Topic: Accessing Entities within Blocks 456 10.7 Important Points to Remember 458 10.8 Labs 459

11. Selection Sets and Nongraphical Entities 463 11.1 Basic Selection Set Functions 464 11.2 Using ssget to Build Custom Selection Sets 470

11.2.1 Using Filters with ssget 475 11.2.2 Advanced Topic: Relational Tests and Logical Operators 478

11.3 Nongraphical Entities 484 11.3.1 Symbol Tables 484 11.3.2 Advanced Topic: Dictionaries 494

11.4 Important Points to Remember 496 11.5 Labs 497

Part V Becoming Better AutoLISP Programmers

12. Error Handling and Debugging 12.1 Frequently Encountered AutoLISP Error Messages

12.1.1 Errors Encountered When Loading Files 12.1.2 Errors Reported by eval 12.1.3 Errors Discovered by Functions

12.2 Gotchas! 12.3 Error-Handling Subrs 12.4 Effective Debugging Techniques

13. Programming Style 13.1 Writing Readable Programs

13.1.1 Using Global Variables 13.1.2 Error-Proofing Code 13.1.3 Tips and Techniques 13.1.4 Writing Larger Programs

13.2 Providing Help for Commands We Write 13.3 Understanding Other Programmers' Code

501

503 504 505 507 510 515 523 526

539 540 543 548 552 556 563 574

xi

Page 11: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Contents

Part VI Introduction to the Computer 579

14. Computer and Programming Basics 581 14.1 Computer Components 582 14.2 Data Representation and Storage 587

14.2.1 Binary Representation of Numbers 588 14.2.2 Bits, Words, and Number Representation 589 14.2.3 Boolean Algebra: Logical AND, lOR, XOR, and NOT 592 14.2.4 Storing Data and Code 595 14.2.5 Advanced Topic: Memory Management 598

14.3 Writing and Running Programs 602 14.3.1 The Edit-Compile-Debug Cycle 605 14.3.2 Calling Functions 611 14.3.3 The AHED Editor 618 14.3.4 The acad.pgp file 621

14.4 Labs 624

Appendixes

A. Lab Answers 627

B. ASCII Codes 691

c. Files on the Enclosed Floppy 693

D. Common Error Messages 695

E. Commonly Used Entity Group Codes 697

F. AutoLISP Function Reference 699

Index 709

xii

Page 12: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

The Purpose of Essential AutoLISP and What Makes It Unique

AutoLISP is a wonderful language. It is powerful and fun to use. Once we know how the language works we can write, debug, and modify programs extremely quickly, especially as compared with FORTRAN, C, or other high-level lan­guages.

The way we typically learn a computer language is to read the definition of some commands, write a little program, learn some more commands, write a larger program, and so forth. Or maybe we take an existing program and mod­ify it to fit our needs. Unfortunately this approach does not work well for AutoLISP. To use AutoLISP productively, we must understand how the language itself works.

One of the peculiarities of AutoLISP is that what gets printed on the screen is an approximation of the actual data and code. What we type in gets altered as it enters the computer. Thus, code that appears correct on paper can easily give rise to errors.

Part I of Essential Auto LISP introduces an easy-to-learn pictorial representation for our data and code. We'll discover how to draw pictures that exactly specify

xiii

Page 13: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

The Purpose of Essential AutoLISP and What Makes It Unique

the organization of data in the computer, thereby eliminating all ambiguity and confusion. Furthermore, these pictures will allow us to easily solve prob­lems that we would otherwise have to approach by a trial-and-error method. At the conclusion of one of my workshops, students always say, "I can't imagine comprehending AutoLISP without the pictorial representation."

In Part II of Essential Auto LISP we learn how AutoLISP processes the code we enter. Everything we type into AutoLISP is executed by the function eval. To utilize the language to its fullest extent, we must have a strong understanding of how the eval function works. This book is unique in that an entire chapter is devoted to the exploration of eval. Why is understanding this function so important?

+ eval often alters the code we enter, thereby executing something different from what we actually type in. This can be very confusing until we learn how the transformation occurs.

+ Most error messages we receive come from eval or from functions called by eval. Many self-taught AutoLISP users report that the error messages are so cryptic that they just try different approaches until the error is solved, only to encounter the same error again a short while later.

Once we comprehend how eval processes our code we'll clearly understand what causes our errors. Then we can easily avoid most errors and solve many of the ones that do arise in under a minute. At the conclusion of my workshops students typically specify eval as the most important topic they learned dur­ing the week. Essential AutoLISP is the only textbook that uses the pictorial representation and presents an in-depth explanation of eval.

Why Essential AutoLISP Is Special if You're New to Programming

xiv

If you haven't programmed before, you not only need to learn the AutoLISP language, but you must learn some programming concepts as well. This book assumes no knowledge beyond basic AutoCAD and DOS or Windows, and thoroughly explains the programming steps that you need to follow in order to use AutoLISP efficiently.

Chapter 14 is a background chapter in which many basic programming con­cepts are explained. I don't assume that you know what a function is or how to program one. Text editing is also explained, and a text editor that works quite well with AutoLISP (and is on the enclosed floppy) is also explained. If these

Page 14: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

The Purpose of Essential AutoLISP and What Makes It Unique

concepts are new to you, peruse Chapter 14 before beginning your study of AutoLISP.

Why Essential AutoLISP Is Special if You're New to AutoLISP

In addition to the unique and essential fundamentals, I explain (and have in­cluded on the enclosed floppy) numerous programs that will aid your produc­tivity. Most important, the floppy contains two on-line tutorials that will help you to understand and practice using AutoLISP. My feeling is that having use­ful programs is nice, but they never quite do exactly what we want, so we have to modify them anyway. It's better to understand the language really well so that we can write the programs ourselves.

How to Get Started Quickly

The best way to understand AutoLISP is to read this book cover to cover. However, since I begin by teaching fundamentals, we don't actually start to write programs until Part II. If you're impatient to get going right away, you can take the following path:

+ Read Section 1.10, Basic Functions.

+ Scan Sections 3.1 and 3.2 to learn how to use the standard arithmetic func­tions.

+ Go to Chapter 5, Writing and Running Functions, and begin reading from there.

If you follow this path, please come back to Chapters 1 to 4 and learn the fun­damentals as soon as you can.

Why Essential AutoLISP Is Special if You're Already Using AutoLISP

Through years of offering AutoLISP workshops I have found that experienced users often know a good portion of the material in Parts III and IV, in which many Auto LISP functions are explained. The focus of this book is not on learn­ing function definitions, although to the best of my knowledge, this is the only AutoLISP textbook that explains every function in the language.

XV

Page 15: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

xvi

The Purpose of Essential AutoLISP and What Makes It Unique

The information that is truly singular to this book is contained in Parts I and II, where I explain how the language works. Almost assuredly this will be new and exciting for you. Furthermore, I promise that once you understand this material you will experience an immediate and spectacular increase in your productivity.

Part Vis unique as well. In Chapter 12 I not only explain the causes of most common error messages and how to solve them, but I also examine numerous other errors that don't necessarily give messages. Good programming style is the focus of Chapter 13. Why write code that just works when we can write programs that are elegant and easily understood? This is what differentiates a coder from a programmer.

You may already know the information in Chapter 14 (although it does have an interesting passage on Memory Management and presents a text editor that you might find works better with AutoLISP than your current one). On the other hand, you'll probably find the "Advanced Topic" sections sprinkled throughout the book to be of immediate use .

••• I have programmed in numerous programming languages and believe Auto­LISP to be among the easiest, and by far the most enjoyable one of all. But I found it confusing until I understood how the language really works. If you take the time to learn the fundamentals and do the exercises, I guarantee that you'll become an accomplished AutoLISP programmer and your productivity will skyrocket. I hope you have as much fun as I did exploring this exciting lan­guage.

Page 16: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Acknowledgments

Much of the inspiration behind the initial AutoLISP workshop came from Mark Sturgess, Autodesk's visionary manager who really made it all happen. At the start, Bill Kramer and Tracy Weaver got me bootstrapped up and run­ning. Bill has graciously allowed the use of his c: long-dist, c: sparse, and c:ss-sieve routines. Tracy contributed the c:new-style command. More recently, Autodesk's Art Cooney patiently answered many questions concern­ing arcane aspects of Release 13.

I give thanks to Michael Covington and Alan Phillips for their permission to include the AHED and PFE editors on the enclosed floppy. I appreciate the use of these editors in my classes and for the book.

It's well known that an instructor learns from his students, and I'm grateful for the many different ideas my students have given me over the years. I enjoy see­ing creative minds think of new and different ways to approach a problem, and in practically every class I present a new approach to a topic that I learned from the previous class.

xvii

Page 17: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

xviii

Acknowledgments

A number of the programs in this book were written by students, although I had to modify most of them to fit the requirements of the book. The following programming credits are due:

David Hill Todd Moen

Tony Palmisano Jim Porter Ray White

c:ped c:midpoint,c:midln,c:midarc,c:syms,andthe idea for c: unblk and c: reblk c: sclblk and c: blkscale c:zzandc:cc c:endit

I'm especially indebted to Todd, who's one of the best practical AutoLISP pro­grammers I know. I'm certain I've learned as much from him as he has from me.

I've discovered that writing a book is not a discrete task. It's a culmination of years of experience and learning. Many people who didn't directly participate in this project were instrumental in its coming to be.

Starting at Ohio State and Unisys I learned so much from Dan and Nancy Saks, and they have continued to provide both technical assistance and friend­ship through the years that have followed. Kevin Pammett, with whom I worked at DEC, is my longtime friend, adviser, and sounding board.

When I first started teaching at DEC, Enrique Alvarez was my mentor. From him I learned how to break down an explanation into its simplest form and to present concepts in a logical, progressive flow. This book is organized with that thought in mind: Let's learn the concepts and underlying principles first, then build on those basics.

When I joined Symbolics, my first Lisp company, I knew less about Lisp than you might know now. This book's approach to teaching AutoLISP was derived from the courses we created at Symbolics. Most of the credit is due Allan Wechsler and Jonathan Balgley. Many of the concepts and technical terms used in this book are Allan's, and as I wrote I frequently heard his voice speak­ing. I also thank Bob Fischer for inspiring me to commit deeply to a project. The idea for the Eval Flowchart on-line tutorial came from a similar program written by Bigbob Wescott at Symbolics.

I am pleased to give special thanks to those who helped me write this book. First and foremost, there are my book reviewers, who donated their time, ef­forts, and expertise to making sure this whole thing makes sense:

Page 18: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Acknowledgments

Keith Thorslund is a superb instructor and AutoCAD/AutoLISP consultant in Ottawa. Keith made certain that my AutoLISP explanations and examples were well presented, topical, and factually correct, and he was always available to answer my bottomless well of difficult questions. He also supplied the c :pass function and greatly enhanced c :midln.

Robert Mathews is an expert in Common Lisp and an excellent technical writer. Bob examined the technical aspects of the book down to the deepest Lisp fundamentals and corrected my grammar along the way.

Dorothy Kent is the author of the AutoCAD Reference Guide but an AutoLISP beginner. Dorothy shared her experience as an author, pointed out where my explanations got too technical, and made sure I emphasized the points most important to AutoCAD users.

Dr. Anthony Hotchkiss is a professor at SUNY College at Buffalo and a fre­quent contributor to CADalyst magazine. Tony's technical expertise was in­valuable on some difficult topics, and his more practical approach to teaching AutoLISP provided a needed balance to my theory. He also supplied the setv and rsetv functions.

Dr. Martin Gilchrist is my editor at Springer-Verlag. I am grateful to Martin for publishing this book and for the assistance he provided during the year it took me to write it. I'd also like to thank the people at Springer and Impressions for implementing my unique design needs and turning my manu­script into a real book.

I am also indebted to two unofficial members of my review team. My good friend Bill Skerker knows nothing about AutoCAD or programming but a fair amount about how to express concepts clearly and enjoyably. He helped greatly in making my approach less technical and more humanistic.

Ingrid Kannel provided lots of love, support, and editing too. When I couldn't figure out some wording, Ingrid patiently reviewed the passage and got me un­stuck. She's also the best children's photographer in Boston, which is why I had her take the photo at the back of this book!

xix

Page 19: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Terms and Conventions Used in Essential AutoLISP

The following conventions are used for representing AutoLISP objects (datatypes), expressions, and keyboard entry. A familiarity with these conven­tions will enhance this book's readability.

+ Keys you press on the keyboard: Fl, <space>, <tab>, <esc>, <enter>

w (])

Enter the following code as you read this example or section.

See the specified section for related information.

Here's an interesting point that may be outside the focus of this discussion.

+ All code and AutoLISP objects except numbers are in code font: defun, (x y z), acad.lsp.

+ Symbol names always appear in the text of this book in lowercase courier for readability. In programming examples, symbol names are lowercase

xxi

Page 20: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Jodi

Terms and Conventions Used in Essential AutoLISP

when the user types them and uppercase when the system prints them. In the pictorial representation, symbol names are always uppercase, because that is how they are stored in the machine.

+ Code font is also used for programming examples and keyboard input. When showing input at the Command: prompt, what we enter is shown in normal type and what the machine prints is in bold type. For example, we show the addition of 3 and 4 in the following manner:

COIIIIII&Ild: ( + 3 4 )

7

+ In the middle of an example we will occasionally provide a directive or de­scription in parentheses, like so:

COIIIIII&Ild: (prompt "Select a circle: ") Select a circle: (Click on a circle)

This parenthetical comment, (Click on a circle), requests you to take an ac­tion rather than showing what either you or AutoLISP has printed on the screen.

+ Sometimes you'll see an example with dots in the middle:

(defun foo ()

The ellipses indicate that more code would be needed to make the function runnable, but it isn't shown. This is because it's not relevant to the point that's being explained and I'm trying to keep the example simple and straightforward.

+ Function definitions are displayed in the following manner:

write-line str [file} Outputs a string to the screen or to a file.

The name of the function, write-line, is in code font. This function ac­cepts two arguments (i.e., data to work with), as shown by the following two words. str specifies that the first argument must be a string. file denotes that the second argument must be a file. The curly braces around file indi­cate that this second argument is optional.

Page 21: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Terms and Conventions Used in Essential AutoLISP

list obj ... obj Returns a new list with each of the arguments as members.

In this example, list is the function name. obj means that the argument can be any AutoLISP object (i.e., datatype); there is no restriction. The el­lipses ( ... ) indicate that this function accepts as many arguments as we choose to provide; there is no restriction there either.

Following is a list of most datatype abbreviations used in this book:

ang alist body

edata ename file form func int num obj prompt pt ss str sym

Angle expressed in radians Association list One or more functions to be evaluated in order. It returns whatever the last form evaluated returns. Entity data Entity name File descriptor Argument given to eval for evaluation Function Integer Number Any AutoLISP object (datatype) String message sent to the user's screen Point Selection set String Symbol

+ The AutoLISP Reference often specifies that a function takes an optional argument that, if "non-nil," causes an action to occur. In this book I always use the symbol t to indicate a "non-nil" condition.

+ Several places in the book we examine a program that contains functions we've not yet seen. That's unavoidable if the examples are to be at all prac­tical. When this situation arises, I'll explain enough about the function for you to understand the example. All of the AutoLISP functions are listed in the index both alphabetically and under the heading "function definitions." User-defined functions are listed under "programs." Appendix F contains a categorical listing of all AutoLISP function definitions.

xxiii

Page 22: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Terms and Conventions Used in Essential AutoLISP

+ Most chapters have labs at the end. The initial labs are tailored to present only one new function; the later ones combine functions, concepts, and techniques covered in previous chapters.

Following a lab statement there is usually one or more examples of how to call the function once it has been written. These show the results you should get when you run your own function. Use the data that are pre­sented in the examples so you know whether your solution is correct.

Appendix A contains the answers to all of the labs. You will gain more from a lab if you attempt to solve it prior to looking at the solution. However, the answers frequently contain additional text that will aid your understanding of the concept at hand, so they're well worth reading. The lab solutions are also in the file answers .lsp on the enclosed floppy.

If you don't understand what a problem is asking you to do, run the pro­gram from the answers .lsp file at the Command: prompt.

Windows versus DOS

Essential AutoLISP is PC-oriented. Although most AutoCAD users work in a DOS environment, more and more are moving over to Windows. For this rea­son I've given a slight Windows tilt to the book.

Except for appearances, there aren't many differences in AutoLISP between the Windows and DOS environments. I'll always point out where variations do occur and, if appropriate, show coding inconsistencies between both platforms.

A couple of differences between DOS and Windows should be mentioned at the start:

+ In DOS we move between the text and graphics windows by pressing the Fl key. In Windows, Fl brings up a help window, so F2 is used to flip screens.

+ To cancel a command or function in DOS we enter Ctrl-c; in Windows we can press the <esc> key. Essential AutoLISP uses <esc> to indicate the command termination key.

Release 13 versus Release 12 and Prior Releases

xxiv

This book is based on Release 13 AutoLISP, which, for the most part, is a su­perset of Release 12. Each new release adds functionality to AutoLISP but makes no fundamental changes to the language. With few exceptions, Release 12 programs should run in Release 13, and the programs used in this book run

Page 23: Essential AutoLISP® - Springer978-1-4612-2350-4/1.pdf · Contents The Purpose of Essential AutoLISP and What Makes It Unique xiii Why Essential AutoLISP Is Special if You're New

Terms and Conventions Used in Essential AutoLISP

on both. Most of the exceptions, of course, are programs that utilize the new Release 13 functionality.

In Release 13 the AutoLISP Programmers Reference Manual has been folded into the Customization Guide. I use the term AutoLISP Reference to refer to either. Similarly, the AutoCAD Reference Manual is now called the AutoCAD Command Reference. When you program, you should have these manuals nearby.

The following functions were added to AutoLISP in Release 13. Next to the topic is the section number where it is explained in this book.

startapp-9.7 tblobjname-11.3.1 dictnext, dictsearch, namedobjdict, and snvalid-11.3.2 getcfg and setcfg-13.1.1 au toload-13.1.4 help and setfunhelp-13.2

The entity database has been reorganized. If you are running a release prior to 13, the entity data will appear different from what's shown in this book. These differences are explained in the entities chapter (10) and summarized here.

First, the numbers used in printed representation of entity names have changed. Here's how an entity name might appear:

Release 12 Windows DOS

<Entity name: 60000022> <Entity name: 88450510> <Entity name: bb7510>

We use the Windows version in this book.

The entity data has been changed as well; there are a few more group codes in each entity data list. In prior releases the entity data for a line might appear as follows:

( (-1 . <Entity name: 60000022>) (0 . "LINE") (8 . "0") (10 2.0 3.0 0.0) (11 4.0 5.0 0.0) (210 0.0 0.0 1.0))

In Release 13 it looks like this:

((-1 . <Entity name:88450510>) (0 . "LINE") (5 . "22") (100 . "AcDbEntity") (67 . 0) (8. "0") (100. "AcDbLine") (10 2.0 3.0 0.0) (11 4.0 5.0 0.0) (210 0.0 0.0 1.0))

The extra information present in Release 13 data won't affect our programming.

XXV