So, you responded with a lot of great options and Guilherme C. Webster sent in one that not only added color to parts in an assembly, but also added color to multibodies in a part. Rock. and. ROLL.
Very nice Guilherme, and a huge thanks to everyone who contributed to the conversation. If you missed it there’s some great macro hackin’ fun in the comments for randomized color. Here are the results you’ll get with the macro and the macro to download and play with yourself. As you see it even works with virtual parts. Aw yeah.




Here’s the code if you would like to cute & paste.
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swElement As Object
Dim vElementArr As Variant
Dim vElement As Variant
Dim vMatProp As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
vMatProp = swModel.MaterialPropertyValues
'Get all elements
If swModel.GetType = swDocPART Then
vElementArr = swModel.GetBodies2(swAllBodies, False)
For Each vElement In vElementArr
Set swElement = vElement
Randomize
vMatProp(0) = Rnd 'Red
vMatProp(1) = Rnd 'Green
vMatProp(2) = Rnd 'Blue
vMatProp(3) = Rnd / 2 + 0.5 'Ambient
vMatProp(4) = Rnd / 2 + 0.5 'Diffuse
vMatProp(5) = Rnd 'Specular
vMatProp(6) = Rnd * 0.9 + 0.1 'Shininess
swElement.MaterialPropertyValues2 = vMatProp
Next
ElseIf swModel.GetType = swDocASSEMBLY Then
vElementArr = swModel.GetComponents(False)
For Each vElement In vElementArr
Set swElement = vElement
Randomize
vMatProp(0) = Rnd 'Red
vMatProp(1) = Rnd 'Green
vMatProp(2) = Rnd 'Blue
vMatProp(3) = Rnd / 2 + 0.5 'Ambient
vMatProp(4) = Rnd / 2 + 0.5 'Diffuse
vMatProp(5) = Rnd 'Specular
vMatProp(6) = Rnd * 0.9 + 0.1 'Shininess
swElement.MaterialPropertyValues = vMatProp
Next
ElseIf swModel.GetType = swDocDRAWING Then
MsgBox ("You can only apply random colors to part bodies or assembly components.")
Exit Sub
End If
'Redraw to see new color
swModel.GraphicsRedraw2
End Sub
Here’s the macro download. Just download, extract, and via your macro toolbar in SolidWorks, play the macro with an assembly or part (with multibodies) open…
Thanks again everyone! If you have anything you would like to add to this, hit the comments!