Option Strict Off
Option Explicit On
Friend Class frmAssembly
    Inherits System.Windows.Forms.Form

    Private Sub cmdBrowse_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBrowse.Click
        Dim objApp As Object
        Dim objDocument As Object

        ' Set the flag to hide the read only check box on the open dialog.
        cmnBrowseOpen.ShowReadOnly = False

        ' Display the open dialog.
        cmnBrowseOpen.ShowDialog()

        ' Make sure a filename was specified.
        If cmnBrowseOpen.FileName <> "" Then
            ' Clear the list box.
            lstAssembly.Items.Clear()

            lblStatus.Text = "Processing"
            lblStatus.Refresh()

            ' Create the revision manager object
            objApp = CreateObject("RevisionManager.Application")

            ' Write the top level assembly to the list.
            lstAssembly.Items.Add(cmnBrowseOpen.FileName)

            ' Open the top level assembly.
            objDocument = objApp.Open(cmnBrowseOpen.FileName)

            ' CAll the recursive function to get the full
            ' link tree for the top level assembly.
            ShowAssemblyStructure(objDocument, 1)

            objDocument = Nothing
            objApp = Nothing
        End If

        lblStatus.Text = "Processing Complete"
        lblStatus.Refresh()

    End Sub

    '
    ' Purpose: This subroutine uses the Revision Manager Automation to determine all of the
    '          links that the document being passed in contains.  Each link is written to
    '          the list box.  If a link is an assembly file this function is recursively
    '          called to get the links within the assembly file.
    '
    ' Input: Document - The input Revision Manager document object.
    '        AssemblyDepth - Integer that specifies the current assembly depth.
    '
    ' Output: All output is to the list box.
    '
    Private Sub ShowAssemblyStructure(ByRef Document As Object, ByRef AssemblyDepth As Short)
        Dim i As Short
        Dim objLinkedDocs As Object

        ' Loop through the items in the assembly
        objLinkedDocs = Document.LinkedDocuments
        For i = 1 To objLinkedDocs.Count
            lstAssembly.Items.Add(AssemblyDepth & Space(3 * AssemblyDepth) & objLinkedDocs(i).FullName)
            If UCase(objLinkedDocs(i).fullname.substring(objLinkedDocs(i).fullname.length - 3, 3)) = "ASM" Then
                ' there is a problem in traversing back up through the tree that throws an exception, so using a try-catch.
                Try
                    ShowAssemblyStructure(objLinkedDocs(i), AssemblyDepth + 1)
                Catch ex As Exception
                    'MsgBox("Problem with traversing back up the tree " & objLinkedDocs(i).fullname)
                End Try
            End If
        Next
        objLinkedDocs = Nothing

    End Sub

    Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click

        Close()

    End Sub
End Class