Other Objects of Interest

The following sections describe some other objects of interest, starting with the Hatch object.

The Hatch Object

The Hatch object represents an area filled with a pattern. The pattern may be associated with the area boundary (i.e., if the boundary changes, the pattern also changes appropriately), or the pattern may be independent of the area boundary. The read-only AssociativeHatch property determines whether the pattern is associated with the Hatch object. You set this property when you create the Hatch object, using the AddHatch method:

Set HatchObject = Object.AddHatch(PatternType, PatternName, Associativity)

Table 8-12 provides a brief description of each argument.

Table 8-12. AddHatch Method Parameters

Name

Data Type

Description

PatternType

Long

Specifies the type of pattern to use. It may take one of the three possible values given in Table 8-13.

PatternName

String

Specifies which hatch pattern to use from the .pat file.

Associativity

Boolean

Determines whether the hatch is associative. Set this parameter to True to create an associative hatch.

varEnd) varEnd)

Table 8-13 shows the three possible values for the PatternType parameter.

Table 8-13. PatternType Parameter Values

Constant

Value

Description

AcHatchPatternTypeUserDefined

0

Allows you to define a pattern of lines using the current linetype

AcHatchPatternTypePredefined

1

Uses a pattern name from those defined in the acad.pat file

AcHatchPatternTypeCustomDefined

2

Uses a pattern name from those defined in a .pat file other than the acad.pat file

After you create the Hatch object, you must specify the boundary or loop using the AppendOuterLoop method. You must close and add this loop before any inner loops. You create any inner loops you may need one at a time using the AppendInnerLoop method.

■Caution The documentation for the AddHatch method warns that you must call AppendOuterLoop immediately after you add the Hatch object; otherwise, AutoCAD will enter an unpredictable state.

The following code gets a center point and radius from the user, and then creates some entities using these inputs. The new entities are then used to specify the inner and outer boundaries for a new Hatch object. The resulting Hatch object is shown in Figure 8-10.

Public Sub TestAddHatch() Dim varCenter As Variant Dim dblRadius As Double Dim dblAngle As Double Dim objEnt As AcadHatch Dim varOuter() As AcadEntity Dim varInner() As AcadEntity

On Error Resume Next

'' get input from user With ThisDrawing.Utility varCenter = .GetPoint(, vbCr & "Pick the center point: ") dblRadius = .GetDistance(varCenter, vbCr & "Indicate the radius: ") dblAngle = .AngleToReal("180", acDegrees) End With

'' draw the entities With ThisDrawing.ModelSpace

'' draw the Outer loop (circle) ReDim varOuter(0)

Set varOuter(0) = .AddCircle(varCenter, dblRadius)

'' draw then Inner loop (semicircle) ReDim varInner(1)

Set varInner(0) = .AddArc(varCenter, dblRadius * 0.5, 0, dblAngle) Set varInner(1) = .AddLine(varInner(0).StartPoint, _

varInner(0).EndPoint)

'' create the Hatch object

Set objEnt = .AddHatch(acHatchPatternTypePreDefined, "ANSI31", True)

'' append boundaries to the hatch objEnt.AppendOuterLoop varOuter objEnt.AppendInnerLoop varInner

'' evaluate and display hatched boundaries objEnt.Evaluate objEnt.Update End With End Sub

Acad2006 Vba Pline
Figure 8-10. The Hatch object

As you can see, the AppendOuterLoop and AppendInnerLoop methods take as a parameter an array of objects forming a closed loop. The Evaluate method of the Hatch object evaluates the lines or fill for the hatch pattern.

■ Note In AutoCAD 2004, Hatch objects support a gradient fill display behavior. This in turn provides several new properties, such as GradientAngle, GradientCentered, GradientColor1, and GradientColor2. Consult the AutoCAD 2004 Customization Guide for more information about new Hatch object features.

The MText Object

The MText object represents a paragraph of alphanumeric characters that fits within a nonprinting bounding box. Each MText object is treated as a single object, regardless of the number of lines of text it contains. The bounding box remains an integral part of the entity, even though it isn't plotted or printed. MText objects use word wrap to break long lines into paragraphs. You can create an MText entity using the AddMText method:

Set MTextObject = Object.AddMText(InsertionPoint, Width, TextString)

Table 8-14 provides a brief description of each argument.

Table 8-14. AddMText Method Parameters

Name

Data Type

Description

InsertionPoint

Variant

A three-element array of doubles specifying the point at which the MText bounding box will be inserted with respect to the WCS

Width

Double

The width of the text block

TextString

String

The text displayed in the drawing space

Figure 8-11 illustrates the MText object. Width

/ This is the text / string for the / MText object

Figure 8-11. The MText object

The following example demonstrates the AddMText method:

Public Sub TestAddMText() Dim varStart As Variant Dim dblWidth As Double Dim strText As String Dim objEnt As AcadMText

On Error Resume Next

'' get input from user With ThisDrawing.Utility varStart = .GetPoint(, vbCr & "Pick the start point: ") dblWidth = .GetDistance(varStart, vbCr & "Indicate the width: ") strText = .GetString(True, vbCr & "Enter the text: ") End With

'' add font and size formatting strText = "\Fromand.shx;\H0.5;" & strText

'' create the mtext

Set objEnt = ThisDrawing.ModelSpace.AddMText(varStart, dblWidth, strText) objEnt.Update End Sub

Rich text format (RTF) control codes aren't recognized in an MText entity. Text from other programs with embedded formatting will lose that formatting when you import it into an MText-style paragraph if you're working with AutoCAD 2000, 2000i, or 2002. Formatting is well preserved within AutoCAD 2004 and higher, however. You can paste Microsoft Word documents directly into AutoCAD 2002 and higher as MText and maintain their original formatting.

AutoCAD 2000, 2000i, and 2002 exhibit odd behavior with respect to exploding block insertions that contain MText entities. In these versions, the nested MText entities are automatically exploded into individual Text entities when you use the VBA Explode method. This doesn't occur in AutoCAD 2004 and higher, however, and MText is preserved after the Explode method is executed. Text styles may be applied similarly to normal Text objects.

2005 and 2006 allows you to retrieve the field codes used to display special characters within the text string. However, the special codes inserted using an AutoCAD command may not be retrieved. A good rule of thumb is, if you can add a code programmatically, then you can retrieve it as well.

The Point Object

Point objects can act as nodes to which you can snap objects. You can specify a full 3-D WCS location for a point. You use the AddPoint method to create a new Point object, as follows:

Set PointObject = Object.AddPoint(PointPosition)

Table 8-15 provides a brief description of the PointPosition argument.

Table 8-15. AddPoint Method Parameter

Name

Data Type

Description

PointPosition

Variant

A three-element array of doubles specifying the point's WCS coordinates

Controlling How a Point Object Is Displayed

The appearance of Point objects is controlled by the system variables PDMODE and PDSIZE. The PDMODE variable specifies the figure used to represent a point. This variable may take a value from 0 to 4, as Figure 8-12 illustrates. A value of 1 means that nothing is displayed.

You can combine the PDMODE variable value with the number 32, 64, or 96, which represents a surrounding shape. Figure 8-13 shows the possible combined symbols for a Point object.

The PDSIZE variable controls the size of the Point object figure. It has no effect if you set PDMODE to 0 or 1. You may set PDSIZE to a positive, negative, or zero value:

• A positive value specifies an absolute size for the point in drawing units.

• A negative value specifies the point symbol size as a percentage of the viewport area.

• A zero setting will generate the points at five percent of the height of the graphics area.

When you regenerate the drawing, AutoCAD recalculates the size and symbol of all points. So if you change PDMODE and PDSIZE, the appearance of existing points doesn't change until AutoCAD regenerates the drawing. You set the PDMODE and PDSIZE variables using the SetVariable method, which I discuss in Appendix C.

The following example illustrates how to use the PDMODE and PDSIZE variables and the AddPoint method:

Public Sub TestAddPoint() Dim objEnt As AcadPoint Dim varPick As Variant Dim strType As String Dim intType As Integer Dim dblSize As Double

On Error Resume Next

With ThisDrawing.Utility

'' get the pdmode center type .InitializeUserInput 1, "Dot None Cross X Tick"

strType = .GetKeyword(vbCr & "Center type [Dot/None/Cross/X/Tick]: ") If Err Then Exit Sub

Select Case strType

Case "Dot": intType = 0 Case "None": intType = 1 Case "Cross": intType = 2 Case "X": intType = 3 Case "Tick": intType = 4 End Select

'' get the pdmode surrounding type .InitializeUserInput 1, "Circle Square Both"

strType = .GetKeyword(vbCr & "Outer type [Circle/Square/Both]: ") If Err Then Exit Sub

Figure 8-12.

The Point object

©

o

M

O

32

33

34

35

36

*

M

64

65

66

67

68

rh x[x

M

n

96

97

98

99

100

Figure 8-13. Various point styles

Select Case strType

Case "Circle": intType = intType + 32 Case "Square": intType = intType + 64 Case "Both": intType = intType + 96 End Select

'' get the pdsize .InitializeUserInput 1, ""

dblSize = .GetDistance(, vbCr & "Enter a point size: ") If Err Then Exit Sub

'' set the system varibles With ThisDrawing .SetVariable "PDMODE", intType .SetVariable "PDSIZE", dblSize End With

'' now add points in a loop Do

'' get user point for new vertex, use last pick as basepoint varPick = .GetPoint(, vbCr & "Pick a point <exit>: ")

'' exit loop if no point picked If Err Then Exit Do

'' add new vertex to pline at last offset ThisDrawing.ModelSpace.AddPoint varPick

Loop End With End Sub

The Region Object

The Region object represents a bounded plane and may consist of Line, Arc, Circle, Elliptical Arc, LightweightPolyline, and Spline objects. To create a Region object from a set of drawing entities, use the AddRegion method as follows:

RegionArray = Object.AddRegion(ObjectsArray)

Table 8-16 provides a brief description of the ObjectsArray argument.

Table 8-16. AddRegion Method Parameter

Name

Data Type

Description

ObjectsArray

Array of objects

The objects used to make the region objects. These objects must form a closed area and must be coplanar.

AddRegion returns an array of the Region objects created from the ObjectsArray parameter, as shown in Figure 8-14.

Arc Object

Arc Object

Combined to Form

Line Object

Figure 8-14. Region objects

Combined to Form

Region Object

Line Object

Region Object

Figure 8-14. Region objects

The ObjectsArray may contain drawing objects that form more than one enclosed plane. For each enclosed plane, a Region object is formed and held in an array of Region objects. If two or more curves share end points, the region created may be arbitrary.

The following example demonstrates how to build and manipulate regions. The objEnts variable is an array of objects that forms two closed coplanar faces to be converted into Region objects. One region is a full circle and the other is a closed semicircle. Once the AddRegion method is executed, these two regions are held in the regions variable. Next, the code uses the Boolean method (detailed in Chapter 9) to subtract the semicircle from the larger full circle. Finally, the region is moved to a new location, showing that it's a single composite object.

Public Sub TestAddRegion() Dim varCenter As Variant Dim varMove As Variant Dim dblRadius As Double Dim dblAngle As Double Dim varRegions As Variant Dim objEnts() As AcadEntity

On Error Resume Next '' get input from user With ThisDrawing.Utility varCenter = .GetPoint(, vbCr & "Pick the center point: ") dblRadius = .GetDistance(varCenter, vbCr & "Indicate the radius: ") dblAngle = .AngleToReal("180", acDegrees) End With

'' draw the entities With ThisDrawing.ModelSpace

'' draw the outer region (circle) ReDim objEnts(2)

Set objEnts(0) = .AddCircle(varCenter, dblRadius) '' draw the inner region (semicircle)

Set objEnts(1) = .AddArc(varCenter, dblRadius * 0.5, 0, dblAngle) Set objEnts(2) = .AddLine(objEnts(1).StartPoint, objEnts(1).EndPoint)

'' create the regions varRegions = .AddRegion(objEnts) End With

'' get new position from user varMove = ThisDrawing.Utility.GetPoint(varCenter, vbCr & _

"Pick a new location: ")

'' subtract the inner region from the outer varRegions(l).Boolean acSubtraction, varRegions(o)

'' move the composite region to a new location varRegions(l).Move varCenter, varMove End Sub

You may also explode individual regions by using the Explode method. Be aware that invoking the Region entity incurs a performance hit while AutoCAD loads the ACIS or ShapeManager solid-modeling engine, depending upon what version of AutoCAD you're working with.

The Solid Object

The Solid object represents a 2-D polygon, not to be confused with the 3-D solids described in Chapter 9. You can create a polygon by specifying two points to define the first edge. The third point defines an edge back to the start point, not to the second point as you might imagine. Next, you have the option to define a fourth point in the same position as the third, which results in a triangular shape. Alternatively, you can specify a different fourth point to define a variety of shapes (e.g., a rectangle, a parallelogram, a trapezoid, a chevron, or a bow tie). Use the AddSolid method as follows:

Set SolidObject = Object.AddSolid(Point1, Point2, Point3, Point4) Table 8-17 provides a brief description of each argument.

Table 8-17. AddSolid Method Parameters

Name

Data Type

Description

Pointl

Variant

A three-element array of doubles defining the start of the polygon's first side in WCS coordinates.

Point2

Variant

A three-element array of doubles defining the end of the polygon's first side in WCS coordinates.

Point3

Variant

A three-element array of doubles defining the end of the polygon's second side in WCS coordinates. This point is opposite Pointl on the second edge.

Point4

Variant

A three-element array of doubles specifying the last point that defines the polygon's third and optionally fourth edges in WCS coordinates. It may be the same as Point3, in which case only three polygon edges are visible.

If the FILLMODE system variable is set to 1, the shape will be shaded; otherwise, only the boundary lines are visible. The following example uses AddSolid with input from the user:

Public Sub TestAddSolid() Dim varP1 As Variant Dim varP2 As Variant Dim varP3 As Variant Dim varP4 As Variant Dim objEnt As AcadSolid

On Error Resume Next

'' ensure that solid fill is enabled ThisDrawing.SetVariable "FILLMODE", 1

'' get input from user With ThisDrawing.Utility varP1 = .GetPoint(, vbCr & "Pick the start point: ") varP2 = .GetPoint(varP1, vbCr & "Pick the second point: ") varP3 = .GetPoint(varP1, vbCr & "Pick a point opposite the start: ") varP4 = .GetPoint(varP3, vbCr & "Pick the last point: ") End With

'' draw the entity

Set objEnt = ThisDrawing.ModelSpace.AddSolid(varP1, varP2, varP3, varP4) objEnt.Update End Sub

The Text Object

The Text object represents text on a drawing. It differs from the MText object in that it consists of only one line of text. It also lacks the embedded formatting capabilities of MText. The AddText method creates a new Text object:

Set TextObject = Object.AddText(TextString, InsertionPoint, Height) Table 8-18 provides a brief description of each argument.

Table 8-18. AddText Method Parameters

Name

Data Type

Description

TextString

String

The text to be displayed in the drawing

InsertionPoint

Variant

A three-element array of doubles representing the text's position with respect to the WCS

Height

Double

A positive number indicating the text's height

The following code example shows the AddText method in practice:

Public Sub TestAddText() Dim varStart As Variant Dim dblHeight As Double Dim strText As String Dim objEnt As AcadText

On Error Resume Next

'' get input from user With ThisDrawing.Utility varStart = .GetPoint(, vbCr & "Pick the start point: ") dblHeight = .GetDistance(varStart, vbCr & "Indicate the height: ") strText = .GetString(True, vbCr & "Enter the text: ") End With

'' create the text If ThisDrawing.ActiveSpace = acModelSpace Then

Set objEnt = ThisDrawing.ModelSpace.AddText(strText, varStart, dblHeight) Else

Set objEnt = ThisDrawing.PaperSpace.AddText(strText, varStart, dblHeight) End If objEnt.Update End Sub

Was this article helpful?

0 -1

Responses

  • Rowan
    How to hatch a hatch in autocad?
    8 years ago

Post a comment