Essential Autolisp® Springer Science+ Business Media, LLC Royharkow
Total Page:16
File Type:pdf, Size:1020Kb
Essential AutoLISP® Springer Science+ Business Media, LLC RoyHarkow Essential AutoLISP® With a Quick Reference Card and a Diskette Springer 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 1st 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 "If my words did glow With the gold of sunshine ... " -Robert Hunter 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 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 123 4. Evaluation of AutoLISP Expressions 125 4.1 How the eval Function Processes Our Code 127 4.1.1 How eval Handles Lists 128 4.1.2 Errors Reported by eval 132 4.1.3 Examples 134 4.2 The Eval Flowchart Program 144 4.3 The DRAW Program (Reprise) 146 4.4 Important Points to Remember 147 4.5 Labs 148 viii 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 Contents 7.2 Association Lists 293 7.3 Advanced Functions: apply, mapcar, and lambda 297 7.4 Important Points to Remember 303 7.5 Labs 304 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 10.1.1 Complex Entities 424 10.2 Entity Name Functions 427 10.3 Entity Data Functions 433 10.4 Handles 448 X 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 501 12. Error Handling and Debugging 503 12.1 Frequently Encountered AutoLISP Error Messages 504 12.1.1 Errors Encountered When Loading Files 505 12.1.2 Errors Reported by eval 507 12.1.3 Errors Discovered by Functions 510 12.2 Gotchas! 515 12.3 Error-Handling Subrs 523 12.4 Effective Debugging Techniques 526 13. Programming Style 539 13.1 Writing Readable Programs 540 13.1.1 Using Global Variables 543 13.1.2 Error-Proofing Code 548 13.1.3 Tips and Techniques 552 13.1.4 Writing Larger Programs 556 13.2 Providing Help for Commands We Write 563 13.3 Understanding Other Programmers' Code 574 xi 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 The Purpose of Essential AutoLISP and What Makes It Unique AutoLISP is a wonderful language.