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

Also, here is a version that uses a preset/hardcoded color, in case that is preferred over the color picker.

 

Just change the hardcoded R, G, B values in bold to change the color

 

You can look up RGB values for colors here:

https://www.rapidtables.com/web/color/RGB_Color.html 

 

Sub Main
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	Dim oColor As Asset = GetColor()

	' Ensure that a color has been selected
	If oColor Is Nothing Then
		MessageBox.Show("No color selected. Exiting the script.")
		Exit Sub
	End If

	' Traverse the assembly and color parts where CompliantWithMachine is False
	Call TraverseAssembly(oOccs, oColor)

	' Show completion message
	MessageBox.Show("Coloring successfully completed.")
End Sub

Sub TraverseAssembly(oOccs As ComponentOccurrences, oColor As Asset)
	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			Call TraverseAssembly(oOcc.SubOccurrences, oColor)
		ElseIf oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then

			'set default 
			isCompliant = True

			Try
				isCompliant = iProperties.Value(oOcc.Name, "Custom", "CompliantWithMachine")
			Catch
				' If the property is missing or an error occurs, do not color the part
			End Try

			If isCompliant = False Then oOcc.Appearance = oColor

		End If
	Next
End Sub

Function GetColor() As Asset

	'hard coded color values
	'https://www.rapidtables.com/web/color/RGB_Color.html
	oRed = 255
	oGreen = 0
	oBlue = 0

	Dim oName As String = "Paint Color"
	Dim oAppearance As Asset

	Try 'create new appearance 
		oAppearance = ThisDoc.Document.Assets.Add(AssetTypeEnum.kAssetTypeAppearance,
		"Generic", "Appearances", oName)
	Catch
		oAppearance = ThisDoc.Document.Assets.Item(oName)
	End Try

	'set colors
	Dim oNewColor As ColorAssetValue
	oNewColor = oAppearance.Item("generic_diffuse")
	oNewColor.Value = ThisApplication.TransientObjects.CreateColor(oRed, oGreen, oBlue)

	Return oAppearance
End Function