Hello,
I need to rename and replace occurences in an assembly, then modify the occurence name.
I rename and replace only Farme generator components, which is tested by <condiftion>.
What I want to do :
- Num = ABC
- Original filename = toto.ipt
- New filename = ABC_toto.ipt, and Occurence name = ABC_toto
The code :
fileDir contain the file path
docFName contain the original file name (for example, toto.ipt)
Num contain the prefix to add to the name and the occurence name (for example, ABC)
If <condition>Then
New_name = fileDir & Num & "_" & docFName
Call oFileNameOcc.SaveAs(New_name, True)
Call oOccurrence.Replace(New_name, False)
oOccurrence.name = Num & "_" & oOccurrence.name
End If
The problem is, the first time I use the macro, the prefix is added twice in the occurence name (the filename is ok) : I obtain ABC_ABC_toto. This happen only the first time, and only on an orrignal Frame Generator assembly.
If I use the macro again, with for example Num = 123, the result is : 123_ABC_ABC_toto.Where is the problem ?
Hi,
using a simple assembly with a frame generator and the code below, I did not hit the problem you mentioned. The attachment is a snapshot.
Sub test()
Dim oTopAss As AssemblyDocument
Set oTopAss = ThisApplication.ActiveDocument
Dim oTopDef As AssemblyComponentDefinition
Set oTopDef = oTopAss.ComponentDefinition
oTopDef.Occurrences(2).Name = "123" + oTopDef.Occurrences(2).Name
Dim oSubAss As AssemblyDocument
Set oSubAss = oTopDef.Occurrences(2).Definition.Document
Dim oSubDef As AssemblyComponentDefinition
Set oSubDef = oSubAss.ComponentDefinition
oSubDef.Occurrences(2).Name = "123" + oSubDef.Occurrences(2).Name
End Sub
Perhaps is it because I replace the file and rename the occurrence ?
Or a 2010 bug...
oOccurrence = openDoc.AllReferencedDocuments.ComponentDefinition.Occurrences
To get the occurence document, your code use "oOccurrence.Definition.Document", mine use "oOccurrence.ReferencedDocumentDescriptor.ReferencedDocument".
What is the difference ?
I replaced with ReferencedDocumentDescriptor.ReferencedDocument as below. It still works. I have no Inventor 2010 installed. If you could post your code demo, I could give a try at my Inventor 2013.
Sub test()
Dim oTopAss As AssemblyDocument
Set oTopAss = ThisApplication.ActiveDocument
Dim oTopDef As AssemblyComponentDefinition
Set oTopDef = oTopAss.ComponentDefinition
oTopDef.Occurrences(2).Name = "123" + oTopDef.Occurrences(2).Name
Dim oSubAss As AssemblyDocument
'Set oSubAss = oTopDef.Occurrences(2).Definition.Document
Set oSubAss = oTopDef.Occurrences(2).ReferencedDocumentDescriptor.ReferencedDocument
Dim oSubDef As AssemblyComponentDefinition
Set oSubDef = oSubAss.ComponentDefinition
oSubDef.Occurrences(2).Name = "123" + oSubDef.Occurrences(2).Name
End Sub
Hi,
Sorry, the code is not well formatted. Could you re-post it? Thanks.
I think it is better :
Sub Renommer_fers()
'''Rename all components ao a Frame Generator assembly by adding a prefix at the beginning
'''Files are renamed and replaced in the assembly. The occurrence name is modified.
Dim openDoc As Document
Dim docFile As Document
Dim oFileNameOcc As Document
Dim oOccurrence As ComponentOccurrence
Dim Num_aff As String
Dim Debut_nom_fichier As String
Dim Nouveau_nom As String
Set openDoc = ThisApplication.ActiveDocument
For Each docFile In openDoc.AllReferencedDocuments 'Parcourir tous les fichiers référencés par l'assemblage courant
'Format file name
Dim FNamePos As Long
FNamePos = InStrRev(docFile.FullFileName, "\", -1)
Dim docFName As String
Dim fileDir As String
docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
'Is it a FG component ?
On Error Resume Next
Dim Frame_OK As Boolean
Frame_OK = False
Frame_OK = docFile.ComponentDefinition.AttributeSets.Item(1).name = "com.autodesk.FG"
On Error GoTo 0
'If it is an assembly and a FG component...
If docFile.DocumentType = kAssemblyDocumentObject And Frame_OK Then
Num_aff = InputBox("Traitement de " & docFile.FullFileName, "Renommage des profilés", "Numéro d'affaire") 'Ask for prefix, to rename parts
If Num_aff <> "" Then 'Num_Aff = ""if user clicked Cancel
For Each oOccurrence In docFile.ComponentDefinition.Occurrences 'Browse occurrences
Set oFileNameOcc = oOccurrence.ReferencedDocumentDescriptor.ReferencedDocument
FNamePos = InStrRev(oFileNameOcc.FullFileName, "\", -1)
fileDir = Left(oFileNameOcc.FullFileName, FNamePos)
docFName = Right(oFileNameOcc.FullFileName, Len(oFileNameOcc.FullFileName) - FNamePos)
Debut_nom_fichier = Left(docFName, Len(Num_aff))
If Debut_nom_fichier <> Num_aff Then 'If the beginning of the file name is different from the prefix, rename and replace the part
Nouveau_nom = fileDir & Num_aff & "_" & docFName
Call oFileNameOcc.SaveAs(Nouveau_nom, True)
Call oOccurrence.Replace(Nouveau_nom, False)
'oOccurrence.name = Num_Aff & "_" & oOccurrence.name
oOccurrence.name = Left(Num_aff & "_" & docFName, Len(Num_aff & "_" & docFName) - 4)
End If
Next
End If
End If
Next
End Sub
The attachment is the test video in Inventor 2013. Please check anything I missed to reproduce your problem.