Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
George Omura. Lisp programing tutorial for AutoCAD customization / Using VBA to Create AutoCAD Applications - Omura, George.pdf
Скачиваний:
113
Добавлен:
02.05.2014
Размер:
883.43 Кб
Скачать

FINISHING TOUCHES

37

Figure 99.28 The Locals window

For example, in Figure 99.28, you can see that varStart is an array of three Doubles. You can see the value of each of these Doubles. The Locals window also allows you to make an immediate change to a variable value while you’re in break mode, by doubleclicking the current value and typing in a new value.

The Me node in the Locals window refers to the UserForm itself. If you expand that node, you can inspect (but not change) the values of all the properties of the UserForm and the controls that it contains.

Finishing Touches

You’ve now seen the entire construction of a simple AutoCAD application. There are a few more topics that will help you polish your applications. In this section, you’ll learn how to automatically load and run VBA code when the user launches AutoCAD, how you can protect your VBA code from prying eyes, and how you can move a form from one AutoCAD VBA project to another.

Automatically Loading a VBA Project

You may want to make the UserForms and procedures in your VBA project available by default whenever AutoCAD is launched. You can accomplish this by creating a default VBA project, and telling AutoCAD at startup that it should load this project. Without both of these steps, AutoCAD will not even initialize the VBA layer at startup.

To create a default VBA project and make sure it loads whenever AutoCAD is started:

1.Save your project with the name acad.dvb in the main AutoCAD folder (the folder where acad.exe is located).

Copyright ©2001 SYBEX, Inc., Alameda, CA

www.sybex.com

38

CHAPTER NINETY-NINE • USING VBA TO CREATE AUTOCAD APPLICATIONS

2.Open your acad.rx file in Notepad or in another text editor. This file is also located in the main AutoCAD folder. If it doesn’t exist, just create a new text file and rename it acad.rx.

3.Add the following line to the acad.rx file:

acadvba.arx

After you’ve followed these steps, open AutoCAD and use Tools Macro Visual Basic Editor to open the editor. You’ll find that all of the VBA objects in the acad.dvb file are already available, without having to explicitly load the project from the menus or from the command line.

Automatically Running a VBA Procedure

You may also want to run a VBA procedure as soon as AutoCAD is opened. For example, you might have a UserForm that allows users to find a drawing file using a database search, and it would be nice to start with this UserForm open. You can accomplish this by placing that UserForm in the startup project, and using an AutoCAD command from elsewhere in the startup process to launch it. For example, you can include a command in the acad.lsp file, which AutoCAD processes at the start of each session.

To run a particular macro at startup, take the following steps:

1.Create a startup VBA project, following the instructions in the previous section. Make sure the procedure that you want to run automatically is in this project.

2.Edit the acad.lsp file (in the main AutoCAD folder), adding these lines at the end:

(defun S::START-UP()

(command _-vbarun modulename.procedurename))

Protecting Your VBA Code

There are several reasons why you may not want users to be able to view the code in your AutoCAD VBA application. First, of course, you may want to protect your intellectual property. Second, it’s dangerous to allow users without knowledge of VBA to get to windows where they might accidentally modify part of the application.

You can use VBA’s built-in protection mechanism to keep these problems from occurring. From the VBA editor menu, select Tools Project Properties. Click the Protection tab and select the Lock Project for Viewing check box. Now supply a password and click OK.

Copyright ©2001 SYBEX, Inc., Alameda, CA

www.sybex.com

IF YOU WANT TO EXPERIMENT…

39

Save the VBA project, and use the AutoCAD Tools Macro menu to unload and then reload it. Now open the VBA editor. You’ll find that the editor opens, but the Project tree is completely unexpanded. You can’t expand it for access to the individual objects in the project until you supply the proper password.

Importing and Exporting Components

Because AutoCAD can only load one VBA project at a time, you may find yourself wanting to combine two projects into one. This allows you to develop a new UserForm, for example, in a test project and then move it to your main application only when it’s ready to use.

The key to combining projects is to use VBA’s built-in import and export functionality. To export any object, select any object in the Project Browser, and choose File Export File from the menus. Select a name and location for the file, and click OK.

To import this object into another project, close the first project and open the second. Then select File Import File from the menus. You’ll be able to choose any type of VBA file (module, UserForm, or class module) to import into the new project.

If You Want to Experiment…

This chapter introduced you to UserForms and their use in turning VBA code into an actual AutoCAD application. By combining these tools with your knowledge of the AutoCAD object model (see Chapter 98, also on this CD) you can create a variety of useful AutoCAD add-ons. For example, try the following activities:

You can create a form that selects a circle and uses a Scrollbar control to expand or contract its radius.

You can create a form that displays the objects contained in a particular layer and lets you choose a set to move to another layer.

You can create a form that counts the number of each type of block in a drawing and present a summary on screen.

Copyright ©2001 SYBEX, Inc., Alameda, CA

www.sybex.com