111 2

A mere month ago we asked y’all how to go about slapping color across parts in SolidWorks. Unless you have a program to apply a random color to a part when you put it in an assembly, they all have the same curiously drab color. That’s not the diverse kind of 3D modeling world we want to live in, now is it? We need color.

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.

The assembly with virtual parts before the multi-color macro. (Click to enlarge)

The assembly with virtual parts before the multi-color macro. (Click to enlarge)

The assembly with virtual parts after the multi-color macro is used. (Click to enlarge)

The assembly with virtual parts after the multi-color macro is used. (Click to enlarge)

A part with multibodies before the multi-color macro. (Click to enlarge)

A part with multibodies before the multi-color macro. (Click to enlarge)

A part with multibodies after the multi-color macro is used. (Click to enlarge)

A part with multibodies after the multi-color macro is used. (Click to enlarge)

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…
green arrowmulti-color.zip

Thanks again everyone! If you have anything you would like to add to this, hit the comments!

Filed under: CAD