Author Topic: Numeric Polyline Script  (Read 25661 times)

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7378
    • View Profile
    • Cambam V1.0 French Doc
Numeric Polyline Script
« on: April 12, 2015, 21:52:13 pm »
Hello,

This afternoon I works on a script designed to draw line with a given length and angle.

run the script then draw a rubber line close to the size and angle you want ; the first point will be fixed.

enter the new values for length and angle in the box (or hit enter/OK if no change for the value.

the cancel button in the window, the ESC key or the MMB are used to terminate/cancel the drawing (the function auto repeat)

I'm working to convert it to a plugin soon.

Code: [Select]
 ' New CamBam VBScript
  ' sized lines - dh42 2015


    Dim abort As Boolean, finished As Boolean, redo As Boolean
    Dim mPoint As Point3F()

    '*******************************************************************************************

    Sub main()

        redo = True

        Dim lgt As Double, ang As Double'lenght, angle
        Dim p As Polyline
dim rep as string 'reply of the inputbox -> "" = cancel
        

        Do While redo = True  'repeat the command until ESC or MMB is pressed

            abort = False
            finished = False

            RuberLine()'use MeasureEditMode to draw a line

            If finished = False Then Exit Do 'aborted by ESC before 'finished' become true

            ' test if 2 identical points = line of null lenght
            If mPoint(0).Equals(mPoint(1)) Then Exit Do

            ' add the polyline to the drawing
            p = AddPoly(mPoint(0), mPoint(1))

            ' display the datas and get the new values

'lenght
            rep = InputBox("Lenght = " & LineLenght(p), "Lenght", Format(LineLenght(p), "0.00#"))
if rep ="" then Exit Do 'canceled with the msgbox button
            lgt = Val(rep)
if lgt = 0 then Exit Do ' new lenght = 0 -> cancel

'angle
            rep = InputBox("Angle = " & LineAngle(p), "Angle", Format(LineAngle(p), "0.00#"))
if rep ="" then Exit Do 'canceled with the msgbox button
            ang = Val(rep)


            'modify the polyline with new lenght and angle
            RedoPoly(p, lgt, ang)
                        
        Loop

    End Sub

    '*******************************************************************************************

    Sub point_clicked(ByVal sender As Object, ByVal e As EventArgs)

        mPoint = sender.returnvalue
        abort = True
        finished = True

    End Sub

    '*******************************************************************************************

    Sub point_clicked_clr(ByVal sender As Object, ByVal e As EventArgs)

        abort = True
        redo = False

    End Sub

    '*******************************************************************************************

    Function LineLenght(p As Polyline) As Double

        'find the distance between the 2 points (2D on XY axis)

        Dim v As Vector2F

        v = New Vector2F(p.FirstPoint, p.LastPoint)

        Return v.Length

    End Function

    '*******************************************************************************************

    Function LineAngle(p As Polyline) As Double

        'find the angle between the 2 points (2D on XY axis)

        Dim v As Vector2F

        v = New Vector2F(p.FirstPoint, p.LastPoint)

        Return (v.Angle / math.Pi) * 180

    End Function

    '*******************************************************************************************

    Sub RedoPoly(poly As Polyline, l As Double, a As Double)

        ' change the polyline lenght, according to the user request
        'l = lenght    a = angle (°)

        ' the news end points ; p1 don't change, it stay the firts point of the polyline at the same location

        Dim p1 As Point3F = New Point3F, p2 As Point3F = New Point3F

        p1 = poly.FirstPoint'catch the firts point of the polyline

        'calculate new lenght

        Dim newlx As Double
        Dim newly As Double

        'convert to radians
        a = DegToRad(a)

        'x lenght
        newlx = l * Math.Cos(a)

        'y lenght
        newly = l * Math.Sin(a)

        'new coord for p2
        p2.X = p1.X + newlx
        p2.Y = p1.Y + newly

        
        'create and add the new polyline
        AddPoly(p1, p2)

'and remove the old polyline (in this order because if not, after the line is created, the snap point
' to the last created line is not working until a new line is created, or removed ..)
'
        CamBamUI.MainUI.ActiveView.CADFile.RemovePrimitive(poly)

    End Sub

    '*******************************************************************************************

    Function DegToRad(angle As Double) As Double
        'convert degré to radians
        Return angle * Math.PI / 180
    End Function

    '*******************************************************************************************

    Function AddPoly(p1 As Point3F, p2 As Point3F) As Polyline

        Dim poly As Polyline = New Polyline

        poly.Add(p1)
        poly.Add(p2)

        'display the polyline
        CamBamUI.MainUI.ActiveView.CADFile.Add(poly)
        CamBamUI.MainUI.ActiveView.RefreshView()

        Return poly

    End Function

    '*******************************************************************************************

    Sub RuberLine()

        Dim edmode As New MeasureEditMode(CamBamUI.MainUI.ActiveView)
        edmode.DefaultValue = vbNull
        edmode.Prompt = "Cliquer du bouton du milieu ou Escape pour terminer"

        AddHandler edmode.OnReturnOK, AddressOf point_clicked
        AddHandler edmode.OnReturnCancel, AddressOf point_clicked_clr

        CamBamUI.MainUI.ActiveView.SetEditMode(edmode)
CamBamUI.MainUI.ActiveView.RepaintEditMode()

        'wait for edit mode finished (or ESC)
        Do While abort = False
            System.Windows.Forms.Application.DoEvents()
        Loop

        RemoveHandler edmode.OnReturnOK, AddressOf point_clicked
        RemoveHandler edmode.OnReturnCancel, AddressOf point_clicked_clr

    End Sub

'*******************************************************************************************

++
David

Edit: plugin version added.
« Last Edit: January 05, 2016, 15:49:48 pm by dh42 »

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1812
    • View Profile
Re: Sized Lines Script
« Reply #1 on: April 12, 2015, 23:07:05 pm »
HI David
Good Idea sometimes I miss the ability to do something like this, In a entry line at the bottom of the screen
like autocad and other cads I've worked with.
type --->   @200 < 90 and you get a line 200 long at 90 deg.
I don't know if its worth exploring but if you pop up a entry box and parse the input you may not need the editmode at all. Just thinking out aloud. ;D
Dave

Offline Garyhlucas

  • CNC Jedi
  • *****
  • Posts: 1459
    • View Profile
Re: Sized Lines Script
« Reply #2 on: April 13, 2015, 00:28:21 am »
David,
What I would really like to see is direct numeric input of the first point, as well as snapping it. That would be a huge improvement to me.
Gary H. Lucas

Have you read my blog?
 http://a-little-business.blogspot.com/

Offline klystron

  • Ewok
  • *
  • Posts: 36
    • View Profile
Re: Sized Lines Script
« Reply #3 on: April 13, 2015, 13:49:27 pm »
Salut David,
ton script pour dessiner une droite avec sa longueur et un angle , fonctionne bien .
Yves .

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7378
    • View Profile
    • Cambam V1.0 French Doc
Re: Sized Lines Script
« Reply #4 on: April 13, 2015, 13:52:18 pm »
Hello,

@Dave: Yes no editmode nedeed with this method, it's an idea ;)

I try in my old autocad,  it seems it don't know this formulation with angle ; I can do something like @ 100,20 that give the coordinates x,y for a point (relative to the last point) or without the @ that give absolutes coordinate.

@Gary, currently I'm not able to mix both ; the measureeditmode that I use need 2 clicked points ; I can't give a point by coordinates Or with mouse …that need a custom edit mode that I can't get working yet L .. but maybe I can do 2 differents tool until I found a way to mix it

++
David

Offline klystron

  • Ewok
  • *
  • Posts: 36
    • View Profile
Re: Sized Lines Script
« Reply #5 on: April 13, 2015, 14:03:00 pm »
Re salut David,
oui si on pouvait lui entrer les coordonnées du départ de la droite ( X... Y ... ) au lieu de la faire avec la souris ?.
cela serait plus précis .
c'est peut-être prévu a la prochaine version ?
si non c'est sympa .
Yves.

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7378
    • View Profile
    • Cambam V1.0 French Doc
Re: Sized Lines Script
« Reply #6 on: April 13, 2015, 18:07:26 pm »
Hello,

Quote
David,
What I would really like to see is direct numeric input of the first point, as well as snapping it. That would be a huge improvement to me.

Ah ah, I think I can do that ... numerical or mouse input for each point, abs or incr XY values OR length/angle depending of the syntax

currently the command line interpreter is working as following:

@20.2,10 -> X Y, incremental coords from the last point or 0,0 if first point

20.2,10 -> absolute coords

100<30 -> length / angle starting from the last point or 0,0 if first given point

P -> switch to mouse click to enter the point

Maybe it will be usable soon ...  ;)

++
David

Offline Bubba

  • CNC Jedi
  • *****
  • Posts: 3353
    • View Profile
Re: Sized Lines Script
« Reply #7 on: April 13, 2015, 20:06:39 pm »
David, it is possible to incorporate the dotted line somewhere(as reference line)? I could use sometimes. Thanks.

Keep on the good work! :D ;D
My 2¢

Win11, CB(1.0)rc 1(64 bit) Mach3, ESS, G540, 4th Axis, Endurance Laser.

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1812
    • View Profile
Re: Sized Lines Script
« Reply #8 on: April 13, 2015, 21:56:00 pm »
HI David
Fantastic Looks like you have moved pretty fast.
Look forward to seeing it.
Dave.

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7378
    • View Profile
    • Cambam V1.0 French Doc
Re: Sized Lines Script
« Reply #9 on: April 13, 2015, 22:15:42 pm »
Re

Quote
David, it is possible to incorporate the dotted line somewhere(as reference line)? I could use sometimes. Thanks.

With Eddy's Mtext object method or with multiple small lines ?

Quote
Fantastic Looks like you have moved pretty fast.

Ah ah .. internet is brocken all the days up to 22:00/23:00 (here it's 00:40::) ... and I've no job to do currently ... then I can spend time to program  ;D

the pb is that I can't access to the VB doc when internet is broken ..  :'(

++
David

Offline Bubba

  • CNC Jedi
  • *****
  • Posts: 3353
    • View Profile
Re: Sized Lines Script
« Reply #10 on: April 13, 2015, 22:21:28 pm »
Mtext object method?
*************
What is it, I guess I must have miss it :-[
My 2¢

Win11, CB(1.0)rc 1(64 bit) Mach3, ESS, G540, 4th Axis, Endurance Laser.

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7378
    • View Profile
    • Cambam V1.0 French Doc
Re: Sized Lines Script
« Reply #11 on: April 13, 2015, 22:32:52 pm »
re

it's like on MSDOS, line are created with a text like ------ or -.-.-.  etc ...

the advantage is that it's an unique object, the limits is that you can't snap on it and it can only be used on straight lines.(and it need also the modified stick fonts installed)

http://www.cambam.co.uk/forum/index.php?topic=4297.msg35371#msg35371

I can create a separate utilities to draw this kind of lines, or maybe to convert existing lines to dashed lines after they as been created as normal polyline .. for me it's more easy to do than mix polylines and Mtext line in the same code ..

++
David
« Last Edit: April 13, 2015, 22:36:15 pm by dh42 »

Offline Garyhlucas

  • CNC Jedi
  • *****
  • Posts: 1459
    • View Profile
Re: Sized Lines Script
« Reply #12 on: April 14, 2015, 01:00:40 am »
David,
Your data entry method is consistent with the way it works in AutoCad, so I like it. (your script, not AutoCad!)
Gary H. Lucas

Have you read my blog?
 http://a-little-business.blogspot.com/

Offline Bubba

  • CNC Jedi
  • *****
  • Posts: 3353
    • View Profile
Re: Sized Lines Script
« Reply #13 on: April 14, 2015, 02:07:15 am »
it's like on MSDOS, line are created with a text like ------
********************
 That would work! :D
My 2¢

Win11, CB(1.0)rc 1(64 bit) Mach3, ESS, G540, 4th Axis, Endurance Laser.

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5253
  • Made in England
    • View Profile
Re: Sized Lines Script
« Reply #14 on: April 14, 2015, 07:32:54 am »
re

it's like on MSDOS, line are created with a text like ------ or -.-.-.  etc ...

the advantage is that it's an unique object, the limits is that you can't snap on it and it can only be used on straight lines.(and it need also the modified stick fonts installed)
++
David

I decided those types of lines might look okay but in practice they are of little use.
Filmed in Supermarionation