Preferences Object (The AutoCAD Object Model) (AutoCAD VBA)

The Preferences object (Figure 5.11), contains all the settings from the pages of the AutoCAD Options dialog box (Figure 5.12).

The Preferences object contains nine properties that return objects, each representing the settings for the nine pages (tabs) in the Options dialog box. I’ve ordered the objects in Figure 5.11 to match the order of the pages shown in Figure 5.12. These objects allow you to control the way AutoCAD is customized from your code.

The following steps show you how to set the Source Content Units and Target Drawing Units text boxes to centimeters from the AutoCAD window, and then how to set them back to inches with VBA code. You’ll see the strong similarities between both operations. To change inches to centimeters from the AutoCAD window, follow these steps:

1. From the AutoCAD window, choose Tools ^ Options. The Options dialog box appears.

The Preferences object's structure

Figure 5.11 The Preferences object’s structure

AutoCAD's Options dialog box


Figure 5.12 AutoCAD’s Options dialog box

2.    Click the User Preferences tab to open it (Figure 5.13). Choose Centimeters from the drop-down lists for both Source Content Units and Target Drawing Units.

3.    Click OK to return to the AutoCAD window.

 The User Preferences page from the Options dialog box, and the VBA properties that correspond to its items

Figure 5.13 The User Preferences page from the Options dialog box, and the VBA properties that correspond to its items

Now I’m going to show you how to change the centimeters setting back to inches using VBA code, to demonstrate your interaction with the AutoCAD window as compared with creating code statements to execute your actions:

1.    Open the IDE and double-click ThisDrawing from the Project Explorer window. (Choose View ^ Project Explorer if this window is not already open.) The Code window for ThisDrawing opens.

2.    Type the following macro declaration, and press Enter:

tmp757e239_thumb

When you press Enter, parentheses are automatically appended to the macro’s name, a blinking cursor appears at the start of the next line, and the End Sub statement is added as a third line.

3. Type

tmp757e240_thumb

A drop-down list of properties and methods should appear as you enter each period character (. ). If this doesn’t happen, you’ll need to go to Tools ^ Options, open the Editor tab, and enable the Auto Quick Info option.

4. After you enter the last period character (after user), the drop-down list of properties shown in Figure 5.14 appears.

Drop-down list of properties associated with User Preferences

Figure 5.14 Drop-down list of properties associated with User Preferences

Notice how closely the names of these properties match the settings available in the User Preferences page shown earlier in Figure 5.13. I’ve labeled its boxes and frames with these property names to demonstrate the association between what you’ve been doing in the AutoCAD window versus how it’s done in VBA code.

5. Select the ADCInsertUnitsDefaultSource property from the drop-down list and type an equals sign (=).

The ADCInsertUnitsDefaultSource property is entered into the Code window, followed by the equals sign. You’ll also get the drop-down list containing all the constants representing unit names, as shown here:

tmp757e242_thumb

The ADC in ADCInsertUnitsDefaultSource stands for AutoCAD Design Center, which is the caption of the frame that contains the text boxes displaying the settings for the ADCInsertUnitsDefaultSouce and ADCInsertUnitsDefaultTarget properties.

6.    Select the constant acInsertUnitsInches from the drop-down list and press Enter. The constant appears after the equals sign, and the blinking cursor is positioned at the start of the next line.

7.    Repeat steps 3 through 6 again, but this time select ADCInsertUnitsDefault-Target at step 5.

8.    Choose File ^ Close And Return To AutoCAD, and then Tools ^ Macro ^ Macros. The Macros dialog box appears.

9.    Make sure that the Global1!ThisDrawing.SetDrawingUnits macro appears in the Macro Name box, and click Run.

10.    Choose Tools ^ Options and select the User Preferences page. “Inches” should once again appear in the two boxes in the AutoCAD Design Center frame.

At step 5, you may have noticed three properties listed that all begin with SCM, and that haven’t been identified in Figure 5.13. These are Short Cut Menu properties, which represent the settings for the menu that pops up when you right-click the drawing object in AutoCAD. Figure 5.15 shows the Right-Click Customization dialog box that opens when you click the Right-Click Customization button on the User Preferences page.

Shortcut menu properties used in the Right-Click Customization dialog box

Figure 5.15 Shortcut menu properties used in the Right-Click Customization dialog box

Next post:

Previous post: