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

A quick update to the previous example, this one only colors components named in a list

 

 

Imports System.ComponentModel
AddReference "System.drawing"
Imports System.Windows.Forms
Imports System.Drawing
Sub Main

	Dim myList As New List(Of String)(New String() {"Foo", "Foo Too" }) 'list with part names

	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	Dim oColor As Asset = GetColor
	Call TraverseAssembly(oOccs, oColor, myList)

End Sub

Sub TraverseAssembly(oOccs As ComponentOccurrences, oColor As Asset, myList As List(Of String))

	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			Call TraverseAssembly(oOcc.SubOccurrences, oColor, myList)
		ElseIf oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
			If myList.Contains(oOcc.Name) 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
	End If

	oName = "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