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

Hi Curtis, 

 

Thank you so much! The code works very well. Now I'm trying to improve the code where the user does not need to supply a list of part names as strings separated by comma, because usually an assembly has too many parts that needs to be colored.

 

I'm adding the functionality where instead of MyList, the code will parse through the assembly, read the iProperty information, and color the part where "CompliantWithMachine" = No. 

 

The "Compliant with Machine" is a custom iProperty, the type is Yes or No. I'm trying to make the code do three things:

1. If Compliant with Machine = No, color the part

2. If Compliant with Machine = Yes, don't color the part

3. If Compliant with Machine doesn't exist for a part, don't color the part. 

My current code is below:

Imports System.ComponentModel
AddReference "System.drawing"
Imports System.Windows.Forms
Imports System.Drawing
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
			Try
				' Get the "CompliantWithMachine" property value as a Boolean
				Dim CompliantWithMachine As Boolean = False ' Default to False in case of any issues
				CompliantWithMachine = oOcc.Definition.PropertySets("Inventor User Defined Properties")("CompliantWithMachine").Value
				' Check the property value
				If Not CompliantWithMachine Then
					' If property = False (equivalent to "No"), color the part
					oOcc.Appearance = oColor
				Else

				End If

			Catch
				' If the property is missing or an error occurs, do not color the part
			End Try
		End If
	Next
End Sub

Function GetColor() As Asset
	Dim oClDlg As New System.Windows.Forms.ColorDialog
	Dim oColor As System.Drawing.Color
	If oClDlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
		oColor = oClDlg.Color
	Else
		Return Nothing
	End If

	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(oColor.R, oColor.G, oColor.B)

	Return oAppearance
End Function

When I run the code, no parts are colored. 
When I add debugging messages, it shows that all parts are identified as the property doesn't exist, which is not true. 

Could you help me with this issue? Thank you so much!