Setting the Drawing Units (Templates and Reusability) (AutoCAD VBA)

AutoCAD provides several different units of measurement for use in your drawings. These units range from microinches to miles in imperial units, and from millimeters to kilometers in metric. Let’s start by taking a look at the list of unit formats available.

1. From the AutoCAD window choose Format ^ Units. The Drawing Units dialog box appears (Figure 8.1).

Drawing Units dialog box

Figure 8.1 Drawing Units dialog box

2.    In the Length section of the dialog box, click the down-arrow button to open the Type drop-down list. Choose Architectural.

3.    Open the Precision drop-down list and choose 0"-0 1/16”. Click OK. In the status bar, the units displayed on the left are now displayed in feet and inches. As you move the cursor, the coordinates are updated in increments of 1/16" according to the selected Precision setting.

Following are before-and-after pictures of the status bar. The coordinates in the status bar here are in inches as a decimal fraction:


tmp89fa-109_thumb

And here is the status bar showing the same point after the unit and precision settings were changed:

tmp89fa-110_thumb

Both the Engineering and Architectural unit formats are in feet and inches and assume that each unit represents an inch. The other formats can be in any unit of measurement.

Setting Drawing Units from VBA Code

Setting the drawing units from a macro involves setting the UnitsFormat property of one of the Dimension objects to the unit required. The formats available can be represented by an AutoCAD constant such as acDimLDecimal (the default), acDimLEngineering, or acDimLArchitectural. Figure 8.2 shows the complete list of constants available.

AutoCAD constants representing the Length types available in the Drawing Units dialog box

Figure 8.2 AutoCAD constants representing the Length types available in the Drawing Units dialog box

The UnitsFormat property setting of a linear Dimension object, such as the aligned Dimension (DimAligned) object, determines the unit format that is used for any dimension text displayed in your drawing.

Let’s create a set of macros (Listing 8.1) that allow you to input a line, set the unit required, position the dimension text, and display it aligned with the drawing object. The SetUnitsFormat macro uses the DimensionObject variable that was set up as a reference to the DimAligned Dimension object, which was added to the ModelSpace collection in the DrawAlignedDimension macro. The SetUnitsFormat procedure ensures the Architectural format is used. The Update method called at the end of the procedure causes any change to be immediately reflected on the screen.

Listing 8.1 shows the five macros from the set. These can all be placed in ThisDraw-ing module’s Code window.

Listing 8.1: Five Macros for Automating Dimensions

Listing 8.1: Five Macros for Automating Dimensions

 

 

Listing 8.1: Five Macros for Automating Dimensions

Analysis

•    Line 1 declares the global variable TextPosition that will be set to the coordinates of the halfway mark of the line in the SetTextPosition macro. The TextPosition variable is then used by the DrawAlignedDimension macro to position the dimension text.

•   Line 2 declares the DimensionObject as a variable capable of referencing an AutoCAD Dimension object.

•   Line 3 declares the StartPoint and EndPoint variables as the Variant type, so that the three-element arrays containing the 3D points defining the line can be assigned to these variables from the GetPoint method in a single statement.

•   Line 5 starts the SetUnitsFormat macro, which sets the format for the units to Architectural. Figure 8.3 shows the dimension text using this format.

Drawing aligned dimensions using architectural units

Figure 8.3 Drawing aligned dimensions using architectural units

•    Line 7 assigns the value of the AutoCAD constant acDimLArchitectural to the UnitsFormat property of the Dimension object.

•    Line 8 calls the Update method of the Dimension object to ensure that its dimension text is displayed in the Architectural format.

•    Line 9 ends the SetUnitsFormat macro.

•    Line 11 starts the SetTextPosition macro that calculates the halfway points for each coordinate and assigns them to the TextPosition array.

•    Lines 13 through 15 assign to each coordinate the point halfway along the line being dimensioned. This code deducts 2 from the x-coordinate, to display the dimension text just to the left of the extension line.

•    Line 16 finishes the SetTextPosition macro.

•    Line 18 starts the DrawAlignedDimension macro,which creates a Dimension object and draws the dimension text in the drawing.

•    Line 21 calls the AddDimAligned method to create a Dimension object containing the definition of an aligned dimension that’s drawn parallel to the original line. The extension line is defined by the coordinates of its start and endpoints, which are passed to the AddDimAligned method as arguments.The dimension text is displayed at the TextPosition passed as the third argument. The DimensionObject variable is set up to reference this new Dimension object.

•    Line 22 updates the screen so that the new dimension will appear.

•    Line 23 ends the DrawAlignedDimension macro.

•    Line 25 starts the DrawALine macro that calls the others to provide the complete service.

•    Line 27 declares the LineObject variable as being capable of referring to the AcadLine object.

•    Line 28 declares the StartVariant and EndVariant variables of variant type so they can be passed the value returned by the GetPoint method.

•    Line 29 starts the With statement block.

•    Lines 30 and 31 call the GetPoint method of the Utility object to prompt the user to click on the start and endpoints defining a line.

•    Line 32 ends the With statement.

•    Lines 33 through 38 assign the coordinates of the points selected by the user from the StartVariant and EndVariant to the StartPoint and EndPoint arrays.

•    Line 39 creates a Line object based on the points just entered and assigns a reference to the object to the LineObject variable.

•    Line 40 calls the Update method of the Line object to ensure that it is displayed on the screen.

•    Line 41 ends the DrawALine macro.

•    Line 43 starts the CreateDimensionedLine macro that calls the other macros, to create a line and draw it with Architectural format dimensions.

•    Line 45 calls the DrawALine macro that interacts with the user to create and display a line.

•    Line 46 calls the SetTextPosition macro to calculate the position for the dimension text.

•    Line 47 calls the DrawAlignedDimension macro to draw the dimension text.

•    Line 48 calls the SetUnitsFormat macro to ensure that the Architectural format is used.

•    Line 49 ends the CreateDimensionedLine macro.

Try running your macros from the AutoCAD window. Choose the DrawALine macro from the Macros dialog box and click Run.

In addition to DimAligned used here, there are other linear Dimension objects available: DimDiametric, DimOrdinate, DimRadial, and DimRotated. These objects can be added to the ModelSpace collection in the normal way, by their Add… methods. There are also two angular Dimension objects: Dim3PointAngular and DimAngular.

Next post:

Previous post: