Author Topic: [script] Find projection point of two lines  (Read 2151 times)

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6209
    • View Profile
    • Cambam V1.0 French Doc
[script] Find projection point of two lines
« on: November 03, 2016, 00:27:33 am »
Hello,

This VBS script allow to draw the projection point of 2 lines (even if they not intersect)

Select 2 lines and run the script

note: you must select lines and not polylines ; if you use polylines, first explode them to obtain lines.

http://screencast.com/t/YE2cuFKoTakO

Code: [Select]
' dh42 11/2016
' projection point
' select 2 lines then run the plugin.
'
' to convert polyline to line, use explode
'
'

    Sub main()
        Dim ent As Entity
        Dim match As Boolean = True

        If (view.SelectedEntities.Length = 2) Then  'only 2 objects selected

            For Each ent In view.SelectedEntities
                If Not TypeOf ent Is Line Then  'not a line
                    match = False
                End If
            Next ent
        Else
            match = False
        End If

        If match = False Then Exit Sub 'not the correct number of lines, or not lines

        'Ok, 2 lines objects selected
        'get the lines in the current selection
        Dim line1 As Line = CType(view.SelectedEntities(0), Line)
        Dim line2 As Line = CType(view.SelectedEntities(1), Line)

        ' creates Line2F from Line
        Dim l1 As Line2F = New Line2F(line1.Points.Item(0).X, line1.Points.Item(0).Y, _
                                      line1.Points.Item(1).X, line1.Points.Item(1).Y)

        Dim l2 As Line2F = New Line2F(line2.Points.Item(0).X, line2.Points.Item(0).Y, _
                                      line2.Points.Item(1).X, line2.Points.Item(1).Y)

        'get the projection point
        Dim pt As Point2F = l1.ProjectionIntersect(l1, l2)

        If Not Double.IsNaN(pt.X) Then  'test for NaN in the x value = no return, = parallel lines
            Dim pl As PointList = New PointList(pt)
            Doc.Add(pl)
        Else
            App.Log("parallel lines")
        End If
    End Sub

++
David
« Last Edit: November 03, 2016, 00:39:51 am by dh42 »

Online Bubba

  • CNC Jedi
  • *****
  • Posts: 3026
    • View Profile
Re: [script] Find projection point of two lines
« Reply #1 on: November 03, 2016, 10:56:47 am »
This is really useful script. Thank you David.
My 2ยข

Win 10 64 bit, CB [1.0} rc 1 64 bit, Mach3, ESS, G540

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6209
    • View Profile
    • Cambam V1.0 French Doc
Re: [script] Find projection point of two lines
« Reply #2 on: November 03, 2016, 14:01:12 pm »
I'll turn it into a plugin with 2 options, the same as the script (intersection point) and "corner" to do ... corner with 2 lines.

++
David