Imports System.IO

Public Class FormGrafika
    Public g As Graphics

    Private Sub FormGrafika_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        g = Me.CreateGraphics()

    End Sub

    ' Gumb: Prikaži
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        GrafikaIzris()
        Form1.tsbSaveAs.Enabled = False
        Form1.ShraniToolStripMenuItem.Enabled = False
    End Sub

    ' Gumb: Zapri
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Dispose()
        Me.Close()
    End Sub

    Public Sub DrawLineMer(ByVal xPos As Single, ByVal yPos As Single, ByVal xPos2 As Single, ByVal yPos2 As Single)
        g.DrawLine(Pens.LightGray, New Point(xPos, yPos), New Point(xPos2, yPos2))

    End Sub

    Public Sub GrafikaIzris()
        Dim dataStolpci As String = ""
        If My.Settings.Stolpci = 0 Then
            dataStolpci = " "
        ElseIf My.Settings.Stolpci = 1 Then
            dataStolpci = vbTab
        End If

        Form1.RichTextBox1.Clear()
        Form1.RichTextBox1.BackColor = Color.PaleTurquoise
        Form1.RichTextBox1.Text = "Koordinate točk v grafiki..." + vbCrLf
        Dim streamReader As StreamReader = New StreamReader(Form1.OpenFileDialog1.FileName)
        Dim i As Integer = 0
        Dim lin As Integer = 0
        ' Pripravi variable za stojišče in merjene točke
        Dim measuredPoints As Integer = 0
        Dim stojiceSt As Integer = 0
        Dim meritevSt As Integer = 0
        Dim dataKoda As Integer = 0
        Dim staX As Double = 0
        Dim staY As Double = 0
        Dim merX As Double = 0
        Dim merY As Double = 0
        Dim imeToc As String = ""

        g.DrawRectangle(Pens.Gray, 5, 5, 1000, 1000)

        Do Until streamReader.EndOfStream
            Form1.ToolStripProgressBar1.Value = i
            ' Read and Split each line to separate it into fields
            Dim line() As String = streamReader.ReadLine().Split(" ")
            ' Remove * sign from index line
            line(0) = Replace(line(0), "*", "")
            If Microsoft.VisualBasic.Left(line(0), 2) = "41" Then
                dataKoda = 1
            Else
                dataKoda = 0
            End If

            If Microsoft.VisualBasic.Left(line(1), 2) = "25" Or Microsoft.VisualBasic.Left(line(1), 2) = "84" Then
                measuredPoints = 1
            End If
            ' Seaparate word index and data
            For Each readLine As String In line

                If readLine = "" Then
                    ' do nothing
                Else
                    ' wordindex is always 6 characters long and is read fron left to right
                    Dim wordIndex As String
                    Dim data As String
                    Dim numData As Double

                    wordIndex = Microsoft.VisualBasic.Left(readLine, 2)
                    data = Microsoft.VisualBasic.Right(readLine, 17)

                    If wordIndex = "41" Then
                        Dim dataInfo As String = Microsoft.VisualBasic.Right(readLine, 16).TrimStart("0"c)
                        If dataInfo = My.Settings.KodaMeas Then
                            measuredPoints = 2
                        ElseIf dataInfo = My.Settings.KodaMeasOri Then
                            measuredPoints = 3
                        ElseIf dataInfo = My.Settings.KodaUserImport Then
                            measuredPoints = 0
                        Else
                            measuredPoints = 0
                        End If
                    End If

                    If wordIndex = "11" And measuredPoints >= 1 Then
                        data = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                        imeToc = data
                        Form1.RichTextBox1.AppendText(data)
                        i = i + 1
                    ElseIf wordIndex = "81" And measuredPoints >= 2 Then    ' Target Easting
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If My.Settings.DecimalnoLocilo = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        merX = (numData - Form1.kooEMin) * Form1.kooFaktor
                        Form1.RichTextBox1.AppendText(dataStolpci + CInt(merX).ToString)
                    ElseIf wordIndex = "82" And measuredPoints >= 2 Then    ' Target Northing
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If My.Settings.DecimalnoLocilo = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        merY = (numData - Form1.kooNMin) * Form1.kooFaktor
                        Form1.RichTextBox1.AppendText(dataStolpci + CInt(merY).ToString)
                    ElseIf wordIndex = "83" And measuredPoints >= 2 Then    ' Target Height
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If My.Settings.DecimalnoLocilo = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        If measuredPoints = 2 Then
                            Form1.RichTextBox1.AppendText(dataStolpci + "MEAS")
                            g.DrawRectangle(Pens.Blue, CInt(merX) + 19, CInt(merY) + 19, 2, 2)
                            g.DrawString(imeToc, DefaultFont, Brushes.Blue, CInt(merX) + 22, CInt(merY) + 22)
                            ' ElseIf measuredPoints = 4 Then
                            '     Form1.RichTextBox1.AppendText(dataStolpci + data + dataStolpci + "REF")
                        ElseIf measuredPoints = 3 Then
                            Form1.RichTextBox1.AppendText(dataStolpci + "ORI")
                            g.DrawRectangle(Pens.DarkMagenta, CInt(merX) + 19, CInt(merY) + 19, 2, 2)
                            g.DrawString(imeToc, DefaultFont, Brushes.Magenta, CInt(merX) + 22, CInt(merY) + 22)
                            DrawLineMer(CInt(staX) + 20, CInt(staY) + 20, CInt(merX) + 20, CInt(merY) + 20)
                        End If
                        If stojiceSt > 0 Then
                            meritevSt = meritevSt + 1
                        End If
                    ElseIf wordIndex = "84" And measuredPoints = 1 Then    ' Station Easting
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If My.Settings.DecimalnoLocilo = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        staX = (numData - Form1.kooEMin) * Form1.kooFaktor
                        Form1.RichTextBox1.AppendText(dataStolpci + CInt(staX).ToString)
                    ElseIf wordIndex = "85" And measuredPoints = 1 Then    ' Station Northing
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If My.Settings.DecimalnoLocilo = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        staY = (numData - Form1.kooNMin) * Form1.kooFaktor
                        Form1.RichTextBox1.AppendText(dataStolpci + CInt(staY).ToString)
                    ElseIf wordIndex = "86" And measuredPoints = 1 Then    ' Station Height
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If My.Settings.DecimalnoLocilo = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        Form1.RichTextBox1.AppendText(dataStolpci + "STA")
                        stojiceSt = stojiceSt + 1
                        meritevSt = 0
                        Label1.Text = stojiceSt.ToString
                        g.DrawRectangle(Pens.DarkOrange, CInt(staX) + 18, CInt(staY) + 18, 4, 4)
                        g.DrawString(imeToc, DefaultFont, Brushes.DarkOrange, CInt(staX) + 25, CInt(staY) + 25)
                    Else
                        numData = 0
                        data = ""
                    End If
                End If
                ' If stojiceSt > 0 And meritevSt > 0 Then
                ' 
                '     'DrawLineMer(CInt(staX), CInt(staY), CInt(merX), CInt(merY))
                ' End If
            Next


            If dataKoda = 0 And measuredPoints >= 1 Then
                Form1.RichTextBox1.AppendText(vbCrLf)
            End If
            lin = lin + 1
            Form1.ToolStripProgressBar1.Value = lin
        Loop
        streamReader.Close()

    End Sub

End Class