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()
        Label2.Text = Form1.kooEdelta.ToString
        Label3.Text = Form1.kooNdelta.ToString
        Label4.Text = Form1.kooFaktor.ToString
    End Sub

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

    Public Sub DrawLineMer(ByVal xPos As Single, ByVal yPos As Single, ByVal xPos2 As Single, ByVal yPos2 As Single)
        g.DrawLine(Pens.Black, 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
        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 = ""

        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 = 2
            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 = 1
                        ElseIf dataInfo = My.Settings.KodaUserImport Then
                            measuredPoints = 2
                        ElseIf dataInfo = My.Settings.KodaMeasOri Then
                            measuredPoints = 3
                        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 = 1 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 = 1 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 = 1 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 = 1 Then
                        '    Form1.RichTextBox1.AppendText(dataStolpci + data + dataStolpci + "MEAS")
                        'ElseIf measuredPoints = 2 Then
                        '    Form1.RichTextBox1.AppendText(dataStolpci + data + dataStolpci + "REF")
                        'ElseIf measuredPoints = 3 Then
                        '    Form1.RichTextBox1.AppendText(dataStolpci + data + dataStolpci + "ORI")
                        '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 + data + dataStolpci + "STA")
                        stojiceSt = stojiceSt + 1
                        meritevSt = 0
                        g.DrawRectangle(Pens.Red, CInt(staX) + 30, CInt(staY) + 30, 5, 5)
                        g.DrawString(imeToc, DefaultFont, Brushes.Red, CInt(staX) + 35, CInt(staY) + 35)
                    Else
                        numData = 0
                        data = ""
                    End If
                End If
                If stojiceSt > 0 And meritevSt > 0 Then
                    Label1.Text = stojiceSt.ToString
                    'DrawLineMer(CInt(staX), CInt(staY), CInt(merX), CInt(merY))
                    g.DrawRectangle(Pens.Blue, CInt(merX) + 30, CInt(merY) + 30, 3, 3)
                    g.DrawString(imeToc, DefaultFont, Brushes.Blue, CInt(merX) + 33, CInt(merY) + 33)
                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