
- •Contents
- •Contents at a Glance
- •Acknowledgments
- •Preface
- •Is This Book for You?
- •How This Book Is Organized
- •How to Use This Book
- •Doing the Exercises
- •Conventions Used in This Book
- •What the Icons Mean
- •About the CD-ROM
- •Other Information
- •Contacting the Author
- •Foreword
- •Credits
- •About the Author
- •Summary
- •AutoCAD’s Advantages
- •Comparing AutoCAD and AutoCAD LT
- •Starting AutoCAD and AutoCAD LT
- •Creating a New Drawing
- •Using the AutoCAD and AutoCAD LT Interface
- •Creating a New Folder
- •Using the Interface
- •Saving a Drawing
- •Closing a Drawing and Exiting from AutoCAD and AutoCAD LT
- •Summary
- •Creating a New Drawing from a Template
- •Working with Templates
- •Opening a Drawing with Default Settings
- •Opening an Existing Drawing
- •Using an Existing Drawing as a Prototype
- •Saving a Drawing Under a New Name
- •Summary
- •The Command Line and Dynamic Input
- •Command Techniques
- •Of Mice and Pucks
- •Getting Help
- •Summary
- •Typing Coordinates
- •Displaying Coordinates
- •Picking Coordinates on the Screen
- •Overriding Coordinate Settings
- •Locating Points
- •Summary
- •Choosing Unit Types
- •Drawing Limits
- •Understanding Scales
- •Creating a Title Block
- •Specifying Common Setup Options
- •Customizing with the MVSETUP Command
- •Using the Setup Wizards
- •Summary
- •Using the LINE Command
- •Drawing Rectangles
- •Drawing Polygons
- •Creating Construction Lines
- •Creating Rays
- •Summary
- •Drawing Circles
- •Drawing Arcs
- •Creating Ellipses and Elliptical Arcs
- •Making Donuts
- •Placing Points
- •Summary
- •Panning
- •Using the ZOOM Command
- •Using Aerial View
- •Saving Named Views
- •Working with Tiled Viewports
- •Using Snap Rotation
- •Understanding User Coordinate Systems
- •Creating Isometric Drawings
- •Summary
- •Editing a Drawing
- •Selecting Objects
- •Summary
- •Copying and Moving Objects
- •Resizing Commands
- •Using Construction Commands
- •Creating a Revision Cloud
- •Hiding Objects with a Wipeout
- •Double-Clicking to Edit Objects
- •Grips
- •Editing with the Properties Palette
- •Selection Filters
- •Groups
- •Summary
- •Working with Layers
- •Changing Object Color, Linetype, and Lineweight
- •Working with Linetype Scales
- •Importing Layers and Linetypes from Other Drawings
- •Matching Properties
- •Summary
- •Drawing-Level Information
- •Object-Level Information
- •Measurement Commands
- •AutoCAD’s Calculator
- •Summary
- •Creating Single-Line Text
- •Understanding Text Styles
- •Creating Multiline Text
- •Creating Tables
- •Inserting Fields
- •Managing Text
- •Finding Text in Your Drawing
- •Checking Your Spelling
- •Customizing the spelling dictionary
- •Summary
- •Working with Dimensions
- •Drawing Linear Dimensions
- •Drawing Aligned Dimensions
- •Creating Baseline and Continued Dimensions
- •Dimensioning Arcs and Circles
- •Dimensioning Angles
- •Creating Ordinate Dimensions
- •Drawing Leaders
- •Using Quick Dimension
- •Editing Dimensions
- •Summary
- •Understanding Dimension Styles
- •Defining a New Dimension Style
- •Changing Dimension Styles
- •Creating Geometric Tolerances
- •Summary
- •Creating and Editing Polylines
- •Drawing and Editing Splines
- •Creating Regions
- •Creating Boundaries
- •Creating Hatches
- •Creating and Editing Multilines
- •Creating Dlines
- •Using the SKETCH Command
- •Digitizing Drawings with the TABLET Command
- •Summary
- •Preparing a Drawing for Plotting or Printing
- •Creating a Layout in Paper Space
- •Working with Plot Styles
- •Plotting a Drawing
- •Summary
- •Combining Objects into Blocks
- •Inserting Blocks and Files into Drawings
- •Managing Blocks
- •Creating and Using Dynamic Blocks
- •Using Windows Features
- •Working with Attributes
- •Summary
- •Understanding External References
- •Editing an Xref within Your Drawing
- •Controlling Xref Display
- •Managing Xrefs
- •Summary
- •Preparing for Database Connectivity
- •Connecting to Your Database
- •Linking Data to Drawing Objects
- •Creating Labels
- •Querying with the Query Editor
- •Working with Query Files
- •Summary
- •Working with 3D Coordinates
- •Using Elevation and Thickness
- •Working with the User Coordinate System
- •Summary
- •Working with the Standard Viewpoints
- •Using DDVPOINT
- •Working with the Tripod and Compass
- •Displaying a Quick Plan View
- •Shading Your Drawing
- •Using 3D Orbit
- •Using Tiled Viewports
- •Defining a Perspective View
- •Laying Out 3D Drawings
- •Summary
- •Drawing Surfaces with 3DFACE
- •Drawing Surfaces with PFACE
- •Creating Polygon Meshes with 3DMESH
- •Drawing Standard 3D Shapes
- •Drawing a Revolved Surface
- •Drawing an Extruded Surface
- •Drawing Ruled Surfaces
- •Drawing Edge Surfaces
- •Summary
- •Drawing Standard Shapes
- •Creating Extruded Solids
- •Drawing Revolved Solids
- •Creating Complex Solids
- •Sectioning and Slicing Solids
- •Using Editing Commands in 3D
- •Editing Solids
- •Listing Solid Properties
- •Summary
- •Understanding Rendering
- •Creating Lights
- •Creating Scenes
- •Working with Materials
- •Using Backgrounds
- •Doing the Final Render
- •Summary
- •Accessing Drawing Components with the DesignCenter
- •Accessing Drawing Content with Tool Palettes
- •Setting Standards for Drawings
- •Organizing Your Drawings
- •Working with Sheet Sets
- •Maintaining Security
- •Keeping Track of Referenced Files
- •Handling Errors and Crashes
- •Managing Drawings from Prior Releases
- •Summary
- •Importing and Exporting Other File Formats
- •Working with Raster Images
- •Pasting, Linking, and Embedding Objects
- •Summary
- •Sending Drawings
- •Opening Drawings from the Web
- •Creating Object Hyperlinks
- •Publishing Drawings
- •Summary
- •Working with Customizable Files
- •Creating Keyboard Shortcuts for Commands
- •Customizing Toolbars
- •Customizing Tool Palettes
- •Summary
- •Creating Macros with Script Files
- •Creating Slide Shows
- •Creating Slide Libraries
- •Summary
- •Creating Linetypes
- •Creating Hatch Patterns
- •Summary
- •Creating Shapes
- •Creating Fonts
- •Summary
- •Working with the Customization File
- •Customizing a Menu
- •Summary
- •Introducing Visual LISP
- •Getting Help in Visual LISP
- •Working with AutoLISP Expressions
- •Using AutoLISP on the Command Line
- •Creating AutoLISP Files
- •Summary
- •Creating Variables
- •Working with AutoCAD Commands
- •Working with Lists
- •Setting Conditions
- •Managing Drawing Objects
- •Getting Input from the User
- •Putting on the Finishing Touches
- •Summary
- •Understanding Local and Global Variables
- •Working with Visual LISP ActiveX Functions
- •Debugging Code
- •Summary
- •Starting to Work with VBA
- •Writing VBA Code
- •Getting User Input
- •Creating Dialog Boxes
- •Modifying Objects
- •Debugging and Trapping Errors
- •Moving to Advanced Programming
- •Summary
- •A Final Word
- •Installing AutoCAD and AutoCAD LT
- •Configuring and Using Workspaces
- •Configuring AutoCAD
- •Starting AutoCAD Your Way
- •Configuring a Plotter
- •Discovering AutoCAD and AutoCAD LT
- •Accessing Technical Support
- •Autodesk User Groups
- •Internet Resources
- •System Requirements
- •Using the CD-ROM with Microsoft Windows
- •What’s on the CD-ROM
- •Troubleshooting
- •Index

Chapter 35 Exploring AutoLISP Further 1075
If the selected object is a circle, you see its radius change to match the value that you specified to be the new text height. This is definitely not what you intended when writing this program.
3. Modify the program so that it reads as follows, and save it as ab35-06.lsp:
;;;modifies text height and content (value) (defun c:chgmytext (/ src_object new_ht new_str)
(terpri)
(setq src_object (entget (car (entsel))))
(if (equal (assoc 0 src_object) ‘(0 . “TEXT”)) (progn
(princ “What is the new height for the text? “) (setq new_ht (getreal))
(princ “What is the new text value? “) (setq new_str (getstring))
(setq src_object
(subst (cons 40 new_ht) (assoc 40 src_object) src_object)
)
(setq src_object
(subst (cons 1 new_str) (assoc 1 src_object)src_object)
)
(entmod src_object)
)
(princ “You must select a text object.”)
)
(princ)
)
4.Load ab35-06.lsp. Start chgmytext and try out the routine again with a circle or other non-text object.
Don’t save your drawing.
Summary
In this chapter, you learned:
How to create variables
How to create AutoLISP functions
How to work with AutoCAD commands and system variables
About extending AutoLISP’s power by using lists and looping
How to modify and get information about drawing objects
How to create selection sets
About obtaining user input
Tools for finishing off your AutoLISP routines by adding some error handling, making sure the routine exits quietly, and adding helpful comments about the routine’s function.
In the next chapter, you read about some of the more-advanced features of Visual LISP.
|
|
|


Exploring Advanced
AutoLISP Topics
This chapter builds on the previous two chapters and introduces you to a few advanced AutoLISP topics, including local and global
variables, ActiveX, and debugging.
AutoCAD LT does not support AutoLISP, and so this entire chapter is for AutoCAD only.
Understanding Local and
Global Variables
In this section, you read how local and global variables are accessed within a function, as well as some common syntax. You also discover what can happen when global variables are not properly documented.
Chapter 35 explained that a variable is a symbolic name that can be operated on in a given program. An important part of using variables is being able to assign values to them. There are two types of variables, global and local.
A global variable is exposed, or available, to all AutoLISP functions that you’ve loaded into your drawing. A global variable retains its value after the program that defined it is finished. You use a global variable when you want its value to be available across an entire project, as opposed to just one function within a project. This allows you to retain a fixed value that might be used and assigned by different functions, or for debugging. Any variable that you don’t specifically define as a local variable is a global variable.
A local variable is temporarily assigned a value during a function’s execution. After the function completes executing, the local variable value is discarded. AutoLISP can now use the memory that was taken up by that local variable. You use a local variable when you want to be sure that you don’t have variable values floating around and interfering with other functions. Local variables are also easier to debug because they affect only the code within their function. In general, most of your variables should be local. You create a local variable and declare it in the DEFUN statement after the slash and a space, as in this example:
36C H A P T E R
In This Chapter
Understanding local and global variables
Working with Visual
LISP ActiveX functions
Debugging code
Using the
Error Trace window
Using the
Watch window
(defun list-objects ( / counter sset)...

1078 Part VII Programming AutoCAD
Caution |
Global variables can be tricky. For example, they can easily cause bugs. This is because their val- |
|
ues persist and can be hard to debug because the values are hard to find. A common syntax for |
|
global variables is to prefix and suffix the variable with an asterisk, as in *aGlobal*. In this way, |
|
you can easily identify global variables in your code. Keep your use of global variables to a min- |
|
imum, and carefully document those that you do use. Failure to follow these simple rules could |
|
result in undesirable and difficult-to-trace bugs. |
STEPS: Using Local and Global Variables
1.Start a new drawing using the acad.dwt template. You should not have any other drawing open.
2.Open the Visual LISP Editor.
3.In the Console window, type the following line and then press Ctrl+Enter. You use Ctrl+Enter in the Console window to enter in code of more than one line. This line declares one local variable:
(defun local-variable (/ var1)
4.Type the second line of code in the Console window as follows:
(setq var1 “I’m local”))
This sets the local variable to the string that you typed. The Console returns the name of the function:
LOCAL-VARIABLE
5.Before you test this function, you can check out the current value of the local variable, var1. Type var1 in the Visual LISP Console. The Console returns:
nil
As you can see, the value is nil.
6.Test the local-variable function to check the value that it sets to the var1 variable. In the Console, type (local-variable) . The Console returns:
“I’m local”
You now know that the local-variable function definitely assigns the value of “I’m local” to the variable var1.
7.To create a global variable, type (setq var1 “I’m global”) in the Console. The Console returns:
“I’m global”
You know that the value of var1 is now “I’m global”.
8.Test the local variable again by typing (local-variable) in the Console. The Console returns “I’m local” because it executes the local-variable function.
9.Test the variable var1 to see what its value is now. In the Console, type var1 . The Console returns “I’m global”. The local variable was not retained when the function used the variable because the variable was local to the function. However, the global variable’s value persisted.