Here below is the updated version of the code. It sould now also work for multi level assemblies (you can disable it by commenting out the blue lines).
It no longer goes throught every occurrence, so it sould be a bit fater.
Here's the whole functionality of the code:
1) Set's focus on currently open (active) document
2) Goes throught every refferenced document...
- If the document is an assembly, it goes throught every of it's refferenced documents
- Renames it and saves under new name
- Writes in the new PartNumber
- Replaces the refference in the assembly
3) Renames, updates PartNumber and saves under new name the top assembly (can be disabled by commenting out the green text)
Let me know if this is what you wanted the code to do and/or if there're any mistakes or errors.
Also this is the VBA version of the code (it took some time to translate), so let me know if you want the VB.Net version instead.
Sub Test()
ThisApplication.SilentOperation = True
Dim TextToFind As String
TextToFind = "CFB"
Dim TextToReplace As String
TextToReplace = "AAACFB"
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
Call ReplaceSubs(oDoc, TextToFind, TextToReplace)
Dim oNewName As String
oNewName = oDoc.FullFileName
oNewName = GetNewName(oNewName, TextToFind, TextToReplace)
oDoc.SaveAs oNewName, False
UpdatePartNumber(oDoc)
oDoc.Save
ThisApplication.SilentOperation = False
End Sub
Public Sub ReplaceSubs(oDoc As Inventor.Document, TextToFind As String, TextToReplace As String)
Dim oRefFile As FileDescriptor
For Each oRefFile In oDoc.File.ReferencedFileDescriptors
Dim oName As String
oName = oRefFile.FullFileName
Dim aDoc As Inventor.Document
Set aDoc = ThisApplication.Documents.Open(oName, False)
If aDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
Call ReplaceSubs(aDoc, TextToFind, TextToReplace)
End If
Dim oNewName As String
oNewName = GetNewName(oName, TextToFind, TextToReplace)
aDoc.SaveAs oNewName, False
UpdatePartNumber(aDoc)
aDoc.Save
aDoc.Close (True)
oRefFile.ReplaceReference (oNewName)
Next
End Sub
Public Function GetNewName(oName As String, TextToFind As String, TextToReplace As String) As String
Dim FNP As Integer
FNP = InStrRev(oName, "\", -1)
Dim oPath As String
oPath = Left(oName, FNP)
Dim oNewName As String
oNewName = Mid(oName, FNP + 1)
GetNewName = Replace(oNewName, TextToFind, TextToReplace)
End Function
Public Sub UpdatePartNumber(oDoc As Inventor.Document)
Dim PN As String
PN = oDoc.FullFileName
Dim FNP As Integer
FNP = InStrRev(PN, "\", -1)
PN = Mid(PN, FNP + 1)
PN = Left(PN, Len(PN) - 4)
iProperty(oDoc, "Part Number").Expression = PN
End Sub
Public Function iProperty(oDoc As Inventor.Document, oProp As String) As Inventor.Property
Dim iPro As Inventor.Property
On Error GoTo IPCatch
Dim oPropsets As PropertySets
Set oPropsets = oDoc.PropertySets
Dim oPropSet As PropertySet
Set oPropSet = oPropsets.Item("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")
iProperty = oPropSet.Item(oProp)
IPCatch:
Call oPropSet.Add("", oProp)
iProperty = oPropSet.Item(oProp)
End Function
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods