从文件对话框选择零件插入到装配

本文介绍了一个用于解决Inventor2014中装配插入失败问题的小测试程序。通过创建文件对话框并指定过滤器,程序允许用户从指定目录选择部件文件并将其插入到打开的装配中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有客户报告说在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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值