“Replace some” components in Autodesk Inventor 2014

There seems to be a fairly common frustration amongst Inventor users when it comes to replacing components in an assembly. Autodesk have given you two options; “replace component,” or “replace all.” In some scenarios, you may only want to replace a subset of a group of components, but when you multi-select components to replace, the “replace” option disappears from the RMB context menu.

While I can’t say this has really ever bothered me personally, a colleague (and all-round gentleman) recently brought it to my attention and asked if I knew of a workaround. There was nothing in-the-box that I knew about, but I wrote the following macro to do the job.

'Copyright CADPRO Systems 2014 - SelectFile function code taken from Autodesk sample code
Sub Main()
'Make a ref to active doc
Dim doc As Document
Set doc = ThisApplication.ActiveDocument
'Create collection to hold selected components
Dim oSelectedOccs As ObjectCollection
Set oSelectedOccs = ThisApplication.TransientObjects.CreateObjectCollection
Dim i As Long
For i = 1 To doc.selectSet.Count
If doc.selectSet.Item(i).Type = kComponentOccurrenceObject Then
oSelectedOccs.Add doc.selectSet.Item(i)
End If
Next
'Check that more than 1 is selected, otherwise exit
If oSelectedOccs.Count < 2 Then
MsgBox ("Please select more than one component to replace or use standard ""Replace"" command.")
GoTo getOut:
End If
'Create flag to keep track of whether components are all identical
Dim checkSameFlag As Boolean
checkSameFlag = True
'Check that components are the same
Dim selectedOcc As ComponentOccurrence
For Each selectedOcc In oSelectedOccs
If selectedOcc.Definition.Document.InternalName = oSelectedOccs.Item(1).Definition.Document.InternalName Then
Else
checkSameFlag = False
End If
Next
'Provide appropriate feedback
If checkSameFlag = False Then
MsgBox ("Please select multiple instances of same component")
Else
Dim fileForReplacement As String
fileForReplacement = SelectFile()
For Each selectedOcc In oSelectedOccs
Call selectedOcc.Replace(fileForReplacement, False)
Next
End If
getOut:
End Sub
Public Function SelectFile() As String
' 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 = "Select replacement component"
' 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
Dim replacementFile As String
' 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."
replacementFile = oFileDlg.FileName
End If
SelectFile = replacementFile
End Function

If you want to try this out in Inventor 2014, go to the “Options” panel on the “Tools” tab and click “VBA Editor.” Right click the modules folder in the browser and select insert module. Give the module a name and then paste the code into the new module. Save it and then exit the editor.

Inventor VBA editor

VBA Editor – On “Tools” ribbon

Open an assembly and select a few instances of the same component, then go back to the “Options” panel on the “Tools” tab and this time click “Macros.” Select the module name you just created and a file open dialog will appear prompting you to select the file you want to use to replace the selected components. Easy as that.

Macros command

Click “Macros” on the Tools ribbon.

To make it simpler to use, you can right click the ribbon and select “Customize User Commands” and then select your new module from the “Macros” category in the “Choose commands from” dropdown list. I suggest putting it into the “Assembly | Assemble” panel.