Friend Class frmSELibrary
    Inherits System.Windows.Forms.Form

    'Dim WithEvents objFileUIEvents As SolidEdgeFramework.ISEFileUIEvents_Event

    Private Sub frmSELibrary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        On Error Resume Next
        Dim intReturn As Short
        Dim i, j As Integer

        ' get the INI file and read the global data path
        If GetGlobalPath() = ER_FATALERROR Then
            MsgBox(LE_NOINI, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            End
        End If

        ' is the data file found?

        If CheckDatFile() = ER_FILENOTFOUND Then
            OpenFileDialog1.Filter = "dat files (*.dat)|*.dat|All files (*.*)|*.*"
            OpenFileDialog1.InitialDirectory = strGlobalPath
            OpenFileDialog1.FileName = ""
            OpenFileDialog1.Title = "Define SELibrary.dat file"
            OpenFileDialog1.ShowDialog()
            strDataFile = OpenFileDialog1.FileName
            For i = 0 To strDataFile.Length - 1
                If strDataFile.Substring(strDataFile.Length - 1 - i, 1) = "\" Then
                    strGlobalPath = strDataFile.Substring(0, strDataFile.Length - 1 - i)
                    FileOpen(1, strINIFile, OpenMode.Output)
                    PrintLine(1, strGlobalPath)
                    FileClose(1)
                    Exit For
                End If
            Next
        End If

        ' is the data file valid
        intReturn = CheckValidDat(strDataFile)

        If intReturn = ER_VALIDFAIL Then
            MsgBox(LE_FILE & strDataFile & LE_VALIDFAIL, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            End
        ElseIf intReturn = ER_INVALIDDAT Then
            MsgBox(LE_FILE & strDataFile & LE_INVALIDDAT, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            End
        End If

        If GetData() <> ER_NOERROR Then
            ' this is really bad
            MsgBox(LE_NODATREAD, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            End
        End If

        'Now that we have the data, it's time to prepare and load the dialog
        SetupDialog()
        Visible = True
        AddData()

    End Sub


    Sub SetupDialog()
        Dim vwrTop, vwrLeft, vwrWidth, vwrHeight As Short

        On Error Resume Next
        'setup viewers

        vwrTop = vwrFile.Top
        vwrLeft = vwrFile.Left
        vwrWidth = vwrFile.Width
        vwrHeight = vwrFile.Height

        btnViewer.Text = LS_VIEWPART
        vwrFile.Visible = True

        ' update captions and text for localization
        Text = LS_MAIN_CAPTION
        btnExit.Text = LS_BUTTON_DONE
        btnAdd.Text = LS_BUTTON_ADD
        btnReplace.Text = LS_BUTTON_REPLACE

        chkProperties.Text = LS_CHK_PROPERTIES
        chkPlaceAtOrigin.Text = LS_ORIGIN
        lblNoGraphic.Text = ""
        lblNoGraphic.Visible = False
        mnuFile.Text = LM_FILE
        mnuOpen.Text = LM_OPEN
        mnuExit.Text = LM_EXIT
        mnuHelp.Text = LM_HELP
        mnuAbout.Text = LM_ABOUT
        mnuHelpHelp.Text = LM_HELPHELP

        lblMessage.Text = ""
        TopMost = True
        System.Windows.Forms.Application.DoEvents()

    End Sub

    Private Sub tabData_MouseUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles tabData.MouseUp
        Dim Button As Short = eventArgs.Button \ &H100000
        Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys \ &H10000

        On Error Resume Next

        intCurrentTab = intFirstTab + tabData.SelectedIndex

        If LoadClassList() = ER_FATALERROR Then
            ' errors handled in function
        End If

        lblMessage.Text = ""

    End Sub

    Private Sub cboClasses_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboClasses.SelectedIndexChanged
        On Error Resume Next
        'Dim intTypeID As Short

        Dim partbase, i, intPartID As Short

        For i = (LBound(SEData)) To UBound(SEData) - 1
            If SEData(i).TypeID = intCurrentTab Then
                partbase = i
                intTypeID = SEData(i).TypeID
                intPartID = SEData(i).PartID
                intCurrentPartID = cboClasses.SelectedIndex + partbase
                Exit For
            End If
        Next

        LoadGrid(intCurrentPartID)

        If ShowGraphic(intCurrentPartID) = ER_FATALERROR Then
            ' errors handled in function
        End If

        lblMessage.Text = ""
        btnAdd.Enabled = False
        btnReplace.Enabled = False

    End Sub

    Private Sub btnViewer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewer.Click
        On Error Resume Next

        If btnViewer.Text = LS_VIEWPART Then
            btnViewer.Text = LS_VIEWDRAWING
            If ShowGraphic(intCurrentPartID) = ER_FILENOTFOUND Then
                ' part not found error handled in function
            End If
        Else
            btnViewer.Text = LS_VIEWPART

            If ShowGraphic(intCurrentPartID) = ER_FILENOTFOUND Then
                ' part not found error handled in function
            End If

        End If

    End Sub
    Private Sub grdData_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdData.CellMouseClick
        Dim cvalue As String

        Dim i, j As Short
        Dim intRow, partbase, intVarCount As Short
        Dim strFileName As String
        Dim intCurrent As Short
        'Dim intTypeID As Short
        Dim strFullName, intRowKey As String
        On Error Resume Next

        'This section gets the part variation; i.e. the specific row in the grid.
        intRow = grdData.CurrentRow.Index
        intRowKey = intRow + 1

        ReDim SECurrent.Variables(grdData.ColumnCount)
        ReDim SECurrent.Sizes(grdData.ColumnCount)

        ' get the data for the specific row.
        i = -1
        intVarCount = SEData(intCurrentPartID).VariableCount
        For Each kvp As KeyValuePair(Of String, String) In SEData(intCurrentPartID).Sizes
            ' Define the TKey equivalent for intRowKey
            For j = 0 To kvp.Key.Length - 1
                If kvp.Key.Substring(j, 1) = "_" Then
                    Exit For
                End If
            Next
            If kvp.Key.Substring(0, j) = intRowKey Then
                i = i + 1
                SECurrent.Sizes(i) = kvp.Value
                SECurrent.Variables(i) = SEData(intCurrentPartID).Variables(i)
                If i = intVarCount - 1 Then Exit For
            End If
        Next

        strFileName = GetFileName(intCurrentPartID, SECurrent.Sizes)
        strFullName = strGlobalPath & strFileName

        SECurrent.MemberName = SECurrent.Sizes(0)
        SECurrent.ChildName = strFileName
        SECurrent.ParentName = SEData(intCurrentPartID).BasePart
        If Dir(strFullName).Length > 0 Then
            ' found
            lblMessage.Text = LF_FILE & strFullName & LF_FOUND
            chkProperties.Enabled = False
        Else
            ' not found allow property additions
            lblMessage.Text = LF_FILE & strFullName & LF_NOT_FOUND
            chkProperties.Enabled = True
        End If

        btnAdd.Enabled = True
        btnReplace.Enabled = True

    End Sub

    Private Sub mnuHelpHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpHelp.Click
        On Error Resume Next

        Dim strFile As String

        strFile = strGlobalPath & "\readme.txt"

        If CheckForFile(strFile) = ER_FILENOTFOUND Then
            MsgBox(LE_FILE & strFile & LE_NOREADME, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            Exit Sub
        End If

        frmHelp.TopMost = True
        frmHelp.Show()

    End Sub

    Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
        On Error Resume Next
        frmAbout.Show()
        frmAbout.TopMost = True
        frmAbout.ShowDialog()

    End Sub

    Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
        Dim intReturn, i As Short
        Dim strChr As String
        'use standard FileOpenDialog
        OpenFileDialog1.Filter = "Data Files (*.DAT)|*.DAT"
        OpenFileDialog1.FileName = "SE-Library.dat"
        OpenFileDialog1.ShowDialog()

        strDataFile = OpenFileDialog1.FileName
        intReturn = CheckValidDat(strDataFile)

        If intReturn = ER_VALIDFAIL Then
            MsgBox(LE_FILE & strDataFile & LE_VALIDFAIL, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            End
        ElseIf intReturn = ER_INVALIDDAT Then
            MsgBox(LE_FILE & strDataFile & LE_INVALIDDAT, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            End
        End If

        ' need to set the global data path
        ' strip \ from path
        strGlobalPath = ""

        For i = strDataFile.Length - 1 To 0 Step -1
            strChr = strDataFile.Substring(i, 1)
            If strDataFile.Substring(i, 1) = "\" Then
                strGlobalPath = strDataFile.Substring(0, i)
                Exit For
            End If
        Next


        If GetData() <> ER_NOERROR Then
            ' this is really bad
            MsgBox(LE_NODATREAD, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            End
        End If

        'Now that we have the data, it's time to prepare and load the dialog
        SetupDialog()
        AddData()

    End Sub


    Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        On Error Resume Next

        objApp = Nothing

        End

    End Sub

    Private Sub grdData_CellDblClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grdData.CellDoubleClick

        On Error Resume Next
        btnAdd_Click(btnAdd, New System.EventArgs())

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        On Error Resume Next

        Dim intReturn As Short
        Dim strPart As String = Nothing
        Dim objADoc As Object
        Dim objTradDoc As SolidEdgeAssembly.AssemblyDocument
        'Dim objSYNCDoc As SolidEdgeAssemblySync.AssemblyDocument

        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

        lblMessage.Text = LT_PROCESS_CONNECT
        intReturn = SEConnect()

        Sleep(1 * 1000) ' give time to SE to process on initial placement

        GetSEVersion()
        If intVersion < NEED_VERSION Then
            MsgBox(LE_VERSION, vbOKOnly + vbCritical + vbSystemModal, LS_MSGBOX_TITLE)
            ' provide invalid version message
            Exit Sub
        End If

        Sleep(1 * 1000) ' give time to SE to process on initial placement

        If intReturn = ER_NOASSEMBLY Then
            MsgBox(LE_WRONGENVIRONMENT, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            Exit Sub
        End If

        If intReturn = ER_FATALERROR Then
            ' this means that solid edge is in bad shape
            MsgBox(LE_NOASSEMBLY, MsgBoxStyle.SystemModal + MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, LS_MSGBOX_TITLE)
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            Exit Sub
        End If

        lblMessage.Text = ""

        ' all error handling is done within the function
        lblMessage.Text = LT_PROCESS_BUILDPART

        intReturn = SEBuildPart()

        Sleep(5 * 1000) ' give time to SE to process on initial placement

        lblMessage.Text = ""

        If (intReturn = ER_NOERROR) Or (intReturn = ER_FILEEXISTS) Then
            strPart = strGlobalPath & SECurrent.ChildName

            'need to account for SYNC assembly
            'If UCase(objApp.ActiveEnvironment) = "ASSEMBLY" Then
            '    objTradDoc = objApp.ActiveDocument
            '    objParts = objTradDoc.Occurrences
            'ElseIf UCase(objApp.ActiveEnvironment) = "DMASSEMBLY" Then
            '    objSYNCDoc = objApp.ActiveDocument
            '    objParts = objSYNCDoc.Occurrences
            'End If
            objTradDoc = objApp.ActiveDocument
            objParts = objTradDoc.Occurrences

            System.Windows.Forms.Application.DoEvents()

            If chkPlaceAtOrigin.CheckState = 1 Then
                ' place at origin
                objParts.AddByFilename(strPart)
                If Err.Number Then
                    MsgBox(LE_FILE & strPart & LE_ADDNEWPART, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                End If

            Else

                WindowState = System.Windows.Forms.FormWindowState.Minimized

                System.Windows.Forms.Application.DoEvents()

                Sleep(1 * 1000) '11/30/2005 ccprestr PR5342372

                ' while using startcommand seems to work in VB6, it does not work with VB .Net for some reason.
                ' so using place by transform in order to place the part.

                ' NOTE: Resolved apparently by unregistering a Distrim2 addin that seemingly prevent this app from 
                ' catching the event and defining the file name (See SEAutomation.vb objFileUIEvents_OnPlacePartUI
                objApp.StartCommand(SolidEdgeFramework.SolidEdgeCommandConstants.seAssemblyPlacePartCommand)

                Sleep(1 * 1000) '11/30/2005 ccprestr PR5342372

                If Err.Number Then
                    MsgBox(LE_FILE & strPart & LE_ADDNEWPART, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
                End If

            End If

        End If

        Sleep(1 * 1000) ' give time to SE to process on initial placement

        ' release all objects
        objApp = Nothing
        objADoc = Nothing
        objParts = Nothing

        lblMessage.Text = "Part placed."
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default

        Sleep(1 * 1000) ' give time to SE to process on initial placement
    End Sub

    'Private Sub objFileUIEvents_OnPlacePartUI(ByRef Filename As String, ByRef AppendToTitle As String) Handles objFileUIEvents.OnPlacePartUI

    '    On Error Resume Next

    '    Filename = strGlobalPath & SECurrent.ChildName

    'End Sub


    Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
        On Error Resume Next
        Dim intReturn As Short
        Dim strPart As String

        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

        lblMessage.Text = LT_PROCESS_CONNECT

        intReturn = SEConnect()

        GetSEVersion()

        If intVersion < NEED_VERSION Then
            MsgBox(LE_VERSION, vbOKOnly + vbCritical + vbSystemModal, LS_MSGBOX_TITLE)
            ' simply quitting seems too harsh
            Exit Sub
        End If

        If intReturn = ER_NOASSEMBLY Then
            MsgBox(LE_WRONGENVIRONMENT, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            Exit Sub
        End If

        If intReturn = ER_FATALERROR Then
            ' this means that solid edge is not running
            MsgBox(LE_NOASSEMBLY, MsgBoxStyle.SystemModal + MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, LS_MSGBOX_TITLE)
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            Exit Sub
        End If

        lblMessage.Text = ""

        ' all error handling is done within the function
        lblMessage.Text = LT_PROCESS_BUILDPART

        intReturn = SEBuildPart()

        Sleep(5 * 1000) ' give time to SE to process 

        lblMessage.Text = ""

        If (intReturn = ER_NOERROR) Or (intReturn = ER_FILEEXISTS) Then

            ' replace it
            If ReplacePart() <> ER_NOERROR Then
                ' it worked
            End If

        End If

        lblMessage.Text = "Part replaced"
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        AddOrRemoveEventHandlers(False)
        objApp = Nothing

        End
    End Sub
End Class
