SW Macro Color

Hello

I want to apply a color to the last body created by macro

The problem is that although the macro seems to work, the visual is black

Ex here green color chosen via 3 custom properties, in the settings it's green, but visually it's black, it's only if I close and re-open the file that the color is displayed well

Who has an idea?

 Sub coul()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim valout As String
Dim R As String
Dim G As String
Dim B As String
Dim vProp As Variant
Dim bool As Boolean
Dim swModelDocExt As ModelDocExtension
Dim swCustPropMgr  As SldWorks.CustomPropertyManager
Dim swFeat As SldWorks.Feature

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
Set swCustPropMgr = swModelDocExt.CustomPropertyManager("")
Set swFeat = swModel.Extension.GetLastFeatureAdded
    
'vProp = swFeat.GetMaterialPropertyValues2(swInConfigurationOpts_e.swThisConfiguration, Empty)
vProp = swFeat.GetMaterialPropertyValues2(1, Empty)

bool = swCustPropMgr.Get4("couleur_R", False, R, valout)
bool = swCustPropMgr.Get4("couleur_G", False, G, valout)
bool = swCustPropMgr.Get4("couleur_B", False, B, valout)

vProp(0) = R / 255
vProp(1) = G / 255
vProp(2) = B / 255
'vProp(3) = 0 'Ambient
'vProp(4) = 0 'Diffuse
'vProp(5) = 0 'Specularity
'vProp(6) = 0 'Shininess
'vProp(7) = 0 'Transparency
'vProp(8) = 0 'Emission
 
'swFeat.SetMaterialPropertyValues2 vProp, swInConfigurationOpts_e.swAllConfiguration, Empty
swFeat.SetMaterialPropertyValues2 vProp, swAllConfiguration, Empty
swModel.ClearSelection2 True

End Sub

 


piece_1.png
piece_1_a.png

Hello

Unless I'm mistaken, we have to rebuild.

Before swModel.Clearselection, we should add:

swModel.Forcerebuild3 False

 

3 Likes

Hello

I just tried it doesn't work

FYI the 3 custom properties:

couleur_R = "0"

couleur_G = "255"

couleur_B = "0"

Good evening

Could the anomaly be due to the fact that the SolidWorks function concerned by the macro is a blackbody in the physical sense of the term, since no light value is assigned to it? The associated settings default to 0.
It should be enough to give values to the variables vProp(3) to vProp(8) to illuminate the body.
I imagine that when reloading the file, the body is assigned the light values of the room, which is why its color reappears.

And there was light.

Kind regards

1 Like

Hello

No color is applied before launching the macro, it is only by saving, closing and reopening the file that the colors are present

The problem is the same if I run exactly this macro (by changing the file path), concerning the color of the functions

http://help.solidworks.com/2019/english/api/sldworksapi/change_color_of_component_in_specific_display_state_example_vb.htm

Hello

As m.blt explained  , there is no light (the vProp(3) values to 8 are at -1 by default so = no light).

I tested with the code below it gives me a lot of green on the last extrusion. On the other hand, the code as written applies to the function and not to the body.

Sub coul()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim valout As String
Dim R As String
Dim G As String
Dim B As String
Dim vProp As Variant
Dim bool As Boolean
Dim bret As Boolean
Dim swModelDocExt As ModelDocExtension
Dim swCustPropMgr  As SldWorks.CustomPropertyManager
Dim swFeat As SldWorks.Feature

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
Set swCustPropMgr = swModelDocExt.CustomPropertyManager("")
Set swFeat = swModel.Extension.GetLastFeatureAdded
'vProp = swFeat.GetMaterialPropertyValues2(swInConfigurationOpts_e.swThisConfiguration, Empty)
vProp = swFeat.GetMaterialPropertyValues2(1, Empty)

bool = swCustPropMgr.Get4("couleur_R", False, R, valout)
bool = swCustPropMgr.Get4("couleur_G", False, G, valout)
bool = swCustPropMgr.Get4("couleur_B", False, B, valout)

R = 0
G = 255
B = 0
vProp(0) = R / 255
vProp(1) = G / 255
vProp(2) = B / 255
vProp(3) = 1 'Ambient
vProp(4) = 1 'Diffuse
vProp(5) = 0.5 'Specularity
vProp(6) = 0 'Shininess
vProp(7) = 0 'Transparency
vProp(8) = 0 'Emission
 
swFeat.SetMaterialPropertyValues2 vProp, swInConfigurationOpts_e.swAllConfiguration, Empty

 'swFeat.SetMaterialPropertyValues2 vProp, swAllConfiguration, Empty
 swModel.ForceRebuild3 False
swModel.ClearSelection2 True

End Sub

 

Hello

strangely for me SW 2021 SP5.1 it doesn't work

The function remains black until saved, closed, reopened


animation.gif

I had tested on SW2020, on SW2021 SP5.1 I added the value 0.1 to the vProp(6) parameter and the color appears correctly.

After that, if you really want to apply to the body and not to the function, you have to look at this link: Get Material Property Values Example (VBA) - 2021 - SOLIDWORKS API Help

It remains a mystery to me

If I macro apply the above settings and open to manually change the specular/blur dispersion to changed value: 0.004999999989

If I set the correct value by hand to 0.6875, the green color is no longer the same as if I had chosen the green color manually without a macro, it's a dark green

 

If I change the amount of reflection to 0.2 by macro, when I open it is written 0.1

I think we are entering into the mysteries of SW and its management of certain parameters. When you can already see the difference in behaviour between 2020 and 2021.

After that, he probably makes adjustments himself to balance the light on the object.