Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Replace file and modify occurence name

7 REPLIES 7
Reply
Message 1 of 8
TONELLAL
761 Views, 7 Replies

Replace file and modify occurence name

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 ?

7 REPLIES 7
Message 2 of 8
xiaodong_liang
in reply to: TONELLAL

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

Message 3 of 8
TONELLAL
in reply to: xiaodong_liang

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 ?

Message 4 of 8
xiaodong_liang
in reply to: TONELLAL

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

Message 5 of 8
TONELLAL
in reply to: xiaodong_liang

Here is the code : Sub Renommer_fers() ''Rename all FG components by adding a number at the beginning of the file name. Files are renamed and replaced in the assembly. Occurrences name is modified too. 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 'Browse all files referenced by active assembly '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 it is 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 number to use as prefix for renaming files If Num_aff <> "" Then 'Num_Aff = "" 'If user click 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 filename beginning is different from the prefix, rename and replace 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
Message 6 of 8
xiaodong_liang
in reply to: TONELLAL

Hi, 

 

Sorry, the code is not well formatted. Could you re-post it? Thanks.

Message 7 of 8
TONELLAL
in reply to: xiaodong_liang

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 

Message 8 of 8
xiaodong_liang
in reply to: TONELLAL

The attachment is the test video in Inventor 2013. Please check anything I missed to reproduce your problem.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report