'Import Inventor object library
Imports Inventor

Friend Structure CacheData
    Dim bChecked As Boolean
    Dim strUpdateStatus As String
    Dim strDisplayName As String
    Dim strFullFileName As String
    Dim strPartNumber As String
    Dim strStockNumber As String
End Structure

Public Class MainWnd
    Inherits System.Windows.Forms.Form

    'Class variable definitions
    Private oApp As Application
    Private bCloseInventorApp As Boolean
    Private oPartDoc As PartDocument
    Private oMap As New SortedList
    Private strDefaultProject As String
    Private strActiveProject As String
    Private bFirstLoad As Boolean

    Private strDrawFileNames() As String
    Private lNoDrawFiles As Long
    Private FileProgressForm As ProgressIndicatorForm

    Private Const NOT_UPDATED As String = "Not Updated"
    Private Const UPDATE_SUCCEEDED As String = "Cleanup Succeeded"
    Private Const UPDATE_FAILED As String = "Cleanup Failed"

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents cbProjectPath As System.Windows.Forms.ComboBox
    Friend WithEvents btBrowse As System.Windows.Forms.Button
    Friend WithEvents btUpdate As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents lvDrawData As System.Windows.Forms.ListView
    Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader0 As System.Windows.Forms.ColumnHeader
    Friend WithEvents btClose As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents LabelNoiParts As System.Windows.Forms.Label
    Friend WithEvents btnRefresh As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.cbProjectPath = New System.Windows.Forms.ComboBox
        Me.btBrowse = New System.Windows.Forms.Button
        Me.btUpdate = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.btnRefresh = New System.Windows.Forms.Button
        Me.LabelNoiParts = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.lvDrawData = New System.Windows.Forms.ListView
        Me.ColumnHeader0 = New System.Windows.Forms.ColumnHeader
        Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
        Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader
        Me.btClose = New System.Windows.Forms.Button
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.SuspendLayout()
        '
        'cbProjectPath
        '
        Me.cbProjectPath.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cbProjectPath.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbProjectPath.Location = New System.Drawing.Point(24, 24)
        Me.cbProjectPath.Name = "cbProjectPath"
        Me.cbProjectPath.Size = New System.Drawing.Size(619, 21)
        Me.cbProjectPath.TabIndex = 0
        '
        'btBrowse
        '
        Me.btBrowse.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btBrowse.Location = New System.Drawing.Point(676, 24)
        Me.btBrowse.Name = "btBrowse"
        Me.btBrowse.Size = New System.Drawing.Size(87, 24)
        Me.btBrowse.TabIndex = 1
        Me.btBrowse.Text = "Browse..."
        '
        'btUpdate
        '
        Me.btUpdate.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btUpdate.Location = New System.Drawing.Point(420, 451)
        Me.btUpdate.Name = "btUpdate"
        Me.btUpdate.Size = New System.Drawing.Size(128, 32)
        Me.btUpdate.TabIndex = 5
        Me.btUpdate.Text = "Start cleaning"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.btBrowse)
        Me.GroupBox1.Controls.Add(Me.cbProjectPath)
        Me.GroupBox1.Location = New System.Drawing.Point(14, 14)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(794, 64)
        Me.GroupBox1.TabIndex = 7
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Clean logo of all drawings in this project file:"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.btnRefresh)
        Me.GroupBox2.Controls.Add(Me.LabelNoiParts)
        Me.GroupBox2.Controls.Add(Me.Label6)
        Me.GroupBox2.Controls.Add(Me.Label5)
        Me.GroupBox2.Controls.Add(Me.Label4)
        Me.GroupBox2.Controls.Add(Me.Label3)
        Me.GroupBox2.Controls.Add(Me.Label2)
        Me.GroupBox2.Controls.Add(Me.Label1)
        Me.GroupBox2.Controls.Add(Me.lvDrawData)
        Me.GroupBox2.Controls.Add(Me.btUpdate)
        Me.GroupBox2.Controls.Add(Me.btClose)
        Me.GroupBox2.Location = New System.Drawing.Point(14, 77)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(794, 492)
        Me.GroupBox2.TabIndex = 8
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Drawings:"
        '
        'btnRefresh
        '
        Me.btnRefresh.Location = New System.Drawing.Point(673, 28)
        Me.btnRefresh.Name = "btnRefresh"
        Me.btnRefresh.Size = New System.Drawing.Size(96, 24)
        Me.btnRefresh.TabIndex = 18
        Me.btnRefresh.Text = "Refresh list"
        '
        'LabelNoiParts
        '
        Me.LabelNoiParts.Location = New System.Drawing.Point(13, 35)
        Me.LabelNoiParts.Name = "LabelNoiParts"
        Me.LabelNoiParts.Size = New System.Drawing.Size(192, 15)
        Me.LabelNoiParts.TabIndex = 16
        Me.LabelNoiParts.Text = "No of drawings in project = 0"
        '
        'Label6
        '
        Me.Label6.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label6.Location = New System.Drawing.Point(27, 451)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(88, 15)
        Me.Label6.TabIndex = 15
        Me.Label6.Text = "Cleaning Failed"
        '
        'Label5
        '
        Me.Label5.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label5.Location = New System.Drawing.Point(140, 451)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(134, 15)
        Me.Label5.TabIndex = 14
        Me.Label5.Text = "Cleaning Successful"
        '
        'Label4
        '
        Me.Label4.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label4.Location = New System.Drawing.Point(294, 451)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(72, 15)
        Me.Label4.TabIndex = 13
        Me.Label4.Text = "Not updated"
        '
        'Label3
        '
        Me.Label3.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label3.BackColor = System.Drawing.Color.White
        Me.Label3.Location = New System.Drawing.Point(280, 451)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(16, 15)
        Me.Label3.TabIndex = 12
        '
        'Label2
        '
        Me.Label2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label2.BackColor = System.Drawing.Color.GreenYellow
        Me.Label2.Location = New System.Drawing.Point(127, 451)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(16, 15)
        Me.Label2.TabIndex = 11
        '
        'Label1
        '
        Me.Label1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label1.BackColor = System.Drawing.Color.Red
        Me.Label1.Location = New System.Drawing.Point(13, 451)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(16, 15)
        Me.Label1.TabIndex = 10
        '
        'lvDrawData
        '
        Me.lvDrawData.AllowColumnReorder = True
        Me.lvDrawData.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lvDrawData.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.lvDrawData.CheckBoxes = True
        Me.lvDrawData.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader0, Me.ColumnHeader1, Me.ColumnHeader2})
        Me.lvDrawData.FullRowSelect = True
        Me.lvDrawData.Location = New System.Drawing.Point(16, 62)
        Me.lvDrawData.Name = "lvDrawData"
        Me.lvDrawData.Size = New System.Drawing.Size(761, 382)
        Me.lvDrawData.Sorting = System.Windows.Forms.SortOrder.Ascending
        Me.lvDrawData.TabIndex = 7
        Me.lvDrawData.UseCompatibleStateImageBehavior = False
        Me.lvDrawData.View = System.Windows.Forms.View.Details
        '
        'ColumnHeader0
        '
        Me.ColumnHeader0.Text = ""
        Me.ColumnHeader0.Width = 25
        '
        'ColumnHeader1
        '
        Me.ColumnHeader1.Text = "Drawing Name"
        Me.ColumnHeader1.Width = 348
        '
        'ColumnHeader2
        '
        Me.ColumnHeader2.Text = "Path"
        Me.ColumnHeader2.Width = 489
        '
        'btClose
        '
        Me.btClose.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btClose.Location = New System.Drawing.Point(587, 451)
        Me.btClose.Name = "btClose"
        Me.btClose.Size = New System.Drawing.Size(103, 32)
        Me.btClose.TabIndex = 9
        Me.btClose.Text = "Close"
        '
        'MainWnd
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(816, 578)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.Name = "MainWnd"
        Me.Padding = New System.Windows.Forms.Padding(11)
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Drawing Logo Cleaner"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub MainWnd_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try

            Try
                oApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
            Catch ex As Exception

            End Try

            If oApp Is Nothing Then
                Dim InventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")
                oApp = System.Activator.CreateInstance(InventorAppType)

                oApp.Visible = False
                bCloseInventorApp = True
            End If

            Dim iPrjNum As Integer
            Dim prjFiles As System.Array = Array.CreateInstance(GetType(String), 0)
            strDefaultProject = oApp.FileLocations.FileLocationsFile
            strActiveProject = strDefaultProject
            oApp.FileLocations.ProjectFiles(iPrjNum, prjFiles)


            bFirstLoad = True
            Dim iIndex As Integer
            For iIndex = 0 To iPrjNum - 1
                Dim strProject As String = prjFiles.GetValue(iIndex)
                If strProject = strActiveProject Then
                    cbProjectPath.Items.Insert(0, strProject & " --- (ACTIVE PROJECT)")
                Else
                    cbProjectPath.Items.Insert(0, strProject)
                End If
            Next iIndex
            iIndex = cbProjectPath.FindStringExact(strActiveProject)

            cbProjectPath.Items.Insert(0, "Please select a project file...")
            cbProjectPath.SelectedIndex = 0
            bFirstLoad = False

        Catch ex As Exception

        End Try

    End Sub

    Private Sub GetDrawingFilesFromProject(ByVal strProjectFile As String)

        Try
            ReDim strDrawFileNames(1)
            lNoDrawFiles = 0

            ' Close all opened documents before changing the active project
            If strProjectFile.CompareTo(strActiveProject) <> 0 Then
                Dim oDoc As Document
                For Each oDoc In oApp.Documents
                    oDoc.Close()
                Next
                strActiveProject = strProjectFile
                oApp.FileLocations.FileLocationsFile = strActiveProject
            End If

            Dim strWorkSpace As String = oApp.FileLocations.Workspace
            Dim strWorkGroupNames As System.Array = Array.CreateInstance(GetType(String), 0)
            Dim strWorkGroupPaths As System.Array = Array.CreateInstance(GetType(String), 0)
            Dim lNum As Long
            oApp.FileLocations.Workgroups(lNum, strWorkGroupNames, strWorkGroupPaths)
            Dim strLibraryNames As System.Array = Array.CreateInstance(GetType(String), 0)
            Dim strLibraryPaths As System.Array = Array.CreateInstance(GetType(String), 0)
            oApp.FileLocations.Libraries(lNum, strLibraryNames, strLibraryPaths)

            Dim iIndex As Integer
            GetDrawingFiles(strWorkSpace)

            For iIndex = 0 To strWorkGroupPaths.Length - 1
                GetDrawingFiles(strWorkGroupPaths.GetValue(iIndex))
            Next iIndex

            For iIndex = 0 To strLibraryPaths.Length - 1
                GetDrawingFiles(strLibraryPaths.GetValue(iIndex))
            Next

        Catch ex As Exception

        End Try

    End Sub

    Private Sub GetDrawingFiles(ByVal strRootDir As String)

        Try

            If strRootDir.Length = 0 Then
                Exit Sub
            End If

            Dim strNewDrawFileNames As String() = System.IO.Directory.GetFiles(strRootDir, "*.idw")
            Dim lNoNewDrawFiles As Long = strNewDrawFileNames.Length

            If lNoNewDrawFiles > 0 Then

                If lNoNewDrawFiles > (strDrawFileNames.Length - lNoDrawFiles) Then
                    ReDim Preserve strDrawFileNames(strDrawFileNames.Length + lNoNewDrawFiles - 1)
                End If

                For DrawFileCount As Long = lNoDrawFiles To lNoDrawFiles + lNoNewDrawFiles - 1
                    strDrawFileNames(DrawFileCount) = strNewDrawFileNames(DrawFileCount - lNoDrawFiles)
                Next

                lNoDrawFiles += lNoNewDrawFiles
            End If

            Dim strOldVersion As String
            If strRootDir.EndsWith("\") Then
                strOldVersion = strRootDir & "OldVersions"
            Else
                strOldVersion = strRootDir & "\OldVersions"
            End If

            Dim strDirNames As String() = System.IO.Directory.GetDirectories(strRootDir)
            Dim strDirName As String
            For Each strDirName In strDirNames
                If strDirName.CompareTo(strOldVersion) <> 0 Then
                    GetDrawingFiles(strDirName)
                End If
            Next

        Catch ex As Exception

        End Try

    End Sub

    Private Sub GetNamesinListForActiveProject()

        Try

            Dim lsDrawData As SortedList
            If oMap.ContainsKey(strActiveProject) Then
                lsDrawData = oMap.Item(strActiveProject)
            Else
                lsDrawData = New SortedList
                oMap.Item(strActiveProject) = lsDrawData
            End If

            FileProgressForm.FileProgressBar.Maximum = strDrawFileNames.Length

            oApp.SilentOperation = True

            Dim strDrawFileName As String
            Dim lDrawFileCount As Long = 0
            For Each strDrawFileName In strDrawFileNames

                Try
                    FileProgressForm.FileProgressBar.PerformStep()
                    lDrawFileCount = lDrawFileCount + 1
                    FileProgressForm.Text = "Processing drawings: " & CStr(lDrawFileCount) & " of " & CStr(strDrawFileNames.Length)

                    Dim oDrawDoc As DrawingDocument
                    oDrawDoc = oApp.Documents.Open(strDrawFileName, False)

                    If Not oDrawDoc Is Nothing Then

                        'If oDrawDoc.ComponentDefinition.IsiPartFactory Then

                        'insert a row into the ListView, also add the items into the stored list
                        Dim lvItem As New ListViewItem
                        Dim cacheItem As New CacheData

                        lvItem.Checked = True
                        cacheItem.bChecked = True
                        cacheItem.strUpdateStatus = NOT_UPDATED

                        Dim strItem As String
                        strItem = oDrawDoc.DisplayName

                        lvItem.SubItems.Add(strItem)
                        cacheItem.strDisplayName = strItem

                        strItem = oDrawDoc.FullFileName.Remove(oDrawDoc.FullFileName.LastIndexOf("\"), oDrawDoc.FullFileName.Length - oDrawDoc.FullFileName.LastIndexOf("\"))
                        lvItem.SubItems.Add(strItem)
                        cacheItem.strFullFileName = oDrawDoc.FullFileName

                        lvDrawData.Items.Add(lvItem)

                        If Not lsDrawData.Item(cacheItem.strFullFileName) Is Nothing Then
                            lsDrawData.Item(cacheItem.strFullFileName) = cacheItem
                        Else
                            lsDrawData.Add(cacheItem.strFullFileName, cacheItem)
                        End If

                        'End If

                        oDrawDoc.Close(True)

                    End If

                Catch ex As Exception

                End Try

            Next

            LabelNoiParts.Text = "No of drawings in project = " & CStr(lvDrawData.Items.Count())

            oApp.SilentOperation = False

        Catch ex As Exception

        End Try

    End Sub

    Private Sub MainWnd_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        Try
            Try
                oApp.SilentOperation = True

                If strDefaultProject.CompareTo(strActiveProject) <> 0 Then
                    Dim oDoc As Document
                    For Each oDoc In oApp.Documents
                        oDoc.Close()
                    Next
                    oApp.FileLocations.FileLocationsFile = strDefaultProject
                End If

                oApp.SilentOperation = False
            Catch ex As Exception

            End Try

            Try
                If bCloseInventorApp = True Then
                    oApp.Quit()
                End If
            Catch ex As Exception

            End Try

            ' Clear the cached data
            Dim iIndex As Integer
            For iIndex = 0 To oMap.Count - 1
                Dim lsDrawData As SortedList = oMap.GetByIndex(iIndex)
                lsDrawData.Clear()
            Next iIndex
            oMap.Clear()

        Catch ex As Exception

        End Try

    End Sub

    Private Sub btBrowse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btBrowse.Click

        Try
            OpenFileDialog1.Filter = "Inventor Project files (*.ipj)|*.ipj|All files (*.*)|*.*"

            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                Dim index As Integer = cbProjectPath.FindStringExact(OpenFileDialog1.FileName)
                If index <> -1 Then
                    cbProjectPath.SelectedIndex = index
                Else
                    cbProjectPath.Items.Insert(0, OpenFileDialog1.FileName)
                    cbProjectPath.SelectedIndex = 0
                End If
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btUpdate.Click

        Try

            Dim strProjectFile As String = cbProjectPath.SelectedItem

            If strProjectFile = "Please select a project file..." Then
                Exit Sub
            End If

            If strProjectFile.EndsWith(" --- (ACTIVE PROJECT)") Then
                strProjectFile = strProjectFile.Substring(0, strProjectFile.IndexOf(" --- (ACTIVE PROJECT)"))
            End If

            Dim lsDrawData As SortedList
            If oMap.ContainsKey(strProjectFile) Then
                lsDrawData = oMap.Item(strProjectFile)
            Else
                lsDrawData = New SortedList
                oMap.Item(strProjectFile) = lsDrawData
            End If

            Dim iItemIndex As Integer
            For iItemIndex = 0 To lvDrawData.Items.Count - 1
                Dim lvItem As ListViewItem = lvDrawData.Items(iItemIndex)

                If lvItem.Checked Then

                    Try
                        Dim strPath As String = lvItem.SubItems(2).Text & "\" & lvItem.SubItems(1).Text

                        Dim oDrawDoc As DrawingDocument
                        Dim olelink As ReferencedOLEFileDescriptor

                        oDrawDoc = oApp.Documents.Open(strPath, False)

                        If oDrawDoc Is Nothing Then
                            lvItem.BackColor = System.Drawing.Color.Red
                        Else
                            Try
                                oApp.SilentOperation = True
                                If oDrawDoc.ReferencedOLEFileDescriptors.Count > 0 Then
                                    For Each olelink In oDrawDoc.ReferencedOLEFileDescriptors
                                        If olelink.FullFileName = "D:\INVENTOR\TEMPLATES\LOGO12.bmp" Then
                                            olelink.Delete()
                                            oDrawDoc.Dirty = True
                                            oDrawDoc.Save()
                                            lvItem.BackColor = System.Drawing.Color.GreenYellow
                                        End If
                                    Next olelink
                                End If
                                oDrawDoc.Close()
                                oApp.SilentOperation = False
                            Catch
                                Debug.WriteLine("Cannot clean logo")
                                lvItem.BackColor = System.Drawing.Color.Red
                                oApp.SilentOperation = False
                            End Try
                            lvItem.Checked = False
                        End If
                    Catch ex As Exception

                    End Try

                    If Not (lsDrawData Is Nothing) Then

                        Dim cacheItem As New CacheData
                        cacheItem = lsDrawData.Item(lvItem.SubItems(2).Text & "\" & lvItem.SubItems(1).Text)

                        cacheItem.bChecked = lvItem.Checked
                        If lvItem.BackColor.Equals(System.Drawing.Color.Red) Then
                            cacheItem.strUpdateStatus = UPDATE_FAILED
                        ElseIf lvItem.BackColor.Equals(System.Drawing.Color.GreenYellow) Then
                            cacheItem.strUpdateStatus = UPDATE_SUCCEEDED
                        End If
                        lsDrawData.Item(lvItem.SubItems(2).Text & "\" & lvItem.SubItems(1).Text) = cacheItem

                    End If

                End If

            Next iItemIndex

        Catch ex As Exception

        End Try

    End Sub

    Private Sub cbProjectPath_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbProjectPath.SelectedIndexChanged

        If bFirstLoad Then
            Exit Sub
        End If

        lvDrawData.Items.Clear()

        Dim strProjectFile As String = cbProjectPath.SelectedItem

        If strProjectFile = "Please select a project file..." Then
            Exit Sub
        End If

        If strProjectFile.EndsWith(" --- (ACTIVE PROJECT)") Then
            strProjectFile = strProjectFile.Substring(0, strProjectFile.IndexOf(" --- (ACTIVE PROJECT)"))
        End If

        If oMap.ContainsKey(strProjectFile) Then
            Try

                Dim lsDrawData As SortedList = oMap.Item(strProjectFile)
                Dim iIndex As Integer
                For iIndex = 0 To lsDrawData.Count - 1
                    Try
                        Dim cacheItem As CacheData = lsDrawData.GetByIndex(iIndex)
                        Dim lvItem As New ListViewItem
                        lvItem.Checked = cacheItem.bChecked
                        If cacheItem.strUpdateStatus = UPDATE_FAILED Then
                            lvItem.BackColor = System.Drawing.Color.Red
                        ElseIf cacheItem.strUpdateStatus = UPDATE_SUCCEEDED Then
                            lvItem.BackColor = System.Drawing.Color.GreenYellow
                        ElseIf cacheItem.strUpdateStatus = NOT_UPDATED Then
                            lvItem.BackColor = System.Drawing.Color.White
                        End If
                        lvItem.SubItems.Add(cacheItem.strDisplayName)
                        Dim strItem As String
                        strItem = cacheItem.strFullFileName.Remove(cacheItem.strFullFileName.LastIndexOf("\"), cacheItem.strFullFileName.Length - cacheItem.strFullFileName.LastIndexOf("\"))
                        lvItem.SubItems.Add(strItem)
                        lvDrawData.Items.Add(lvItem)
                    Catch ex As Exception

                    End Try
                Next iIndex

                LabelNoiParts.Text = "No of drawings in project = " & CStr(lvDrawData.Items.Count())
            Catch ex As Exception

            End Try
        Else
            Try
                FileProgressForm = New ProgressIndicatorForm
                FileProgressForm.Show()

                GetDrawingFilesFromProject(strProjectFile)
                GetNamesinListForActiveProject()

                FileProgressForm.Close()
                FileProgressForm = Nothing
            Catch ex As Exception

            End Try

        End If

    End Sub

    Private Sub btSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Try

            Dim lvItem As ListViewItem
            For Each lvItem In lvDrawData.Items
                lvItem.Checked = True
            Next

        Catch ex As Exception

        End Try

    End Sub

    Private Sub btDeselectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Try

            Dim lvitem As ListViewItem
            For Each lvitem In lvDrawData.Items
                lvitem.Checked = False
            Next

        Catch ex As Exception

        End Try

    End Sub

    Private Sub btClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClose.Click

        Me.Close()

    End Sub

    Private Sub lvDrawData_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvDrawData.ColumnClick

        ' Set the ListViewItemSorter property to a new ListViewItemComparer object.
        Me.lvDrawData.ListViewItemSorter = New ListViewItemComparer(e.Column)

        ' Call the sort method to manually sort the column based on the ListViewItemComparer implementation.
        lvDrawData.Sort()

    End Sub



    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

        lvDrawData.Items.Clear()

        Dim strProjectFile As String = cbProjectPath.SelectedItem

        If strProjectFile = "Please select a project file..." Then
            Exit Sub
        End If

        If strProjectFile.EndsWith(" --- (ACTIVE PROJECT)") Then
            strProjectFile = strProjectFile.Substring(0, strProjectFile.IndexOf(" --- (ACTIVE PROJECT)"))
        End If

        Try
            FileProgressForm = New ProgressIndicatorForm
            FileProgressForm.Show()

            GetDrawingFilesFromProject(strProjectFile)
            GetNamesinListForActiveProject()

            FileProgressForm.Close()
            FileProgressForm = Nothing
        Catch ex As Exception

        End Try

    End Sub


End Class

' Implements the manual sorting of items by columns.
Friend Class ListViewItemComparer
    Implements IComparer

    Private col As Integer

    Public Sub New()
        col = 0
    End Sub

    Public Sub New(ByVal column As Integer)
        col = column
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
       Implements IComparer.Compare
        Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
    End Function
End Class

