I added comments to my original code.
Dim doc As AssemblyDocument = ThisDoc.Document
' the first loop is to find each pattern
For Each pattern As OccurrencePattern In doc.ComponentDefinition.OccurrencePatterns
' in each pattern we loop over all elemetns
For Each patternElement As OccurrencePatternElement In pattern.OccurrencePatternElements
' get the name of the element (e.g. "Element:123")
Dim elementName As String = patternElement.Name
' split the name in parts. use ":" as split char
' e.g. {"Element" , "123"}
Dim nameParts = elementName.Split(":") ' this creates an array
' the element number is the last part of the name.
' e.g. "123"
Dim number = nameParts(nameParts.Count - 1)
' loop over all occurrences in the element.
For Each occ As ComponentOccurrence In patternElement.Occurrences
' get the name of the occurrence (e.g. "SomeOccurrence:456"
Dim orgOccName = occ.Name
' split the name in parts. use ":" as split char
' e.g. {"SomeOccurrence" , "456"}
Dim occNameParts = orgOccName.Split(":")' this creates an array
' replace the last part with the number of the element.
' e.g. the array will now be
' {"SomeOccurrence" , "123"}
occNameParts(occNameParts.Count - 1) = number
' Join the parts of the array to 1 string
' e.g. "SomeOccurrence:123"
Dim newOccName = String.Join(":", occNameParts)
' the try/catch block will handel exeptions that may occure
Try
' Try to rename the occurence with the new name.
occ.Name = newOccName
Catch ex As Exception
' if an exception was thrown then its handeld by
' showing a message
MsgBox(String.Format("Could not rename occurence: '{0}' to '{1}'. (Probably the name was already used)",
orgOccName, newOccName))
End Try
Next
Next
Next
(if you want to see an other example of the try/catch block checkout this blog post by @Anonymous )
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Blog: hjalte.nl - github.com