Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
JhoelForshav
in reply to: GosponZ

Hi @GosponZ 

If I understand correctly, you want to use the part number as the name of each occurrence in the assembly.

Something like this should do the trick :slightly_smiling_face:

 

Class ThisRule
	Sub Main
		iLogicvb.UpdateWhenDone = True
		Dim def As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
		For Each oOcc As ComponentOccurrence In def.Occurrences
			TraverseAndSetNames(oOcc)
		Next
	End Sub
	Sub RenameOcc(oOcc As ComponentOccurrence)
		Dim partNum As String = oOcc.Definition.Document.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
		If oOcc.Parent.Occurrences.OfType(Of ComponentOccurrence).FirstOrDefault(Function(x) x.Name = partNum Or x.Name.StartsWith(partNum & ":")) Is Nothing
			oOcc.Name = partNum
		ElseIf oOcc.Parent.Occurrences.OfType(Of ComponentOccurrence).FirstOrDefault(Function(x) x.Name = partNum AndAlso x IsNot oOcc) Is Nothing
			oOcc.Name = partNum
		Else
			Dim i As Integer = 1
			For Each eOcc As ComponentOccurrence In oOcc.Parent.Occurrences
				If eOcc.Name = partNum Or eOcc.Name.StartsWith(partNum & ":")
					If eOcc IsNot oOcc
						Dim oName As String = partNum & ":" & i
						Dim existingOcc As ComponentOccurrence = oOcc.Parent.Occurrences.OfType(Of ComponentOccurrence).FirstOrDefault(Function(x) x.Name = oName)
						If existingOcc IsNot Nothing Then existingOcc.Name = oName & "_temp"
						eOcc.Name = oName
						i += 1
					End If
				End If
			Next
			oOcc.Name = partNum & ":" & i
		End If
		
	End Sub
	Sub TraverseAndSetNames(oOcc As ComponentOccurrence)
		RenameOcc(oOcc)
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject
			If oOcc.IsiAssemblyMember
				Dim iAssyMember As iAssemblyMember = oOcc.Definition.iAssemblyMember
				Dim def As AssemblyComponentDefinition = iAssyMember.ReferencedDocumentDescriptor.ReferencedDocument.ComponentDefinition
				For Each sOcc As ComponentOccurrence In def.Occurrences
					TraverseAndSetNames(sOcc)
				Next
				iAssyMember.ReferencedDocumentDescriptor.ReferencedDocument.Save2(True)
			Else
				For Each sOcc As ComponentOccurrence In oOcc.SubOccurrences
					TraverseAndSetNames(sOcc)
				Next
			End If
		End If
	End Sub
End Class

 

 

 

 I had to add numbering to the names if there are multiple occurrences in the same assembly with the same part number. I used a colon as a separator for this (<Part Number>:<Number>). If you use part numbers containing colons maybe use another character for the saparation :slightly_smiling_face: