Line Objects

This section demonstrates how to create the various line-shaped objects.

The Line Object

The AddLine method creates a Line object, which is a single, straight line running between two points:

Set LineObject = Object.AddLine(StartPoint, EndPoint) Table 8-5 provides a brief description of each argument.

Table 8-5. AddLine Method Parameters

Name

Data Type

Description

StartPoint

Variant

A three-element array of doubles specifying the line's start in the

WCS

EndPoint

Variant

A three-element array of doubles specifying the line's end in the

WCS

Figure 8-4 illustrates the Line object.

Figure 8-4 illustrates the Line object.

StartPoint •-EndPoint

Figure 8-4. The Line object

This example gets a start and end point from the user and then creates a new line:

Public Sub TestAddLine() Dim varStart As Variant Dim varEnd As Variant Dim objEnt As AcadLine

On Error Resume Next

'' get input from user With ThisDrawing.Utility varStart = .GetPoint(, vbCr & "Pick the start point: ") varEnd = .GetPoint(varStart, vbCr & "Pick the end point: ") End With

'' draw the entity If ThisDrawing.ActiveSpace = acModelSpace Then

Set objEnt = ThisDrawing.ModelSpace.AddLine(varStart, varEnd) Else

Set objEnt = ThisDrawing.PaperSpace.AddLine(varStart, varEnd) End If objEnt.Update End Sub

The LWPolyline Object

A lightweight polyline is a 2-D line consisting of straight and arced segments. It's functionally similar to the legacy Polyline entity, but it's internally represented in a "flat" data structure as opposed to the tree-structured Polyline entity. This results in a more compact data size per entity and faster graphics regeneration; hence the "lightweight" name.

You create a new LWPolyline object using the AddLightWeightPolyline method, as follows:

Set LWPolylineObject = Object.AddLightWeightPolyline(Vertices)

Table 8-6 provides a brief description of the Vertices argument.

Table 8-6. The AddLightWeightPolyline Method Parameter

Name_Data Type Description_

Vertices Variant An array of doubles specifying a list of 2-D vertex points in WCS

coordinates. It's a simple array with a single dimension composed of alternating X and Y values (i.e., p1x, p1y, p2x, p2y, etc.). Because you must supply at least two points to create a polyline, this array must have a minimum of four elements.

■ Note Lightweight polylines don't store Z-axis or elevation information with each vertex point. Instead, the Polyline object has an Elevation property that specifies the Z elevation relative to the coordinate system for all vertices.

Figure 8-5 illustrates the LWPolyline object.

Figure 8-5. The LWPolyline object

When you create an LWPolyline object, it's open as shown in Figure 8-5. To close the polyline (i.e., to join the first vertex to the last), set its Closed property to True. All the segments are straight lines by default. See the section titled "Polyline Arc Segments" later in this chapter for details on how to optionally create arcs.

The following example creates a new lightweight polyline:

Public Sub TestAddLWPolyline() Dim objEnt As AcadLWPolyline Dim dblVertices() As Double

'' setup initial points ReDim dblVertices(ll) dblVertices(o) = 0#: dblVertices(l) = 0# dblVertices(2) = 10#: dblVertices(3) = 0# dblVertices(4) = 10#: dblVertices(s) = 10# dblVertices(6) = 5#: dblVertices(7) = 5# dblVertices(s) = 2#: dblVertices(g) = 2# dblVertices(l0) = 0#: dblVertices(ll) = 10#

'' draw the entity If ThisDrawing.ActiveSpace = acModelSpace Then

Set objEnt = ThisDrawing.ModelSpace.AddLightWeightPolyline(dblVertices) Else

Set objEnt = ThisDrawing.PaperSpace.AddLightWeightPolyline(dblVertices) End If objEnt.Closed = True objEnt.Update End Sub

To add vertices to an LWPolyline object one at a time instead of all at once, use the AddVertex method. The following example uses this method in a loop, adding vertices to a selected polyline until the user is satisfied. Try using it on the polyline you created in the previous example:

Public Sub TestAddVertex() On Error Resume Next Dim objEnt As AcadLWPolyline Dim dblNew(0 To l) As Double Dim lngLastVertex As Long Dim varPick As Variant Dim varWCS As Variant

With ThisDrawing.Utility

'' get entity from user

.GetEntity objEnt, varPick, vbCr & "Pick a polyline <exit>: "

If objEnt Is Nothing Then Exit Sub

'' exit if not a lwpolyline If objEnt.ObjectName <> "AcDbPolyline" Then MsgBox "You did not pick a polyline" Exit Sub End If

'' copy last vertex of pline into pickpoint to begin loop ReDim varPick(2)

varPick(0) = objEnt.Coordinates(UBound(objEnt.Coordinates) - 1) varPick(1) = objEnt.Coordinates(UBound(objEnt.Coordinates)) varPick(2) = 0

'' append vertexes in a loop Do

'' translate picked point to UCS for basepoint below varWCS = .TranslateCoordinates(varPick, acWorld, acUCS, True)

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

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

'' copy picked point X and Y into new 2d point dblNew(0) = varPick(0): dblNew(1) = varPick(1)

'' get last vertex offset. it is one half the array size lngLastVertex = (UBound(objEnt.Coordinates) + 1) / 2

'' add new vertex to pline at last offset objEnt.AddVertex lngLastVertex, dblNew

Loop End With objEnt.Update End Sub

The MLine Object

The MLine object is a single graphic entity that consists of multiple parallel straight-line segments. The maximum number of parallel lines is 16. You use the AddMLine method to create a new MLine object:

Set MLineObject = Object.AddMLine(Vertices)

Table 8-7 provides a brief description of the Vertices argument.

Table 8-7. The AddMLine Method Parameter

Name_Data Type_Description_

Vertices Variant An array of doubles specifying a list of 3-D vertex points in WCS

coordinates. It's a simple array with a single dimension composed of alternating X, Y, and Z values (i.e., p1x, p1y, p1z, p2x, p2y, p2z, etc.). Because you must supply at least two points to create an MLine, this array must have a minimum of six elements.

Although an MLine object consists of multiple parallel lines, the AutoCAD Automation model doesn't expose those lines to VBA. Similarly, MLine styles aren't directly accessible from VBA. However, you have a limited amount of control when you use the CMLJUST, CMLSCALE, and CMLSTYLE system variables to control the justification, scale, and style of newly created MLines.

Note You may create or load additional multiline styles using the MLSTYLE command.

The following example illustrates the AddMLine method:

Public Sub TestAddMLine() Dim objEnt As AcadMLine Dim dblVertices(17) As Double

'' setup initial points dblVertices(0) = 0: dblVertices(1) = 0: dblVertices(2) = 0 dblVertices(3) = 10: dblVertices(4) = 0: dblVertices(5) = 0 dblVertices(6) = 10: dblVertices(7) = 10: dblVertices(8) = 0 dblVertices(9) = 5: dblVertices(10) = 10: dblVertices(11) = 0 dblVertices(12) = 5: dblVertices(13) = 5: dblVertices(14) = 0 dblVertices(15) = 0: dblVertices(16) = 5: dblVertices(17) = 0

'' draw the entity If ThisDrawing.ActiveSpace = acModelSpace Then

Set objEnt = ThisDrawing.ModelSpace.AddMLine(dblVertices) Else

Set objEnt = ThisDrawing.PaperSpace.AddMLine(dblVertices) End If objEnt.Update End Sub

The Polyline Object

The Polyline object is similar in function to the LWPolyline object, but it's stored in an alternate format. It's a 2-D or 3-D line consisting of straight and arced segments, but the segments are stored as distinct entities. A number of different polyline types are available; the following list illustrates their AutoCAD command-line equivalent and a description of each type:

PLINE = 2D polyline (AcadPolyline)

3DPOLY = 3D polyline (Acad3DPolyline)

3DMESH (also EDGESURF, RULESURF, TABSURF, etc.) = N x M 3D mesh (AcadPolygonMesh)

PFACE = Irregular 3D mesh (AcadPolyfaceMesh)

Each segment in an AcadPolyline object has a start point, an end point, and several other unique properties. This object is less efficient than the lightweight polyline object, in which segments are defined as a collection of vertex points.

Use the AddPolyline method to create a Polyline defined by a set of vertices, as follows:

Set PolylineObject = Object.AddPolyline(Vertices)

Table 8-8 provides a brief description of the Vertices argument.

Table 8-8. The AddPolyline Method Parameter

Name_Data Type_Description_

Vertices Variant An array of doubles specifying a list of 3-D vertex points in WCS

coordinates. It's a simple array with a single dimension composed of alternating X, Y, and Z values (i.e., p1x, p1y, p1z, p2x, p2y, p2z, etc.). Because you must supply at least two points to create a polyline, this array must have a minimum of six elements.

The following example illustrates the AddPolyline method and shows how to close the polyline and set its type:

Public Sub TestAddPolyline() Dim objEnt As AcadPolyline Dim dblVertices(17) As Double

'' setup initial points dblVertices(0) = 0: dblVertices(1) = 0: dblVertices(2) = 0 dblVertices(3) = 10: dblVertices(4) = 0: dblVertices(5) = 0 dblVertices(6) = 7: dblVertices(7) = 10: dblVertices(8) = 0 dblVertices(9) = 5: dblVertices(10) = 7: dblVertices(11) = 0 dblVertices(12) = 6: dblVertices(13) = 2: dblVertices(14) = 0 dblVertices(15) = 0: dblVertices(16) = 4: dblVertices(17) = 0

'' draw the entity If ThisDrawing.ActiveSpace = acModelSpace Then

Set objEnt = ThisDrawing.ModelSpace.AddPolyline(dblVertices) Else

Set objEnt = ThisDrawing.PaperSpace.AddPolyline(dblVertices) End If objEnt.Type = acFitCurvePoly objEnt.Closed = True objEnt.Update End Sub

AutoCAD is set to draw lightweight polylines by default because of their efficiency; you may change this default setting using the PLINETYPE system variable. You can convert existing polylines from lightweight to heavy and vice versa by using the Convertpoly command.

Although it's less efficient for simple polyline representations, the Polyline object has features not present in the lightweight form, such as the capability to represent splines and other smoothly curved shapes. The following example allows the user to change a specified Polyline curve type from the command line. Try it on the polyline you created in the preceding example:

Public Sub TestPolylineType() Dim objEnt As AcadPolyline Dim varPick As Variant Dim strType As String Dim intType As Integer

On Error Resume Next

With ThisDrawing.Utility

.GetEntity objEnt, varPick, vbCr & "Pick a polyline: " If Err Then

MsgBox "That is not a Polyline" Exit Sub End If

.InitializeUserInput 1, "Simple Fit Quad Cubic"

strType = .GetKeyword(vbCr & "Change type [Simple/Fit/Quad/Cubic]: ")

Select Case strType

Case "Simple": intType = acSimplePoly Case "Fit": intType = acFitCurvePoly Case "Quad": intType = acQuadSplinePoly Case "Cubic": intType = acCubicSplinePoly End Select End With objEnt.Type = intType objEnt.Closed = True objEnt.Update End Sub

Polyline Arc Segments

When you first create them, both the LWPolyline and Polyline objects consist of only straight-line segments. Each polyline segment has an arc bulge factor that determines the segment's arc radius. By default, all segments have a bulge factor of 0, which is a straight line. A positive bulge factor denotes a counterclockwise arc relative to the start and end points of the polyline segment, and a negative bulge factor denotes a clockwise arc.

You derive the bulge factor from the arc radius by calculating the tangent of one-fourth of the included angle between the polyline segment's start and end points, as shown in Figure 8-6.

Because AutoCAD is already storing the segment points, a bulge factor is a very efficient way to capture the arc radius. Further, because the bulge factor is calculated using the segment's start and end points, the resulting arc is automatically scaled as the distance between these points changes (e.g., when the user scales or grip-edits the polyline).

Figure 8-6. A polyline segment

To assign a bulge factor to the polyline segments, use the SetBulge method. The following example sets the bulge on a Polyline segment:

Public Sub TestAddBulge() Dim objEnt As AcadPolyline Dim dblVertices(17) As Double

'' setup initial points dblVertices(0) = 0: dblVertices(1) = 0: dblVertices(2) = 0 dblVertices(3) = 10: dblVertices(4) = 0: dblVertices(5) = 0 dblVertices(6) = 7: dblVertices(7) = 10: dblVertices(8) = 0 dblVertices(9) = 5: dblVertices(10) = 7: dblVertices(11) = 0 dblVertices(12) = 6: dblVertices(13) = 2: dblVertices(14) = 0 dblVertices(15) = 0: dblVertices(16) = 4: dblVertices(17) = 0

'' draw the entity If ThisDrawing.ActiveSpace = acModelSpace Then

Set objEnt = ThisDrawing.ModelSpace.AddPolyline(dblVertices) Else

Set objEnt = ThisDrawing.PaperSpace.AddPolyline(dblVertices) End If objEnt.Type = acSimplePoly 'add bulge to the fourth segment objEnt.SetBulge 3, 0.5 objEnt.Update End Sub

The Ray Object

The Ray object represents a line that extends to infinity in one direction from a specified start point. The AddRay method uses the following syntax to create a new ray:

Set RayObject = Object.AddRay(StartPoint, SecondPoint)

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

Table 8-9. AddRay Method Parameters

Name_Data Type_Description_

StartPoint Variant A three-element array of doubles that specifies the ray's start with respect to the WCS.

SecondPoint Variant A three-element array of doubles that specifies a second point through which the ray passes. This point simply determines the ray's direction.

Figure 8-7 illustrates the Ray object.

Figure 8-7. The Ray object

The following example uses the AddRay method with points obtained from the user:

Public Sub TestAddRay() Dim varStart As Variant Dim varEnd As Variant Dim objEnt As AcadRay

On Error Resume Next

'' get input from user With ThisDrawing.Utility varStart = .GetPoint(, vbCr & "Pick the start point: ") varEnd = .GetPoint(varStart, vbCr & "Indicate a direction: ") End With

'' draw the entity If ThisDrawing.ActiveSpace = acModelSpace Then

Set objEnt = ThisDrawing.ModelSpace.AddRay(varStart, varEnd) Else

Set objEnt = ThisDrawing.PaperSpace.AddRay(varStart, varEnd) End If objEnt.Update End Sub

The Spline Object

The Spline object represents a nonuniform rational B-spline (NURBS) quadratic or cubic curve. You use the AddSpline method to create a new Spline object:

Set SplineObject = Object.AddSpline(FitPoints, StartTangent, EndTangent) Table 8-10 provides a brief description of each argument.

Table 8-10. AddSpline Method Parameters Name Data Type Description

FitPoints Variant An array of doubles specifying a list of 3-D fit points in WCS

coordinates. It's a simple array with a single dimension composed of alternating X, Y, and Z values (i.e., p1x, p1y, p1z, p2x, p2y, p2z, etc.). Because you must supply at least two points to create a spline, this array must have a minimum of six elements.

StartTangent Variant A three-element array of doubles that determines the tangent of the spline at its first fit point.

EndTangent Variant A three-element array of doubles that determines the tangent of the spline at its last fit point.

Figure 8-8 illustrates the Spline object.

Figure 8-8 illustrates the Spline object.

StartTangent specified from first FitPoint

Figure 8-8. The Spline object

The following code sample demonstrates the AddSpline method:

Public Sub TestAddSpline() Dim objEnt As AcadSpline Dim dblBegin(0 To 2) As Double Dim dblEnd(0 To 2) As Double Dim dblPoints(14) As Double

'' set tangencies dblBegin(0) = 1.5: dblBegin(1) = 0#: dblBegin(2) = 0 dblEnd(0) = 1.5: dblEnd(1) = 0#: dblEnd(2) = 0

'' set the fit dblPoints dblPoints(0) = 0: dblPoints(1) = 0: dblPoints(2) = 0 dblPoints(3) = 3: dblPoints(4) = 5: dblPoints(5) = 0 dblPoints(6) = 5: dblPoints(7) = 0: dblPoints(8) = 0 dblPoints(9) = 7: dblPoints(10) = -5: dblPoints(11) = 0 dblPoints(12) = 10: dblPoints(13) = 0: dblPoints(14) = 0

'' draw the entity If ThisDrawing.ActiveSpace = acModelSpace Then

Set objEnt = ThisDrawing.ModelSpace.AddSpline(dblPoints, dblBegin, dblEnd) Else

Set objEnt = ThisDrawing.PaperSpace.AddSpline(dblPoints, dblBegin, dblEnd) End If objEnt.Update End Sub

The XLine Object

The XLine object represents a straight line that passes through two specified points and extends to infinity in both directions. You use the AddXline method to create a new XLine object:

Set XLineObject = Object.AddXline(FirstPoint, SecondPoint)

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

Table 8-11. AddXline Method Parameters

Name

Data Type

Description

FirstPoint

Variant

A three-element array of doubles specifying a point through which the XLine object passes in WCS coordinates.

SecondPoint

Variant

A three-element array of doubles specifying a second point through which the XLine passes. This point simply determines the direction of the XLine.

Figure 8-9 illustrates the Xline object.

Figure 8-9 illustrates the Xline object.

Figure 8-9. The Xline object

Figure 8-9. The Xline object

The following example illustrates how to implement the AddXline method:

Public Sub TestAddXline() Dim varStart As Variant Dim varEnd As Variant Dim objEnt As AcadXline

On Error Resume Next

'' get input from user With ThisDrawing.Utility varStart = .GetPoint(, vbCr & "Pick the start point: ") varEnd = .GetPoint(varStart, vbCr & "Indicate an angle: ") End With

'' draw the entity If ThisDrawing.ActiveSpace = acModelSpace Then

Set objEnt = ThisDrawing.ModelSpace.AddXline(varStart, Else

Set objEnt = ThisDrawing.PaperSpace.AddXline(varStart, End If objEnt.Update End Sub

Was this article helpful?

0 -2

Post a comment