Draw line between two ends of lines

Draw line between two ends of lines

Anonymous
Not applicable
1,622 Views
5 Replies
Message 1 of 6

Draw line between two ends of lines

Anonymous
Not applicable

I am getting Run-time error 5 "invalid procedure call or argument" when i try to draw a line from Point to End_line, see below:

Dim Dline As AcadLine
Dim Point(0 To 1), End_line(0 To 1) As Variant

Set Dline = ThisDrawing.ModelSpace.AddLine(Point, End_line)

 here is the full code

Option Explicit
Sub Find_Lines()
    Dim ent As AcadEntity
    Dim SP, EP, Point(0 To 1), End_line(0 To 1) As Variant
    Dim Dline As AcadLine
    Dim min_x, min_y, min_Dist, dist As Double
    Dim min_name As String
    Point(0) = 0
    Point(1) = 0
    min_Dist = 100001
        
    For Each ent In ThisDrawing.ModelSpace
        If TypeOf ent Is AcadLine Then
            If ent.Layer = "Travel_Path" Then
                SP = ent.StartPoint
                EP = ent.EndPoint
                Call Find_Min_Point(Point, SP, EP, dist, End_line)
                If dist < min_Dist Then
                    min_name = ent.Handle
                    min_Dist = dist
                    min_x = End_line(0)
                    min_y = End_line(1)
                End If
                'i = i + 1
            End If
        End If
    Next ent
    
    Debug.Print min_name, min_Dist, min_x, min_y
    End_line(0) = min_x
    End_line(1) = min_y
        
        
        
    Set Dline = ThisDrawing.ModelSpace.AddLine(Point, End_line)
    
End Sub
Sub Find_Min_Point(Point As Variant, SP As Variant, EP As Variant, dist As Double, End_line As Variant)
    Dim m, b, step, x, y, x_plus, y_plus, this_dist, next_dist As Double
    step = 0.01
    this_dist = 100001
    next_dist = 100000
    m = Get_Slope(SP, EP)
    b = Get_Intercept(SP, m)
    'Debug.Print "StartPoint: ", SP(0), SP(1), "EndPoint: "; EP(0), EP(1), "m: ", m, "b: ", b
    If SP(0) < EP(0) Then
        x = SP(0)
    Else
        x = EP(0)
    End If
    Do
        x_plus = x + step
        y = m * x + b
        y_plus = m * x_plus + b
        this_dist = GetDistance(Point(0), x, Point(1), y)
        next_dist = GetDistance(Point(0), x_plus, Point(1), y_plus)
        x = x_plus
    Loop Until next_dist > this_dist
    'Debug.Print x, y, this_dist
    dist = this_dist
    End_line(0) = x
    End_line(1) = y
End Sub
Function Get_Slope(SP As Variant, EP As Variant) As Double
    If (EP(0) - SP(0)) = 0 Then
        Get_Slope = 0
    Else
        Get_Slope = (EP(1) - SP(1)) / (EP(0) - SP(0))
    End If
End Function
Function Get_Intercept(SP As Variant, m As Variant) As Double
    Get_Intercept = SP(1) + m * SP(0)
End Function
Function GetDistance(x1 As Variant, x2 As Variant, y1 As Variant, y2 As Variant) As Double
    GetDistance = Sqr((x1 - x2) ^ 2 + (y1 - y2) ^ 2)
End Function
Sub Ch4_AddLightWeightPolyline()
  Dim plineObj As AcadLWPolyline
  Dim points(0 To 5) As Double

  ' Define the 2D polyline points
  points(0) = 2: points(1) = 4
  points(2) = 4: points(3) = 2
  points(4) = 6: points(5) = 4

  ' Create a light weight Polyline object in model space
  Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)
  ThisDrawing.Application.ZoomAll
End Sub
0 Likes
1,623 Views
5 Replies
Replies (5)
Message 2 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

points for a line are defined by an array of 3 doubles, not 2 as you defined in:

Endpoint of lines always have X/Y and Z-value, so your array definition (0 to 1) can't hold all 3 values.

 

I have not tested the code after that change, but hopefully this helps you for your next steps.

In case your code crashes please let us know which of the code-lines is the source of the error.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 6

Anonymous
Not applicable

I had them in 3D before I posted this, I just redid the z axis and still get the same error

0 Likes
Message 4 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> and still get the same error

At which line of your code?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 6

Anonymous
Not applicable
Set Dline = ThisDrawing.ModelSpace.AddLine(Point, End_line)

Could the fact that they are variants be a problem?

 

I did this as a clean up and and don't get the issue:

 

Sub Find_Lines2()
Dim Point(0 To 2) As Double
Dim End_line(0 To 2) As Double
Dim Dline As AcadLine

Point(0) = 0
Point(1) = 0
Point(2) = 0

End_line(0) = 2
End_line(1) = 2
End_line(2) = 0

Set Dline = ThisDrawing.ModelSpace.AddLine(Point, End_line)

End Sub
0 Likes
Message 6 of 6

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> Could the fact that they are variants be a problem?

It's the question when you use it.

 

When you want to define a variable for a point as your last code you need the PT(0 to 2) as double.

 

However when you need to get a point from AutoCAD (user shows point with cursor) then you have to defined the variable as Variant ... as shown in that example:

 

Dim returnPnt As Variant
returnPnt = ThisDrawing.Utility.GetPoint(, "Enter a point: ")

And that "returnPnt" variable can be casted to an array of doubles and so it can be used to create a line too.

 

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes