Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
philippe.lagrue
1217 Views, 16 Replies

Renaming components in assembly

I have tried this code for renaming all components in an assembly :

Class ChgDisplayName
	Public name As String = "Part "
	Public cpt As Integer = 1
	
	
Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oOcc As ComponentOccurrence
	
	'Start a Transaction to bundle all the name changes into a single item in the 'Undo' menu.
	Dim oTransaction As Transaction = ThisApplication.TransactionManager.StartTransaction(oADoc, "Rename Components")
		
	'rename all comps in top level first
	For Each oOcc In oADef.Occurrences
		RenameOcc(oOcc)
	Next
	'now try to rename comps at deeper levels
	For Each oOcc In oADef.Occurrences
		If oOcc.SubOccurrences.Count > 0 Then 'it is a sub-assembly
			'run 'recursive' sub here
			'and supply the SubOccurrences to it
			Iterate(oOcc.SubOccurrences)
		End If
	Next
	'end the Transaction
	oTransaction.End
End Sub

Sub RenameOcc(oComp As ComponentOccurrence)
	'create new variable to enable 'Intellisense' recognition
	Dim oCO As ComponentOccurrence = oComp
	'get the PN
	Dim oCODoc As Document = oCO.Definition.Document
	'Dim oPN As String = oCODoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
	Dim oPN As String = name & cpt
	'check if PN is empty (not filled in)
	If oPN = "" Or oPN = " " Then
		MsgBox("Occurrence '" & oCO.Name & "' has an 'Empty' Part Number." & vbCrLf & _
		"Leaving original component name as it was.", , "")
		'oPN = oCO.Name
		Exit Sub
	End If
	
	'attempt to rename the component
	Dim oWorked As Boolean = False
	Try
		oCO.Name = oPN
	Catch
		Dim oInt As Integer = 0
		Do Until oWorked = True
			oInt = oInt + 1
			Try
				oCO.Name = oPN & ":" & oInt
				oWorked = True
			Catch
				oWorked = False
				If oInt > 10 Then
					oWorked = True
				End If
			End Try
		Loop
	Catch
		MsgBox("Failed to rename:  " & oCO.Name,,"")
	End Try
	
	If oWorked Then
		cpt += 1	
	End If
End Sub

Sub Iterate(oOccs As ComponentOccurrencesEnumerator)
	'create new variable to enable 'Intellisense' recognition
	Dim oComps As ComponentOccurrencesEnumerator = oOccs
	Dim oCO As ComponentOccurrence
	'try to rename all comps at this level first
	For Each oCO In oComps
		RenameOcc(oCO)
	Next
	'now loop through again checking for SubOccurrences, then Iterate
	For Each oCO In oComps
		If oCO.SubOccurrences.Count > 0 Then 'it is a sub-assembly
			Iterate(oCO.SubOccurrences)
		End If
	Next
End Sub

End Class

But it don't work for internal parts of an iAssembly :

2023-06-30_11h26_45.jpg

Do you know why ?

Is there a solution ?