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
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.
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
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.