Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Книги_AutoCad_2 / AutoCAD 2006 and AutoCAD LT 2006 Bible_2004г__.pdf
Скачиваний:
142
Добавлен:
09.04.2015
Размер:
17.83 Mб
Скачать

Chapter 36 Exploring Advanced AutoLISP Topics 1093

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 that you can use with AutoCAD.

 

 

 

Programming with Visual Basic for Applications

Visual Basic for Applications (VBA) is a programming language and environment that is 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, which I 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 file name extension, but can also be stored within the drawing file.

Starting to Work with VBA

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

After you decide to program AutoCAD, the first step is to select a programming language to use.

1096 Part VII Programming AutoCAD

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 of the parts that are 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 integrated development environment, or IDE. 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 that 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 to 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 1097

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 of the objects that 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.

1098 Part VII Programming AutoCAD

Figure 37-2: You can use the Object Browser to see the

AutoCAD library of objects.

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; this is 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.)

Second, objects have methods. A method is an action that 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 that 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

Chapter 37 Programming with Visual Basic for Applications 1099

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. For example, the hierarchy makes it possible to distinguish between an object in your drawing and an object in an Excel spreadsheet.

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)

A shortcut for Application.ActiveDocument is ThisDrawing, and 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 to 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 Additional Resources 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 hierarchical 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 of the VBA objects so that you can work with them.

1100 Part VII Programming AutoCAD

STEPS: Becoming 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.

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, which is labeled 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 of 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 exercise.

Accessing 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 access 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.

Соседние файлы в папке Книги_AutoCad_2