Author Topic: Numeric Polyline Script  (Read 25163 times)

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7371
    • View Profile
    • Cambam V1.0 French Doc
Re: Sized Lines Script
« Reply #15 on: April 18, 2015, 20:18:12 pm »
Hello,

And a new version

 @x;y  = incremental mode
 x;y   = absolute mode
 *     = replace x or y with the last x or y coord used
 l<a   = lenght / angle mode
 M     = switch to mouse clic
 R     = remove last point
 C     = close polyline and exit
other entry terminate the drawing

You can change the SEP constant if you want another separator for XY ... I use ";" because on Europa "," is mostly used for decimal separator ..


I'll translate in plugin when time permit ;)

++
David


Code: [Select]
'*****************************************************************************
'VB script Numerical Lines
'dh42 2015

' @x;y  = incremental mode
' x;y   = absolute mode
' *     = replace x or y with the last x or y coord used
' l<a   = lenght / angle mode
' M     = switch to mouse clic
' R     = remove last point
' C     = close polyline and exit
'other entry terminate the drawing

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

Dim cmd_string As String
Dim current_pt As Point2F, previous_pt As Point2F
Dim lgt As Double, ang As Double
Dim absolute As Boolean

Dim canceled As Boolean 'canceled by ESC
Dim finished As Boolean
Dim removed As Boolean  'a point as been removed

Dim FirstPoint As Boolean   'Is the first created point ?

Const SEP = ";" 'char used for x y separator

Const XPOS = 100    'msgbox position
Const YPOS = 80


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

Sub main()

Dim input_info As String    'text for the inputbox
Dim ret As Boolean = True ' function return

absolute = True
previous_pt.X = 0
previous_pt.Y = 0
FirstPoint = True
removed = False

'create a undo point for the active layer
CamBamUI.MainUI.UndoBuffer.AddUndoPoint("NumLine")
CamBamUI.MainUI.UndoBuffer.Add(CamBamUI.MainUI.ActiveView.CADFile.ActiveLayer.Entities)

'a pointlist with an unique point used to show the position on the screen
Dim DisplayPoint As PointList = New PointList
DisplayPoint.add(0, 0, 0)

'add the object to the doc
CamBamUI.MainUI.ActiveView.CADFile.Add(DisplayPoint)

'the polyline to create
Dim poly As Polyline = New Polyline
poly.Closed = False

Do While ret = True
'get command string

'create the text with last position
input_info = "last X = " & previous_pt.X & VbNewline & _
"last Y = " & previous_pt.Y

cmd_string = InputBox(input_info, "Numerical Lines", "", XPOS, YPOS)

' START THE CONVERSION OF THE COMMAND LINE
ret = Str_to_Point(poly)

If ret = True Then   'cmd string OK

UpdatePoint(DisplayPoint)   'update the position of the display point

If FirstPoint = True Then    'first entry ; set a undo point and create the polyline object

poly.Add(current_pt.X, current_pt.Y, 0)   'the first point
CamBamUI.MainUI.ActiveView.CADFile.Add(poly)    'add the polyline object to the doc
FirstPoint = False
Else    'it's not the first point entered
If removed = False Then
UpdatePolyline(poly)
End If
End If

If removed = False Then
'retain the last position
previous_pt.X = current_pt.X
previous_pt.Y = current_pt.Y
Else
removed = False
UpdatePoint(DisplayPoint)
End If

End If

Loop

'remove the diplay point object
CamBamUI.MainUI.ActiveView.CADFile.RemovePrimitive(DisplayPoint)

'remove the polyline if it contain no segment
if poly.Points.Count < 2 then
CamBamUI.MainUI.ActiveView.CADFile.RemovePrimitive(poly)
End If

CamBamUI.MainUI.ActiveView.UpdateViewport()

End Sub

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

Function Str_to_Point(p As Polyline) As Boolean

' read the command line and extract the datas
' return true if commande is correct, false if not
' return false if string ="" -> end of the job

Dim cmd As String 'a temp string
Dim conv_return As Boolean = False

If cmd_string = "" Then Return False 'exit function, drawing aborted

'remove spaces
cmd = Trim(cmd_string)

If Left(cmd, 1) = "@" Then
absolute = False
cmd = Right(cmd, Len(cmd) - 1)  'remove the @
Else
absolute = True
End If

'mouse click asked (m or M)
If Left(cmd, 1) = "m" Or Left(cmd, 1) = "M" Then
If GetOnePointByMouse() = True Then  'catch a point by mouse
cmd_string = ""
Return True
Else
Return False    'canceled by mouse
End If
End If

'remove last point (r or R)
If Left(cmd, 1) = "r" Or Left(cmd, 1) = "R" Then
RemovePoint(p)
cmd_string = ""
Return True
End If

'close and end(c or C)
If Left(cmd, 1) = "c" Or Left(cmd, 1) = "C" Then
p.Closed = True
p.Update()
cmd_string = ""
Return False
End If


'detect if coordinates or lenght/angle
If InStr(cmd, SEP) <> 0 Then    'coord
conv_return = Convert_to_values(cmd, 1)
If conv_return = True Then
cmd_string = ""
Return True
Else
Return False
End If
Else    'lenght/angle
conv_return = Convert_to_values(cmd, 2)
If conv_return = True Then
cmd_string = ""
Return True
Else
Return False
End If
End If

End Function

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

Function Convert_to_values(command As String, mode As Integer) As Boolean

Dim s As String 'char scanned
Dim separator As String = ""
Dim coord_string As String = "" 'the coordinate as string
Dim a As Double, b As Double

Dim ct As Integer

Select Case mode
Case 1
separator = SEP 'coordinates
Case 2
separator = "<" 'lenght, angle
End Select

'search for separator char
For ct = 0 To Len(command) - 1
s = command.Chars(ct)

If s <> separator Then
coord_string = coord_string & s
Else    'separator found
If IsNumeric(coord_string) Then
a = Val(coord_string)   'convert to double
Exit For
Else
If coord_string = "*" Then 'use last X absolute position
If absolute = True Then
a = previous_pt.X
Else
a = 0   'in inc mode, * = no change
End If
Exit For
Else
Return False    'bad syntax, not a number or * for a
End If
End If
End If
Next ct

coord_string = ""

'read for b
For ct2 As Integer = ct + 1 To Len(command) - 1
s = command.Chars(ct2)
coord_string = coord_string & s
Next ct2

If IsNumeric(coord_string) Then
b = Val(coord_string)   'convert to double
Else
If coord_string = "*" Then 'use last Y absolute position
If absolute = True Then
b = previous_pt.Y
Else
b = 0   'in inc mode, * = no change
End If
Else
Return False    'bad syntax, not a number for b
End If
End If

' terminate ok
' calcultae the new point
Select Case mode
Case 1  'x;y mode

If absolute = True Then 'absolute value
current_pt.X = a
current_pt.Y = b
Else                    'incremental
current_pt.X = a + previous_pt.X
current_pt.Y = b + previous_pt.Y
End If

Case 2  'len<ang mode
lgt = a
ang = b

PointFromLenAng()

End Select

Return True

End Function

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

Sub UpdatePoint(ByRef pl As PointList)

'update display point datas
pl.Points.RemoveAt(0)   'remove the first point in the point list
pl.Points.Add(current_pt.X, current_pt.Y)   'replace with a point with the new values
pl.Update()

CamBamUI.MainUI.ActiveView.RefreshView()

End Sub

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

Sub UpdatePolyline(ByRef p As Polyline)

'update the polyline
p.Add(current_pt.X, current_pt.Y, 0)
p.Update()

CamBamUI.MainUI.ActiveView.RefreshView()

End Sub

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

Sub RemovePoint(ByRef p As Polyline)
'remove the last point entered

If p.Points.Count > 0 Then  'if at least one point is in the polyline
p.Points.RemoveAt(p.Points.Count - 1)   'remove the last point
p.Update()

CamBamUI.MainUI.ActiveView.RefreshView()

If p.Points.Count >= 1 Then  'if at least on point remain in the polyline

previous_pt.X = p.Points.Item(p.Points.Count - 1).Point.X
previous_pt.Y = p.Points.Item(p.Points.Count - 1).Point.Y
Else
previous_pt.X = 0
previous_pt.Y = 0

End If

If p.Points.Count = 0 Then
current_pt.X = 0
current_pt.Y = 0
Else
current_pt.X = p.Points.Item(p.Points.Count - 1).Point.X
current_pt.Y = p.Points.Item(p.Points.Count - 1).Point.Y

End If

End If

removed = True

End Sub

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

Sub PointFromLenAng()

' lgt = lenght    ang = angle (°) double
' previous_pt.X, previous_pt.Y -> the start point

'calculate new point

Dim newlx As Double
Dim newly As Double

'convert to radians
ang = ang * Math.PI / 180 'convert degré to radians

'x lenght
newlx = lgt * Math.Cos(ang)

'y lenght
newly = lgt * Math.Sin(ang)

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

End Sub

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

Function GetOnePointByMouse() As Boolean

finished = False
canceled = False

Dim edmode As New PointSelectEditMode(CamBamUI.MainUI.ActiveView)
edmode.DefaultValue = vbNull
edmode.Prompt = "Click a point"
AddHandler edmode.OnReturnOK, AddressOf point_clicked
AddHandler edmode.OnReturnCancel, AddressOf point_canceled

CamBamUI.MainUI.ActiveView.SetEditMode(edmode)  'run the point edit mode
CamBamUI.MainUI.ActiveView.RepaintEditMode()

Do While finished = False
'repeat until  the point is clicked
System.Windows.Forms.Application.DoEvents()
Loop

If canceled = False Then
Return True
Else
Return False
End If

End Function

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

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

Dim ClickedPoint As Point3F
ClickedPoint = sender.ReturnValue

current_pt.X = ClickedPoint.X
current_pt.Y = ClickedPoint.Y
finished = True

End Sub

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

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

canceled = True
finished = True

End Sub
« Last Edit: April 19, 2015, 21:07:29 pm by dh42 »

Offline klystron

  • Ewok
  • *
  • Posts: 36
    • View Profile
Re: Sized Lines Script
« Reply #16 on: April 18, 2015, 21:54:38 pm »
hello David,
je viens de regarder ta dernière version de ton script, je me suis amusé un peu avec, c'est un utilitaire qui est très intéressant, pour la construction .
maintenant il faut avoir l'habitude de s'en servir.
tu es un as en programmation Cambam, bravo.
Yves.

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7371
    • View Profile
    • Cambam V1.0 French Doc
Re: Sized Lines Script
« Reply #17 on: April 18, 2015, 22:05:31 pm »
Quote
tu es un as en programmation Cambam, bravo.

Je n'irais pas jusque la .... je bricole diront-nous .. ;D

++
David

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1807
    • View Profile
Re: Sized Lines Script
« Reply #18 on: April 18, 2015, 22:15:12 pm »
Hi David
Well done, this a a very useful script and I will use it.
Dave

Offline Bubba

  • CNC Jedi
  • *****
  • Posts: 3353
    • View Profile
Re: Sized Lines Script
« Reply #19 on: April 19, 2015, 00:46:23 am »
I decided those types of lines might look okay but in practice they are of little use.
*****************
 I beg to differ..
 In my experience in the shop environment when drawing complex parts in 2D dotted lines a must. I often find need for them doing my 4th axis design work. It let you visualize better rotated part. For me at least is much faster and easier to draw part in 2D vs solid because my skills of doing so are poor. If is difficult to incorporate dotted lines to the script, fine.. I survived without them. But IMHO it would be nice addition. I'm sure some here would find use for them.   
My 2¢

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

Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8938
    • View Profile
Re: Sized Lines Script
« Reply #20 on: April 19, 2015, 01:25:13 am »
I certainly find any documentation aids to be helpful.  CamBam is STRONG on capabilities, but not among them is proper drawing documentation.  This would be a boon.

Lloyd
"Pyro for Fun and Profit for More Than Fifty Years"

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5248
  • Made in England
    • View Profile
Re: Sized Lines Script
« Reply #21 on: April 19, 2015, 10:40:23 am »
I decided those types of lines might look okay but in practice they are of little use.
*****************
 I beg to differ..
 In my experience in the shop environment when drawing complex parts in 2D dotted lines a must. I often find need for them doing my 4th axis design work. It let you visualize better rotated part. For me at least is much faster and easier to draw part in 2D vs solid because my skills of doing so are poor. If is difficult to incorporate dotted lines to the script, fine.. I survived without them. But IMHO it would be nice addition. I'm sure some here would find use for them.   

Bubba, I agree with you that dotted construction lines are an excellent aid and I use them myself, but what I meant was, having the lines as 'text' like in the examples I did might not be useful. The main reasons being, they are not accurately placed and they can't be snapped to, or any other operation that polylines can do.
Filmed in Supermarionation

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7371
    • View Profile
    • Cambam V1.0 French Doc
Re: Sized Lines Script
« Reply #22 on: April 19, 2015, 15:25:14 pm »
Hello,

Bubba, as I said, I'll add something to do dashed line but not as a part of this plugin. That need a separate tool to be able to:

- dashed any shape (line, circle etc)
- select the dashed object without that you need to select each separate line one by one ... in other words, I need also to add the "group" tool I show on another topic to manage the dashed objects.

bee patient ;) ... first I'll finish the jobs in progress.

++
David

Offline Bubba

  • CNC Jedi
  • *****
  • Posts: 3353
    • View Profile
Re: Sized Lines Script
« Reply #23 on: April 19, 2015, 16:20:53 pm »
OK, Guys I got it! Thanks.;D
My 2¢

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

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7371
    • View Profile
    • Cambam V1.0 French Doc
Re: Sized Lines Script
« Reply #24 on: April 19, 2015, 20:08:33 pm »
https://www.youtube.com/watch?v=nKHXDNZcsvc

And I find a bug .... if we create a incremental line just after we have removed a point, the new point is in abs mode even if @ is used ... (but not each time .. ???)

++
David
« Last Edit: April 19, 2015, 20:23:30 pm by dh42 »

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7371
    • View Profile
    • Cambam V1.0 French Doc
Re: Sized Lines Script
« Reply #25 on: April 19, 2015, 21:09:55 pm »
re

bug corrected ; I do the change in the code in the previous message.

in Sub RemovePoint()  

If p.Points.Count > 1Then   'if at least on point remain in the polyline

replaced by

If p.Points.Count >= 1 Then  'if at least on point remain in the polyline

 ::) ::)

++
David

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7371
    • View Profile
    • Cambam V1.0 French Doc
Re: Numeric Polyline Script
« Reply #26 on: April 26, 2015, 16:20:50 pm »
Hello,

And the plugin version. It is in the Draw menu.

Now you can use either "," or ";" as separator for x,y

++
David
« Last Edit: June 17, 2015, 16:52:06 pm by dh42 »

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7371
    • View Profile
    • Cambam V1.0 French Doc
Re: Numeric Polyline Script
« Reply #27 on: June 17, 2015, 16:52:54 pm »

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5248
  • Made in England
    • View Profile
Re: Numeric Polyline Script
« Reply #28 on: December 04, 2015, 14:04:33 pm »
David,

Could you please change the dialogue box to be modeless ?

I think it needs;
promptDialog.ShowDialog()
to be;
promptDialog.Show()

It would be much more useful then by having it always in view.

Thanks.
Filmed in Supermarionation

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7371
    • View Profile
    • Cambam V1.0 French Doc
Re: Numeric Polyline Script
« Reply #29 on: December 04, 2015, 20:37:14 pm »
Hello Eddy,

I take a look, but I can't, the PromptDialog don't recognize the Show, only the ShowDialog ... :-\

To do that, I must create my own form to replace the CamBam build in dialog. (the build in VB InputBox() can't be set as non-modal too)


To be exact, the .Show can be used .... BUT in this case .... the Dialog can't return a value !

++
David
« Last Edit: December 04, 2015, 20:41:30 pm by dh42 »