Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBA to get various points array from the user..

Status
Not open for further replies.

basepointdesignz

Programmer
Jul 23, 2002
566
GB
Hi all,

I've asked this question before, but had no real good replies from it to with VBA.. Now I got some of the code I'm constructing to show you..

The story so far is....
I'm trying to write a small VBA program that the user can use to get the area of any shape and then it'll format the results to show in metre-squared (this is for calculating floor area). So far I've got the 'Select Entity part down, which simply asks the user to select a polyline object already drawn, then the program displays the formatted result on the form, then if needed, the user can insert the result as Text..
The problem I have is that I'd like the user to be able to select as many point as required (will always be random) to draw the desired shape of the room or whatever. Then the progam draws a polyline using the points selected as it's vertexes and then get the area of the polyline. Then it deletes the polyline so all the user sees is the results..
What I can't do is set up the program to accept all the selected points into the vertex list for the polyline..

Anyway, here's a selection of the code. All it does so far is to add a AcadPoint at each selected point, then show the number of points (a msgbox purely for my purposes to show me that the code is working OK), then the polyline is only drawn from 0,0 to the last selected point!!



Code:
' SELECT AREA (BY POINTS)..
Private Sub CommandButton7_Click()
Dim pointx As AcadPoint  'Add an AcadPoint at each selected point, to show where user has already picked..
Dim ptx As Variant  'Point array (multiple if necessary)..
Dim polyXX As AcadLWPolyline ' Polyline to be drawn from the points array..
Dim countx As Integer 'Counter for loop..
Dim countxx As Double  'Counter value * 2 (to get correct point array numbers (X,Y))..

countx = 0  'Set counter to nothing..

areaform.hide  'Hide form to allow user to slect points from AutoCAD..

ptx = ThisDrawing.Utility.GetPoint(, "Pick the first point..")  'Getpoint method..

'*******************************
'** Start the point pick loop **
'*******************************
Do
    
countx = countx + 1  'Add 1 to the counter..
    Set pointx = ThisDrawing.ModelSpace.AddPoint(ptx)
    
    countxx = countx * 2   'Multiply count number by 2 (to get correct array numbers)..
ReDim Preserve ptx(0 To countxx - 1)  'Re-dimension the ptx variable..
ptx = ThisDrawing.Utility.GetPoint(, "Pick another point. (Press ENTER to get area)..")  'Getpoint method..

    On Error GoTo END_DO 'Exit the loop if ENTER or another key is hit (basically an error)..
Loop
'*******************************
'******** End the loop *********
'*******************************

END_DO:
'Show number of points selected.
' (This is purely for code construction purposes to check that
' the correct number of points has been accounted for)..
MsgBox "Number of points selected is " & countx & ".."

Set polyXX = ThisDrawing.ModelSpace.AddLightWeightPolyline(ptx)   'Draw a polyline from the points array..
areaform.Show  'Show the form again..
End Sub



Anyway, any ideas would be very much appreciated..

Cheers,

Renegade..

BULLET-PROOF DESiGNZ
renegade@tiscali.co.uk
 
I think this may be what you are looking for. I changed the sub name and added some variables. Take a look and let me know...

Private Sub PickPoints()
Dim pointx As AcadPoint 'Add an AcadPoint at each selected point, to show where user has already picked..
Dim ptx As Variant 'Point array (multiple if necessary)..
Dim ptsX() As Double 'ADDED TO HOLD TOTAL VALUES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Dim polyXX As AcadLWPolyline ' Polyline to be drawn from the points array..
Dim countx As Integer 'Counter for loop..
Dim countxx As Double 'Counter value * 2 (to get correct point array numbers (X,Y))..

countx = 0 'Set counter to nothing..

'areaform.hide 'Hide form to allow user to slect points from AutoCAD..

ptx = ThisDrawing.Utility.GetPoint(, "Pick the first point..") 'Getpoint method..

'*******************************
'** Start the point pick loop **
'*******************************
Do

countx = countx + 1 'Add 1 to the counter..
Set pointx = ThisDrawing.ModelSpace.AddPoint(ptx)

countxx = countx * 2 'Multiply count number by 2 (to get correct array numbers)..
ReDim Preserve ptsX(0 To countxx - 1) 'CHANGED TO NEW VARIABLE
ptsX(countxx - 2) = ptx(0): ptsX(countxx - 1) = ptx(1) 'STORE NEW POINTS TO VARIABLE

ptx = ThisDrawing.Utility.GetPoint(, "Pick another point. (Press ENTER to get area)..") 'Getpoint method..

On Error GoTo END_DO 'Exit the loop if ENTER or another key is hit (basically an error)..
Loop
'*******************************
'******** End the loop *********
'*******************************

END_DO:
'Show number of points selected.
' (This is purely for code construction purposes to check that
' the correct number of points has been accounted for)..


MsgBox "Number of points selected is " & countx & ".."
ReDim Preserve ptsX(0 To UBound(ptsX) + 2) 'CHANGED TO NEW VARIABLE
ptsX(UBound(ptsX) - 1) = ptsX(0): ptsX(UBound(ptsX)) = ptsX(1) 'USE PT1 TO CLOSE POLYLINE

Set polyXX = ThisDrawing.ModelSpace.AddLightWeightPolyline(ptsX) 'CHANGED TO DRAW NEW ARRAY LIST
'areaform.Show 'Show the form again..
End Sub
 
Thanks Borgunit,

You know that old addage about when you stare at something for too long........

That works perfectly - I had to alter and add a few things to suit it to what was required but thanks, that's a great help..

Cheers buddy,

Renegade..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top