Imports System.IO
Imports System.Runtime.InteropServices.ComTypes

Public Class Form1
    Public DatotekaLogfile As String = My.Settings.DatLogfile
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If DatotekaLogfile = "" Then
            DatotekaLogfile = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "TPS1100_zapisnik.txt")
            My.Settings.DatLogfile = DatotekaLogfile
            My.Settings.Save()
        End If
        My.Forms.Form1.Text = My.Application.Info.Title + " " + My.Application.Info.Version.Major.ToString + "." + My.Application.Info.Version.Minor.ToString
        RichTextBox1.AppendText(My.Settings.DatLogfile.ToString)

        If My.Settings.Pisava = "" Then
            My.Settings.Pisava = RichTextBox1.Font.Name
            My.Settings.PisavaVelikost = RichTextBox1.Font.Size
            My.Settings.Save()
        End If
        RichTextBox1.Font = New Font(My.Settings.Pisava, My.Settings.PisavaVelikost)
        NaloziNast()
        ToolStripLabel2.Text = My.Settings.Projekt
        If ToolStripLabel2.Text = "Virtualna Kartica" Then
            ToolStripLabel2.Image = My.Resources.icons8_micro_sd_50
        Else
            ToolStripLabel2.Image = My.Resources.icons8_browse_folder_50_2
        End If
    End Sub

    Public Sub NaloziNast()
        ' Uvoz nastavitev iz datototeke
        If My.Computer.FileSystem.FileExists("tps1100_kode.txt") Then
            Dim streamReader As StreamReader = New StreamReader("tps1100_kode.txt")
            Do Until streamReader.EndOfStream
                ' Read and Split each line to separate it into fields
                Dim line() As String = streamReader.ReadLine().Split("=")
                Dim kodaIme As String
                Dim kodaVal As String
                kodaIme = Replace(line(0), " ", "")
                kodaVal = Replace(line(1), " ", "")
                If kodaIme = "DecimalnoLocilo" Then
                    My.Settings.DecimalnoLocilo = kodaVal
                End If
                If kodaIme = "StolpciTab" Then
                    My.Settings.Stolpci = kodaVal
                End If
                If kodaIme = "KodaStojisce" Then
                    My.Settings.KodaStojisce = kodaVal
                End If
                If kodaIme = "KodaStojisceInfo" Then
                    My.Settings.KodaStojisceInfo = kodaVal
                End If
                If kodaIme = "KodaStojisceOri" Then
                    My.Settings.KodaStojisceOri = kodaVal
                End If
                If kodaIme = "KodaQuickSet" Then
                    My.Settings.KodaQuickSet = kodaVal
                End If
                If kodaIme = "KodaSetAzi" Then
                    My.Settings.KodaSetAzi = kodaVal
                End If
                If kodaIme = "KodaResection" Then
                    My.Settings.KodaResection = kodaVal
                End If
                If kodaIme = "KodaFreeStation" Then
                    My.Settings.KodaFreeStation = kodaVal
                End If
                If kodaIme = "KodaOriHtTrans" Then
                    My.Settings.KodaOriHtTrans = kodaVal
                End If
                If kodaIme = "KodaMeasOri" Then
                    My.Settings.KodaMeasOri = kodaVal
                End If
                If kodaIme = "KodaMeas" Then
                    My.Settings.KodaMeas = kodaVal
                End If
                If kodaIme = "KodaUserImport" Then
                    My.Settings.KodaUserImport = kodaVal
                End If
            Loop

            streamReader.Close()
            streamReader.Dispose()
            My.Settings.Save()

        Else
            ' Izvoz kodiranja v TXT datoteko
            Dim dataNast As String = ""

            dataNast = dataNast + "DecimalnoLocilo = " + My.Settings.DecimalnoLocilo + vbCrLf
            dataNast = dataNast + "StolpciTab = " + My.Settings.Stolpci.ToString + vbCrLf
            dataNast = dataNast + "KodaStojisce = " + My.Settings.KodaStojisce + vbCrLf
            dataNast = dataNast + "KodaStojisceInfo = " + My.Settings.KodaStojisceInfo + vbCrLf
            dataNast = dataNast + "KodaStojisceOri = " + My.Settings.KodaStojisceOri + vbCrLf
            dataNast = dataNast + "KodaQuickSet = " + My.Settings.KodaQuickSet + vbCrLf
            dataNast = dataNast + "KodaSetAzi = " + My.Settings.KodaSetAzi + vbCrLf
            dataNast = dataNast + "KodaResection = " + My.Settings.KodaResection + vbCrLf
            dataNast = dataNast + "KodaFreeStation = " + My.Settings.KodaFreeStation + vbCrLf
            dataNast = dataNast + "KodaOriHtTrans = " + My.Settings.KodaOriHtTrans + vbCrLf
            dataNast = dataNast + "KodaMeasOri = " + My.Settings.KodaMeasOri + vbCrLf
            dataNast = dataNast + "KodaMeas = " + My.Settings.KodaMeas + vbCrLf
            dataNast = dataNast + "KodaUserImport = " + My.Settings.KodaUserImport + vbCrLf

            My.Computer.FileSystem.WriteAllText("tps1100_kode.txt", dataNast, False)

        End If
    End Sub

    Public Sub OdpriDatotekoGsi()
        OpenFileDialog1.ReadOnlyChecked = False
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            RichTextBox1.Clear()
            ToolStripProgressBar1.Value = 0
            streamLines = 0
            Dim streamReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
            Do Until streamReader.EndOfStream
                RichTextBox1.AppendText(streamReader.ReadLine + vbCrLf)
                streamLines = streamLines + 1
            Loop
            ToolStripProgressBar1.Maximum = streamLines
            streamReader.Close()
            streamReader.Dispose()

            My.Settings.Datoteka = OpenFileDialog1.SafeFileName
            My.Settings.ZadnjaMapa = Path.GetDirectoryName(OpenFileDialog1.FileName)
            tsbFileName.Text = My.Settings.Datoteka
            tsbFileName.ToolTipText = OpenFileDialog1.FileName
            ToolStripStatusLabel1.Text = My.Settings.ZadnjaMapa
            tsbSaveAs.Text = "Shrani ..."
            tsbSaveAs.Enabled = False
            ShraniToolStripMenuItem.Enabled = False
            tsbConvert.Enabled = True
            tsbPointsOnly.Enabled = True
            ToolStripButtonGeo.Enabled = True
            ButtonUrediDat.Enabled = True
            RichTextBox1.BackColor = SystemColors.Info
            ' RichTextBox1.SelectionStart = 0
            LabelDatoteka.Image = My.Resources.icons8_ok_50
            My.Settings.Save()
            ' Else
            '     tsbFileName.Text = "Odpri GSI datoteko..."
            '     RichTextBox1.Clear()
            '     tsbSaveAs.Text = "Shrani ..."
            '     tsbSaveAs.Enabled = False
            '     ShraniToolStripMenuItem.Enabled = False
            '     tsbConvert.Enabled = False
            '     tsbPointsOnly.Enabled = False
        End If
        OpenFileDialog1.Dispose()
    End Sub

    Private Sub UvoziGsiJob()
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim delovnaMapa = My.Application.Info.DirectoryPath.ToString + "\" + My.Settings.ProjektMapa.ToString + "\"
            Dim fileUvoz = OpenFileDialog1.FileName
            Dim fileIme = Path.GetFileName(OpenFileDialog1.FileName)
            Dim fileIzvoz = delovnaMapa + fileIme
            My.Computer.FileSystem.CopyFile(fileUvoz, fileIzvoz, True)
            OpenFileDialog1.FileName = fileIzvoz

            RichTextBox1.Clear()
            ToolStripProgressBar1.Value = 0
            streamLines = 0
            Dim streamReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
            Do Until streamReader.EndOfStream
                RichTextBox1.AppendText(streamReader.ReadLine + vbCrLf)
                streamLines = streamLines + 1
            Loop
            ToolStripProgressBar1.Maximum = streamLines
            streamReader.Close()
            streamReader.Dispose()

            My.Settings.Datoteka = OpenFileDialog1.SafeFileName
            My.Settings.ZadnjaMapa = Path.GetDirectoryName(fileIzvoz)
            tsbFileName.Text = My.Settings.Datoteka
            ToolStripStatusLabel1.Text = fileUvoz.ToString + " -> " + fileIzvoz.ToString
            tsbSaveAs.Text = "Shrani ..."
            tsbSaveAs.Enabled = False
            ShraniToolStripMenuItem.Enabled = False
            tsbConvert.Enabled = True
            tsbPointsOnly.Enabled = True
            ToolStripButtonGeo.Enabled = True
            ButtonUrediDat.Enabled = True
            RichTextBox1.BackColor = SystemColors.Info
            ' RichTextBox1.SelectionStart = 0
            LabelDatoteka.Image = My.Resources.icons8_ok_50
            My.Settings.Save()
        End If

        OpenFileDialog1.Dispose()
    End Sub
    Public Sub PripraviDatotekoGsi()
        If OpenFileDialogTocke.ShowDialog = DialogResult.OK Then
            RichTextBox1.Clear()
            Dim streamReader As StreamReader = New StreamReader(OpenFileDialogTocke.FileName)
            Do Until streamReader.EndOfStream
                streamLines = streamLines + 1
                Dim readLine As String = streamReader.ReadLine().Replace(" ", vbTab)
                readLine = readLine.Replace(";", vbTab)
                readLine = readLine.Replace(",", ".")
                Dim line() As String = readLine.Split(vbTab)
                Dim podPtId As String = "*11" + Format(streamLines, "0000") + "+" + line(0).PadLeft(16, "0") + " "
                Dim izrE As Double = line(1) * 1000
                Dim podE As String = "81..00+" + Format(izrE, "#0000000000000000") + " "
                Dim izrN As Double = line(2) * 1000
                Dim podN As String = "82..00+" + Format(izrN, "#0000000000000000") + " "
                Dim izrH As Double = line(3) * 1000
                Dim podH As String = "83..00+" + Format(izrH, "#0000000000000000") + " "
                ' Dim podE As String = "81..00+" + izrE2.PadLeft(16, "0") + " "
                RichTextBox1.AppendText(podPtId + podE + podN + podH + vbCrLf)
            Loop
            ToolStripProgressBar1.Maximum = streamLines
            streamReader.Close()
            streamReader.Dispose()
            ToolStripStatusLabel1.Text = "Prebrano: " + streamLines.ToString + "/" + ToolStripProgressBar1.Maximum.ToString + " vrstic"
            SaveFileDialog1.Filter = "GSI datoteka|*.GSI"
            SaveFileDialog1.FileName = Path.GetFileNameWithoutExtension(OpenFileDialogTocke.FileName)
            tsbSaveAs.Enabled = True
            ShraniToolStripMenuItem.Enabled = True
            tsbSaveAs.Text = "Shrani GSI"
            ' RichTextBox1.SelectionStart = 0
        End If
    End Sub

    Public Sub ShraniDatoteko()
        SaveFileDialog1.FileName = Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)
        SaveFileDialog1.InitialDirectory = My.Settings.MapaShrani
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
            My.Computer.FileSystem.WriteAllText(DatotekaLogfile, System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " | " + OpenFileDialog1.FileName.ToString + " -> " + SaveFileDialog1.FileName.ToString + vbCrLf, True)
            My.Settings.MapaShrani = Path.GetDirectoryName(SaveFileDialog1.FileName)
            My.Settings.Save()
            ToolStripStatusLabel1.Text = "Datoteka shranjena >>> " + SaveFileDialog1.FileName.ToString
            RichTextBox1.BackColor = SystemColors.ControlLightLight
            ShraniToolStripMenuItem.Enabled = False
        End If
    End Sub

    Public streamLines As Integer = 0

    Private Sub tsbConvert_Click(sender As Object, e As EventArgs) Handles tsbConvert.Click
        ToolStripButton3.Enabled = False
        ToolStripStatusLabel1.Text = "Procesiranje..."
        RichTextBox1.Clear()
        RichTextBox1.BackColor = Color.LemonChiffon
        RichTextBox1.AppendText("TPS 1100 Zapisnik" + vbCrLf + "++++++++++++++++++++++" + vbCrLf)
        ToolStripProgressBar1.Maximum = streamLines
        ' prepare StreamReader
        Dim streamReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
        Dim i As Integer = 0
        Dim PodatkiStojisce As Boolean = False
        Dim dataStolpci As String = ""
        If My.Settings.Stolpci = 0 Then
            dataStolpci = " "
        ElseIf My.Settings.Stolpci = 1 Then
            dataStolpci = vbTab
        End If
        Dim dataKoda As String = ""

        ' loop untill end of stream
        Do Until streamReader.EndOfStream
            ' 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), "*", "")
            ToolStripProgressBar1.Value = i
            ' Check if it's a new station setup
            If Microsoft.VisualBasic.Left(line(1), 2) = "25" Then
                RichTextBox1.AppendText(vbCrLf + "Station:" + vbCrLf + "****************" + vbCrLf)
                RichTextBox1.AppendText("PtID" + dataStolpci + "HzCor" + dataStolpci + "StnE" + dataStolpci + "StnN" + dataStolpci + "StnH" + dataStolpci + "RefHt" + dataStolpci + "InstHt" + vbCrLf)
            End If

            If Microsoft.VisualBasic.Left(line(1), 2) = "84" Then
                RichTextBox1.AppendText(vbCrLf + "Station:" + vbCrLf + "****************" + vbCrLf)
                RichTextBox1.AppendText("PtID" + dataStolpci + "StnE" + dataStolpci + "StnN" + dataStolpci + "StnH" + dataStolpci + "RefHt" + dataStolpci + "InstHt" + vbCrLf)
            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
                    wordIndex = Microsoft.VisualBasic.Left(readLine, 2)

                    ' data is always 17 characters long and is read from right to left and has a +/- sign in first place
                    Dim data As String
                    Dim dataTmp As String
                    Dim numData As Double
                    data = Microsoft.VisualBasic.Right(readLine, 17)

                    If wordIndex = "11" Then
                        data = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "19" Then    ' Date/Time
                        data = dataStolpci + "DateTime= " + Microsoft.VisualBasic.Right(data, 8)
                    ElseIf wordIndex = "21" Then    ' Horizontal angle
                        numData = data / 100000
                        data = dataStolpci + Format(numData, "0.0000")
                    ElseIf wordIndex = "22" Then    ' Vertical angle
                        numData = data / 100000
                        data = dataStolpci + Format(numData, "0.0000")
                    ElseIf wordIndex = "25" Then    ' Hz. Orientation correction
                        numData = data / 100000
                        data = dataStolpci + Format(numData, "0.0000")
                    ElseIf wordIndex = "31" Then    ' Slope distance
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "32" Then    ' Horizontal distance
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "33" Then    ' Height difference
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "34" Then    ' Stationing
                        numData = data / 1000
                        data = dataStolpci + "Station= " + Format(numData, "0.000")
                    ElseIf wordIndex = "35" Then    ' Info za offset???
                        numData = data / 1000
                        data = dataStolpci + "Offset= " + Format(numData, "0.000")
                    ElseIf wordIndex = "41" Then    ' Code
                        'numData = data
                        dataKoda = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                        If My.Settings.KodeUporabnik = "Da" Then
                            If dataKoda = My.Settings.KodaQuickSet Then
                                data = vbCrLf + "Setup: Quick Set" + vbCrLf + "======================" + vbCrLf + "Hz" + dataStolpci + "V" + dataStolpci + "SD"
                                PodatkiStojisce = True
                            End If
                            If dataKoda = My.Settings.KodaSetAzi Then
                                data = vbCrLf + "Setup: Set Hz" + vbCrLf + "======================" + vbCrLf + "PtID" + dataStolpci + "HZ" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "RefHt" + dataStolpci + "PrCons" + dataStolpci + "HtDif" + dataStolpci + "PtCode"
                                PodatkiStojisce = True
                            End If
                            If dataKoda = My.Settings.KodaResection Then
                                data = vbCrLf + "Setup: Resection" + vbCrLf + "======================" + vbCrLf + "PtID" + dataStolpci + "HZ" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "RefHt" + dataStolpci + "PrCons" + dataStolpci + "HtDif" + dataStolpci + "PtCode"
                                PodatkiStojisce = True
                            End If
                            If dataKoda = My.Settings.KodaFreeStation Then
                                data = vbCrLf + "Setup: Free Station" + vbCrLf + "======================" + vbCrLf + "PtID" + dataStolpci + "HZ" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "RefHt" + dataStolpci + "PrCons" + dataStolpci + "HtDif" + dataStolpci + "PtCode"
                                PodatkiStojisce = True
                            End If
                            If dataKoda = My.Settings.KodaOriHtTrans Then
                                data = vbCrLf + "Setup: Orientation & Ht. transfer" + vbCrLf + "======================" + vbCrLf + "PtID" + dataStolpci + "HZ" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "RefHt" + dataStolpci + "PrCons" + dataStolpci + "HtDif" + dataStolpci + "PtCode"
                                PodatkiStojisce = True
                            End If
                            If dataKoda = My.Settings.KodaMeasOri Then
                                data = vbCrLf + "Meritve Orientacije" + vbCrLf + "----------------------" + vbCrLf + "PtID" + dataStolpci + "Azi" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "RefHt" + dataStolpci + "PrCons" + dataStolpci + "HtDif" + dataStolpci + "E" + dataStolpci + "N" + dataStolpci + "H" + dataStolpci + "PtCode"
                                PodatkiStojisce = False
                            End If
                            If dataKoda = My.Settings.KodaMeas Then
                                data = vbCrLf + "Merjeno" + vbCrLf + "----------------------" + vbCrLf + "PtID" + dataStolpci + "Azi" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "RefHt" + dataStolpci + "PrCons" + dataStolpci + "HtDif" + dataStolpci + "E" + dataStolpci + "N" + dataStolpci + "H" + dataStolpci + "PtCode"
                                PodatkiStojisce = False
                            End If
                            If dataKoda = My.Settings.KodaUserImport Then
                                data = vbCrLf + "Uvoz/Ročni vnos" + vbCrLf + "----------------------" + vbCrLf + "PtID" + dataStolpci + "E" + dataStolpci + "N" + dataStolpci + "H"
                                PodatkiStojisce = False
                            End If
                            If dataKoda = My.Settings.KodaStojisce Then
                                data = vbCrLf + "Stojišče Info" + vbCrLf + "----------------------" + vbCrLf + "StID" + dataStolpci + "InstHt" + vbCrLf
                                PodatkiStojisce = False
                            End If
                            If dataKoda = My.Settings.KodaStojisceInfo Then
                                data = vbCrLf + "Temp.(°C)/Prit.(mbar)" + vbCrLf + "----------------------" + vbCrLf
                                PodatkiStojisce = False
                            End If
                            If dataKoda = My.Settings.KodaStojisceOri Then
                                data = vbCrLf + "Orientacija Info" + vbCrLf + "----------------------" + vbCrLf + "OrID" + dataStolpci + "Hz" + vbCrLf
                                PodatkiStojisce = False
                            End If
                            If dataKoda = "STA_OFFS" Then
                                data = "Offsets:" + vbCrLf
                                PodatkiStojisce = False
                            End If
                        End If
                    ElseIf wordIndex = "42" Then    ' Code: Info 1
                        data = dataStolpci + "Info1= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "43" Then    ' Code: Info 2
                        data = dataStolpci + "Info2= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "44" Then    ' Code: Info 3
                        data = dataStolpci + "Info3= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "45" Then    ' Code: Info 4
                        data = dataStolpci + "Info4= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "47" Then    ' Code: Info 6
                        data = dataStolpci + "Info6= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "58" Then    ' Prism constant
                        numData = data / 10000
                        data = dataStolpci + Format(numData, "0.0000")
                    ElseIf wordIndex = "71" Then    ' Point Code
                        dataTmp = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                        If dataKoda = My.Settings.KodaStojisce Then
                            data = dataTmp
                        End If
                        If dataKoda = My.Settings.KodaStojisceInfo Then
                            data = dataTmp
                        End If
                        If dataKoda = My.Settings.KodaStojisceOri Then
                            data = dataTmp
                        End If
                        If dataTmp = "" Then
                            data = ""
                        End If
                    ElseIf wordIndex = "72" Then    ' Point Code
                        If data = 0 Then
                            dataTmp = 0
                        Else
                            dataTmp = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                        End If
                        If dataKoda = My.Settings.KodaStojisce Then
                            Dim dataNum As Double
                            dataNum = dataTmp / 1000
                            data = dataStolpci + Format(dataNum, "0.000")
                        End If
                        If dataKoda = My.Settings.KodaStojisceInfo Then
                            data = dataStolpci + dataTmp
                        End If
                        If dataKoda = My.Settings.KodaStojisceOri Then
                            Dim dataNum As Double
                            dataNum = dataTmp / 100000
                            data = dataStolpci + Format(dataNum, "0.0000")
                        End If
                    ElseIf wordIndex = "79" Then    ' Point Code
                        dataTmp = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                        If dataTmp = "" Then
                            data = ""
                        Else
                            data = dataStolpci + dataTmp
                        End If
                    ElseIf wordIndex = "81" And PodatkiStojisce = False Then    ' Target Easting
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "82" And PodatkiStojisce = False Then    ' Target Northing
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "83" And PodatkiStojisce = False Then    ' Target Height
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "84" Then    ' Station Easting
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "85" Then    ' Station Northing
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "86" Then    ' Station Height
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "87" Then    ' Reflector height
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "88" Then    ' Instriment height
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "18" Then    ' DATE YY.ss
                        numData = data / 1000000
                        data = dataStolpci + "Year.Sec= " + Format(numData, "0.00")
                    Else
                        numData = 0
                        data = ""
                    End If
                    If My.Settings.DecimalnoLocilo = "Pika" Then
                        If wordIndex IsNot "11" Then
                            data = data.ToString.Replace(",", ".")
                        End If

                    End If
                    RichTextBox1.AppendText(data)
                End If
            Next
            RichTextBox1.AppendText(vbCrLf)
            i = i + 1
        Loop
        streamReader.Close()
        ToolStripStatusLabel1.Text = "Prebrano: " + i.ToString + "/" + ToolStripProgressBar1.Maximum.ToString + " vrstic"
        SaveFileDialog1.Filter = "Zapisnik meritve|*.mer|Text file|*.txt|All files|*.*"
        tsbSaveAs.Enabled = True
        ShraniToolStripMenuItem.Enabled = True
        tsbSaveAs.Text = "Shrani Zapisnik"
        ' RichTextBox1.SelectionStart = 0
    End Sub

    ' Deklaracija vrednosti za pripravo grafike
    Public kooEMin As Double
    Public kooEMax As Double
    Public kooNMin As Double
    Public kooNMax As Double
    Public kooEdelta As Double
    Public kooNdelta As Double
    Public kooFaktor

    Private Sub tsbPointsOnly_Click(sender As Object, e As EventArgs) Handles tsbPointsOnly.Click
        Dim dataStolpci As String = ""
        If My.Settings.Stolpci = 0 Then
            dataStolpci = " "
        ElseIf My.Settings.Stolpci = 1 Then
            dataStolpci = vbTab
        End If

        RichTextBox1.Clear()
        RichTextBox1.BackColor = Color.PaleTurquoise
        Dim measuredPoints As Integer = 0
        Dim dataKoda As Integer = 0
        Dim streamReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
        Dim i As Integer = 0
        Dim lin As Integer = 0
        Do Until streamReader.EndOfStream
            ' 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
            ToolStripProgressBar1.Value = i
            'If Microsoft.VisualBasic.Left(line(1), 2) = "81" Then
            '    measuredPoints = 1
            'Else
            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 = "71" And measuredPoints >= 1 Then    ' Point Code
                        data = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                        If data = "" Then
                            ' nič
                        Else
                            RichTextBox1.AppendText(dataStolpci + data)
                        End If
                    End If
                    If wordIndex = "11" And measuredPoints >= 1 Then
                        data = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                        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
                        If kooEMax = 0 Then
                            kooEMax = numData
                        ElseIf kooEMax < numData Then
                            kooEMax = numData
                        End If
                        If kooEMin = 0 Then
                            kooEMin = numData
                        ElseIf kooEMin > numData Then
                            kooEMin = numData
                        End If
                        RichTextBox1.AppendText(dataStolpci + data)
                    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
                        If kooNMax = 0 Then
                            kooNMax = numData
                        ElseIf kooNMax < numData Then
                            kooNMax = numData
                        End If
                        If kooNMin = 0 Then
                            kooNMin = numData
                        ElseIf kooNMin > numData Then
                            kooNMin = numData
                        End If
                        RichTextBox1.AppendText(dataStolpci + data)
                    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
                            RichTextBox1.AppendText(dataStolpci + data + dataStolpci + "MEAS")
                        ElseIf measuredPoints = 2 Then
                            RichTextBox1.AppendText(dataStolpci + data + dataStolpci + "REF")
                        ElseIf measuredPoints = 3 Then
                            RichTextBox1.AppendText(dataStolpci + data + dataStolpci + "ORI")
                        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
                        If kooEMax = 0 Then
                            kooEMax = numData
                        ElseIf kooEMax < numData Then
                            kooEMax = numData
                        End If
                        If kooEMin = 0 Then
                            kooEMin = numData
                        ElseIf kooEMin > numData Then
                            kooEMin = numData
                        End If
                        RichTextBox1.AppendText(dataStolpci + data)
                    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
                        If kooNMax = 0 Then
                            kooNMax = numData
                        ElseIf kooNMax < numData Then
                            kooNMax = numData
                        End If
                        If kooNMin = 0 Then
                            kooNMin = numData
                        ElseIf kooNMin > numData Then
                            kooNMin = numData
                        End If
                        RichTextBox1.AppendText(dataStolpci + data)
                    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
                        RichTextBox1.AppendText(dataStolpci + data + dataStolpci + "STA")
                    Else
                        numData = 0
                        data = ""
                    End If
                End If
            Next
            If dataKoda = 0 And measuredPoints >= 1 Then
                RichTextBox1.AppendText(vbCrLf)
            End If
            lin = lin + 1
            ToolStripProgressBar1.Value = lin
        Loop

        kooEdelta = kooEMax - kooEMin
        kooNdelta = kooNMax - kooNMin

        If kooEdelta > kooNdelta Then
            kooFaktor = 1000 / kooEdelta
        Else
            kooFaktor = 1000 / kooNdelta
        End If

        ToolStripStatusLabel1.Text = "Prebrano točk: " + i.ToString
        SaveFileDialog1.Filter = "KOO|*.koo|TXT|*.txt|All files|*.*"
        streamReader.Close()
        tsbSaveAs.Enabled = True
        ShraniToolStripMenuItem.Enabled = True
        tsbSaveAs.Text = "Shrani Točke"
        ToolStripButton3.Enabled = True

        ' RichTextBox1.SelectionStart = 0
    End Sub

    ' Priprava GEO datoteke
    Private Sub ToolStripButtonGeo_Click(sender As Object, e As EventArgs) Handles ToolStripButtonGeo.Click
        ToolStripButton3.Enabled = False
        RichTextBox1.Clear()
        RichTextBox1.BackColor = Color.LightGray
        Dim measuredPoints As Integer = 0
        Dim dataKoda As String = ""
        Dim streamReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
        Dim i As Integer = 0
        Dim lin As Integer = 0
        Dim infoTemp As String = "20"
        Dim infoPress As String = "759.81"

        ' Branje datoteke
        Do Until streamReader.EndOfStream
            ' Beri datoteko po vrsticah, razdelitev na posamezne podatke (array)
            Dim line() As String = streamReader.ReadLine().Split(" ")
            line(0) = Replace(line(0), "*", "") ' Odstrani * na začetku vrstice

            Dim izpisText As String = ""

            If Microsoft.VisualBasic.Left(line(0), 2) = "41" Then

                dataKoda = Microsoft.VisualBasic.Right(line(0), 16).TrimStart("0"c)

                If dataKoda = My.Settings.KodaStojisce Then
                    Dim infoSta As String
                    infoSta = Microsoft.VisualBasic.Right(line(1), 16).TrimStart("0"c)
                    Dim infoStaHt As String
                    infoStaHt = Microsoft.VisualBasic.Right(line(2), 17).TrimStart("0"c)
                    Dim numStaHt As Double
                    numStaHt = infoStaHt / 1000
                    izpisText = infoSta + " " + Format(numStaHt, "0.000").ToString + vbCrLf
                    measuredPoints = 0
                End If

                ' Nastavi podatke o temperaturi in pritisku, če je podana koda za "KodaStojisceInfo"
                ' Sicer uporabi prednastavljene vrednosti "infoTemp" in "infoPress"
                If dataKoda = My.Settings.KodaStojisceInfo Then
                    infoTemp = Microsoft.VisualBasic.Right(line(1), 16).TrimStart("0"c)
                    infoPress = Microsoft.VisualBasic.Right(line(2), 16).TrimStart("0"c)
                    Dim numPress As Double
                    numPress = infoPress * 0.750062
                    infoPress = Format(numPress, "0.00").ToString
                    'If measuredPoints = 0 Then
                    '    izpisText = infoTemp + " " + infoPress + vbCrLf
                    'End If

                    measuredPoints = 0
                End If

                If dataKoda = My.Settings.KodaStojisceOri Then
                    Dim infoOri As String
                    infoOri = Microsoft.VisualBasic.Right(line(1), 16).TrimStart("0"c)
                    Dim infoOriHz As String
                    infoOriHz = Microsoft.VisualBasic.Right(line(2), 17).TrimStart("0"c)
                    Dim numOriHz As Double
                    numOriHz = infoOriHz / 100000
                    izpisText = infoOri.ToString + " " + Format(numOriHz, "0.0000").ToString + vbCrLf
                    measuredPoints = 0
                End If

                If dataKoda = My.Settings.KodaUserImport Then
                    measuredPoints = 1
                End If

                If dataKoda = My.Settings.KodaMeasOri Then
                    izpisText = infoTemp + " " + infoPress + vbCrLf
                    measuredPoints = 0
                End If

                If dataKoda = My.Settings.KodaMeas Then
                    izpisText = "0 0" + vbCrLf
                    measuredPoints = 1
                End If

                If dataKoda = My.Settings.KodaQuickSet And lin > 0 And measuredPoints = 1 Then
                    izpisText = "-1 0 0 0 0" + vbCrLf
                    measuredPoints = 0
                End If

                If dataKoda = My.Settings.KodaSetAzi And lin > 0 And measuredPoints = 1 Then
                    izpisText = "-1 0 0 0 0" + vbCrLf
                    measuredPoints = 0
                End If

                If dataKoda = My.Settings.KodaResection And lin > 0 And measuredPoints = 1 Then
                    izpisText = "-1 0 0 0 0" + vbCrLf
                    measuredPoints = 0
                End If

                If dataKoda = My.Settings.KodaFreeStation And lin > 0 And measuredPoints = 1 Then
                    izpisText = "-1 0 0 0 0" + vbCrLf
                    measuredPoints = 0
                End If

                If dataKoda = My.Settings.KodaOriHtTrans And lin > 0 And measuredPoints = 1 Then
                    izpisText = "-1 0 0 0 0" + vbCrLf
                    measuredPoints = 0
                End If
            End If

            If Microsoft.VisualBasic.Left(line(0), 2) = "11" Then
                If dataKoda = My.Settings.KodaMeas Then
                    Dim dataId As String
                    dataId = Microsoft.VisualBasic.Right(line(0), 16).TrimStart("0"c)
                    Dim dataAzi As String
                    dataAzi = Microsoft.VisualBasic.Right(line(1), 17)
                    Dim numAzi As Double
                    numAzi = dataAzi / 100000
                    Dim dataV As String
                    dataV = Microsoft.VisualBasic.Right(line(2), 17)
                    Dim numV As Double
                    numV = dataV / 100000
                    Dim dataSD As String
                    dataSD = Microsoft.VisualBasic.Right(line(3), 17)
                    Dim numSD As Double
                    numSD = dataSD / 1000
                    Dim dataHr As String
                    dataHr = Microsoft.VisualBasic.Right(line(5), 17)
                    Dim numHr As Double
                    numHr = dataHr / 1000

                    izpisText = dataId + " " + Format(numAzi, "0.0000").ToString + " " + Format(numV, "0.0000").ToString + " " + Format(numSD, "0.000").ToString + " " + Format(numHr, "0.000").ToString + vbCrLf
                End If

                If dataKoda = My.Settings.KodaMeasOri Then
                    Dim dataId As String
                    dataId = Microsoft.VisualBasic.Right(line(0), 16).TrimStart("0"c)
                    Dim dataAzi As Double
                    dataAzi = Microsoft.VisualBasic.Right(line(1), 17)
                    Dim numAzi As Double
                    numAzi = dataAzi / 100000
                    izpisText = dataId + " " + Format(numAzi, "0.0000").ToString + vbCrLf
                End If

                ' Priprava podatkov za Setup: Quick Set
                If dataKoda = My.Settings.KodaQuickSet Then
                    If Microsoft.VisualBasic.Left(line(1), 2) = "84" Then
                        Dim dataSta As String
                        dataSta = Microsoft.VisualBasic.Right(line(0), 16).TrimStart("0"c)
                        Dim dataHi As Double
                        dataHi = Microsoft.VisualBasic.Right(line(4), 17)
                        Dim numHi As Double
                        numHi = dataHi / 1000
                        izpisText = dataSta + " " + Format(numHi, "0.000").ToString + vbCrLf
                    End If
                End If
                ' Priprava podatkov za Setup: Set Azimut
                If dataKoda = My.Settings.KodaSetAzi Then

                End If

                ' Priprava podatkov za Setup: Resection
                If dataKoda = My.Settings.KodaResection Then

                End If

                ' Priprava podatkov za Setup: Free Station
                If dataKoda = My.Settings.KodaFreeStation Then
                    If Microsoft.VisualBasic.Left(line(1), 2) = "25" Then
                        Dim dataSta As String
                        dataSta = Microsoft.VisualBasic.Right(line(0), 16).TrimStart("0"c)
                        Dim dataHi As Double
                        dataHi = Microsoft.VisualBasic.Right(line(6), 17)
                        Dim numHi As Double
                        numHi = dataHi / 1000
                        izpisText = dataSta + " " + Format(numHi, "0.000").ToString + vbCrLf
                    End If
                End If

                ' Priprava podatkov za Setup: Set Azimut
                If dataKoda = My.Settings.KodaOriHtTrans Then

                End If
            End If
                RichTextBox1.AppendText(izpisText)
            lin = lin + 1
            ToolStripProgressBar1.Value = lin
        Loop
        RichTextBox1.AppendText("-1 0 0 0 0" + vbCrLf)

        streamReader.Close()
        streamReader.Dispose()
        ToolStripStatusLabel1.Text = "Prebrano: " + lin.ToString + "/" + ToolStripProgressBar1.Maximum.ToString + " vrstic"
        SaveFileDialog1.Filter = "GEO datoteka|*.GEO"
        SaveFileDialog1.FileName = Path.GetFileNameWithoutExtension(OpenFileDialogTocke.FileName)
        tsbSaveAs.Enabled = True
        ShraniToolStripMenuItem.Enabled = True
        tsbSaveAs.Text = "Shrani GEO"
        'RichTextBox1.SelectionStart = 0
    End Sub

    Private Sub InfoOAplikacijiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InfoOAplikacijiToolStripMenuItem.Click
        ' MessageBox.Show("Za konvertiranje GSI datoteke najprej izberi datoteko, ki jo želiš konvertirati z ukazom 'Open File'. Nato izberi metodo konverzije (Poročilo, Merjene točke, Vse točke). Po končani knverziji shrani novo datoteko z ukazom 'Save As...'","Kratka navodila")
        SplashScreen1.ShowDialog()
    End Sub

    Private Sub PripraviGSIToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PripraviGSIToolStripMenuItem.Click
        Process.Start("Navodila.pdf")
    End Sub

    Private Sub OdpriGSIToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OdpriGSIToolStripMenuItem.Click
        FormNastavitve.ShowDialog()
    End Sub

    Private Sub OdpriGSIToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles OdpriGSIToolStripMenuItem1.Click
        UvoziGsiJob()
        ' OdpriDatotekoGsi()

    End Sub
    Private Sub tsbOpenFile_Click(sender As Object, e As EventArgs) Handles tsbOpenFile.Click
        UvoziGsiJob()
        ' OdpriDatotekoGsi()
        ToolStripButton3.Enabled = False

    End Sub
    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ButtonUrediDat.Click
        Form2.ShowDialog()
    End Sub

    Private Sub ToolStripButtonNast_Click(sender As Object, e As EventArgs)
        FormNastavitve.ShowDialog()
    End Sub

    Private Sub ToolStripButtonPripravi_Click(sender As Object, e As EventArgs) Handles ToolStripButtonPripravi.Click
        PripraviDatotekoGsi()
        ToolStripButton3.Enabled = False

    End Sub

    Private Sub PripraviGSIToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles PripraviGSIToolStripMenuItem1.Click
        PripraviDatotekoGsi()

    End Sub
    Private Sub tsbSaveAs_Click(sender As Object, e As EventArgs) Handles tsbSaveAs.Click
        ShraniDatoteko()

    End Sub

    Private Sub ShraniToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ShraniToolStripMenuItem.Click
        ShraniDatoteko()

    End Sub

    Private Sub IzhodToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles IzhodToolStripMenuItem.Click
        Me.Close()
        Application.Exit()

    End Sub

    Private Sub PosodobitevToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PosodobitevToolStripMenuItem.Click
        ' Preveri verzijo programa iz Gitea - MultiBox
        Dim infoVerzija As String = My.Application.Info.Version.Major.ToString + "." + My.Application.Info.Version.Minor.ToString + "." + My.Application.Info.Version.Build.ToString + "." + My.Application.Info.Version.Revision.ToString
        Dim gitVerzija As String = ""
        My.Computer.Network.DownloadFile("https://multibox.nohost.me/gitea/boris/TPS1100-Convert/raw/branch/main/version.txt", "version.txt")
        Dim verReader As StreamReader = New StreamReader("version.txt")

        ' Branje datoteke
        Do Until verReader.EndOfStream
            ' Preberi vrstico z verzijo na git
            gitVerzija = verReader.ReadLine()
        Loop
        verReader.Dispose()
        verReader.Close()

        My.Computer.FileSystem.DeleteFile("version.txt")

        If gitVerzija = infoVerzija Then
            Dim Msg, Style, Title, Response

            Msg = "Trenutna verzija: " + infoVerzija + vbCrLf + vbCrLf + "Ni posodobitev..."    ' Define message.
            Title = "Preverjanje posodobitev"    ' Define title.
            Style = vbOKOnly

            ' Display message.
            Response = MsgBox(Msg, Style, Title)
        Else
            Dim Msg, Style, Title, Response

            Msg = "Trenutna verzija: " + infoVerzija + vbCrLf + "Nova verzija: " + gitVerzija + vbCrLf + vbCrLf + "Odprem stran za prenos?"  ' Define message.
            Title = "Preverjanje posodobitev"    ' Define title.
            Style = vbYesNo

            ' Display message.
            Response = MsgBox(Msg, Style, Title)

            If Response = vbYes Then    ' User chose Yes.
                Process.Start("https://multibox.nohost.me/gitea/boris/TPS1100-Convert/releases")
            End If
        End If
    End Sub

    Private Sub ToolStripButton1_Click_1(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        RichTextBox1.SelectionStart = 0
    End Sub

    Private Sub ManagerToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ManagerToolStripMenuItem.Click
        FormGsiJobs.Show()

    End Sub

    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click, OdpriJobToolStripMenuItem.Click
        OpenFileDialog1.InitialDirectory = My.Application.Info.DirectoryPath.ToString + "\" + My.Settings.ProjektMapa.ToString
        OdpriDatotekoGsi()
        ToolStripButton3.Enabled = False

    End Sub

    Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
        FormGrafika.Show()
    End Sub

End Class
