0

我是 VB.net 的新手,我正在尝试编写一个与 Autodesk Inventor 一起使用的表单应用程序。

不幸的是,每次我关闭 Inventor 时,“Inventor.exe”进程仍然存在。我在调试时没有意识到这一点,直到整个系统开始滞后后我检查任务管理器时才意识到这一点。杀死每个同名的进程相当简单,但问题是最终用户可能在另一个 Inventor 窗口中打开了单独的文档。所以我需要编写一个函数,只杀死没有打开窗口的 Inventor 进程。

Public Class Form1
    Dim _invApp As Inventor.Application
    Dim _started As Boolean = False
    Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Try
        _invApp = Marshal.GetActiveObject("Inventor.Application")

    Catch ex As Exception
        Try
            Dim invAppType As Type = _
              GetTypeFromProgID("Inventor.Application")

            _invApp = CreateInstance(invAppType)
            _invApp.Visible = True

            'Note: if you shut down the Inventor session that was started
            'this(way) there is still an Inventor.exe running. We will use
            'this Boolean to test whether or not the Inventor App  will
            'need to be shut down.
            _started = True

        Catch ex2 As Exception
            MsgBox(ex2.ToString())
            MsgBox("Unable to get or start Inventor")
        End Try
    End Try

End Sub

我开始一个过程的另一个部分,它是一个特定的 3D 模型文件。

Public Sub SaveAs(oTemplate As String)
    'define the active document
    Dim oPartDoc As PartDocument = _invApp.ActiveDocument
    'create a file dialog box
    Dim oFileDlg As Inventor.FileDialog = Nothing
    Dim oInitialPath As String = System.IO.Path.GetFullPath("TemplatesResources\" & oTemplate)
    _invApp.CreateFileDialog(oFileDlg)

    'check file type and set dialog filter
    If oPartDoc.DocumentType = kPartDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
    ElseIf oPartDoc.DocumentType = kAssemblyDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
    ElseIf oPartDoc.DocumentType = kDrawingDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw"
    End If

    If oPartDoc.DocumentType = kAssemblyDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
    End If

    'set the directory to open the dialog at
    oFileDlg.InitialDirectory = "C:\Vault WorkSpace\Draft"
    'set the file name string to use in the input box
    oFileDlg.FileName = "######-AAAA-AAA-@@"

    'work with an error created by the user backing out of the save
    oFileDlg.CancelError = True
    On Error Resume Next
    'specify the file dialog as a save dialog (rather than a open dialog)
    oFileDlg.ShowSave()

    'catch an empty string in the imput
    If Err.Number <> 0 Then
        MessageBox.Show("Any changes made from here will affect the original template file!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
    ElseIf oFileDlg.FileName <> "" Then
        Dim MyFile As String = oFileDlg.FileName
        'save the file
        oPartDoc.SaveAs(MyFile, False)
        'open the drawing document
        System.Diagnostics.Process.Start(oInitialPath & ".idw")
        Dim oFinalPath As String = oPartDoc.FullFileName
        MessageBox.Show(oFinalPath, "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
        MessageBox.Show("Loaded", "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
        Dim oDrawingDoc As DrawingDocument = _invApp.Documents.ItemByName(oInitialPath & ".idw")
        'oDrawingDoc.SaveAs()

    End If
End Sub

任何帮助表示赞赏。谢谢

4

1 回答 1

1

在您的表单的末尾,可能是 FormClosed,添加以下内容:

  Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    If (_invApp Is Nothing) Then Return ' if empty, then no action needed
    _invApp.Quit()
    Marshal.ReleaseComObject(_invApp)
    _invApp = Nothing
    System.GC.WaitForPendingFinalizers()
    System.GC.Collect()
  End Sub

这应该使 .NET 发布并正确处理 Inventor 的 COM 对象。

并考虑声明 Inventor 变量并分配 Nothing/null,这样更安全(避免错误)

  Dim _invApp As Inventor.Application = Nothing
于 2015-07-24T12:47:14.860 回答