Module GetRoutines
    Function GetGlobalPath() As Object
        Dim i As Short

        On Error Resume Next

        GetGlobalPath = ER_NOERROR
        ' strip \ from path
        strAppPath = Application.ExecutablePath    ' should be uncommented for production
        'strAppPath = "E:\MyPrograms\SELibrary\"      ' for debug purposes
        For i = strAppPath.Length - 1 To 0 Step -1
            If strAppPath.Substring(i, 1) = "\" Then
                strAppPath = strAppPath.Substring(0, i)
                Exit For
            End If
        Next

        strINIFile = strAppPath & "\" & LF_INIFILE
        If Dir(strINIFile).Length = 0 Then
            ' not found create on the fly
            FileOpen(1, strINIFile, OpenMode.Output)
            PrintLine(1, strAppPath)
        End If

        FileClose(1)

        FileOpen(1, strINIFile, OpenMode.Input)
        strGlobalPath = LineInput(1)
        FileClose(1)

        If strGlobalPath = "" Then
            FileOpen(1, strINIFile, OpenMode.Output)
            strGlobalPath = strAppPath
            PrintLine(1, strAppPath)
        End If
        FileClose(1)

        ' strip \ from path
        If strGlobalPath.Substring(strGlobalPath.Length - 1, 1) = "\" Then
            strGlobalPath = strGlobalPath.Substring(0, strGlobalPath.Length - 1)
        End If

        If Err.Number Then
            GetGlobalPath = ER_FATALERROR
        End If
    End Function

    Function GetNextTypeID(ByRef intCurrent As Short) As Short

        On Error GoTo ErrorTrap

        Dim i As Short

        GetNextTypeID = ER_NOERROR

        If intCurrent = 1 Then
            intLastTypeID = intCurrent
            intTotalTabs = intLastTypeID
            Exit Function
        End If
        For i = 0 To intParts - 1
            If SEData(i).Type = SEData(intParts).Type Then
                intCurrent = SEData(i).TypeID
                Exit Function
            End If
        Next
        intLastTypeID = intLastTypeID + 1
        intCurrent = intLastTypeID
        intTotalTabs = intLastTypeID

ErrorTrap:
        If Err.Number Then
            GetNextTypeID = ER_FATALERROR
            Exit Function
        End If

    End Function
    Function GetData() As Short

        On Error GoTo ErrorTrap

        Dim filData As Object
        Dim strLineData As String
        Dim intSizes As Short
        Dim intVariables As Short
        Dim intTemp As Short
        Dim intNextID As Short
        Dim strTemp As String
        Dim strTempData() As String = Nothing
        Dim i As Short

        intParts = -1
        intLastTypeID = 0
        GetData = ER_NOERROR

        ReDim SEData(1)

        FileOpen(1, strDataFile, OpenMode.Input)

        Do While Not EOF(1)

            strLineData = LineInput(1)

            If strLineData = C_NEW_PART Then
                ' add a new part
                intParts = intParts + 1
                ReDim Preserve SEData(intParts + 1)

                ' set both ID's to the counter (unique)
                ' the type ID may get set to non-unique below
                SEData(intParts).PartID = intParts + 1

                intVariables = 0
                intSizes = 0

                ' now get the data
                Do While Not EOF(1)

                    If (strLineData = "") Or (strLineData = "*") Then
                        ' skip

                    ElseIf strLineData.IndexOf(C_PART_TYPE) > -1 Then
                        ' add the part type to the instance
                        strLineData = strLineData.Substring(C_PART_TYPE.Length, strLineData.Length - C_PART_TYPE.Length)
                        If strLineData.Length = 0 Then
                            MsgBox(LE_NOPARTTYPE, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                            FileClose(1)
                            End
                        Else
                            If SEData(intParts).Type <> "" Then
                                MsgBox(SEData(intParts).Type & " - " & LE_TYPEEXISTS, MsgBoxStyle.Critical + MsgBoxStyle.OkCancel + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                                End
                            Else
                                SEData(intParts).Type = strLineData
                            End If

                        End If
                        ' set the typeID (non-unique i.e. all elbows =1)
                        intNextID = intParts + 1
                        intTemp = GetNextTypeID(intNextID)
                        If intTemp = ER_FATALERROR Then
                            ' force and error
                            Err.Raise(1)
                        End If
                        SEData(intParts).TypeID = intNextID

                    ElseIf strLineData.IndexOf(C_PART_CLASS) > -1 Then
                        ' add the part class to the instance
                        ' one type can have many classes
                        strLineData = strLineData.Substring(C_PART_CLASS.Length, strLineData.Length - C_PART_CLASS.Length) & ""
                        If strLineData.Length = 0 Then
                            MsgBox(LE_NOPARTCLASS, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                            FileClose(1)
                            End
                        Else
                            SEData(intParts).PartClass = strLineData
                        End If

                    ElseIf strLineData.IndexOf(C_PART_DRAWING) > -1 Then
                        ' add the part drawing (a dft file for the viewer)
                        strLineData = strLineData.Substring(C_PART_DRAWING.Length, strLineData.Length - C_PART_DRAWING.Length) & ""
                        If strLineData.Length = 0 Then
                            MsgBox(LE_NOPARTDRAWING, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                            FileClose(1)
                            End
                        Else
                            ' make sure it preceeds with a \
                            If strLineData.Substring(0, 1) <> "\" Then
                                SEData(intParts).DraftName = "\" & strLineData
                            Else
                                SEData(intParts).DraftName = strLineData
                            End If
                        End If

                    ElseIf strLineData.IndexOf(C_PART_TEMPLATE) > -1 Then
                        ' add the part to the instance
                        strLineData = strLineData.Substring(C_PART_TEMPLATE.Length, strLineData.Length - C_PART_TEMPLATE.Length) & ""
                        If strLineData.Length = 0 Then
                            MsgBox(LE_NOBASEPART, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                            FileClose(1)
                            End
                        Else
                            ' make sure it preceeds with a \
                            If strLineData.Substring(0, 1) <> "\" Then
                                SEData(intParts).BasePart = "\" & strLineData
                            Else
                                SEData(intParts).BasePart = strLineData
                            End If
                        End If

                    ElseIf strLineData.IndexOf(C_HEADING) > -1 Then
                        ' find the variables
                        strLineData = strLineData.Substring(C_HEADING.Length, strLineData.Length - C_HEADING.Length) & ""
                        If strLineData.Length = 0 Then
                            MsgBox(LE_NOHEADING, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                            FileClose(1)
                            End
                        Else
                            intTemp = GetVariables(strLineData, SEData(intParts).Variables, SEData(intParts).VariableCount)
                            If intTemp = ER_FATALERROR Then
                                Err.Raise(1)
                            ElseIf intTemp = ER_NOVARIABLES Then
                                If MsgBox(LE_NOVARIABLES, MsgBoxStyle.Critical + MsgBoxStyle.YesNo + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE) = MsgBoxResult.Yes Then
                                    FileClose(1)
                                    End
                                End If
                            End If
                        End If
                    ElseIf strLineData.IndexOf(C_PART_DATA) > -1 Then
                        ' add a new size

                        strLineData = strLineData.Substring(C_PART_DATA.Length, strLineData.Length - C_PART_DATA.Length) & ""
                        If strLineData.Length = 0 Then
                            MsgBox(LE_NOPARTDATA, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                            FileClose(1)
                            End
                        Else

                            intTemp = ArrayCount(strLineData, C_FILE_DELIMITER)
                            If intTemp <> SEData(intParts).VariableCount Then
                                ' we have a miss match of dimensions versus variables
                                MsgBox(SEData(intParts).PartClass & " - " & LE_SVMISSMATCH, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                                FileClose(1)
                                End
                            End If
                            ' because VB will not allow redim of multi-size arrays within a data structure
                            ' we'll dump the info to a temp array and updata it later during END PART
                            SEData(intParts).SizeCount = SEData(intParts).SizeCount + 1
                            ReDim Preserve strTempData(SEData(intParts).SizeCount)
                            strTempData(SEData(intParts).SizeCount - 1) = strLineData
                        End If

                    ElseIf strLineData = C_END_PART Then

                        ' make sure all parameters are specified
                        If SEData(intParts).Type = "" Or SEData(intParts).PartClass = "" Then
                            MsgBox(LE_NOTYPECLASS, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                            FileClose(1)
                            End
                        End If

                        ' sizes loaded into temp array now put into the data structure
                        ' check for size count 0 no go
                        If SEData(intParts).SizeCount = 0 Then
                            MsgBox(LE_NOPARTS, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                            FileClose(1)
                            End
                        End If
                        ' transfer data from temp array to the data structure
                        TransferSizes(strTempData, SEData(intParts).SizeCount, SEData(intParts).VariableCount)
                        ' check for images and base part. if they are not found then ask the user to quit or ignore
                        ' check if the image file is present if not not real threat user can quit

                        strTemp = strGlobalPath & SEData(intParts).DraftName

                        If CheckForFile(strTemp) = ER_FILENOTFOUND Then
                            If MsgBox(LE_FILE & strTemp & LE_NOTFOUND, MsgBoxStyle.YesNo + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE) = MsgBoxResult.No Then
                                FileClose(1)
                                End
                            End If
                        End If

                        ' check if the parent file is present

                        strTemp = strGlobalPath & SEData(intParts).BasePart

                        If CheckForFile(strTemp) = ER_FILENOTFOUND Then
                            If MsgBox(LE_FILE & strTemp & LE_NOTFOUND, MsgBoxStyle.YesNo + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE) = MsgBoxResult.No Then
                                FileClose(1)
                                End
                            End If
                        End If

                        Exit Do

                    End If

                    ' read the next line for processing
                    strLineData = LineInput(1)
                Loop

            End If

        Loop

        FileClose(1)

        System.Windows.Forms.Application.DoEvents()

ErrorTrap:
        If Err.Number Then
            GetData = ER_FATALERROR
            Exit Function
        End If

    End Function
    Sub TransferSizes(ByRef strTemp() As String, ByRef intSizeCount As Short, ByRef intVarCount As Short)

        On Error Resume Next
        Dim strTempArray() As String
        Dim intVar As Short
        Dim intCnt As Short
        Dim tkey As String

        ReDim strTempArray(intVarCount)

        SEData(intParts).Sizes = New Dictionary(Of String, String)
        For intCnt = 1 To intSizeCount
            ArrayParse(strTempArray, strTemp(intCnt - 1), C_FILE_DELIMITER)
            For intVar = 1 To intVarCount
                tkey = intCnt & "_" & intVar
                SEData(intParts).Sizes.Add(tkey, strTempArray(intVar - 1))
            Next
        Next

    End Sub
    Function GetVariables(ByRef strData As String, ByRef strVariables() As String, ByRef intVariableCount As Short) As Short


        On Error GoTo ErrorTrap

        GetVariables = ER_NOERROR

        intVariableCount = ArrayCount(strData, C_FILE_DELIMITER)

        If intVariableCount = 0 Then
            ' we really can't proceed
            GetVariables = ER_NOVARIABLES
            Exit Function
        End If

        ReDim Preserve strVariables(intVariableCount)
        ArrayParse(strVariables, strData, C_FILE_DELIMITER)
ErrorTrap:
        If Err.Number Then
            GetVariables = ER_FATALERROR
            Exit Function
        End If

    End Function
    Function GetFileName(ByRef intIndex As Short, ByRef strData() As String) As String

        On Error Resume Next

        Dim strFileName As String
        Dim strPartName As String
        Dim i As Short
        Dim strSize As String
        Dim strChar As String

        strSize = strData(0)
        strPartName = SEData(intIndex).BasePart.Substring(0, SEData(intIndex).BasePart.Length - 4)
        strFileName = "_"

        ' replace any bad characters that may invalidate the file name
        For i = 0 To strSize.Length - 1
            strChar = strSize.Substring(i, 1)
            If LM_INVALIDCHARS.IndexOf(strChar) > -1 Then
                strChar = LM_FRACTION
            End If

            strFileName = strFileName & strChar
        Next

        ' filename base part name (-.par) + size + .par
        GetFileName = strPartName & strFileName & ".PAR"

    End Function
End Module
