Hi @harrisons22K9B. Although the task of renaming components within an assembly is fairly common, it can also get fairly complicated, depending on your expectations/needs. I noticed that in your example, you are injecting some text with two individual numbers within it, between the file's name, and the file's file extension. What would each of those two numbers represent, and how/why/when do you expect them to increment? How many levels deep is your assembly (components within sub assemblies are another level deeper, not top level components)? If your assembly is multiple levels deep, then do you expect the component renaming task to step down into those deeper levels? If so, how should that effect the naming convention?
In my mind, I'm thinking that maybe the first number should only increment for each unique document being represented within the assembly by components. And I'm also thinking that maybe the second number would continue to increase by 1 for every component within the assembly, so that the last component to be renamed would contain a number equal to the total number of components in the assembly. But I don't know if that's what you had in mind or not.
Here is an untested iLogic rule example, in which I am attempting to apply my ideas, as mentioned above, to the assembly to rename all of its top level only components (does not go down into sub assemblies).
Sub Main
'specify which document to target
Dim oADoc As AssemblyDocument = ThisDoc.Document
'get ComponentDefinition contains all components, geometry, features, etc
oADef = oADoc.ComponentDefinition
'get all top level components
oOccs = oADef.Occurrences
'create a new empty Dictionary to hold list of Document objects paired with a number
'we will use this to help us process any duplicates properly
'(multiple components can be representing the same document)
Dim oDocs As New Dictionary(Of Inventor.Document, Integer)
'create a variable to represent the number to pair with each document
Dim oDocNum As Integer = 0
'define main parts of text to add to every component name
'this is the first part of that text to add in
Dim oAddStart As String = " (Num-"
'and this is a variable to represent the first number
Dim oAdd1stNum As Integer
'and this is a variable to represent the second number
Dim oAdd2ndNum As Integer
For Each oOcc As ComponentOccurrence In oOccs
'get the document that this component represents
Dim oOccDoc As Document = oOcc.ReferencedDocumentDescriptor.ReferencedDocument
'check if it is in our list yet
If Not oDocs.Keys.Contains(oOccDoc) Then
'it was not in the list yet, so add it with next available number
'increment our oDocNum variable by 1
oDocNum = oDocNum + 1
'each Dictionary entry has a Key and a Value
'add the document object as Key, then oDocNum as Value in this entry
oDocs.Add(oOccDoc, oDocNum)
'assign this new oDocNum value to our oAdd1stNum variable
oAdd1stNum = oDocNum
Else
'it was already in the list, so get its number from the list
'we can get the Dictionary entry's Value, using the associated Key
oAdd1stNum = oDocs.Item(oOccDoc)
End If
'get document file name, without path, but with file extension
Dim oFileName As String = System.IO.Path.GetFileName(oOccDoc.FullFileName)
'get file extension, (with leading dot (.))
Dim oExt As String = System.IO.Path.GetExtension(oOccDoc.FullFileName)
'put all the text together that we will be adding to the component name
Dim oAddAll As String = oAddStart & oAdd1stNum & "-" & oAdd2ndNum & ")"
'now put the component name together
Dim oNewOccName As String = oFileName & oAddAll & oExt
Try
'try to assign the new name to the component
oOcc.Name = oNewOccName
'if that worked, then increment the second number, for use in next loop
oAdd2ndNum = oAdd2ndNum + 1
Catch oEx As Exception
'it failed to assign the new name to the component
'so write something to the Logger about this failure
Logger.Error("Error trying to rename Occ from '" & oOcc.Name & "' to '" & oNewOccName & "'." & vbCrLf & _
oEx.Message & vbCrLf & oEx.StackTrace)
End Try
'loop back to start of our 'For Each' statement (next component)
Next
'past loop now, no more components to process, end of main code
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) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)