AutoCAD VBA - Adding new dimension style

Published on
12,793 Points
Last Modified:
The following article will describe how to add/edit a dimension style through AutoCAD VBA.

AutoCAD VBA has its quirks and when it comes to dimensions and controlling how they look through VBA.  This is where AutoCAD can be vividly confusing. The real trick is to understand a simple concept about dimension styles in AutoCAD. To use some home construction terms, they are not built “from the ground up”, they are "remodeled". That is to say, you have to start with everything already defined in a dimension style and then you change it to suit your needs. With that in mind, here is a quick snippet of how to create a new dimension style.

You first add a new dimension style to the drawing. It has all of its variables set by the current way the drawing is defined. You then alter the variables how you want them to be in your new dimension style and then "copy" them into your new dimension style. It is a simple concept but confusing if you are not aware of the quirk of "copying" these to your new dimension style. Keep in mind that the variables defined in the code are only a portion of the total number of dimension variables that exist. You will need to add or subtract the variables you wish to control in your dimension style.

This same concept is also how you alter the settings of an existing dimension style. The commented lines of code show how to alter an existing dimension style. Editing an existing dimension style requires the creation of a new dimension style first and then copying those variables into the original style. The steps are as follows; you create a new dimension style, alter the variables, and then restore the original dimension style and "copy" the variables back into the original.
Public Sub UgDimStyle_CreateNew( _
Optional ByVal sDimStyName As String = "Standard_Dim")
Dim CurDimStyle As AcadDimStyle
Dim NewDimstyle As AcadDimStyle
Dim iAltUnits As Integer
Dim dDimScale As Double
'Save copy of current dimstyle
Set CurDimStyle = ThisDrawing.ActiveDimStyle

'Create new dimstyle
Set NewDimstyle = ThisDrawing.DimStyles.Add(sDimStyName)

'Set newly created dimstyle current
ThisDrawing.ActiveDimStyle = NewDimstyle

'Save the target "dimvar" values
dDimScale = ThisDrawing.GetVariable("Dimscale")
iAltUnits = ThisDrawing.GetVariable("Dimalt")
'Alter the target "dimvar" values
ThisDrawing.SetVariable "DIMSCALE", 1# 'will control size of dim text
ThisDrawing.SetVariable "DIMASZ", 2.5 'arrowhead size
ThisDrawing.SetVariable "DIMATFIT", 2 'arrow-text arrangement
ThisDrawing.SetVariable "DIMAZIN", 3 '0 suppression before/after angular
ThisDrawing.SetVariable "DIMBLK", "" 'special arrow blk
ThisDrawing.SetVariable "DIMDLE", 0 'dim line extension past extension
ThisDrawing.SetVariable "DIMDLI", 10 'dist between baseline dims
ThisDrawing.SetVariable "DIMDSEP", "." 'decimal separator
ThisDrawing.SetVariable "DIMEXE", 1 'dim line extension past extension
ThisDrawing.SetVariable "DIMEXO", 1 'dim offset from origin
'ThisDrawing.SetVariable "DIMFIT", 5 'control fit if not enough space
ThisDrawing.SetVariable "DIMGAP", 2 'gap around text
ThisDrawing.SetVariable "DIMJUST", 0 'text placement - above centered
ThisDrawing.SetVariable "DIMLFAC", 1# 'length scaling
ThisDrawing.SetVariable "DIMTAD", 1 'text to dim placement - above
ThisDrawing.SetVariable "DIMTIH", 0 'aligned with dim
ThisDrawing.SetVariable "DIMTIX", 0 'force inside
ThisDrawing.SetVariable "DIMTMOVE", 0 'dim moves with text
ThisDrawing.SetVariable "DIMTSZ", 0 'draw arrowheads
ThisDrawing.SetVariable "DIMTXT", 3.5 'text height
ThisDrawing.SetVariable "DIMTZIN", 12 '0 suppression before/after tol
ThisDrawing.SetVariable "DIMUNIT", 2 'unit format - decimal
ThisDrawing.SetVariable "DIMZIN", 12 '0 suppression before/after

'Copy new document dimvar settings into new dimstyle
NewDimstyle.CopyFrom ThisDrawing

'Set original dimstyle current
'ThisDrawing.ActiveDimStyle = CurDimStyle

'Restore the altered "dimvar" values
''ThisDrawing.SetVariable "Dimscale", dDimScale
''ThisDrawing.SetVariable "Dimalt", iAltUnits

'Copy restored document dimvar settings into original dimstyle
''CurDimStyle.CopyFrom ThisDrawing

Set CurDimStyle = Nothing
Set NewDimstyle = Nothing
End Sub

Open in new window

1 Comment
LVL 10

Author Comment

This comes up from time to time in other forums I participate in.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
See the Basics of Office 365's Note Taking app, OneNote
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month