Using the Text Style Object

A text style determines the appearance of any text you add to a drawing. TextStyle objects represent the settings for different styles and are held in the TextStyles collection. When you first create a drawing, this collection contains only one TextStyle object, named Standard. You can read the settings for this object through code using the GetFont method and the Height, Width, ObliqueAngle, and TextGenerationFlag properties outlined in the following sections. Alternatively, you can view the settings in the Text Style dialog box, which you access through the Format > Text Style menu of the AutoCAD interface.

Figure 11-9 shows the Text Style dialog box.

Figure 11-9. Viewing settings in the Text Style dialog box

Creating TextStyle Objects

By creating new TextStyle objects for different types of text or information, you can have at your fingertips all the text styles that you require for a drawing. To create a new TextStyle object, use the Add method as follows:

Set TextStyleObject = TextStylesCollection.Add(TextStyleName)

Table 11-14 shows the Add method's parameter.

Table 11-14. The Add Method's Parameter

Name

Data Type

Description

TextStyleName

String

An identifying name for the new TextStyle object

If you try to add a TextStyle with the name of an existing TextStyle object, this method will return a reference to the existing object. The newly created object will have the same settings as the active TextStyle object.

Retrieving and Setting Text Styles

Once you've created a new TextStyle object, you'll need to set its font settings. You may also want to modify or check the settings of an existing TextStyle object. You can do this through the SetFont and GetFont methods. In addition, you can use the font settings held within an .shx file through the FontFile and BigFontFile properties.

The SetFont Method

You use the SetFont method to define the font for a TextStyle object. It sets the typeface, sets whether text will be bold and/or italicized, and sets the character set and pitch and family definitions.

TextStyleObject.SetFont TypeFace, Bold, Italic, CharacterSet, PitchAndFamily

■ Note Many TrueType fonts are licensed and aren't provided for public use without explicit permission. Be sure to comply with any licensing terms if you set a custom font and package the font with your drawing files to send to customers.

Table 11-15 shows the SetFont method's parameters.

Table 11-15. The SetFont Method's Parameters

Name_Data Type Description_

TypeFace String The name of a font, for example, Times New Roman. If the

TextStyle is determined by an .shx file, this parameter holds an empty string.

Bold Boolean Determines if the text will be bold. If this value is set to True, the text will be bold. Otherwise, the text style isn't bold.

Italic Boolean Determines if the text will be italicized. If this value is set to

True, the text will be italic. Otherwise, the text style isn't italic.

Continued

250 CHAPTER 11 ■ DIMENSIONS AND ANNOTATIONS Table 11-15. (Continued)

Name Data Type Description

CharacterSet

Long

Specifies the character set for the font. Use the following con-

stants in your application in the declaration section of your

code:

Public Const ANSI CHARSET = 0

Public Const DEFAULT CHARSET = 1

Public Const SYMBOL CHARSET = 2

Public Const SHIFTJIS CHARSET = 128

Public Const OEM_CHARSET = 255

PitchAndFamily

Long

Specifies the pitch and family values for the font. The value is

a combination of three different settings for pitch, family, and

optionally the TrueType flag (required only when you're speci

fying a TrueType font). Use the following constants in your

application in the declaration section of your code:

' Pitch Values

Public Const DEFAULT PITCH = 0

Public Const FIXED PITCH = 1

Public Const VARIABLE_PITCH = 2

' Family Values

Public Const FF DONTCARE = 0

Public Const FF ROMAN = 16

Public Const FF SWISS = 32

Public Const FF MODERN = 48

Public Const FF SCRIPT = 64

Public Const FF_DECORATIVE = 80

' TrueType Flag

Public Const TMPF_TRUETYPE = &H4

This example creates a new TextStyle object and sets its font style to bold Greek symbols:

Public Sub AddTextStyle

Dim objTextStyle As AcadTextStyle

Set objTextStyle = ThisDrawing.TextStyles.Add("Bold Greek Symbols") objTextStyle.SetFont "Symbol", True, False, 0, 0 End Sub

The GetFont Method

The GetFont method works in much the same way as the SetFont method, except it retrieves the font settings.

TextStyleObject.GetFont TypeFace, Bold, Italic, CharacterSet, PitchAndFamily Table 11-16 shows the GetFont method's parameters.

Table 11-16. The GetFont Method's Parameters

Name

Data Type

Description

TypeFace

String

The name of a font, for example, Times New Roman. If the TextStyle is determined by a .shx file, this parameter holds an empty string.

Bold

Boolean

Determines if the text will be bold. If this value is set to True, the text will be bold. Otherwise, the text style isn't bold.

Italic

Boolean

Determines if the text will be italicized. If this value is set to True, the text will be italic. Otherwise the text style isn't italic.

CharacterSet

Long

See Table 11-15 for a description of this parameter.

PitchAndFamily

Long

See Table 11-15 for a description of this parameter.

The following example asks users for the name of a text style and then presents them with the font settings:

Public Sub GetTextSettings() Dim objTextStyle As AcadTextStyle Dim strTextStyleName As String Dim strTextStyles As String Dim strTypeFace As String Dim blnBold As Boolean Dim blnItalic As Boolean Dim lngCharacterSet As Long Dim lngPitchandFamily As Long Dim strText As String

' Get the name of each text style in the drawing For Each objTextStyle In ThisDrawing.TextStyles strTextStyles = strTextStyles & vbCr & objTextStyle.Name

Next

' Ask the user to select the Text Style to look at strTextStyleName = InputBox("Please enter the name of the TextStyle " & _ "whose setting you would like to see" & vbCr & _ strTextStyles,"TextStyles", ThisDrawing.ActiveTextStyle.Name) ' Exit the program if the user input was cancelled or empty If strTextStyleName = "" Then Exit Sub

On Error Resume Next

Set objTextStyle = ThisDrawing.TextStyles(strTextStyleName) ' Check for existence the text style If objTextStyle Is Nothing Then

MsgBox "This text style does not exist" Exit Sub End If

' Get the Font properties objTextStyle.GetFont strTypeFace, blnBold, blnItalic, lngCharacterSet, _

lngPitchandFamily ' Check for Type face If strTypeFace = "" Then ' No True type

MsgBox "Text Style: " & objTextStyle.Name & vbCr & _ "Using file font: " & objTextStyle.fontFile, _ vbInformation, "Text Style: " & objTextStyle.Name

Else

' True Type font info strText = "The text style: " & strTextStyleName & " has " & vbCrLf & _

"a " & strTypeFace & " type face" If blnBold Then strText = strText & vbCrLf & " and is bold" If blnItalic Then strText = strText & vbCrLf & " and is italicized" MsgBox strText & vbCr & "Using file font: " & objTextStyle.fontFile, _ vbInformation, "Text Style: " & objTextStyle.Name

End If End Sub

The FontFile Property

You use the FontFile property to set the TextStyle object to adopt the font style held in an .shx file, or vice versa.

TextStyleObject.FontFile = FontFileName

Table 11-17 shows the FontFile property's parameter.

Table 11-17. The FontFile Property's Parameter

Name

Data Type

Description

FontFileName

String

The path and file name of the required font file

The following code sample sets the font file for a new TextStyle object named Roman to be the romand.shx file:

Public Sub SetFontFile

Dim objTextStyle As AcadTextStyle

Set objTextStyle = ThisDrawing.TextStyles.Add("Roman") objTextStyle.fontFile = "romand.shx" End Sub

To set or retrieve the font settings of an Asian-language, big-font file, use the BigFontFile property.

Other TextStyle Properties

You can use some other properties of a TextStyle object to set the way text appears: Height, Width, ObliqueAngle, and TextGenerationFlag.

Figure 11-10 shows the effects of setting the Height and Width properties. The Height property simply holds the height of uppercase text in the current drawing units. The Width property, on the other hand, sets the character spacing and may lie in the range 0 < Width < 100. A value of more than 1.0 expands the text; a value of less than 1.0 condenses it.

Figure 11-10. Some Height variations

You can use the ObliqueAngle property to set the angle of slant of the text away from its vertical axis. You may set it to any value between -858 and 858, with a negative value indicating a slope to the left. Figure 11-11 shows the result of setting this property.

Figure 11-11. Some ObliqueAngle variations

Finally, the TextGenerationFlag property determines whether the text is written back to front and/or upside down. This property holds one or a combination of the acTextGeneration constants given in Table 11-18.

Table 11-18. TextGeneration's Constants

Constant

Value

Description

acTextFlagBackward

2

The letters of the text appear in reverse order.

acTextFlagUpsideDown

4

The text appears upside down.

Figure 11-12 shows an example of each of the possible settings for the TextGenerationFlag property.

TextGenerationFlag = acText 2bl6W)b6a36.niX9T36 = £6.nnoii6i9n9D±x9T IGXfCGUGigfioutiaS = acicxftJsSnbeiqcDOMU uMoaapTsdnSeidixajpe+pjerupegpcajpe = Se^juoT^eiauBOprai

Figure 11-12. Some TextGenerationFlag variations

+2 0

Post a comment