- •Foreword
- •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
- •Acknowledgments
- •Contents at a Glance
- •Contents
- •Getting Acquainted with AutoCAD and AutoCAD LT
- •Starting AutoCAD and AutoCAD LT
- •Creating a New Drawing
- •Using the AutoCAD and AutoCAD LT Interface
- •Creating Your First Drawing
- •Saving a Drawing
- •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
- •Command Techniques
- •Of Mice and Pucks
- •Getting Help
- •Summary
- •Typing Coordinates
- •Displaying Coordinates
- •Picking Coordinates on the Screen
- •Locating Points
- •Summary
- •Unit Types
- •Drawing Limits
- •Understanding Scales
- •Inserting a Title Block
- •Common Setup Options
- •The MVSETUP Command
- •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
- •The ZOOM Command
- •Aerial View
- •Named Views
- •Tiled Viewports
- •Snap Rotation
- •User Coordinate Systems
- •Isometric Drawing
- •Summary
- •Editing a Drawing
- •Selecting Objects
- •Summary
- •Copying and Moving Objects
- •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
- •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
- •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
- •Getting 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 Menu Files
- •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
- •A Final Word
- •Installing AutoCAD and AutoCAD LT
- •Configuring AutoCAD
- •Starting AutoCAD Your Way
- •Configuring a Plotter
- •System Requirements
- •Using the CD with Microsoft Windows
- •What’s on the CD
- •Troubleshooting
- •Index
Chapter 34 Understanding AutoLISP and Visual LISP Basics |
993 |
Closing a file and Visual LISP
To close a file within Visual LISP, click its Close button. (You can also press Ctrl+F4.) To exit Visual LISP, click its Close button or press Alt+F4. As with AutoCAD, Visual LISP warns you about saving your changes.
On the |
The file used in the following Step-by-Step exercise on using the Visual LISP interface, |
CD-ROM |
ab34-a.lsp, is in the Drawings folder on the CD-ROM. |
STEP-BY-STEP: Using the Visual LISP Interface
1.From AutoCAD, choose Tools AutoLISP Visual LISP Editor to open Visual LISP.
2.Choose Open File on the Visual LISP Standard toolbar and open ab34-a.lsp from the CD-ROM. Save the file as ab34-1.lsp in your AutoCAD Bible folder.
3.Choose Format Edit Window on the Tools toolbar. AutoCAD automatically formats the code.
4.Select the line of the code that reads:
(command “_circle” pt “3”)
5.Choose Comment Block. AutoCAD places three semicolons before the text and shades it. Choose Uncomment Block. AutoCAD returns the code to its previous
state.
6.Choose Check Edit Window on the toolbar. AutoCAD opens the Build Output window and issues the Check done message.
7.Click anywhere in the edit window to activate it. Choose Load Active Edit Window on the toolbar. AutoCAD loads the routine.
8.In the Visual LISP Console window, type (c:circle3) and press Enter. Visual LISP returns you to AutoCAD and starts the program. Pick a point. When you’re done, you’re automatically returned to the Visual LISP window.
9.Click the text editor window and choose Save File. Click the Close button at the topright corner of the text editor window to close the routine.
10.Click the Close button at the top-right corner of your screen to close Visual LISP.
Getting Help in Visual LISP
Visual LISP offers two ways for you to get help while writing AutoLISP code — context-sensitive help on any term you select and the overall Help system.
To get help on any AutoLISP function in your code, select the function (either in the edit window or in the Console window) and choose Help on the Visual LISP Tools toolbar.
Figure 34-6 shows the screen that appears when you select setq and choose Help.
994 Part VII Programming AutoCAD
Figure 34-6: When you select any AutoLISP function in your code and choose Help, Visual LISP opens the help page for that function.
To open the entire Visual LISP and ActiveX Help system, choose Help Visual LISP Help Topics on the Visual LISP menu or Press F1. Visual LISP displays the screen shown in Figure 34-7.
Figure 34-7: The main help page of Visual LISP’s Help system.
Chapter 34 Understanding AutoLISP and Visual LISP Basics |
995 |
All these help features can be a bit overwhelming, but for AutoLISP and Visual LISP you basically have three choices:
AutoLISP Reference: This guide is equivalent to the Command Reference in AutoCAD’s Help system. Here you’ll find an alphabetical list of all AutoLISP functions.
AutoLISP Developer’s Guide: This guide is equivalent to the AutoCAD User’s Guide. It contains all the basic topics you need to know to program in Visual LISP.
AutoLISP Tutorial: If you remember the Garden Path tutorial for AutoLISP (which has been around for many years), you’ll recognize this tutorial, now rewritten for Visual LISP.
Working with AutoLISP Expressions
As with any programming language, AutoLISP has its own functions and syntax. This section explains how to start writing AutoLISP code. One big advantage to AutoLISP is that you can test your code immediately, unlike the case of compiled languages for which extra steps are required. For compiled languages, every time a code change is made, the code must be recompiled and re-linked before you can see the changes in AutoCAD. This is the case with ObjectARX, for example.
Understanding AutoLISP syntax
In AutoLISP’s syntax, an operator always comes first, followed by a number of operands. An operator can be thought of as a function that does something, and the operands are what the function operates on. Place a space between each component of the statement.
Another way to describe the syntax of AutoLISP is that, within each set of parentheses, the first item is a function, and what follows are parameters or arguments to that function.
Working with numbers and text
Table 34-1 lists the basic arithmetic functions. You can use these functions on anything that has a value. For example, if you create a variable with a value of 2, you can use that variable with the arithmetic functions.
|
Table 34-1: Basic Arithmetic Functions |
|
|
Function |
Description |
|
|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
Sqrt |
Square root |
|
|
996 Part VII Programming AutoCAD
Table 34-1 lists only a few fundamental arithmetic operators (or functions). Many operators are available to the AutoLISP programmer; you can find them in the AutoLISP Function Synopsis Appendix of the AutoLISP Developer’s Guide, under Basic Functions Arithmetic Functions.
Some fundamental arithmetic operations can provide a feel for the fundamentals. If you type (+ 2 3) on the AutoCAD command line (being careful to put a blank space after the + and the 2), AutoLISP responds with 5. This syntax of having the operator (the plus sign) come before the operands (2 and 3) is different from that of many languages but is important to understand in AutoLISP.
To nest expressions, simply add another set of parentheses to enclose the nested expression. For example, when you enter (* 5 (+ 2 3)), AutoCAD responds with 25. The nested expression is evaluated from the innermost parenthesis pair out; first 2 and 3 are added, and then that sum is multiplied by 5.
Working with floating point numbers (numbers with decimal points) is as easy as working with integers. If you type (/ 7.3 5.84) , AutoLISP responds with 1.25.
Note |
If you’re using a number less than 1, you must include the leading 0, as in (+ 2.5 0.5). |
Working with text is as easy as working with numbers. In AutoLISP, a string is simply some text. Table 34-2 lists some common string functions.
|
Table 34-2: Basic String Functions |
|
|
Function |
Description |
|
|
STRCAT |
Concatenates (attaches) strings to one another. |
SUBSTR |
Returns (provides you with) a substring (a portion) of a string. The first |
|
argument is the string, the second is an integer that specifies the position |
|
of the first character you want, and the third (optional) is the number of |
|
characters you want. |
STRLEN |
Calculates string length; returns the number of characters including spaces |
|
in the string. |
|
|
Here are two examples:
(strcat “Today is “ “a good day” “.”) “Today is a good day.”
(substr “Today is a good day.” 12 4) “good”
STRCAT stands for string concatenate and appends one string with another. Any number of strings can come after the strcat function. (There are three strings enclosed in quotes in the strcat example.)
SUBSTR stands for substring, and in this example, it returns the four characters starting at position 12, as shown in the example.