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: 

Rename all occurrences and suboccurrences names in browser tree error

5 REPLIES 5
Reply
Message 1 of 6
daniel.puchta
602 Views, 5 Replies

Rename all occurrences and suboccurrences names in browser tree error

Hello,

I am renaming all occurrences and suboccurrences names by this VBA macro, it recursively renames all occurrences.

 

Sub Main()
Call RenameBrowserTree_Call
End Sub
Sub RenameBrowserTree_Call()

        If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then
            ThisApplication.ScreenUpdating = True
            Exit Sub
        End If
        
        Dim oAsmDoc As AssemblyDocument
        Set oAsmDoc = ThisApplication.ActiveDocument
        Dim oAsmCompDef As AssemblyComponentDefinition
        Dim oOccurrence As ComponentOccurrence
        Dim oSubOccurrence As ComponentOccurrence
        Dim oPuvodniuroven As String
        Set oAsmCompDef = oAsmDoc.ComponentDefinition
        
        Call RenameBrowserTree(oAsmCompDef.Occurrences)
        
        ThisApplication.ScreenUpdating = True
        
    End Sub

    Sub RenameBrowserTree(Occurrences As ComponentOccurrences)
        Dim Pozice_podtrzitek As String
        Dim Pozice_dvojtecky As String
        Dim oKonec_nazvu As String
        Dim oOccDocument As Inventor.Document
        For Each oOccurrence In Occurrences
            Pozice_podtrzitek = InStr(oOccurrence.Name, "__")
                    If Pozice_podtrzitek <> 0 Then
                        Pozice_dvojtecky = InStr(oOccurrence.Name, ":")
                        If Pozice_dvojtecky <> 0 Then
                            oKonec_nazvu = Right(oOccurrence.Name, Len(oOccurrence.Name) - Pozice_dvojtecky + 1)
                        Else
                            oKonec_nazvu = ""
                        End If

                        
                        'On Error Resume Next
                        'Err.Clear
                        oOccurrence.Definition.Document.Update
                        oOccurrence.Name = Left(oOccurrence.Name, Pozice_podtrzitek - 1) & oKonec_nazvu

                        'If Err.Number <> 0 Then
                        '    Times_to_rename = Times_to_rename + 1
                        '    oOccurrence.Definition.Document.Update
                        '    oOccurrence.Parent.Document.Open
                        '    oOccurrence.Name = Left(oOccurrence.Name, Pozice_podtrzitek - 1) & oKonec_nazvu
                        'End If
                    End If
            If oOccurrence.DefinitionDocumentType = kAssemblyDocumentObject Then
                Call RenameBrowserTree(oOccurrence.SubOccurrences) 'Rekurzivní volání - volá, dokud se nedostane na nejnižší úroveň komponent
            End If
        Next

End Sub

 

But when I have Top level assembly, and in there I have Assembly A with parts, then I have Assembly B, which contains Assembly A and other files, VBA macro throws this error message: Run-time error: Method of Name of object ComponentOccurrenceProxy failed.

It does this when trying to rename Assembly A in context of Assembly B (note that Assembly A has been renamed in Top-level Assembly). Funny thing is that when I open document of Assembly B  and run there the same macro, it works. Also I noticed that it has no problem with runing newly created Inventor 2022 files, but it throws error when the files are originally created in INV 2020 and then migrated to INV 2022.

Please, do you have any idea how to solve this? What this could be caused by?

Thank you 🙂

Labels (3)
5 REPLIES 5
Message 2 of 6
CattabianiI
in reply to: daniel.puchta

ComponentOccurrence[Proxy].Name property fails if you try to set it with the current name.

Add a check before setting the name:

Dim newName As String = Left(oOccurrence.Name, Pozice_podtrzitek - 1) & oKonec_nazvu
if newName = oOccurrence.Name then
  continue for
end if
oOccurrence.Name = newName

 

Message 3 of 6
daniel.puchta
in reply to: CattabianiI

Thank you for quick reply. 

Check added, it tries to name occurrence with different name but it fails and throws the same error when it tries in subassembly to rename assembly/part occurrence which has been already renamed in Top level assembly 

Message 4 of 6
CattabianiI
in reply to: daniel.puchta

I was trying to ignore the Inv 2022 thing but it looks like also the inventor command Rename Browser Nodes has some issues doing recursion in certain case.
Do you have model states or migrated Lods (which are model states!) in your files?
Could you please post here the simplest assembly where you can reproduce the issue?

Message 5 of 6
CattabianiI
in reply to: daniel.puchta

Are you sure you're getting the error setting the name now?
Sometimes occ.Definition can throw exception. And why are you updating the document?

Could you please run this rule on your assembly?

Sub main()
	Dim thisAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	TraverseAssemblyRule.iLogicLogger = Logger
	TraverseAssemblyRule.TraverseAssembly(thisAsmDoc.ComponentDefinition.Occurrences) 	
End Sub

Class TraverseAssemblyRule			
	Public Shared iLogicLogger As IRuleLogger		
	Public Shared Sub TraverseAssembly(Occurrences As ComponentOccurrences) 
		Dim oOcc As ComponentOccurrence 
		
		For Each oOcc In Occurrences 
           	Try
			oOcc.Definition.Document.Update
		Catch ex As Exception
			iLogicLogger.Error(ex.ToString)
		End Try
			
		Dim randomOccName As String = oOcc.Name + "_" + IO.Path.GetRandomFileName()
		Try
			oOcc.Name = randomOccName
		Catch
			iLogicLogger.Error("Error setting {0} in {1}.", oOcc.Name, randomOccName)
		End Try
			
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then 
			TraverseAssembly(oOcc.SubOccurrences) 
		End If 			
		
		Next 
	End Sub

End Class

  

Message 6 of 6
daniel.puchta
in reply to: CattabianiI

Sorry for not commenting for a while,

in our company we decided that we are able to continue working with old data in 2020 and then new work we will do on 2022. If I try the same rule which  trigerred error in 2020 it works fine, also if I try it on new date made in 2022, it works fine too. 

So problem is really in migrating data from 2020 to 2022. @CattabianiI  your rule worked there but my original rule not.. 

For us the problem is now not existing because we will work only with native 2020 data or native 2022 data. But thank you for your help  

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report