Graphical Object Spaces (The AutoCAD Object Model) (AutoCAD VBA)

All graphical objects belong to either the ModelSpace collection or the PaperSpace collection. These collections, in turn, belong to a Document object, which in turn belongs to a Documents collection. This arrangement is demonstrated in Figure 5.9.

In addition to the ModelSpace and PaperSpace collections, there is also a Blocks collection. Each Block object in the collection is capable of containing a group of graphical objects that can be inserted into drawings or can undergo transformations as if they were a single item.

ModelSpace Collection

The Model Space is the drawing area where you create your drawings—the Model tab from the AutoCAD window. There is only one Model tab, and any updates done to the model within this space are automatically reflected in any Paper Space pages that contain them. All the geometry that describes the model is stored in the ModelSpace collection.

PaperSpace Collection

The Paper Space is where you finalize the layout of specific views of your drawing ready for printing or plotting. Each Paper Space page is called a Layout and has a tab with Layout1, Layout2, and so on in the AutoCAD window. You can have as many Layouts as you like, each representing a formatted page. All the information required for printing or plotting the Layouts is stored in the PaperSpace collection. The PaperSpace property of the Document object refers to the last Layout that was accessed.


Changing the ActiveSpace Property

The ActiveSpace property of the Document object is set to 1 (the built-in constant acModelSpace) if the model space is currently active, or 0 (the acPaperSpace constant) if the Paper Space is currently active. Let’s write two macros that will toggle the active space from Model Space to Paper Space and vice-versa. Follow the steps of Exercise 5.2.

Graphical objects in the object model

Figure 5.9 Graphical objects in the object model

Exercise 5.2: Changing from Model Space to Paper Space and Back Again

1.    Start a new project in the IDE.

2.    Choose Insert ^ Module and enter the code for both macros given in Listing 5.2 into the Code window. The empty Code window for Module1 appears.

3.    Return to the AutoCAD environment. Choose Tools ^ Macro ^ Macros and run the ModelSpaceToPaperSpace macro. The page with the Layout1 tab becomes the active page.

4.    Choose Tools ^ Macro ^ Macros and run the PaperSpaceToModelSpace macro. The page with the Model tab becomes the active page.

Listing 5.2: ModelSpaceToPaperSpace Macro

Listing 5.2: ModelSpaceToPaperSpace Macro

Analysis

•    Line 1 declares ModelSpaceToPaperSpace as a public macro so that it can be used by any module or UserForm in the same project.

•    Line 2 uses the ActiveSpace property of ThisDrawing (the active document) to make the active space in the AutoCAD window the first Layout page from the PaperSpace collection.

•    Line 3 ends the ModelSpaceToPaperSpace macro.

•    Line 4 is a blank line to make the code easier to read.

•    Line 5 opens the PaperSpaceToModelSpace macro as public.

•    Line 6 makes the Model Space the active space, which opens the Model tab.

•    Line 7 ends the ModelSpaceToPaperSpace macro.

Block Objects

A Block object contains one or more AutoCAD objects. As you add graphical objects to your drawing, you can add them to a specific Block object. All the objects in the same Block are considered to be a single entity when it comes to performing transformations or adding the Block to a drawing.

The ModelSpace, PaperSpace, and Blocks collections can be used interchangeably with any of the add-a-graphic methods, depending on which collection you’re adding the graphical object to.

Adding Graphical Objects

Every graphical object that you can add from the AutoCAD window has a method, which allows you to add the same object with a VBA coding statement. AutoCAD also provides methods that you can use to create instances of these objects. Most of these methods have the word Add prefixed to the object’s name—for example AddArc, AddCircle, and AddLine. In Exercise 5.3, you’ll create a macro that adds a line to the Model tab.

Exercise 5.3: Creating a Line

1.    Open a new project in the IDE, and choose Insert ^ Module. Module1’s Code window appears.

2.    In the Code window, enter the CreateLine macro shown in Listing 5.3.

3.    Return to the AutoCAD window and run the macro (choose Tools ^ Macro ^ Macros). A line will appear in the Model tab, as shown in Figure 5.10.

Line generated by the CreateLine macro shown in Listing 5.3

Figure 5.10 Line generated by the CreateLine macro shown in Listing 5.3

Listing 5.3: CreateLine Macro

Listing 5.3: CreateLine Macro

Analysis

•    Line 1 opens the CreateLine macro.

•    Line 2 declares the MyLineObject variable as being capable of referring to an AcadLine object.

•    Line 3 declares the StartPoint and EndPoint as arrays with three elements of type Double.

•    Lines 4 through 9 specify the x-, y-, and z-coordinates of the start and endpoints of the line in the World Coordinate System (WCS).

•    Line 10 starts off by using the AddLine method to add the line defined by passing the two arguments specifying its endpoints to the ModelSpace object. It then uses the Set statement to assign to the variable MyLineObject a reference to the line being created. “Assigning a reference” means that there is never more than one definition of the line, so if it gets updated, all variables that reference it will automatically change, too.

•    Line 11 ends the CreateLine macro.

Accessing Graphical Objects

Accessing a graphical object from the ModelSpace, PaperSpace, or Blocks collections is no different from retrieving an object from any collection. The Item method is used to return the object at the given position in the collection. For example, in the statement

tmp757e233_thumb

the Item method returns the first object from the ModelSpace collection and assigns its reference to the variable MyLineObject—provided of course, that MyLineObject has been declared as an object of the correct type.

Drawing object types are generally given the name of the object (such as Line, Circle, and Ellipse) with the prefix Acad. For example, Line 2 of the AnimatedLine macro in Listing 5.4 shows the variable MyLineObject being declared as type AcadLine. This macro takes the line created by the CreateLine macro shown in Listing 5.3 and animates it by moving it around in the Model Space in 10 incremental steps.

Listing 5.4: AnimatedLine Macro

Listing 5.4: AnimatedLine Macro

Analysis

•    Line 1 starts the AnimatedLine macro.

•    Line 2 declares the variable MyLineObject as an AcadLine type.

•    Lines 3 and 4 declare the variable arrays that will hold the 3D endpoints for the displacement vector that will be used by the Move method in Line 14.

•    Line 5 declares the Count variable to be used in the For loop (Line 7) and the Start variable that will be used by the While loop (Line 16).

•    Line 6 assigns a reference to the first item in the ModelSpace collection of the active drawing to the MyLineObject variable.

•    Line 7 starts the For loop that will be repeated 10 times.

•    Lines 8 through 13 assign values to the endpoints of the displacement vector to be used by the Move method (Line 14) based on the value of the loop counter Count. The displacement vector indicates the direction and distance that the line is to be moved.

•    Line 14 calls the Move method of the line object referred to by MyLineObject using the two displacement vectors as arguments.

•    Line 15 uses the Timer function to return a value representing the number of seconds elapsed since midnight and assigns it to the variable Start.

•    Line 16 uses a While loop to pause the macro for /-^second and slow down the animation. Because this number is very dependent on the speed of the hardware you’re running, you’ll probably need to adjust it to suit your PC.

•    Line 17 marks the end of the While loop that didn’t have any statements in its block because it was used simply as a timing device.

•    Line 18 uses the ZoomAll method so that all the lines are displayed on the Model tab even if they extended outside the drawing area.

•    Line 19 uses the Update method of the object referenced by MyLineObject to update it on the drawing screen.

• Line 20 ends the For statement block.

• Line 21 ends the AnimatedLine macro.

Accessing Generic Graphical Objects

All the drawing objects can be defined by a generic AcadEntity object, which exposes AcDbEntity functionality. All the ObjectName property settings start with AcDb followed by the class of object, such as Line, Point, or Polyline. If you are not sure what type of object will be returned from a collection, you can declare the referencing variable as an AcadEntity type and avoid any problems.

For example, the following AddAnObject macro declares the NewObject variable as being capable of referencing an AcadEntity object. After the declarations, the NewObject is set to the Line Object that’s added to the ModelSpace collection. In the statement before it closes, the NameOfObject variable is assigned the value of the ObjectName property, which is AcDbLine.

tmp757e235_thumb

Next post:

Previous post: