ffi rs.indd 1:31:19:pm/07/26/2014 page ii

30

Upload: others

Post on 07-Jun-2022

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii
Page 2: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

Page 3: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page i

AutoCAD® Platform CustomizationAutoLISP®

Page 4: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

Page 5: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page iii

AutoCAD® Platform CustomizationAutoLISP®

Lee Ambrosius

Page 6: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page iv

Senior Acquisitions Editor: Willem Knibbe

Development Editor: Mary Ellen Schutz

Technical Editor: Craig Black

Production Editor: Dassi Zeidel

Copy Editor: Liz Welch

Editorial Manager: Pete Gaughan

Vice President and Executive Group Publisher: Richard Swadley

Associate Publisher: Chris Webb

Book Designers: Maureen Forys, Happenstance Type-O-Rama; Judy Fung

Proofreader: Candace Cunningham

Indexer: Ted Laux

Project Coordinator, Cover: Patrick Redmond

Cover Designer: Wiley

Cover Image: © Smileyjoanne/iStockphoto.com

Copyright © 2014 by John Wiley & Sons, Inc., Indianapolis, IndianaPublished simultaneously in Canada

ISBN: 978-1-118-90055-0 (ebk.)ISBN: 978-1-118-90696-5 (ebk.)

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechan-ical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for per-mission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S. at (877) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002.

Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley prod-ucts, visit www.wiley.com.

TRADEMARKS: Wiley, the Wiley logo, and the Sybex logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries, and may not be used without written permission. AutoCAD and AutoLISP are registered trademarks of Autodesk, Inc. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

10 9 8 7 6 5 4 3 2 1

Page 7: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page v

Dear Reader,

Thank you for choosing AutoCAD Platform Customization: AutoLISP. This book is part of a family

of premium-quality Sybex books, all of which are written by outstanding authors who combine

practical experience with a gift for teaching.

Sybex was founded in 1976. More than 30 years later, we’re still committed to producing consis-

tently exceptional books. With each of our titles, we’re working hard to set a new standard for

the industry. From the paper we print on, to the authors we work with, our goal is to bring you

the best books available.

I hope you see all that refl ected in these pages. I’d be very interested to hear your comments and

get your feedback on how we’re doing. Feel free to let me know what you think about this or any

other Sybex book by sending me an email at [email protected]. If you think you’ve found

a technical error in this book, please visit http://sybex.custhelp.com. Customer feedback is

critical to our efforts at Sybex.

Best regards,

Chris Webb

Associate Publisher

Sybex, an Imprint of Wiley

Page 8: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page vi

To my wife, who is also my best friend: It is hard

to imagine that I would be writing this book if it

were not for you. It was you, all those years ago,

who encouraged me to step outside of my comfort

zone and share what I knew with others. Thank you

for the push I needed and for coming along on this

journey with me.

Page 9: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page vii

AcknowledgmentsI have to give a very special thanks to all the great folks at Sybex for working on and helping to

get this project off the ground after a few years of talking about it, especially Willem Knibbe.

The next two people I would like to thank are Mary Ellen Schutz and Dassi Zeidel, the develop-

ment and production editors on this book; you two made sure I stayed on track and delivered a

high-quality book. I also want to thank Liz Welch (copyeditor), Candace Cunningham (proof-

reader), and Ted Laux (indexer) for the work you all did on this book.

Thanks to all the folks at Autodesk, who put in the long hours and are dedicated to the work

they do on the Autodesk® AutoCAD® product. Last but not least, to a great friend of mine, Craig

Black. I have known Craig for nearly 20 years. I fi rst met him while attending an AutoLISP® ses-

sion at the local Autodesk Training Center, where he was an instructor. Craig is a great AutoLISP

programmer and it was a joy to have him as the technical editor on this book. Being a technical

editor is never the easiest job, but it is one of the most important and I appreciate what you have

done to make this book better.

Page 10: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page viii

Page 11: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page ix

About the AuthorLee Ambrosius fi rst started working with AutoCAD R12 for DOS in 1994. As a drafter, he

quickly discovered that every project included lots of repetition. Lee, not being one to settle

for “this is just the way things are,” set out on a path that would redefi ne his career. This new

path would lead him into the wondrous world of customization and programming—which you

might catch him referring to as “the rabbit hole.”

In 1996, Lee began learning the core concepts of customizing the AutoCAD user interface

and AutoLISP. The introduction of VBA in AutoCAD R14 would once again redefi ne how Lee

approached programming solutions for AutoCAD. VBA made it much easier to communicate

with external databases and other applications that supported VBA, and transformed the way

information could be moved between project-management and manufacturing systems.

Not being content with VBA, in 1999 Lee attended his fi rst Autodesk University and began to

learn ObjectARX®. Autodesk University made a lasting impression on him. In 2001, he started

helping as a lab assistant. He began presenting on customizing and programming AutoCAD at

the event in 2004. Along the way he learned how to use the AutoCAD Managed .NET API.

In 2005, Lee decided cubicle life was no longer for him, so he ventured off into the CAD

industry as an independent consultant and programmer for his own company, named

HyperPics, LLC. After he spent two years as a consultant, Autodesk invited him to work on

the AutoCAD team; he has been on the AutoCAD team since 2007. For most of his career at

Autodesk, Lee has worked primarily on the customization and end-user documentation.

Recently, he has been working on the AutoLISP, VBA, ObjectARX, .NET, and JavaScript pro-

gramming documentation.

In addition to working on the AutoCAD documentation, Lee has been involved with the

AutoCAD and AutoCAD LT Bible, AutoCAD for Dummies, AutoCAD & AutoCAD LT All-in-One Desk Reference for Dummies, AutoCAD 3D Modeling Workbook for Dummies, and Mastering AutoCAD for Mac books on different editions as a technical writer or author. He has also written

white papers on customization for Autodesk and a variety of articles for AUGIWorld, published

by AUGI®, on customization and programming.

Page 12: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page x

Page 13: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page xi

Contents at a Glance

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix

Chapter 1 • Quick Start for New AutoLISP Programmers . . . . . . . . . . . . . . . . . . . . . . . . 1

Chapter 2 • Understanding AutoLISP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Chapter 3 • Calculating and Working with Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Chapter 4 • Working with Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

Chapter 5 • Requesting Input and Using Conditional and

Looping Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Chapter 6 • Creating and Modifying Graphical Objects. . . . . . . . . . . . . . . . . . . . . . . . 143

Chapter 7 • Creating and Modifying Nongraphical Objects . . . . . . . . . . . . . . . . . . . . 199

Chapter 8 • Working with the Operating System and External Files . . . . . . . . . . . . . 227

Chapter 9 • Catching and Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

Chapter 10 • Authoring, Managing, and Loading AutoLISP Programs. . . . . . . . . . . 271

Chapter 11 • Using the Visual LISP Editor (Windows only) . . . . . . . . . . . . . . . . . . . . 305

Chapter 12 • Working with ActiveX/COM Libraries (Windows only) . . . . . . . . . . . . 325

Chapter 13 • Implementing Dialog Boxes (Windows only) . . . . . . . . . . . . . . . . . . . . . 349

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

Page 14: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ffi rs.indd 1:31:19:PM/07/26/2014 Page xii

Page 15: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

ftoc.indd 6:16:54:PM/07/28/2014 Page xiii

Contents

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix

Chapter 1 • Quick Start for New AutoLISP Programmers . . . . . . . . . . . . . . .1

Working with AutoLISP Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Working with Commands and Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Conditionalizing and Repeating Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Grouping Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Storing and Loading AutoLISP Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Chapter 2 • Understanding AutoLISP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15

Getting Started with AutoLISP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Understanding the Syntax of an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Executing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Accessing the AutoLISP Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Storing and Retrieving Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Setting and Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Working with System Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Accessing Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Exploring Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

Leveraging AutoCAD and Third-Party Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Using the command Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Using the command-s Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

Working with Commands That Display a Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . 33

Defi ning and Using Custom Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Defi ning a Custom Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

Using a Custom Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Example: Drawing a Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Chapter 3 • Calculating and Working with Values . . . . . . . . . . . . . . . . . . . .39

Calculating Values with Math Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Performing Basic Math Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Performing Advanced Math Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Working with Bitwise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Manipulating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

Concatenating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Getting the Length of and Searching for Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Replacing and Trimming Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Changing the Case of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

Evaluating Values to Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

Converting Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

Converting Numeric Values to Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

Page 16: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

XIV | CONTENTS

ftoc.indd 6:16:54:PM/07/28/2014 Page xiv

Converting Strings to Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

Converting Numeric Values to Other Number Types . . . . . . . . . . . . . . . . . . . . . . . . . 60

Returning a Value from a Custom Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

Exercise: Drawing a Rectangle (Revisited). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

Creating the drawplate.lsp File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

Revising the drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Adding the Revised drawplate Function to drawplate.lsp. . . . . . . . . . . . . . . . . . . . . . . . 66

Creating the utility.lsp File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

Loading the LSP Files into AutoCAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

Chapter 4 • Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71

What Are Lists? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

Creating a List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

Getting an Element from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Retrieving a Specifi c Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Stepping Through a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

Appending, Substituting, and Removing Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

Appending Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

Substituting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Removing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Determining Whether an Item Exists in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

Sorting the Elements of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

Using Point Lists to Calculate Geometric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

Measuring Angular and Distance Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Calculating Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

Finding and Snapping to Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

Translating Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

Converting Measurement Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

Accessing the AutoCAD Calculator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

Converting Lists to Strings and Strings to Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

Exercise: Adding Holes to the Plate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

Defi ning the New Get-Sysvars and Set-Sysvars Utility Functions . . . . . . . . . . . . . . . . 91

Defi ning the New createcircle Utility Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

Revising the drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

Using the Revised drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

Chapter 5 • Requesting Input and Using Conditional and Looping

Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97

Interacting with the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Requesting Input at the Command Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

Providing Feedback to the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

Working with the Graphics Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

Conditionalizing and Branching Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

Comparing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

Grouping Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

Validating Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

Page 17: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

CONTENTS | XV

ftoc.indd 6:16:54:PM/07/28/2014 Page xv

Evaluating if a Condition Is Met. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

Testing Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

Repeating and Looping Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

Repeating Expressions a Set Number of Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

Performing a Task While a Condition Is Met . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

Exercise: Getting Input from the User to Draw the Plate . . . . . . . . . . . . . . . . . . . . . . . . 137

Revising the drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

Using the Revised drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

Chapter 6 • Creating and Modifying Graphical Objects . . . . . . . . . . . . . . 143

Working with Entity Names and Dotted Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Creating a Dotted Pair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

Accessing the Elements of an Entity Data List and Dotted Pair . . . . . . . . . . . . . . . . 146

Adding Objects to a Drawing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

Selecting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

Selecting an Individual Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

Working with Selection Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

Filtering Selected Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

Modifying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

Listing and Changing the Properties of an Object Directly . . . . . . . . . . . . . . . . . . . 162

Updating an Object’s Properties with an Entity Data List. . . . . . . . . . . . . . . . . . . . . 168

Deleting an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

Highlighting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

Working with Complex Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

Creating and Modifying Polylines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

Creating and Modifying with Block References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

Extending an Object’s Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

Working with XData. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

Defi ning and Registering an Application Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

Attaching XData to an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

Querying and Modifying the XData Attached to an Object . . . . . . . . . . . . . . . . . . . 184

Removing XData from an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

Selecting Objects Based on XData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

Exercise: Creating, Querying, and Modifying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 187

Revising the Functions in utility.lsp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

Testing the Changes to the drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

Defi ning the New Get-DXF-Value and Set-DXF-Value Utility Functions. . . . . . . . . 189

Moving Objects to Correct Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

Creating a Basic Block Attribute Extraction Program. . . . . . . . . . . . . . . . . . . . . . . . . 192

Using the Functions in the furntools.lsp File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

Chapter 7 • Creating and Modifying Nongraphical Objects. . . . . . . . . . . 199

Working with Symbol Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

Accessing and Stepping through Symbol Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

Adding and Modifying Entries in a Symbol Table . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

Creating and Modifying Block Defi nitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

Page 18: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

XVI | CONTENTS

ftoc.indd 6:16:54:PM/07/28/2014 Page xvi

Working with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

Accessing and Stepping through Dictionaries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

Creating a Custom Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

Storing Information in a Custom Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

Managing Custom Dictionaries and Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

Exercise: Creating and Incrementing Room Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

Revising the createlayer Function in utility.lsp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

Creating the Room Label Block Defi nition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

Inserting a Block Reference Based on the Room Label

Block Defi nition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

Prompting the User for an Insertion Point and Room Number . . . . . . . . . . . . . . . . 222

Adding Room Labels to a Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

Chapter 8 • Working with the Operating System and External Files . . . 227

Storing Information in the Windows Registry or a Plist File . . . . . . . . . . . . . . . . . . . . . 227

Creating and Querying Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

Editing and Removing Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

Accessing Data from External Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

Opening and Creating an External File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

Reading Characters and Lines from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

Writing Characters and Lines from a File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

Closing an Open File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

Working with Directories and Files in the Operating System . . . . . . . . . . . . . . . . . . . . 237

Locating and Listing Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

Managing Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

Getting Information about a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

Exercise: Reading and Writing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

Creating Layers Based on Data Stored in an External File . . . . . . . . . . . . . . . . . . . . 245

Adding Layers to a Drawing with the loadlayers Function . . . . . . . . . . . . . . . . . . . . 247

Writing the Bill of Materials to an External File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

Using the furnbomexport Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

Chapter 9 • Catching and Handling Errors. . . . . . . . . . . . . . . . . . . . . . . . . 251

Identifying and Tracking Down Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

Putting Your Code Under the Microscope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

Displaying Messages During Execution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

Tracing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

Catching Errors in a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

Defi ning and Using a Custom Error Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

Grouping Functions into a Single Undo Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

Exercise: Handling Errors in the drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

Using the drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

Implementing a Custom *error* Handler and Undo Grouping . . . . . . . . . . . . . . . . 268

Testing the Changes to the drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

Page 19: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

CONTENTS | XVII

ftoc.indd 6:16:54:PM/07/28/2014 Page xvii

Chapter 10 • Authoring, Managing, and Loading AutoLISP Programs . 271

Storing AutoLISP Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

Selecting an Editing Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

Creating an AutoLISP File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

Editing an AutoLISP File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

Writing Modular Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

Adding Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

Undefi ning and Redefi ning Standard AutoCAD Commands . . . . . . . . . . . . . . . . . . . . 279

Defi ning a Startup Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

Loading AutoLISP Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

Manually Loading an AutoLISP File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

Automatically Loading an AutoLISP File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

Using the Load/Unload Applications Dialog Box to Load a LSP File . . . . . . . . . . . 284

Managing the Locations of AutoLISP Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

Specifying Support File Search Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

Identifying Trusted Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

Deploying AutoLISP Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

Deployment Methods (Local vs. External) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

Defi ning a Plug-in Bundle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

Implementing Help for Custom Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

Exercise: Deploying the drawplate Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

Loading the utility.lsp File by Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

Loading the drawplate.lsp File on Demand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

Enabling Help Support for the drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

Confi guring the AutoCAD Support and Trusted Paths . . . . . . . . . . . . . . . . . . . . . . . 300

Testing the Deployment of the drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

Creating DrawPlate.bundle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

Deploying and Testing the DrawPlate.bundle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

Chapter 11 • Using the Visual LISP Editor (Windows only) . . . . . . . . . . . 305

Accessing the Visual LISP Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

Managing AutoLISP Files with the Visual LISP Editor . . . . . . . . . . . . . . . . . . . . . . . . . . 306

Formatting an AutoLISP File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

Coloring Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308

Formatting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

Commenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

Validating and Debugging Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

Executing Code from the Visual LISP Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

Checking and Loading Code in the Current Editor Window . . . . . . . . . . . . . . . . . . 311

Debugging Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313

Creating a Visual LISP Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

Compiling LSP and PRJ Files into a VLX File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

Exercise: Working with the Visual LISP Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

Formatting, Checking, and Debugging the badcode Function. . . . . . . . . . . . . . . . . . 318

Stepping Through and Inspecting the badcode Function . . . . . . . . . . . . . . . . . . . . . . 320

Creating and Compiling a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

Page 20: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

XVIII | CONTENTS

ftoc.indd 6:16:54:PM/07/28/2014 Page xviii

Chapter 12 • Working with ActiveX/COM Libraries (Windows only). . . 325

Understanding the Basics of ActiveX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

Accessing Classes, Objects, and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326

Specifying Properties and Invoking Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

Working with Variants and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332

Importing COM Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

Using the AutoCAD COM Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

Accessing the AutoCAD Application and Current Drawing Objects . . . . . . . . . . . 338

Working with Graphical and Nongraphical Objects in the Current Drawing . . . . 339

Monitoring Events with Reactors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342

Leveraging the Windows and Microsoft Offi ce COM Libraries. . . . . . . . . . . . . . . . . . . 344

Accessing the Windows Shell Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

Working with Microsoft Offi ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347

Chapter 13 • Implementing Dialog Boxes (Windows only) . . . . . . . . . . . 349

What Is Dialog Control Language? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

Defi ning and Laying Out a Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

Defi ning a Dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

Adding Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352

Grouping, Aligning, and Laying Out Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356

Creating and Previewing a Dialog in a DCL File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

Loading and Displaying a Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Loading and Unloading a DCL File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Displaying a Dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363

Initializing Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

Interacting with and Responding to a User. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

Specifying the Action of a Tile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

Getting Information about a Tile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370

Terminating or Closing a Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

Hiding a Dialog Box Temporarily . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372

Exercise: Implementing a Dialog Box for the drawplate Function. . . . . . . . . . . . . . . . . . 373

Creating the drawplate Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

Renaming the Existing drawplate Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

Defi ning a New drawplate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

Testing the drawplate.lsp Changes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

Page 21: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

fl ast.indd 1:31:30:PM/07/26/2014 Page xix

Introduction

Welcome to AutoCAD Platform Customization: AutoLISP! Have you ever thought to yourself, Why

doesn’t the Autodesk® AutoCAD® program include every feature I need? Why isn’t it stream-

lined for the type of work I perform? If so, you are not alone. AutoCAD at its core is a drafting

platform that can be shaped and molded to more effi ciently complete the tasks you perform on

a daily basis and to enhance your company’s workfl ows with the use of programming. Take a

deep breath. I did just mention programming, but programming isn’t something to fear. At fi rst,

just the idea of programming makes many people want to run in the opposite direction—myself

included. The productivity gains are what propelled me forward. Programming isn’t all that dif-

ferent from anything else you’ve tried doing for the fi rst time.

In many ways, learning to program is much like learning a foreign language. For many new

to AutoLISP®, the starting place is often a basic understanding of syntax and the command func-

tion. The command function allows you to leverage your knowledge of AutoCAD commands by

being able to pass specifi c values to a command or pausing a command for a value. After you

are comfortable with the syntax of AutoLISP and the command function, you can begin to learn

additional functions that allow for the development of more robust and complex programs.

About Th is BookAutoCAD Platform Customization: AutoLISP provides you with an understanding of the AutoLISP

programming language and how it can help improve your productivity. This book is designed

to be more than just an introduction to AutoLISP—a resource that can be used time and again

when developing AutoLISP programs. As you page through this book, you will notice that it

contains sample code and exercises that are based on real-world solutions.

This book is the second in a series of three that focuses on customizing and programming

AutoCAD. The three-book series as a whole is known as AutoCAD Platform Customization: User Interface, AutoLISP, VBA, and Beyond, which will be available as a printed book in late 2014/early

2015. Book 1 in the series, AutoCAD Platform Customization: User Interface and Beyond, was pub-

lished in early 2014 and focused on CAD standards and general customization of AutoCAD;

book 3, AutoCAD Platform Customization: VBA, will be available in fall/winter 2014 and covers

the VBA programming platform inside AutoCAD.

Page 22: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

XX | INTRODUCTION

fl ast.indd 1:31:30:PM/07/26/2014 Page xx

Is Th is Book for You?AutoCAD Platform Customization: AutoLISP covers many aspects of AutoLISP programming for

AutoCAD on Windows and Mac OS. If any of the following are true, this book will be useful to

you:

◆ You want to develop and load custom programs using the AutoLISP programming lan-

guage for use in the AutoCAD drawing environment.

◆ You want to automate the creation and manipulation of drawing objects.

◆ You want to automate repetitive tasks.

◆ You want to help manage and enforce CAD standards for your company.

AutoLISP in AutoCADAutoLISP is the most popular and is the original supported programming language for the

AutoCAD program. The reason for its popularity with new (and even veteran) programmers

is that it is a natural extension of the AutoCAD program. There is no additional software to

purchase, and AutoLISP can leverage the commands that Autodesk and third-party develop-

ers expose at the Command prompt. For example, with a few simple lines of code you can set a

layer as current and insert a title block with a specifi c insertion point, scale, and rotation. The

block is then inserted on the layer you specifi ed. To perform the same tasks manually, the end

user would have to set a layer as current, choose the block they want to insert, and specify the

properties of the block, which, in the case of a title block, are almost always the same.

The AutoLISP programming language can be used to accomplish the following:

◆ Create custom functions that can be executed from the AutoCAD Command prompt

◆ Create and manipulate graphical objects in a drawing, such as lines, circles, and arcs

◆ Create and manipulate nongraphical objects in a drawing, such as layers, dimension styles,

and named views

◆ Perform mathematical and geometric calculations

◆ Request input from or display messages to the user at the Command prompt

◆ Interact with fi les and directories in the operating system

◆ Read from and write to external fi les

◆ Connect to applications that support ActiveX and COM

◆ Display dialog boxes and get input from the end user

AutoLISP code can be entered directly at the Command prompt or loaded using a LSP fi le.

Once an AutoLISP program has been loaded, you can execute the custom functions from the

Command prompt. Functions executed from the Command prompt can be similar to standard

AutoCAD commands, but the programmer determines the prompts that should be displayed.

It is also possible to use AutoLISP code with a command macro that is activated from the

AutoCAD user interface or a tool on a tool palette.

Page 23: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

INTRODUCTION | XXI

fl ast.indd 1:31:30:PM/07/26/2014 Page xxi

What to ExpectThis book is organized to help you learn AutoLISP fundamentals and how to manage and

implement custom AutoLISP programs. Additional resources and fi les containing the example

code found throughout this book can be found on the companion web page, www.sybex.com/

go/autocadcustomization.

Chapter 1: Quick Start for New AutoLISP Programmers In this chapter, you’ll get an

introduction to the AutoLISP programming language. I begin by showing you how to enter

AutoLISP expressions at the Command prompt and execute standard AutoCAD commands.

After that, you are eased into some basic programming concepts that allow you to perform

conditional tests and repeat expressions. The chapter wraps up with creating and loading an

AutoLISP fi le into the AutoCAD program.

Chapter 2: Understanding AutoLISP In this chapter, you’ll learn the fundamentals of

the AutoLISP programming language. AutoLISP fundamentals include a look at the syn-

tax and structure of an expression, how to use a function, and how to work with variables.

Beyond just syntax and variables, you learn to use AutoCAD commands and group multiple

AutoLISP expressions into custom functions.

Chapter 3: Calculating and Working with Values In this chapter, you’ll learn to work

with mathematical and string-manipulation functions. Math functions allow you to per-

form basic and advanced calculations based on object values or a value that the user might

provide, whereas string-manipulation functions allow you to work with text-based values.

Both numeric and textual values are used when creating or manipulating objects, adding

annotations to a drawing, or displaying a message to the end user. Based on how the values

are used, numeric values can be converted to strings and strings can be converted to numeric

values.

Chapter 4: Working with Lists In this chapter, you’ll learn to work with the list data type.

Lists are used throughout AutoLISP to provide 2D or 3D coordinate values and to defi ne an

object stored in a drawing.

Chapter 5: Requesting Input and Using Conditional and Looping Expressions In this

chapter, you’ll learn to request input from the user, use conditional statements, and repeat

expressions. Requesting input allows you to get values from the user and then use those

values to determine the end result of the program. Conditional statements enable a program

to make choices based on known conditions in a drawing or input from a user. After you

understand conditional statements, you will learn to use them in conjunction with looping

expressions to execute a set of expressions until a condition is met.

Chapter 6: Creating and Modifying Graphical Objects In this chapter, you’ll learn how to

create, modify, and attach extended data to graphical objects using AutoCAD commands and

AutoLISP functions. Graphical objects represent the drawing objects, such as a line, an arc,

or a circle, that are displayed in model space or on a named layout. When modifying objects,

you can choose to step through all the objects in a drawing or let the user select the objects to

be modifi ed. Extended data allows you to store information with an object that can be used to

identify the objects your program creates or link objects to external database records.

Chapter 7: Creating and Modifying Nongraphical Objects In this chapter, you’ll learn

how to create and modify nongraphical objects using AutoCAD commands and AutoLISP

Page 24: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

XXII | INTRODUCTION

fl ast.indd 1:31:30:PM/07/26/2014 Page xxii

functions. Nongraphical objects are used to control the appearance of graphical objects and

store settings that affect the behavior of features in the AutoCAD program. Drawings sup-

port two different types of nongraphical objects: symbol-table objects and dictionaries.

Chapter 8: Working with the Operating System and External Files In this chapter, you

will learn how to work with settings and fi les stored outside of the AutoCAD program.

Settings can be stored in the Windows Registry and Plist fi les on Mac OS, and they can be

used to affect the behavior of the AutoCAD program or persist values for your custom pro-

grams between AutoCAD sessions. Files and folders stored in the operating system can be

accessed and manipulated from the AutoCAD program, which allows you to set up project

folders or populate project information in the title block of a drawing from an external fi le.

Chapter 9: Catching and Handling Errors In this chapter, you will learn how to catch and

handle errors that are caused by an AutoLISP function and keep an AutoLISP program from

terminating early. AutoLISP provides functions that allow you to trace a function, see argu-

ments as they are passed, catch an error and determine how it should be handled, and group

functions together so all the actions performed can be rolled back as a single operation.

Chapter 10: Authoring, Managing, and Loading AutoLISP Programs In this chapter, you

will learn how to store AutoLISP code statements in a fi le, load and manage AutoLISP fi les,

and deploy custom programs with plug-in bundles. Storing AutoLISP code in a fi le allows for

its reuse in multiple drawings. When you load an AutoLISP fi le, all of the functions defi ned

in the fi le are made available while the drawing remains open. Based on how you load or

deploy an AutoLISP fi le, you might need to let the AutoCAD program know where your

AutoLISP fi les are stored.

Chapter 11: Using the Visual LISP Editor (Windows only) In this chapter, you will learn

how to use the Visual LISP® Editor. The editor provides tools for writing, formatting, validat-

ing, and debugging code in an AutoLISP fi le. Using the Visual LISP Editor, you can group

AutoLISP fi les into project fi les, which make them easy to manage and compile. Compiling

an AutoLISP fi le secures the source code contained in the fi le so that it can’t be altered by

others.

Chapter 12: Working with ActiveX/COM Libraries (Windows only) In this chapter, you

will learn how to use ActiveX/COM libraries with AutoLISP. ActiveX provides access to

additional functions, which allow for the creation and manipulation of drawing objects and

AutoCAD application settings that aren’t easily accessible with standard AutoLISP func-

tions. External applications, such as Microsoft Word and Excel, can also be accessed from the

AutoCAD program when using ActiveX.

Chapter 13: Implementing Dialog Boxes (Windows only) In this chapter, you will learn

how to create and use dialog boxes with an AutoLISP program. Dialog boxes provide an

alternative method of requesting input from the user and are implemented using Dialog

Control Language (DCL).

Companion WebsiteAn online counterpart to this book, the companion web page contains the sample fi les required

to complete the exercises found in this book in addition to the sample code and project fi les used

Page 25: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

INTRODUCTION | XXIII

fl ast.indd 1:31:30:PM/07/26/2014 Page xxiii

to demonstrate some of the programming concepts explained in this book. In addition to the

sample fi les and code, the web page contains resources that are not mentioned in this book. The

companion web page can be found at www.sybex.com/go/autocadcustomization.

Other InformationThis book assumes that you know the basics of your operating system—Windows or Mac OS

X—and AutoCAD 2009 or later. When appropriate, I indicate when a feature does not apply to

a specifi c operating system or release of AutoCAD. Most of the images in this book were taken

using AutoCAD 2014 in Windows 8 and AutoCAD 2014 in Mac OS X 10.7.

Since AutoCAD LT® doesn’t support AutoLISP, none of the content in this book applies to that

software package.

Styles and Conventions of Th is BookThis book uses a number of styles and character formats—bold, italic, monotype face, all upper-

case or lowercase letters, and others—to help you distinguish from the text you read, sample

code you can try, text that you need to enter at the AutoCAD Command prompt, or the name of

an object class or method in one of the programming languages.

As you read through this book, keep the following conventions in mind:

◆ User-interface selections are represented by one of the following methods:

◆ Click the Application button ➢ Options.

◆ On the ribbon, click the Manage tab ➢ Customization ➢ User Interface.

◆ On the menu bar, click Tools ➢ Customize ➢ Interface.

◆ In the drawing window, right-click and click Options.

◆ Keyboard input is shown in bold (for example, type cui and press Enter).

◆ Prompts that are displayed at the AutoCAD Command prompt are displayed as mono-

space font (for example, Specify a start point:).

◆ AutoCAD command and AutoLISP function names are displayed in all lowercase letters

with a monospace font (for example, line or command).

◆ Example code and code statements that appear within a paragraph are displayed in mono-

space font. Code samples might look like one of the following:

◆ (command "._circle" PAUSE 3)

◆ The alert method can be used to display an error message to the user.

◆ ; Draw a rectangle

Page 26: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

XXIV | INTRODUCTION

fl ast.indd 1:31:30:PM/07/26/2014 Page xxiv

Contacting the AuthorI hope that you enjoy AutoCAD Platform Customization: AutoLISP and that it changes the way

you think about completing your day-to-day work. If you have any feedback or ideas that could

improve this book, you can contact me using the following address:

Lee Ambrosius: [email protected]

On my blog and website you’ll fi nd additional articles on customization and samples that I

have written over the years. You’ll fi nd these resources here:

Beyond the UI: http://hyperpics.blogs.com

HyperPics: www.hyperpics.com

If you encounter any problems with this publication, please report them to the publisher.

Visit the book’s website, www.sybex.com/go/autocadcustomizat ion, and click the Errata link

to open a form and submit the problem you fou n d.

Page 27: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

c01.indd 1:26:13:PM/07/26/2014 Page 1

Chapter 1

Quick Start for New AutoLISP Programmers

The AutoLISP® language and programming in general are two subjects that I have enjoyed for

over 15 years now, but the same subjects make some people cringe and want to run in the oppo-

site direction. I am not going to claim AutoLISP is easy to learn, but it can be learned by anyone,

whether or not they have a programming background. When I fi rst set out to learn AutoLISP, I

didn’t have any programming experience, but I wanted the benefi ts that AutoLISP could offer.

I understand if you have some hesitation at the thought of learning AutoLISP, but you don’t

need to feel that way—I will help you. This chapter will ease you into some core programming

concepts and the AutoLISP programming language by exposing you to a variety of

functions that are available.

To complete the exercises in this chapter and be able to create and edit LSP fi les, you must

have the following:

For Windows users: Autodesk® AutoCAD® 2006 or later and the Notepad program

For Mac OS users: Autodesk® AutoCAD® 2011 or later and the TextEdit program

NOTE Although I mention AutoCAD 2006 or later, everything covered in this chapter should

work without any problems going all the way back to AutoCAD® 2000 and even possibly

earlier releases.

Working with AutoLISP ExpressionsAutoLISP is a natural extension of AutoCAD, as it can be used seamlessly from the AutoCAD

Command prompt. You can enter AutoLISP when no commands are active or when AutoCAD

prompts you for a value. The programming statements used in AutoLISP are known as expres-sions. You can type expressions at the Command prompt as long as they start with an opening

parenthesis [(] or an exclamation point (!). Follow those symbols with the functions you wish to

execute and the arguments that provide data or further instruction.

Each AutoLISP expression that starts with an opening parenthesis must also end with a clos-

ing parenthesis. AutoLISP expressions must contain the same number of opening and closing

parentheses—this is sometimes referred to as balancing parentheses. You can enter the opening

and closing parentheses on separate lines, though.

Page 28: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

c01.indd 1:26:13:PM/07/26/2014 Page 2

2 | CHAPTER 1 QUICK START FOR NEW AUTOLISP PROGRAMMERS

Use these steps to gain a basic understanding of entering AutoLISP expressions at the

AutoCAD Command prompt:

1. Launch AutoCAD, if it is not already running.

2. At the AutoCAD Command prompt, type ( and press Enter.

AutoCAD responds with the prompt (_>, which is the program’s way of letting you know

that AutoLISP has taken control.

3. Press Esc to return to the standard AutoCAD Command prompt.

4. At the AutoCAD Command prompt, type (+ 3 2) and press Enter.

The AutoLISP expression is evaluated and returns a value of 5, which is the result of

adding 3 and 2 together. The + (plus sign) is the function of the AutoLISP expression; 3

and 2 are the arguments (in this case, data) that are passed to the function. The AutoLISP

function you want to use must be the fi rst item after the opening parenthesis.

5. Type (* 3.5 2) and press Enter.

The value 7.0 is returned as a result of multiplying 3.5 by 2.

6. Type (setq rad (/ 0.375 2)) and press Enter.

The value 0.1875 is returned as a result of dividing 0.375 by 2, but the same value is also

assigned to the user-defi ned variable named rad with the setq function. AutoLISP expres-

sions can be nested one inside of another, and they are evaluated from the

innermost to the outermost expression. In this example, the expression (/ 0.375 2) is

evaluated fi rst and returns 0.1875. The next expression, (setq rad 0.1875), is evaluated

and it also returns 0.1875.

7. Type circle and press Enter.

The AutoCAD circle command is started.

8. At the Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: prompt, type

(list 0 5) and press Enter.

The (list 0 5) expression returns a value of (0 5), which is a list of two values that

presents the 2D coordinate of 0,5. The center of the circle is started at 0,5,0.

9. At the Specify radius of circle or [Diameter]: prompt, type !rad and press Enter.

AutoLISP evaluates the rad user-defi ned variable and returns its value to be used for the

circle’s radius. The radius of the circle should be set to 0.1875.

10. In the drawing area, select the new circle.

11. On Windows, right-click in the drawing area and choose Properties. If you are using

AutoCAD on Mac OS, secondary-click (two-fi nger tap or right-click) in the drawing area

and choose Properties.

12. In the Properties palette (Windows) or Properties Inspector (Mac OS)—see Figure 1.1—

you should notice that the Center properties are set to 0,5,0 (X=0.0, Y=5.0, and Z=0.0) and

the Radius property is set to 0.1875.

Page 29: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

WORKING WITH AUTOLISP EXPRESSIONS | 3

c01.indd 1:26:13:PM/07/26/2014 Page 3

Figure 1.1

Result of using

AutoLISP expres-

sions with the

circle command

In this exercise, you did the following:

◆ Entered AutoLISP expressions at the AutoCAD Command prompt and stored values in a

user-defi ned variable (see Chapter 2, “Understanding AutoLISP,” for more information)

◆ Used functions to perform basic math calculations (see Chapter 3, “Calculating and

Working with Values,” for more information)

◆ Created a list that represented a 2D coordinate (see Chapter 4, “Working with Lists,” for

more information)

Working with Commands and InputIn addition to calculating values with AutoLISP and passing those values to a command, you

can execute a command as part of an AutoLISP expression using the command function. Input can

also be requested and passed to a command or saved to a user-defi ned variable.

Page 30: ffi rs.indd 1:31:19:PM/07/26/2014 Page ii

4 | CHAPTER 1 QUICK START FOR NEW AUTOLISP PROGRAMMERS

c01.indd 1:26:13:PM/07/26/2014 Page 4

The following steps demonstrate how to create a layer named Circles with an AutoCAD

Color Index (ACI) of 30 using the -layer command. You’ll then draw a circle on the new layer

with a user-specifi ed center point and radius.

1. At the AutoCAD Command prompt, type (command "-layer" "m" "Circles" "c"

"30" " " " ") and press Enter.

The -layer command is started. The Make (m) option of the command is used to create

the layer named Circles. After the Circles layer is created (or if it already exists), the Make

option makes that layer current. The Color (c) option is then used to set the color of the

Circles layer to ACI 30.

2. Type (command "circle" PAUSE PAUSE) and press Enter.

The circle command is started and the Specify center point for circle or [3P/2P/Ttr

(tan tan radius)]: prompt is displayed. AutoCAD displays this prompt because the pre-

defi ned PAUSE variable is used as the response to the command’s prompt for a value.

3. At the Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: prompt, pick

a point in the drawing area.

4. At the Specify radius of circle or [Diameter]: prompt, type 0.1875 and press Enter.

This command draws a circle with a radius of 0.1875 and places it on the Circles layer.

5. At the Command prompt, type the following and press Enter: (setq cenPt (getpoint

"\nSpecify a center point: ")).

The getpoint function requests a point in the drawing area and can display an optional

custom prompt to the user.

6. At the Specify a center point: prompt, specify a point in the drawing area.

The point you specifi ed is assigned to the cenPt user-defi ned variable.

7. At the Command prompt, type (setq rad (getreal "\nEnter radius: ")) and

press Enter.

The getreal function requests a numeric value.

8. At the Enter radius: prompt, type 0.25 and press Enter.

The value of 0.25 is assigned to the rad user-defi ned variable.

9. Type (command "circle" cenPt rad) and press Enter.

AutoCAD starts the circle command and draws a new circle based on the values

assigned to the cenPt (center point) and rad (radius) user-defi ned variables.

Now that you’ve entered some short expressions, let’s look at creating long expressions—

expressions that can span multiple lines. Using the following steps, you will also see how to give