有客户报告说在Inventor 2014里,用文件对话框选择文件,插入到装配里,会失败。但他没有提供代码样例。我做了个小测试,一切正常。贴于此或许有用户用得着。
Sub selectPartAndPlace()
'assume an assembly is opened
Dim oAssDoc As AssemblyDocument
Set oAssDoc = ThisApplication.ActiveDocument
' Create a new FileDialog object.
Dim oFileDlg As FileDialog
Call ThisApplication.CreateFileDialog(oFileDlg)
' Define the filter to select part and assembly files or any file.
oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
' Define the part and assembly files filter to be the default filter.
oFileDlg.FilterIndex = 1
' Set the title for the dialog.
oFileDlg.DialogTitle = "Open File Test"
' Set the initial directory that will be displayed in the dialog.
oFileDlg.InitialDirectory = "C:\Temp"
' Set the flag so an error will be raised if the user clicks the Cancel button.
oFileDlg.CancelError = True
' Show the open dialog. The same procedure is also used for the Save dialog.
' The commented code can be used for the Save dialog.
On Error Resume Next
oFileDlg.ShowOpen
' oFileDlg.ShowSave
' If an error was raised, the user clicked cancel, otherwise display the filename.
If Err Then
MsgBox "User cancelled out of dialog"
ElseIf oFileDlg.FileName <> "" Then
'MsgBox "File " & oFileDlg.FileName & " was selected."
Dim oAssDef As AssemblyComponentDefinition
Set oAssDef = oAssDoc.ComponentDefinition
Dim oM As Matrix
Set oM = ThisApplication.TransientGeometry.CreateMatrix()
Dim oNewOcc As ComponentOccurrence
Set oNewOcc = oAssDef.Occurrences.Add(oFileDlg.FileName, oM)
'if you need to ground it
'oNewOcc.Grounded = True
End If
End Sub