AutoCAD Application Object (The AutoCAD Object Model) (AutoCAD VBA)

The AutoCAD Object Model defines the hierarchical structure for all objects that AutoCAD exposes to VBA. The AutoCAD Application object sits at the top and represents the entire AutoCAD application; at the next level down you’ll find the AutoCAD Documents collection and the Document object, which include the drawing objects. Also at this level is the Preferences collection, which allows you to set your preferred AutoCAD options from your code.

In this topic, you’ll learn how to access the AutoCAD’s various spaces and how to communicate with users from code using message boxes and the command line. At the end of the topic you’ll see demonstrations of some of the string-handling functions at work.

This topic marks the beginning of the part of this topic that will be of most benefit to intermediate readers. As an AutoCAD developer, you must be able to find your way around the AutoCAD Object Model.

The AutoCAD Application object sits at the top of the object model hierarchy and represents the whole AutoCAD application (see Figure 5.1). It contains all the details about the state of all components of the current AutoCAD session’s environment, and everything the current session contains. To view the AutoCAD Object Model, choose Help ^ Developer Help from the AutoCAD window, select the Contents tab, and click ActiveX and VBA Reference. The Object Model item is on top of the list.

In the object model hierarchy, objects are shown in boxes with rounded corners, and collections of objects are shown in boxes with squared corners.


The AutoCAD object model

Figure 5.1 The AutoCAD object model

As you can see, the only way to access an AutoCAD object is through the Application object, which can be specified explicitly in a fully qualified statement, or implied by simply omitting the statement. For example, you can change the height of the active AutoCAD drawing from VBA code using the statement

tmp757e204_thumb

where the ActiveDocument is really the current AutoCAD drawing. Because there can only be one Application object ever, and one active document at any particular time, they can be omitted. Thus the preceding statement can be shortened to

tmp757e205_thumb

and the interpreter automatically assumes that you mean the Height property of the current drawing.

The current drawing is automatically exposed to you from the IDE through its special ThisDrawing keyword to provide yet another way of referring to the same object:

tmp757e206_thumb

This third statement performs exactly the same function as the other two; it’s shorter than the first one and much clearer than the second one. Using ThisDrawing, it’s easy to see (for example) that the Height property belongs to the active drawing rather than to the AutoCAD window or an active control. For conciseness and clarity, I use the ThisDrawing keyword throughout this topic to refer to the current drawing.

The ThisDrawing keyword is a good replacement for Application.ActiveDocument not only because it’s more concise but also because it better reflects that the active drawing object is being referred to. Avoid writing code that’s ambiguous, because it is harder to debug and update.

In addition to providing access to documents, the Application object is your means to the MenuBar and MenuGroups collections; these allow you to control AutoCAD’s menus and toolbars from code. Also available from the Application object is the Preferences object that, as mentioned, allows you to adjust the settings of the AutoCAD Options dialog box in your code. Later in this topic, the section about the Preferences object gives you the details of how this is done.

Next post:

Previous post: