Friday, December 21, 2012

iLogic Replace Derived Reference

Issue:
You wish there was an option in parts created from derived components to replace the derived component.



Solution:
Here is a quick iLogic rule that will allow you to browse for a new file with which you can replace the derived reference of an existing derived component.

UPDATE:
A reader recently pointed out a limitation of this iLogic snippet stemming from its use of the API's FileDescriptor.ReplaceReference method. This method requires that the file being replaced and
the replacement file must share ancestry, that is to say that one file was saved from the other file, or both files were saved from the same "parent" file. Simply being aware of this will result in predictable results, but if you were attempting to use this snippet to replace two completely different files you might not have been able to get this to work.

Thanks to Peter for taking the time to bring my attention to this!


Dim oDoc as Document
oDoc = ThisDoc.Document
Dim oRefFile As FileDescriptor
Dim oOrigRefName As Object     

For Each oRefFile In oDoc.file.ReferencedFileDescriptors
'get the full file path to the original internal references
oOrigRefName = oRefFile.FullFileName

'present a File Selection dialog
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.InitialDirectory = oOrigRefName
oFileDlg.CancelError = True
On Error Resume Next
oFileDlg.ShowOpen()
If Err.Number <> 0 Then
Return
ElseIf oFileDlg.FileName <> "" Then
selectedfile = oFileDlg.FileName
End if

‘replace the reference
oRefFile.ReplaceReference (selectedfile)     
InventorVb.DocumentUpdate()
oOrigRefName = “”                                        
Next

iLogicVb.UpdateWhenDone = True