Imports System.IO
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objSEApplication As SolidEdgeFramework.Application = Nothing
Dim objDocuments As SolidEdgeFramework.Documents = Nothing
Dim objAssemblyDoc As SolidEdgeAssembly.AssemblyDocument = Nothing
Dim objPart As SolidEdgeAssembly.Occurrence = Nothing
Dim objSubOcc As SolidEdgeAssembly.SubOccurrence = Nothing
Dim objFolder As DirectoryInfo
Dim objFileInfo As FileInfo
Dim bRecheckMissingFilesArray() As Boolean
Dim bRecheckMissingFilesVerifyArray() As Boolean = {True, True, False, True}
Dim bRecheckMissingFileSubOccArray(1) As Boolean
Dim bRecheckMissingFilesSubOccVerifyArray() As Boolean = {True, True}
Dim index As Integer
Dim i As Integer
Dim occCount As Integer
Dim subOccCount As Integer
Dim strDocFiles As String
Try
objFolder = GetTrainingFolder()
objFileInfo = New FileInfo(Path.Combine(objFolder.FullName, "Coffee Pot.asm"))
' Create/get the application with specific settings
'objSEApplication = Marshal.GetActiveObject("SolidEdge.Application")
If objSEApplication Is Nothing Then
objSEApplication = Activator.CreateInstance(Type.GetTypeFromProgID("SolidEdge.Application"))
objSEApplication.Visible = True
End If
'Makes all the files as missing when the document is open
objSEApplication.ResolveLink = False
strDocFiles = objFileInfo.FullName
objAssemblyDoc = objSEApplication.Documents.Open(strDocFiles)
occCount = objAssemblyDoc.Occurrences.Count
ReDim bRecheckMissingFilesArray(objAssemblyDoc.Occurrences.Count - 1)
objSEApplication.ResolveLink = True
' Recheck will try to search for the file and load it again
For index = 1 To objAssemblyDoc.Occurrences.Count
objPart = objAssemblyDoc.Occurrences.Item(index)
bRecheckMissingFilesArray(index - 1) = objPart.RecheckMissingFile
Next index
'Sub occurrence
'Rename the file before opening the file and then after opening the file rename it back to original.
'In this way we can run recheck missing files API
Dim directoryPath As String = Path.GetDirectoryName(strDocFiles)
Dim sourcepath As String = directoryPath
Dim DestPath As String = IO.Path.GetTempPath() + "ReCheck"
'Create a temporary directory and copy the files from the server.Makes changes in the temporary directory.
If Not Directory.Exists(DestPath) Then
Directory.CreateDirectory(DestPath)
Else
My.Computer.FileSystem.DeleteDirectory(DestPath, _
FileIO.DeleteDirectoryOption.DeleteAllContents)
Directory.CreateDirectory(DestPath)
End If
For Each file__1 As String In Directory.GetFiles(sourcepath)
Dim dest As String = Path.Combine(DestPath, Path.GetFileName(file__1))
File.Copy(file__1, dest)
Next
Dim filePath As String = strDocFiles
filePath = DestPath + "\" + "Strainer Top.par"
Dim asmFile As String = DestPath + "\Coffee Pot.asm"
Call objAssemblyDoc.Close()
My.Computer.FileSystem.RenameFile(filePath, "strainerRecheck.par")
objAssemblyDoc = objSEApplication.Documents.Open(asmFile)
filePath = DestPath + "\" + "strainerRecheck.par"
My.Computer.FileSystem.RenameFile(filePath, "Strainer Top.par")
'Recheck API for Suboccurrence
Dim subOccName As String
For index = 1 To objAssemblyDoc.Occurrences.Count '4 occurrences
objPart = objAssemblyDoc.Occurrences.Item(index)
If (objPart.Type = SolidEdgeFramework.ObjectType.igSubAssembly) Then
If objPart.Name = "strainer.asm:1" Then
subOccCount = objPart.SubOccurrences.Count
ReDim bRecheckMissingFileSubOccArray(subOccCount - 1)
For i = 1 To subOccCount
objSubOcc = objPart.SubOccurrences.Item(i)
subOccName = objSubOcc.Name
bRecheckMissingFileSubOccArray(i - 1) = objSubOcc.RecheckMissingFile
Next i
End If
End If
Next index
Call objAssemblyDoc.Close()
If Directory.Exists(DestPath) Then
My.Computer.FileSystem.DeleteDirectory(DestPath, _
FileIO.DeleteDirectoryOption.DeleteAllContents)
End If
For index = 0 To subOccCount - 1
If (bRecheckMissingFileSubOccArray(index) <> bRecheckMissingFilesSubOccVerifyArray(index)) Then
MsgBox("RecheckMissingFile property of Occurrence object fails")
Exit For
End If
Next index
'SubOccurrence verify
For index = 0 To occCount - 1
If (bRecheckMissingFilesArray(index) <> bRecheckMissingFilesVerifyArray(index)) Then
MsgBox("RecheckMissingFile property of Occurrence object fails")
Exit For
End If
Next index
Catch ex As Exception
MessageBox.Show(ex.ToString)
Clipboard.SetText(ex.ToString)
End Try
End Sub
Function GetTrainingFolder() As DirectoryInfo
Dim objInstallData As SEInstallDataLib.SEInstallData = Nothing
Dim objInstallFolder As DirectoryInfo = Nothing
Dim objTrainingFolder As DirectoryInfo = Nothing
Try
objInstallData = New SEInstallDataLib.SEInstallData
objInstallFolder = New DirectoryInfo(objInstallData.GetInstalledPath())
objTrainingFolder = New DirectoryInfo(Path.Combine(objInstallFolder.Parent.FullName, "Training"))
Catch
Finally
If Not (objInstallData Is Nothing) Then
Marshal.FinalReleaseComObject(objInstallData)
objInstallData = Nothing
End If
End Try
Return objTrainingFolder
End Function
End Class