the AcadLine loop

Autocad VBA Acadline requires two separate three element array of doubles – two endpoints. The line segment is drawn each time through the loop.

To graph the function y=f(x) we have to select a definite span and increment for the independent variable X.  The span divided by the increment produce the number of line segments we are going to draw.  We are drawing one line segment each time through the loop so we use the number of line segments as the loop control. We calculate two values for X and two values for Y each time through the loop. We draw the line then we increment the loop. We save the values in an array whose only purpose is to write to a table for reference. Since we are creating the array for excel and not for autocad, we make it a two dimensional array with 3 columns, X, Y and Z.

we have to add the last point outside the loop.  This is a demo program with canned values.

Sub temp_line_method()
Call connect_acad

Dim lineobj As AcadLine
Dim x1 As Double, x2 As Double
Dim y1 As Double, y2 As Double
Dim z1 As Double, z2 As Double

Dim pt1(0 To 2) As Double
Dim pt2(0 To 2) As Double
Dim i As Integer, numpts As Integer, numlines As Integer

'canned simple values

Xmin = 1
Xmax = 3
X_inc = 0.5

numlines = (Xmax - Xmin) / X_inc 'number of line segments
numpts = numlines + 1 'one more pt than line segment

Dim pt3() As Double
'this array for writing list to excel
'might as well make it in a 3 wide column
ReDim pt3(1 To numpts, 1 To 3)

'we are drawing one line each time thru loop
'so loop counter should be numlines
For i = 1 To numlines
x1 = Xmin + ((i - 1) * X_inc)
x2 = x1 + X_inc

'y1 = F(x1) 'function goes here
'y2 = F(x2)

'test function
y1 = x1 ^ 2
y2 = x2 ^ 2

'not using z at this time but Addline requires it
z1 = 0
z2 = 0

pt1(0) = x1: pt1(1) = y1: pt1(2) = 0
pt2(0) = x2: pt2(1) = y2: pt2(2) = 0

'draw the line inside the loop
Set lineobj = acadDoc.ModelSpace.AddLine(pt1, pt2)

'add values to the array for excel only

pt3(i, 1) = x1
pt3(i, 2) = y1
pt3(i, 3) = z1

Next i

'that extra point
'i does increment one past numlines
pt3(i, 1) = x2
pt3(i, 2) = y2
pt3(i, 3) = z2


Call display_pt3(pt3)

End Sub

Sub display_pt3(pt3() As Double)
'we are expecting a 3-wide array of variable length

If UBound(pt3, 2) <> 3 Then
MsgBox "array not 3-wide"
Exit Sub
End If

NewSht ("func_data_list_3")

Dim i As Integer
Dim numrows As Integer
'expects LBound to be 1
numrows = UBound(pt3, 1)

For i = 1 To numrows
Cells(i, 1) = pt3(i, 1)
Cells(i, 2) = pt3(i, 2)
Cells(i, 3) = pt3(i, 3)
Next i

End Sub

Sub NewSht(strSheetName As String)
Application.DisplayAlerts = False
On Error Resume Next
Worksheets.Add.Name = strSheetName
Worksheets(strSheetName).Move after:=Worksheets(Worksheets.Count)
End Sub





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.