DimStyles and TextStyles

Dimstyles are a collection. Each dimstyle object is itself a collection of 78 variables (at least) that are not visible in VBA. They can be set through the setvariable method of the activedocument, but I dont think there is any way to survey for them except through the autocad interface. Typing -Dimstyle at the command line (with a dash) brings up the command line version with a Variables option. Type V, then type STANDARD to get a nice screen list of every variable and its value in the STANDARD style. I cut and pasted this to a text editor capable of column mode, then eventually pasted it into a visual basic module for the purpose of using it to make a new dimension style. It looked better in the text editor. this is HTML which eliminates extra spaces. So you get a screenshot. This is Autocad 2018.

The STANDARD dimstyle is the default. Creating a new dimstyle in VBA with AcadDoc.DimStyles.Add(“my_new_name”) creates a copy of the intrinsic standard, the same settings STANDARD is made from. The settings desired are then made by changing the current variables in the activedrawing, then using Dimstyle.CopyFrom method to load all current dim variables into the Dimstyle object. So that means we dont have to rewrite all these values. We create a new clean style, its our only option, we run a list of AcadDoc.Setvariable “DIMXXX” changes we want to use, and then we run the CopyFrom method.

I looked at some Dimstyles I use and came up with a list to vary the standard to an Inch style i use, then having worked out those variables, i saw that my feet style only changed 3 of the Inch variables. So these can be run incrementally. If i want Feet, i run Inches then Feet over the top of it. Similarly Decimal only changes 2 variables from Feet.

The dimension textstyle and dimscale used vary as needed. We want to be able to change them at will.

here are the “scripts” to change variables, with textstyle and dimscale removed. Everything not shown will be the same as the Standard. The details are subject to change.

 Sub dim_inch()
 '  acadDoc.setvariable "DIMSCALE", 1#                    '  Overall scale factor"
 '  acadDoc.setvariable "DIMTXSTY", "Standard"            '  Text style"
  acadDoc.setvariable "DIMLUNIT", 5                      '  Linear unit format"
  acadDoc.setvariable "DIMFRAC", 1                       '  Fraction format"
 
  acadDoc.setvariable "DIMTXT", 0.125                    '  Text height"
  acadDoc.setvariable "DIMASZ", 0.09375                  '  Arrow size"
  
  acadDoc.setvariable "DIMCLRT", 7                       '  Dimension text color

  acadDoc.setvariable "DIMTAD", 1                        '  Place text above the dimension line"
  acadDoc.setvariable "DIMTOH", 0                        '  Text outside horizontal"
  acadDoc.setvariable "DIMTIH", 0                        '  Text inside extensions is horizontal"
  acadDoc.setvariable "DIMTOFL", 1                       '  Force line inside extension lines"
  acadDoc.setvariable "DIMTIX", 1                        '  Place text inside extensions"
 
  acadDoc.setvariable "DIMTMOVE", 2                      '  Text movement - dont move the line"
  acadDoc.setvariable "DIMEXE", 0.0625                   '  Extension above dimension line"
 End Sub
 
 
 Sub dim_feet()
  acadDoc.setvariable "DIMLUNIT", 4                      '  Linear unit format"
  acadDoc.setvariable "DIMFRAC", 2                       '  Fraction format"
  acadDoc.setvariable "DIMZIN", 3                       '  Zero suppression"
 End Sub
 
 
 Sub dim_decimal()
  acadDoc.setvariable "DIMLUNIT", 2                      '  Linear unit format"
  acadDoc.setvariable "DIMADEC", 1                      '  Angular decimal places
 End Sub

For this approach, i want the dimstyle to use whatever textstyle is current. I want to tell it the dimscale and the textstyle. Textstyles are made very similar to Dimstyles. They are a collection, a new name is added that has default settings, the settings are changed. They dont have many settings. When programming you mostly need to keep the user supplied name mentally separate from the official system font name. usually they are the same or similar, but the font system name has to be given exactly. its helpful to look at the GetFont method first to see what the values are that autocad returns for the settings of textstyles set up through the interface Styles dialog.

Sub getfont()
Connect_Acad
Dim styles As AcadTextStyles
Dim style As AcadTextStyle
Set styles = acadDoc.TextStyles

Dim strtypeface As String
Dim bold As Boolean, italic As Boolean
Dim lngchar As Long, lngpitch As Long

For Each style In styles
    style.getfont strtypeface, bold, italic, lngchar, lngpitch

    Debug.Print style.Name    'user supplied name in the list box of the style dialog
    Debug.Print style.fontFile  'actual file name not shown in style dialog
    Debug.Print strtypeface     'font name dropdown box in style dialog
    Debug.Print bold
    Debug.Print italic
    Debug.Print lngchar
    Debug.Print lngpitch
    Debug.Print
Next
End Sub

Once that is clear, you can send the same values you see in the Debug (Immediate) window. TextStyles.Add(“usernamehere”) will add any name you choose, but the typeface name must be as you see in the Autocad Style Dialog Font Name pulldown box.

Sub new_textstyle(str_stylename As String, str_typeface As String)

    Dim bold As Boolean, italic As Boolean
    Dim lngchar As Long, lngpitch As Long
    lngchar = 0
    lngpitch = 34 'i am sure this is not meaningless but this is typ(swiss 32 variable 2)
    bold = False
    italic = False
     
    Dim TextStyles As AcadTextStyles
    Dim curStyle As AcadTextStyle
    Dim newStyle As AcadTextStyle

    Set curStyle = acadDoc.ActiveTextStyle
    Set TextStyles = acadDoc.TextStyles
    
    Set newStyle = TextStyles.Add(str_stylename)
    acadDoc.ActiveTextStyle = newStyle
 
 'new style is added with no font information
 'autocad assigns defaults similar or same as standard
 
    newStyle.SetFont str_typeface, bold, italic, lngchar, lngpitch
    
    'sometimes i get a transient new style at this point
    'eliminated by the next new style unless i create actual text
End Sub

now we need the dimstyle creation that accepts parameters for the type (Inch, Feet, Decimal) and scale, using the current textstyle.
I am going to name the dimstyle according to the parameters used to make it.

Sub new_dimstyle(strname As String, strtype As String, dm As Integer)
    
    Dim style As AcadDimStyle
    Dim strdimstyle As String
    Dim strtextstyle As String
        strtextstyle = acadDoc.GetVariable("textstyle")
    
    strdimstyle = strname & "_" & strtype & "_" & dm
        
     Set style = acadDoc.DimStyles.Add(strdimstyle)
     acadDoc.ActiveDimStyle = style
     
      Select Case strtype
        Case "Inch"
        Call dim_inch
        
        Case "Feet"
        Call dim_inch
        Call dim_feet
        
        Case "Decimal"
        Call dim_inch
        Call dim_feet
        Call dim_decimal
      End Select

     acadDoc.setvariable "DIMTXSTY", strtextstyle
     acadDoc.setvariable "DIMSCALE", dm
    
    style.CopyFrom acadDoc  'the basic method for changing style contents

End Sub

and finally we can call it various ways.

Sub test_dim()
Call Connect_Acad

Call new_textstyle("Arial Narrow", "Arial Narrow")
Call new_dimstyle("ArialN", "Inch", 24)

Call new_textstyle("Technic", "Technic")
Call new_dimstyle("Technic", "Feet", 24)

Call new_textstyle("Courier", "Courier New")
Call new_dimstyle("Courier", "Decimal", 24)

End Sub

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s