- •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 36 Exploring Advanced AutoLISP Topics 1045
12.To remove the break point, choose View Breakpoints Window to open the Breakpoints dialog box. Click Delete All to delete the break point. Visual LISP automatically closes the dialog box.
13.Click Reset on the Debug toolbar.
14.Close the Visual LISP IDE without saving the file.
As you can see, Visual LISP is not just an editor; it’s a full-featured, integrated development environment for AutoLISP.
Summary
In this chapter, you examined some of the advanced features of AutoLISP and Visual LISP. You read about:
Some of the features of ActiveX
How to use some of Visual LISP’s debugging features, including the Error Trace window, break points, and the Watch window
In the next chapter, you read about the basics of Visual Basic for Applications, another programming language you can use with AutoCAD.
|
|
|
Programming with Visual Basic for Applications
Visual Basic for Applications (VBA) is a programming language and environment included with many Microsoft applications,
such as Word, Excel, PowerPoint, and Access. Since Release 14, VBA has been available with AutoCAD as well. VBA is ideally suited for situations in which you need to work with more than one application at a time. ActiveX, discussed in the last chapter in relation to Visual LISP, enables you to access objects in other applications. However, you can also use VBA to program AutoCAD alone. This chapter introduces you to VBA and shows how you can start to use this powerful language to customize AutoCAD.
AutoCAD LT does not support VBA. This entire chapter applies to AutoCAD only.
Visual Basic for Applications is a variation of Visual Basic. Visual Basic is not related to any specific application. Visual Basic code is compiled into an executable file that stands alone, unrelated to any specific document. VBA, on the other hand, is connected to its application and the document in which you created the code.
VBA provides a simple way to customize AutoCAD, automate tasks, and program applications from within the application.
VBA in AutoCAD works slightly differently from VBA in most other applications in that VBA projects are stored in a separate file, with the DVB filename extension, but can also be stored within the drawing file.
37C H A P T E R
In This Chapter
Understanding VBA and
AutoCAD
Writing VBA code
Getting user input
Creating dialog boxes
Modifying objects
Creating loops and conditions
Debugging and trapping errors
1048 Part VII Programming AutoCAD
Starting to Work with VBA
After you decide to program AutoCAD, the first step is to select a programming language to use.
VBA has the following advantages:
VBA is faster than AutoLISP, even when AutoLISP is compiled.
VBA is common to many other applications. If you’ve used VBA before, you can easily transfer your knowledge to using VBA in AutoCAD. You’re also more likely to find other programmers who know VBA compared to AutoLISP.
VBA is generally easier to learn than AutoLISP because of its syntax.
On the other hand, AutoLISP has the advantage of backward compatibility with prior releases of AutoCAD. Of course, if you’re familiar with AutoLISP but not VBA, it’s hard to beat the ease of working with a language that you already know and use.
VBA programs are saved in projects. A project contains all the parts needed to execute the function of the program. You can use the VBA Manager to view your VBA projects. The VBA Manager also enables you to load, unload, save, and create VBA projects. To open the VBA Manager, choose Tools Macro VBA Manager.
Opening the VBA environment
To start working with VBA, you must open the VBA environment. VBA has its own interface, just like Visual LISP. To open VBA in AutoCAD, choose Tools Macro Visual Basic Editor (or type vbaide ). Like Visual LISP, VBA has its own interface, called an IDE, for integrated development environment. AutoCAD displays the VBA environment window.
VBA projects can contain modules. A module is a self-contained piece of programming code. A VBA project can have one or more modules.
To add a module, choose Insert Module or click the drop-down list to the right of the second button on the VBA IDE Standard toolbar; then choose Module. AutoCAD opens a
|
module text editor window so you can start typing code. In the Project window, VBA adds a |
|
new module to the list of modules. By default, the first module is called Module1. Figure 37-1 |
|
shows the VBA IDE including the text editor. If you don’t see the Project Explorer or the |
|
Properties window, use the View menu of the VBA IDE and choose Project Explorer or Properties |
|
Window. |
Tip |
You can resize the module text editor as you would any window. As you start adding code to |
|
the text editor, you’ll find it easier to work with a larger window. Click the Maximize button |
|
to enlarge the text editor to its maximum size. |
AutoCAD is still running in the background. You can return to it at any time by clicking its button on the Windows task bar or by clicking View AutoCAD on the VBA IDE
Standard toolbar.
Chapter 37 Programming with Visual Basic for Applications 1049
Figure 37-1: The VBA environment window.
Getting acquainted with VBA
|
VBA enables you to easily retrieve, create, and manipulate objects. To get a list of |
|
objects, open the Object Browser by clicking Object Browser on the toolbar or choosing |
|
View Object Browser. Objects are organized into libraries. All the objects you work with in |
|
AutoCAD are in the AutoCAD library. |
|
To see the list of AutoCAD objects, click the <All Libraries> drop-down list and choose |
|
AutoCAD. You can see the result in Figure 37-2. |
Tip |
You can resize the panes in the VBA window. Place the mouse cursor on the bar between the |
|
panes until you see the double-headed arrow, and drag it either left or right. |
Objects and collections of objects
In the left pane, labeled Classes, you see the list of objects. In VBA, you can have both individual objects and collections of objects. For example, AcadLayer would be the layer object and AcadLayers would be the collection of layers. The purpose of collections is to enable you to work with a group of objects. For example, to add a layer, you add it to the collection of layers because the new layer is not related to any existing layer.
Methods and properties
What can you do with objects in VBA? First, objects can have properties. For example, you can set an ellipse to the color red because one of the properties of the ellipse object is color. (Of course, all drawing objects have color as one of their properties.)
1050 Part VII Programming AutoCAD
Figure 37-2: You can use the Object Browser to see the
AutoCAD library of objects.
Second, objects have methods. A method is an action you can take on the object. For example, you can erase an ellipse because erase is a method of the ellipse object (as well as of all drawing objects).
In the Object Browser, the right pane, Members, lists the properties and methods of any object you choose in the Classes pane.
Investigating the hierarchy model
Although you might first think that an object in VBA is the same as an object in an AutoCAD drawing, there is more to the story. In VBA, everything is an object. For example, AutoCAD as an application is an object. Your current drawing is also an object. Model space and paper space are also objects. Therefore, to specify an object in your drawing, you need to specify the application, the drawing, and finally the object in the drawing. To do this, VBA works with a hierarchy of objects. The hierarchy makes it possible to distinguish between an object in your drawing and an object in an Excel spreadsheet, for example.
Objects are specified from the most general to the most specific with a period between each part of the definition. You then add the desired method or properties after another period. For example, you can use the following VBA code to add a circle:
Application.ActiveDocument.ModelSpace.AddCircle(center, radius)
Chapter 37 Programming with Visual Basic for Applications 1051
A shortcut for Application.ActiveDocument is ThisDrawing, so you can also use:
ThisDrawing.ModelSpace.AddCircle(center, radius)
In order to work with any object, you need to know where it fits in the hierarchy. The quickest way to see the hierarchical structure from the VBA IDE is to choose any method
or property in the Object Browser and choose Help on the Object Browser’s toolbar. On the Contents tab, choose Object Model to see the listing in Figure 37-3.
Note |
Within AutoCAD, choose Help Developer Help. You’re now in a new Help system. On the |
|
|
Contents tab, double-click ActiveX and VBA Reference. Click Object Model to see the hierar- |
|
|
chical model shown in Figure 37-3 or double-click Objects to see the alphabetical object list. |
|
|
|
|
|
|
|
Figure 37-3: The object model shows you the hierarchy of all the VBA objects so that you can work with them.
STEP-BY-STEP: Getting Acquainted with the VBA Environment
1.With any drawing open in AutoCAD, choose Tools Macro Visual Basic Editor. AutoCAD opens the VBA IDE.
2.Choose Insert Module from the menu. The VBA IDE opens the module text editor window.
1052 Part VII Programming AutoCAD
3.Move down to the Windows task bar and click the AutoCAD button to return to AutoCAD. Now click the Microsoft Visual Basic button to return to the VBA IDE.
4.Click Object Browser on the VBA IDE Standard toolbar. Click the <All Libraries> drop-down list and choose AutoCAD. If necessary, maximize the window by
clicking the maximize button at the top-right corner of the window.
5.In the Classes pane, click AcadLine. You see the associated properties and methods in the right pane.
6.In the right pane (Members of AcadLine), click Delete. You see the following at the bottom of the window:
Sub Delete()
Member of AutoCAD.AcadLine Deletes a specified object.
Sub indicates the start of a VBA subroutine. Methods are listed in this way.
7.In the right pane, click Layer. At the bottom of the window, you see the following:
Property Layer As String Member of AutoCAD.AcadLine
Specifies the current layer of the object.
This indicates that Layer is a property of AcadLine. String refers to the data type, discussed later in this chapter.
8.Click Help in the Object Browser window. You see the Help page for the Layer property.
9.On the Contents tab, double-click Objects and then click Line object. Scroll down to see all the properties and methods that belong to the Line object.
10.In the second paragraph of the description, the word AddLine is underlined with a hypertext line. Click it to see the description of the AddLine method.
11.At the top of the page, click Example (also with a hypertext underline). You see an example of VBA code for creating a line.
12.Close Help by clicking the Close button at the top-right corner of each window. Leave the VBA IDE window open if you’re continuing on to the next Step-by-Step exercise.
Getting help
VBA offers several help features. You’ve already seen the Object Browser, which provides you with a list of objects as well as their properties and methods. To get help on an object, choose it in Object Browser and click Help. You can do the same for a method or property, as shown in Figure 37-4.
After you open a help page, click Example to see an example. These examples are a great way to learn VBA. You can copy a snippet of VBA code and paste it into your own routine and then edit it as you want.