Creating Dimensions

Dimension objects are entities that provide information about distances and angles within an AutoCAD drawing. It makes sense then that, just as for the drawing objects covered in Chapters 7 and 8, the AddDimXXX methods used to add dimensions to drawings are exposed by the ModelSpace, PaperSpace, and Block objects.

It's worth noting that not all properties of dimension entities are exposed through the ActiveX object model. For example, the dimension line control points of DimAligned objects are available only as DXF 13 and 14 codes. In addition, some dimension objects behave differently between AutoCAD 2002 and 2004 (and later versions), and I will emphasize that developers should research the changes in newer versions, especially with respect to dimensions and dimension styles.

The Dim3PointAngular Object

The Dim3PointAngular object displays the angular distance between three points. You can use the AddDim3PointAngular method to add this type of dimension to a drawing.

Set Dim3PointAngularObject = _

Object.AddDim3PointAngular(VertexPoint, Point1, Point2, _ TextPosition)

Table 11-5 shows the AddDim3PointAngular method's parameters.

Table 11-5. The AddDim3PointAngular Method's Parameters

Name

Data Type

Description

VertexPoint

Variant

A three-element array of doubles specifying the WCS position of the vertex whose angle is to be measured

Point1

Variant

A three-element array of doubles specifying the WCS position of one of the end points

Point2

Variant

A three-element array of doubles specifying the WCS position of the other end point

TextPosition

Variant

A three-element array of doubles specifying the WCS position of the text displaying the angle size

Figure 11-1 depicts a three-point angular-style dimension.

Figure 11-1 depicts a three-point angular-style dimension.

Figure 11-1. A three-point angular dimension

This code illustrates how to implement the AddDim3PointAngular method:

Public Sub Add3PointAngularDimension()

Dim varAngularVertex As Variant

Dim varFirstPoint As Variant

Dim varSecondPoint As Variant

Dim varTextLocation As Variant

Dim objDim3PointAngular As AcadDim3PointAngular

'Define the dimension varAngularVertex = ThisDrawing.Utility.GetPoint(, _

"Enter the center point: ") varFirstPoint = ThisDrawing.Utility.GetPoint(varAngularVertex, _

"Select first point: ") varSecondPoint = ThisDrawing.Utility.GetPoint(varAngularVertex, _

"Select second point: ") varTextLocation = ThisDrawing.Utility.GetPoint(varAngularVertex, _ "Pick dimension text location: ")

Set objDim3PointAngular = ThisDrawing.ModelSpace.AddDim3PointAngular( _

varAngularVertex, varFirstPoint, varSecondPoint, varTextLocation) objDim3PointAngular.Update End Sub

The DimAligned Object

You use the DimAligned object to display the length of a line. Extension lines emanate at right angles from the ends of the line to be measured to the level of the dimension text. You use the AddDimAligned method to add this object to your drawing.

Set DimAlignedObject = Object.AddDimAligned(Point1, Point2, TextPosition) Table 11-6 shows the AddDimAligned method's parameters.

Table 11-6. The AddDimAligned Method's Parameters

Name

Data Type

Description

Point1

Variant

A three-element array of doubles specifying the WCS position of one end of the line to be measured

Point2

Variant

A three-element array of doubles specifying the WCS position of the other end of the line to be measured

TextPosition

Variant

A three-element array of doubles specifying the WCS position of the text to be displayed

Figure 11-2 depicts an aligned-style dimension.

Figure 11-2 depicts an aligned-style dimension.

Dimaligned

The following example asks users to select the points they would like to dimension. It then uses the AddDimAligned method to add the dimension. Finally, the units are changed to engineering units.

Public Sub AddAlignedDimension() Dim varFirstPoint As Variant Dim varSecondPoint As Variant Dim varTextLocation As Variant Dim objDimAligned As AcadDimAligned 'Define the dimension varFirstPoint = ThisDrawing.Utility.GetPoint(, "Select first point: ") varSecondPoint = ThisDrawing.Utility.GetPoint(varFirstPoint, _

"Select second point: ") varTextLocation = ThisDrawing.Utility.GetPoint(, _

"Pick dimension text location: ")

'Create an aligned dimension

Set objDimAligned = ThisDrawing.ModelSpace.AddDimAligned(varFirstPoint, _

varSecondPoint, varTextLocation)

objDimAligned.Update

MsgBox "Now we will change to Engineering units format"

objDimAligned.UnitsFormat = acDimLEngineering objDimAligned.Update End Sub

The DimAngular Object

The DimAngular object is much like the Dim3PointAngular object. It's designed to hold dimension text displaying the angle between two lines or spanned by an arc or circle. You use the AddDimAngular method to create a new DimAngular object.

Set DimAngularObject = Object.AddDimAngular(Vertex, Point1, Point2, TextPosition) Table 11-7 shows the AddDimAngular method's parameters.

Table 11-7. The AddDimAngular Method's Parameters

Name

Data Type

Description

Vertex

Variant

A three-element array of doubles specifying the WCS position of the vertex whose angle is to be measured

Point1

Variant

A three-element array of doubles specifying the WCS position of one of the end points

Point2

Variant

A three-element array of doubles specifying the WCS position of the other end point

TextPosition

Variant

A three-element array of doubles specifying the WCS position of the text displaying the angle size

Figure 11-3 depicts an angular dimension for an arc.

Figure 11-3 depicts an angular dimension for an arc.

The following code sample shows how to use the AddDimAngular method. Once you've added the new dimension, its units are changed to degrees, minutes, and seconds.

Public Sub AddAngularDimension() Dim varAngularVertex As Variant Dim varFirstPoint As Variant Dim varSecondPoint As Variant Dim varTextLocation As Variant Dim objDimAngular As AcadDimAngular

'Define the dimension varAngularVertex = ThisDrawing.Utility.GetPoint(, _

"Enter the center point: ") varFirstPoint = ThisDrawing.Utility.GetPoint(varAngularVertex, _

"Select first point: ") varSecondPoint = ThisDrawing.Utility.GetPoint(varAngularVertex, _

"Select second point: ") varTextLocation = ThisDrawing.Utility.GetPoint(varAngularVertex, _ "Pick dimension text location: ")

'Create an angular dimension

Set objDimAngular = ThisDrawing.ModelSpace.AddDimAngular( _

varAngularVertex, varFirstPoint, varSecondPoint, varTextLocation) objDimAngular.AngleFormat = acGrads objDimAngular.Update MsgBox "Angle measured in GRADS"

objDimAngular.AngleFormat = acDegreeMinuteSeconds objDimAngular.TextPrecision = acDimPrecisionFour objDimAngular.Update

MsgBox "Angle measured in Degrees Minutes Seconds" End Sub

The DimDiametric Object

The DimDiametric object represents a dimension showing the length of a chord across a circle or arc. The AddDimDiametric method creates a new diametric dimension.

DimDiametricObject = Object.AddDimDiametric _ (ChordPoint1, ChordPoint2, LeaderLength)

Table 11-8 shows the AddDimDiametric method's parameters.

Table 11-8. The AddDimDiametric Method's Parameters

Name

Data Type

Description

ChordPoint1

Variant

A three-element array of doubles specifying the WCS position of one end of the chord to be measured

ChordPoint2

Variant

A three-element array of doubles specifying the WCS position of the other end of the chord to be measured

LeaderLength

Double

The distance from ChordPoint1 to the dimension text

Figure 11-4 depicts a diametric-style dimension.

Figure 11-4 depicts a diametric-style dimension.

Diameter Dimensioning Autocad

The following code sample adds a new diametric dimension. The object snap mode setting is changed so that the users can select only the diameter of their chosen circle. After the dimension has been added, this setting returns to its original value.

Public Sub AddDiametricDimension()

Dim varFirstPoint As Variant

Dim varSecondPoint As Variant

Dim dblLeaderLength As Double

Dim objDimDiametric As AcadDimDiametric

Dim intOsmode As Integer

'get original object snap settings intOsmode = ThisDrawing.GetVariable("osmode") ThisDrawing.SetVariable "osmode", 512 ' Near

With ThisDrawing.Utility varFirstPoint = .GetPoint(, "Select first point on circle: ") ThisDrawing.SetVariable "osmode", 128 ' Per varSecondPoint = .GetPoint(varFirstPoint, _

"Select a point opposite the first: ") dblLeaderLength = .GetDistance(varFirstPoint, _

"Enter leader length from first point: ")

End With

Set objDimDiametric = ThisDrawing.ModelSpace.AddDimDiametric( _

varFirstPoint, varSecondPoint, dblLeaderLength) objDimDiametric.UnitsFormat = acDimLEngineering objDimDiametric.PrimaryUnitsPrecision = acDimPrecisionFive objDimDiametric.FractionFormat = acNotStacked objDimDiametric.Update

'reinstate original object snap settings ThisDrawing.SetVariable "osmode", intOsmode End Sub

The DimOrdinate Object

The DimOrdinate object represents dimensioning text displaying the absolute value of the X-coordinate or Y-coordinate of a given point. You use the AddDimOrdinate method to create a new ordinate dimension.

Set DimOrdinateObject = Object.AddDimOrdinate(DefinitionPoint, _ LeaderEndPoint, UseXAxis)

Table 11-9 shows the AddDimOrdinate method's parameters.

Table 11-9. The AddDimOrdinate Method's Parameters

Name

Data Type

Description

DefinitionPoint

Variant

A three-element array of doubles specifying, with respect to the WCS, the point whose projection onto the X-axis or Y-axis is to be measured.

LeaderEndPoint

Variant

A three-element array of doubles specifying the WCS position of the dimension text placed at the end of an arrow.

UseXAxis

Boolean

Determines whether the DefinitionPoint is measured from the X-axis or Y-axis. If this is set to True, the X-axis is used as a reference.

Figure 11-5 depicts an ordinate-style dimension.

Figure 11-5 depicts an ordinate-style dimension.

Figure 11-5. An ordinate dimension

The following example sets an ordinate dimension for either the X or Y direction:

Public Sub AddOrdinateDimension()

Dim varBasePoint As Variant

Dim varLeaderEndPoint As Variant

Dim blnUseXAxis As Boolean

Dim strKeywordList As String

Dim strAnswer As String

Dim objDimOrdinate As AcadDimOrdinate strKeywordList = "X Y" 'Define the dimension varBasePoint = ThisDrawing.Utility.GetPoint(, _

"Select ordinate dimension position: ") ThisDrawing.Utility.InitializeUserInput 1, strKeywordList strAnswer = ThisDrawing.Utility.GetKeyword("Along Which Axis? <X/Y>: ")

If strAnswer = "X" Then varLeaderEndPoint = ThisDrawing.Utility.GetPoint(varBasePoint, _

"Select X point for dimension text: ")

blnUseXAxis = True Else varLeaderEndPoint = ThisDrawing.Utility.GetPoint(varBasePoint, _

"Select Y point for dimension text: ")

blnUseXAxis = False End If

'Create an ordinate dimension

Set objDimOrdinate = ThisDrawing.ModelSpace.AddDimOrdinate( _

varBasePoint, varLeaderEndPoint, blnUseXAxis) objDimOrdinate.TextSuffix = "units" objDimOrdinate.Update End Sub

The DimRadial Object

A radial dimension displays the length of the radius of a circle or arc. You create it using the AddDimRadial method.

Set DimRadialObject = Object.AddDimRadial (CenterPoint, ChordPoint, LeaderLength) Table 11-10 shows the AddDimRadial method's parameters.

Table 11-10. The AddDimRadial Method's Parameters

Name

Data Type

Description

CenterPoint

Variant

A three-element array of doubles specifying the WCS coordinates of the center of an arc or circle

ChordPoint

Variant

A three-element array of doubles holding the WCS coordinates of a point on the edge of the circle or arc at which an arrow and the text will be placed

LeaderLength

Double

The distance from the text to the ChordPoint

Figure 11-6 depicts a radial-style dimension.

ChordPoint

yfo''LeaderLength

*ZenterPoint

Figure 11-6. A radial dimension

The following example illustrates the AddDimRadial method by adding a radial dimension to a circle or arc picked by the user:

Public Sub AddRadialDimension() Dim objUserPickedEntity As Object Dim varEntityPickedPoint As Variant Dim varEdgePoint As Variant Dim dblLeaderLength As Double Dim objDimRadial As AcadDimRadial Dim intOsmode As Integer intOsmode = ThisDrawing.GetVariable("osmode") ThisDrawing.SetVariable "osmode", 512 ' Near

'Define the dimension On Error Resume Next With ThisDrawing.Utility

.GetEntity objUserPickedEntity, varEntityPickedPoint, _

"Pick Arc or Circle:" If objUserPickedEntity Is Nothing Then MsgBox "You did not pick an entity" Exit Sub End If varEdgePoint = .GetPoint(objUserPickedEntity.Center, _

"Pick edge point") dblLeaderLength = .GetReal("Enter leader length from this point: ") End With

'Create the radial dimension

Set objDimRadial = ThisDrawing.ModelSpace.AddDimRadial( _

objUserPickedEntity.Center, varEdgePoint, dblLeaderLength) objDimRadial.ArrowheadType = acArrowArchTick objDimRadial.Update 'reinstate original setting ThisDrawing.SetVariable "osmode", intOsmode End Sub

The DimRotated Object

The DimRotated object measures the length of the projection of a line onto a direction specified by an angle from the X-axis.

Set DimRotatedObject = Object.AddDimRotated(Point1, Point2, _ DimLocationPoint, RotationAngle)

Table 11-11 shows the AddDimRotated method's parameters.

Table 11-11. The AddDimRotated Method's Parameters

Name

Data Type

Description

Point1

Variant

A three-element array of doubles specifying the WCS coordinates of one end of a line

Point2

Variant

A three-element array of doubles specifying the WCS coordinates of the other end of a line

DimLocationPoint

Variant

A three-element array of doubles specifying the WCS coordinates of a point lying on the line of the arrow and text

RotationAngle

Double

The angle of projection measured in radians from the X-axis

Figure 11-7 depicts a rotated-style dimension.

Figure 11-7 depicts a rotated-style dimension.

Figure 11-7. A rotated dimension

The following example illustrates how to add a rotated dimension at a position and angle chosen by the user:

Public Sub AddRotatedDimension() Dim varFirstPoint As Variant Dim varSecondPoint As Variant Dim varTextLocation As Variant Dim strRotationAngle As String Dim objDimRotated As AcadDimRotated

'Define the dimension With ThisDrawing.Utility varFirstPoint = .GetPoint(, "Select first point: ") varSecondPoint = .GetPoint(varFirstPoint, "Select second point: ") varTextLocation = .GetPoint(, "Pick dimension text location: ") strRotationAngle = .GetString(False, "Enter rotation angle in degrees") End With

'Create a rotated dimension

Set objDimRotated = ThisDrawing.ModelSpace.AddDimRotated(varFirstPoint, _ varSecondPoint, varTextLocation, _

ThisDrawing.Utility.AngleToReal(strRotationAngle, acDegrees)) objDimRotated.DecimalSeparator = "," objDimRotated.Update End Sub

Was this article helpful?

+6 -2

Responses

  • AILA
    How to create a dimension in vba?
    1 year ago

Post a comment