When you set a reference to an application's object library, the Object Browser lists all the objects, methods, properties, constants, and events that application exposes. To open the Object Browser, choose View > Object Browser or press F2. By default, the Object Browser lists all libraries your project currently references. To view just the AutoCAD library, for example, click AutoCAD in the drop-down list in the Object Browser, as shown in Figure 1-21. The Object Browser gives you a perspective of the objects an application exposes and the methods, properties, events, and constants those objects expose for your application to manipulate.
Loading and Running Applications
You can load and subsequently execute your VBA applications in many different ways. This section explains the most common ways.
AutoCAD searches the support file search path for the file acad.dvb. If AutoCAD finds this file, it loads it into the current session. The following example illustrates how to implement this feature.
Public Sub Start()
Application.ActiveDocument.SetVariable "OSMODE", 35 End Sub
Place this code in the ThisDrawing module and save the file as acad.dvb. Save this file in any subdirectory specified in the support file search path. Now each time you start an AutoCAD session, AutoCAD loads this file. Also, if you include a routine called AcadStartup, AutoCAD executes it when it loads acad.dvb.
The example in Figure 1-22 shows how to use the macros that you have stored in the acad.dvb file.
J Macro name: start
Type -vbarun then the name of the macro to run (Start in this instance). In this example, the value of the system variable OSMODE is set to 35. This is a convenient way to store macros that will invoke AutoCAD with a particular setup.
AutoCAD automatically searches the default search path (a combination of the support/file path list, the current working folder, and the shortcut's startup folder) for a file named acaddoc.lsp. If AutoCAD finds this file, it loads it into the current drawing. Unlike with acad.dvb, which loads only when you start a new AutoCAD session, AutoCAD loads acaddoc.lsp each time you open or create a drawing.
Tip To verify the actual search-path list, go to the command prompt, type -insert, enter a meaningless string of characters such as sdfsdfsdf (keep it less than 31 characters, though), and press Enter. AutoCAD displays a list of folders in which it tried to find your file when it failed.
AutoCAD provides a special programmer-defined function called S::STARTUP that, if included in acaddoc.lsp or any default startup LISP file, is automatically executed when you open or create a drawing. You can define S::STARTUP in acaddoc.lsp to perform setup operations for each drawing.
You can define an S::STARTUP function in several different places, including acad.lsp, acaddoc.lsp, a .mnl file, or any AutoLISP file loaded from any of these files. You can overwrite a previously defined S::STARTUP function—which means that another definition can overwrite your S::STARTUP routine.
■ Note Never modify or replace the files acad2006.lsp or acad2006doc.lsp, as they may be overwritten by a service pack installation without warning. It is also a good idea to append the S::STARTUP function instead of defining it, as many third-party products rely upon S::STARTUP to initialize their environments. If you define a new S::STARTUP, you could disable some or all of another loaded product in the process.
The following example shows how to ensure that your startup function works with other functions.
(defun-q Startup () (command "-vbarun" "Start")
(setq S::STARTUP (append S::STARTUP Startup))
This code appends your startup function to any existing S::STARTUP function, and then redefines the S::STARTUP function to include your startup code. This works regardless of any other existence of an S::STARTUP function.
■ Note In AutoLISP, you must use defun-q as opposed to defun for this example to work properly. Visual LISP constructs functions differently from AutoLISP between defun and defun-q.
AutoCAD lets you embed a VBA project into a drawing. Each time the drawing is loaded, AutoCAD also loads the VBA project embedded in the drawing. Of all the options to load VBA projects automatically, this is the worst one. It stores the VBA project with the drawing, making your drawing file that much bigger. If you copy the drawing file to create a new drawing, you also copy its VBA project. If you want to change the VBA project, you need to change each VBA project in every drawing file that you created from the original. In addition, delivering drawings with embedded macros to customers imposes a serious potential security risk on their part. Well, you get the picture of why this is the worst option to choose!
Was this article helpful?