Public Class CL2GcodeII
    'Public X, Y, Z, I, J, K, R, F, oldF As Double
    Public X, Y, Z, I, J, K, R, F, oldF As String
    Public RLn As Integer = 0
    Public WLn As Integer = 1
    Public Currentline As Integer = 1
    Public CurrentString As String = ""
    Public LineBuffer(8) As String
    'Public JJ As Integer
    Public BP As Integer = 1
    Public BMaxP As Integer = 8
    Public S, T, G As Integer
    Public Direction As Integer
    Public Gcode As String
    Public WriteCode As Integer
    Public inFileName As String = "ncvout.cl"
    Public outFileName As String = "Gcode.txt"
    Public inPath As String = "C:\Program Files\procad\Verify\"
    Public outPath As String = "C:\Users\George\Documents\Procad-CL\"
    Public fileReader As System.IO.StreamReader
    Public stringReader As String
    Public stringWriter As String


    Public Sub bPost_Click(sender As System.Object, e As System.EventArgs) Handles bPost.Click
        bPost.Enabled = False
        bPost.Text = "wait"
        'Dim fileReader As System.IO.StreamReader
        Dim fileWriter As System.IO.StreamWriter
        Dim saveFileDialog1 As New SaveFileDialog()

        Try
            saveFileDialog1.Filter = "NC File|*.nc|Text File|*.txt|CNC File|*.cnc"
            saveFileDialog1.Title = "Save Gcode File"
            saveFileDialog1.AddExtension = True
            saveFileDialog1.DefaultExt = "nc"
            saveFileDialog1.InitialDirectory = outPath
            saveFileDialog1.ShowDialog()
            If saveFileDialog1.FileName <> "" Then
                outPath = GetPathPart(saveFileDialog1.FileName)
                outFileName = GetFileNamePart(saveFileDialog1.FileName)
            End If

            System.IO.File.Delete(outPath & outFileName)
            fileReader = My.Computer.FileSystem.OpenTextFileReader(inPath & inFileName)
            fileWriter = My.Computer.FileSystem.OpenTextFileWriter(outPath & outFileName, True)
            Dim stringReader As String
            Dim stringWriter As String
            'Read in an 8 line buffer
            'if there arent 8 lines BmaxP will be less than 8
            Try
                For JJ = 1 To 8
                    stringReader = fileReader.ReadLine()
                    LineBuffer(JJ) = stringReader
                    BMaxP = JJ
                Next

            Catch ex As Exception
                MsgBox("Create 8 Line Buffer, something went wrong in Buffer Input " & vbCrLf & ex.Message)
            End Try
            RLn = RLn + 7
            While (fileReader.EndOfStream = False)
                stringReader = fileReader.ReadLine()
                'Shift the Buffer down one line
                For JJ = 0 To 7
                    LineBuffer(JJ) = LineBuffer(JJ + 1)
                Next
                'set the last buffer to the current line read
                LineBuffer(8) = stringReader
                'process the 1sat buffered line
                WriteCode = ParseLine(LineBuffer(0), 4)
                RLn = RLn + 1
                stringWriter = LineBuffer(0)
                If WriteCode = 1 Then
                    fileWriter.WriteLine(Gcode)
                    WLn = WLn + 1
                End If
            End While
            While LineBuffer(0) <> ""
                'no more lines to read, so process just from the buffer
                For JJ = 0 To 7
                    LineBuffer(JJ) = LineBuffer(JJ + 1)
                Next
                LineBuffer(8) = ""
                BMaxP = BMaxP - 1
                WriteCode = ParseLine(LineBuffer(0), 4)
                stringWriter = LineBuffer(0)
                If WriteCode = 1 Then
                    fileWriter.WriteLine(Gcode)
                    WLn = WLn + 1
                End If
            End While

            fileWriter.WriteLine("M30")
            fileReader.Close()
            fileWriter.Close()
            bQuit.Enabled = True
            bPost.Text = "Done"
            MsgBox(outPath & outFileName & " Lines read = " & RLn & " Lines write = " & WLn)
        Catch ex As Exception
            MsgBox("I ran into an error" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Function ParseLine(CLLine As String, nDigits As Integer)
        Dim tLine As String
        Dim CMD As String
        Dim temp As String
        Dim FC As Integer
        Dim TLS As Integer
        'RLn = RLn + 1
        TLS = InStr(CLLine, " ")
        tLine = Microsoft.VisualBasic.Right(CLLine, Len(CLLine) - TLS)
        CMD = UCase(Trim(Microsoft.VisualBasic.Left(tLine, 6)))
        Select Case CMD
            Case "LOADTL"
                T = Val(Mid(tLine, 8, Len(tLine) - 7))
                ParseLine = 1
                Gcode = "M06 T" & T
                oldF = "0."
            Case "SPINDL"
                temp = Trim(UCase(Mid(tLine, 8, 4)))
                If temp = "CLW" Then
                    Direction = 3
                Else
                    Direction = 4
                End If
                S = Val(UCase(Mid(tLine, 13, Len(tLine) - 12)))
                ParseLine = 1
                Gcode = "M" & Direction & " S" & S
                oldF = "0."
            Case "RAPID"
                G = 0
                ParseLine = 0
            Case "FEDRAT"
                G = 1
                F = Round(Val(UCase(Mid(tLine, 13, Len(tLine) - 12))), nDigits)
                ParseLine = 0
            Case "GOTO"
                'trime off the un-need left most 8 characters
                temp = Microsoft.VisualBasic.Right(tLine, Len(tLine) - 8)
                'Find X
                'Find where the first "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                X = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Y
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                Y = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Z
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                ''Z = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                Z = Round(Val(temp), nDigits)
                'Trim off the Left uo to the ","
                ''temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find I
                'Find where the "," is
                '' FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                '' I = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                '' temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find J
                'Find where the "," is
                '' FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                '' J = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                '' temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find K
                'Find where the "," is
                '' FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                'K = Val(Microsoft.VisualBasic.Left(temp, FC - 1))
                ''  K = Round(Val(temp), nDigits)
                'Trim off the Left uo to the ","
                'temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                ParseLine = 0
                If ParseGoto(tLine, nDigits) = 1 Then
                    ParseLine = 1
                    If G <> 0 Then G = 1
                    Gcode = "G" & G & " X" & X & " Y" & Y & " Z" & Z
                    If G = 1 Then
                        If oldF <> F Then
                            Gcode = Gcode & " F" & F
                            oldF = F
                        End If
                    End If
                End If

            Case "CIRCLE"
                'trime off the un-need left most 8 characters
                temp = Microsoft.VisualBasic.Right(tLine, Len(tLine) - 8)
                'Find X
                'Find where the first "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                X = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Y
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                Y = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Z
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                Z = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find I
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                I = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find J
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                J = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find K
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                K = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find R
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                'K = Val(Microsoft.VisualBasic.Left(temp, FC - 1))
                R = Round(Val(temp), nDigits)

                '*******************************
                'Need to re-write for Buffer use
                'get next GOTO for End of Circle
                For JJ = 0 To 7
                    LineBuffer(JJ) = LineBuffer(JJ + 1)
                Next
                'Read in One more line to the end of the buffer
                'Can the Get a Line be made into a Function or Sub???
                Try
                    LineBuffer(8) = fileReader.ReadLine()
                Catch ex As Exception
                    MsgBox("I ran into an error" & vbCrLf & ex.Message)
                End Try
                CLLine = LineBuffer(0)
                RLn = RLn + 1
                '***************************
                TLS = InStr(CLLine, " ")
                tLine = Microsoft.VisualBasic.Right(CLLine, Len(CLLine) - TLS)
                CMD = UCase(Trim(Microsoft.VisualBasic.Left(tLine, 6)))
                'trime off the un-need left most 8 characters
                temp = Microsoft.VisualBasic.Right(tLine, Len(tLine) - 8)
                'Find X
                'Find where the first "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                X = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Y
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                Y = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Z
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                ''Z = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                Z = Round(Val(temp), nDigits)
                'Trim off the Left uo to the ","
                ''temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)
                '********************************************************

                ParseLine = 1
                If K = 1 Then G = 3
                If K = -1 Then G = 2
                Gcode = "G" & G & " X" & X & " Y" & Y & " Z" & Z & " R" & R
                If oldF <> F Then
                    Gcode = Gcode & " F" & F
                    oldF = F
                End If

            Case Else
                ParseLine = 0
        End Select
    End Function

    Public Function Round(nn As Double, nDigits As Integer) As String
        Round = Int(nn * 10 ^ nDigits + 0.5) / (10 ^ nDigits)
        'edit 3-9-2018 - no need to test value. just check for missing decimal point
        'If ((Val(Round) >= 0) Or (Val(Round) < 0)) Then
        If InStr(Round, ".") = 0 Then
            Round = Round & "."
        End If
        'End If
    End Function

    Private Sub bQuit_Click(sender As System.Object, e As System.EventArgs) Handles bQuit.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "NC File|*.nc|Text File|*.txt|CNC File|*.cnc"
        saveFileDialog1.Title = "Save Gcode File"
        saveFileDialog1.AddExtension = True
        saveFileDialog1.DefaultExt = "nc"
        saveFileDialog1.InitialDirectory = outPath
        saveFileDialog1.ShowDialog()

        ' If the file name is not an empty string open it for saving.
        If saveFileDialog1.FileName <> "" Then
            outPath = GetPathPart(saveFileDialog1.FileName)
            outFileName = GetFileNamePart(saveFileDialog1.FileName)
        End If


        'Dim BrowseFolder As New FolderBrowserDialog
        'BrowseFolder.SelectedPath = outPath
        'BrowseFolder.ShowDialog()
        'outPath = BrowseFolder.SelectedPath
        '
        'Dim message, title, defaultValue As String
        ' Dim myValue As Object
        ' Set prompt.
        'message = "Enter a New File Name"
        ' Set title.
        'title = "CL2Gcode File Name"
        'defaultValue = outFileName   ' Set default value.

        ' Display message, title, and default value.
        'myValue = InputBox(message, title, defaultValue)
        ' If user has clicked Cancel, set myValue to defaultValue 
        'If myValue <> "" Then outFileName = myValue

    End Sub

    Private Function GetFileNamePart(ByRef aFilename As String)
        Dim LF As Integer
        Dim FTemp As String
        Dim LSlash As Integer
        FTemp = aFilename
        LF = Len(FTemp)
        LSlash = InStrRev(FTemp, "\")
        If LSlash <> 0 Then
            FTemp = Mid(FTemp, LSlash + 1, LF - LSlash)
        End If
        Return FTemp
    End Function

    Private Function GetPathPart(ByRef aFilename As String)
        Dim LF As Integer
        Dim FTemp As String
        Dim LSlash As Integer
        FTemp = aFilename
        LF = Len(FTemp)
        LSlash = InStrRev(FTemp, "\")
        If LSlash <> 0 Then
            FTemp = Mid(FTemp, 1, LSlash)
        End If
        Return FTemp
    End Function

    Public Function ParseGoto(ByVal CLLine As String, ByVal nDigits As Integer)
        Dim tLine As String
        Dim CMD As String
        Dim temp As String
        Dim FC As Integer
        Dim TLS As Integer

        'Parse the the command 
        TLS = InStr(CLLine, " ")
        tLine = Microsoft.VisualBasic.Right(CLLine, Len(CLLine) - TLS)
        CMD = UCase(Trim(Microsoft.VisualBasic.Left(tLine, 6)))
        Select Case CMD

            Case "GOTO"
                'trime off the un-need left most 8 characters
                temp = Microsoft.VisualBasic.Right(tLine, Len(tLine) - 8)
                'Find X
                'Find where the first "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                X = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Y
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                Y = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Z
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                ''Z = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                Z = Round(Val(temp), nDigits)
                'Trim off the Left uo to the ","
                ''temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find I
                'Find where the "," is
                '' FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                '' I = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                '' temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find J
                'Find where the "," is
                '' FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                '' J = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                '' temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find K
                'Find where the "," is
                '' FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                'K = Val(Microsoft.VisualBasic.Left(temp, FC - 1))
                ''  K = Round(Val(temp), nDigits)
                'Trim off the Left uo to the ","
                'temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                ParseGoto = 1
                If G <> 0 Then G = 1
                Gcode = "G" & G & " X" & X & " Y" & Y & " Z" & Z
                If G = 1 Then
                    If oldF <> F Then
                        Gcode = Gcode & " F" & F
                        oldF = F
                    End If
                End If
                'End Goto

            Case "CIRCLE"
                'trime off the un-need left most 8 characters
                temp = Microsoft.VisualBasic.Right(tLine, Len(tLine) - 8)
                'Find X
                'Find where the first "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                X = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Y
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                Y = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Z
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                Z = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find I
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                I = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find J
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                J = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find K
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                K = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find R
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                'K = Val(Microsoft.VisualBasic.Left(temp, FC - 1))
                R = Round(Val(temp), nDigits)

                '*******************************
                'Need to re-write for Buffer use
                'get next GOTO for End of Circle
                For JJ = 0 To 7
                    LineBuffer(JJ) = LineBuffer(JJ + 1)
                Next
                'Read in One more line to the end of the buffer
                'Can the Get a Line be made into a Function or Sub???
                Try
                    LineBuffer(8) = fileReader.ReadLine()
                Catch ex As Exception
                    MsgBox("I ran into an error" & vbCrLf & ex.Message)
                End Try
                CLLine = LineBuffer(0)
                RLn = RLn + 1
                '***************************
                TLS = InStr(CLLine, " ")
                tLine = Microsoft.VisualBasic.Right(CLLine, Len(CLLine) - TLS)
                CMD = UCase(Trim(Microsoft.VisualBasic.Left(tLine, 6)))
                'trime off the un-need left most 8 characters
                temp = Microsoft.VisualBasic.Right(tLine, Len(tLine) - 8)
                'Find X
                'Find where the first "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                X = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Y
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                Y = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                'Trim off the Left uo to the ","
                temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)

                'Find Z
                'Find where the "," is
                FC = InStr(temp, ",")
                'Get the Value Of X up to the ","
                ''Z = Round(Val(Microsoft.VisualBasic.Left(temp, FC - 1)), nDigits)
                Z = Round(Val(temp), nDigits)
                'Trim off the Left uo to the ","
                ''temp = Microsoft.VisualBasic.Right(temp, Len(temp) - FC)
                '********************************************************

                ParseGoto = 1
                If K = 1 Then G = 3
                If K = -1 Then G = 2
                Gcode = "G" & G & " X" & X & " Y" & Y & " Z" & Z & " R" & R
                If oldF <> F Then
                    Gcode = Gcode & " F" & F
                    oldF = F
                End If
                'end Circle



        End Select
        ParseGoto = 0
    End Function

    Private Sub CL2GcodeII_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.Label1.Text = Chr(169) & " GSS 2018 " & "Version " & Application.ProductVersion
    End Sub
End Class
