How to have variable array variable?

How to have variable array variable?

Anonymous
Not applicable
648 Views
2 Replies
Message 1 of 3

How to have variable array variable?

Anonymous
Not applicable

As the code below shows I am trying to use an array with
a variable subscript. I have the dynamic array GetPoint() into
which I want to put an unknown number of points.

When trying to assign a value to GetPoint(x) I keep getting the
message:

Subscript out of range

because the code doesn't like variable subscripts, ie, GetPoint(x).

Is there a way to do this?

The idea behind the code is to select on screen all points needed
for a boundary, have the code sort the points based on the Handle
property of the points then draw a polyline (or 3dPolyline) connecting
all the boundary points.

This assumes the points are first acquired in the proper order for a
boundary, then paste the coordinates in x,y,z format (from an Excel
spreadsheet) onto the command line using the

Draw/Point/Multiple point

command. This will give each point a sequential numbered handle. But
when selecting points en masse on screen the order is all jumbled up,
hence the need to sort.

bc


Sub DrawBoundary()

Dim Points() As Double
Dim LineObj As AcadPolyline
Dim PLineObj As Acad3DPolyline
Dim ssetObj1 As AcadSelectionSet
Dim cnt As Integer
Dim c As Integer
Dim x As Integer
Dim GetPoint() As Variant
Dim GetCoord() As Variant
Dim Point As AcadPoint

On Error Resume Next
ThisDrawing.SelectionSets.Item("Points").Delete
On Error GoTo 0

Set ssetObj1 = ThisDrawing.SelectionSets.Add("Points")

Dim intCode(1) As Integer
Dim varData(1) As Variant
intCode(0) = 0: varData(0) = "POINT" 'this is type of object looked for
intCode(1) = 8: varData(1) = "Points" 'this is layer object is on

ThisDrawing.Utility.Prompt "Select on screen all boundary points:"
ssetObj1.SelectOnScreen intCode, varData
cnt = ssetObj1.Count

x = 0: c = 0
For Each Point In ssetObj1
GetPoint(x) = Point.Handle
GetCoord(c) = Point.Coordinates(0)
GetCoord(c + 1) = Point.Coordinates(1)
GetCoord(c + 2) = Point.Coordinates(2)
x = x + 1: c = c + 3
Next

'sort routine will eventually go here
'
'
'assign sorted points to Points() here
'
'
' Create a lightweight Polyline object in model space
Set LineObj = ThisDrawing.ModelSpace.AddPolyline(Points)

LineObj.Closed = True

End Sub

0 Likes
649 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Well, I would have set the job up so it was a bit less confusing. You have a selection set named "Points", looking for Points, on a layer called points? Then you are trying to store the data in an array called Points? while looping thru objects named "Point"




..... get the point?






You declared your array, but never told VB what size it was to be. You can dim an array up front "Dim MyArray(5) as Double"


.. you can erase/resize it afterwards "ReDim MyArray(7) as Double".. you can even dim it empty at the start, and then, somewhere down the line, add/change the size dyanamically "ReDim Preserve MyArray(MyCounter) as Double" and keep all the stuff inside....





But somewhere along the line, you have to put in a hard number, or a variable, or *something* that lets VB know how much "stuff" it has to store .. at least until you tell it some other size, in your next pass thru your loop





Since your "getPoint" array is a string (Handle), Id dim it up front as such:

Dim GetPoint() As String



And Similary, Dim your 'GetCoord" array as Double:

Dim GetCoord() As Double



Now, once you get the number of items you are looping thru, in the "cnt" property of the selection set, REDIM the array to the proper
size, before you run thru the loop. (Remembering that a COUNT property is 1-based, and arrays are 0-based:



redim GetPoint(cnt-1)

redim GetCoord((cnt*3)-1)





Thats all for now, my lunch is almost over - but im sure we'll be seeing you back here soon 🙂












eclared an array of variants, and not a variant array. If that makes your head hurt, then do what I do and stick to doubles
0 Likes
Message 3 of 3

Anonymous
Not applicable
Ok, ok, so my naming convention needs to be a bit less
linear. ;^)

I ran a test after adding the redim statements just to make
sure I did it right. Lo and behold, not only did it work but
without even having the sort routine in there it drew the
correctly. (well except for closing the polyline)

I did a number of tests on a lot of different boundaries with
anywhere from 40 points to >500 points. They all came out
great.

The only reason the line doesn't close is because the previous
line

Set PLineObj = ThisDrawing.ModelSpace.AddPolyline(GetCoord)

bombs out on a Type mismatch error. I'll figure that out myself.

>but im sure we'll be seeing you back here soon 🙂
Are you saying I have too many questions? 🙂

Believe me, I use this great forum as a last resort when all my
own experimenting fails.

bc
0 Likes