Autocad Point Variable

(Edit – this is an important post, to emphasize, autocad vba help always declares point values as a static 3 place array (0 to 2) of doubles. A better way is to always declare the variable as a dynamic array and fill them with a function. The function code is the takeaway. Look at the first two PT examples. The first sub PT1 uses standard method static (0 to 2) array. This is an improvement over manually filling points and many of my older posts use this to clean up the code. The second function PT uses a static array internally but returns a dynamic array. Look at the assignments for each method. Using a function is cleaner. With dynamic arrays you can duplicate points by assignment. At the end is a discussion of autocad method polarpoint (typical of many methods) that returns an array value that cannot be received by a static array so autocad help uses a variant. A dynamic array works just as well and is my preferred method for all point values.)

All of Autocad graphics methods use a 3 place array of doubles for the coordinates.

The simplest way to sub this is by passing the static array for the sub to fill data.

Dim PtA(0 To 2) As Double
Call pt1(ptA, 1, 2, 0)

Sub pt1(ByRef pnt() As Double, x As Double, y As Double, z As Double)
pnt(0) = x: pnt(1) = y: pnt(2) = z
End Sub

This changes ptA in the calling program. Another way (a better way) is to make a function and pass the array back to a receiving variable.
This requires a dynamic array.

Dim ptA() As Double
ptA = Pt(1, 2, 0)

Function Pt(x As Double, y As Double, z As Double) As Double()
Dim pnt(0 To 2) As Double
pnt(0) = x: pnt(1) = y: pnt(2) = z
Pt = pnt
End Function

This is a better way to deal with point values.

We can even get away with this – no variables at all.

Set lineAB = acadDoc.ModelSpace.AddLine(Pt(1, 4, 0), Pt(9, 1, 0))

Dynamic arrays can accept point values. A lot of objects in autocad return point values. Autocad VBA help always uses a single variant to hold the array, because their usual point is a static array and you cannot assign to that. The same thing can be done by just dispensing with the static array all the time.

For example – the utility polarpoint method returns a point value. A variant has to be declared to receive it.

from help –
RetVal = object.PolarPoint(Point, Angle, Distance)
Point Type: Variant (three-element array of doubles)
Return Value Type: Variant (three-element array of doubles)

example from help –
Dim polarPnt As Variant
Dim basePnt(0 To 2) As Double
polarPnt = ThisDrawing.Utility.PolarPoint(basePnt, angle, distance)
Set lineObj = ThisDrawing.ModelSpace.AddLine(basePnt, polarPnt)

PolarPoint returns an array. You cannot assign to a static array. Dynamic arrays can assign. If the declaration is to a dynamic array with the correct type Double it works. There is no reason I can think of to ever declare a point except as a dynamic array.

Dim polarPnt() As Double


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.