Hi @karram. I do not currently use Vault, and have not used it before either, so I am not sure how having Vault involved in this process of renaming Inventor files will work on your end. I do know that traditionally, when you rename Inventor files, that usually cased a mess. Because then you have to figure out what all other assemblies, drawings, and such were referencing that file, then open each of those other Inventor documents, then fix the now broken reference, and redirect all of those to the renamed file. If Vault is involved, then I assume that this may be easier to deal with, but I am not sure. But I would definitely not be able to write a simple iLogic rule to reach into your Vault file space, and rename files in there, because they would be protected by that Vault system (they would all be like ReadOnly).
However, if those files were all copied locally on your computer, then at least those files may not be seen as ReadOnly. Still, in order for Vault to know that you renamed all those files, and know what to do with those renamed files, that is another story altogether, and I do not know how that would work on your end.
That being said, I do have a highly experimental (and untested) iLogic rule that you could review for ideas towards designing your own solution for this type of task. If you plan on testing this code as it currently is, I highly recommend that you only try this out on files that are completely unrelated to anything production related, and only on files that are not important in any way to start with, because they may get all messed up.
- This code will first launch a file browser dialog, and ask you to select an assembly file to start from.
- If a valid selection is made, it will then open that assembly invisibly (not visible, no document tab).
- Then it will try to get the collection of files that it references.
- Then it will start a process (block of code) that will attempt to iterate through each of those file references, and if any of those file references references any other files, this process will also attempt to iterate through those too (recursively).
- For each file reference:
- Gets its full file name (path, file name, & file extension)
- Check if its file name starts with the 'old prefix' ("0980_").
- If it does, it will create a new full file name (just a String) with that old prefix replaced by the 'new prefix' ("0970_").
- Then checks if any already existing file already has that 'new' full file name.
- If an existing file does exist, it writes that to the iLogic Log window, with a Warning, and does not rename the old file.
- If no existing file with the new name was found, it renames the existing file with the new name.
- If it renamed the file, then it will also try to replace that file reference using the newly renamed file.
- If it fails to rename the file, or fails to replace that file reference, it will also write an error message to the iLogic Log window about this incident.
...At least that was the plan...
Sub Main
Dim sSourceFile As String = UseFileDialog(True, "Select Source Assembly File.")
If sSourceFile = "" Then Return
' ThisApplication.FileManager.IsFileNameValid(sSourceFile)
Dim oSourceDoc As Inventor.Document = ThisApplication.Documents.Open(sSourceFile, False)
Dim oRefFileDescs As Inventor.FileDescriptorsEnumerator = oSourceDoc.File.ReferencedFileDescriptors
RecurseFileDescriptors(oRefFileDescs, AddressOf ProcessFileDescriptor)
oSourceDoc.Update2(True)
oSourceDoc.Save2(True)
End Sub
Function UseFileDialog(Optional bSave As Boolean = False, Optional sTitle As String = vbNullString, _
Optional sInitialDirectory As String = vbNullString, Optional sFileName As String = vbNullString) As String
Dim oFDlg As Inventor.FileDialog : ThisApplication.CreateFileDialog(oFDlg)
If Not String.IsNullOrEmpty(sTitle) Then oFDlg.DialogTitle = sTitle
If String.IsNullOrEmpty(sInitialDirectory) Then
oFDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
Else
oFDlg.InitialDirectory = sInitialDirectory
End If
If Not String.IsNullOrEmpty(sFileName) Then oFDlg.FileName = sFileName
oFDlg.Filter = "Autodesk Inventor Files (*.iam;*.dwg;*.idw;*.ipt) | *.iam;*.dwg;*.idw;*ipt | All files (*.*)|*.*"
oFDlg.MultiSelectEnabled = False : oFDlg.OptionsEnabled = False
oFDlg.InsertMode = False: oFDlg.CancelError = True
Try : If bSave = True Then : oFDlg.ShowSave : Else : oFDlg.ShowOpen : End If : Catch : End Try
Return oFDlg.FileName
End Function
Sub RecurseFileDescriptors(oFileDescs As FileDescriptorsEnumerator, FileDescriptorProcess As Action(Of Inventor.FileDescriptor))
If oFileDescs Is Nothing OrElse oFileDescs.Count = 0 Then Return
For Each oFD As Inventor.FileDescriptor In oFileDescs
FileDescriptorProcess(oFD)
Dim oChildFDs As FileDescriptorsEnumerator = Nothing
If oFD.ReferencedFile IsNot Nothing Then
Try : oChildFDs = oFD.ReferencedFile.ReferencedFileDescriptors : Catch : End Try
If oChildFDs IsNot Nothing AndAlso oChildFDs.Count > 0 Then
RecurseFileDescriptors(oChildFDs, FileDescriptorProcess)
End If
End If
Next
End Sub
Sub ProcessFileDescriptor(oFD As Inventor.FileDescriptor)
If oFD Is Nothing Then Return
If oFD.ReferenceMissing OrElse oFD.ReferenceDisabled Then Return
'<<< only process it if it is a part >>>
If oFD.ReferencedFileType <> FileTypeEnum.kPartFileType Then Return
Dim sFFN As String = oFD.FullFileName
If sFFN = "" Then Return
Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(sFFN)
Dim sExt As String = System.IO.Path.GetExtension(sFFN) 'includes the dot
Dim sOldPrefix As String = "0980_"
Dim sNewPrefix As String = "0970_"
If Not sFileName.StartsWith(sOldPrefix) Then Return
Dim sNewFileName As String = sFileName.Replace(sOldPrefix, sNewPrefix) & sExt
Dim sNewFFN As String = sFFN.Replace(sFileName & sExt, sNewFileName)
If System.IO.File.Exists(sNewFFN) Then
'let user know about it
Logger.Warn(vbCrLf & "The following file already existed:" & vbCrLf & _
sNewFFN & vbCrLf & _
"So, the following original file was not renamed:" & vbCrLf & sFFN)
Else 'a file with the new name does not already exist, so rename it
FileSystem.Rename(sFFN, sNewFFN)
'now you may have to fix the file references referring to the old file name
oFD.ReplaceReference(sNewFFN)
' Logger.Info(vbCrLf & "The following 'original' file:" & vbCrLf & _
' sFFN & vbCrLf & _
' "was renamed to the following:" & vbCrLf & sNewFFN)
End If
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)