08-13-2024
09:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
08-13-2024
09:59 AM
HI @jeyuan , give this version a try
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
'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
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