Dim swApp As Object
Sub main()
Dim swModel As ModelDoc2
Dim swModel2 As ModelDoc2
Dim vMatProp As Variant
Dim swCustProp As CustomPropertyManager
Dim val As String
Dim valout As String
Dim bool As Boolean
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
vMatProp = swModel.MaterialPropertyValues
If swModel.GetType = swDocPART Then
MsgBox ("Cette macro n'est à utiliser que sur les assemblages.")
Exit Sub
ElseIf swModel.GetType = swDocASSEMBLY Then
vElementArr = swModel.GetComponents(True)
For Each vElement In vElementArr
Set swElement = vElement
Set swModel2 = swElement.GetModelDoc2
Set swCustProp = swModel2.Extension.CustomPropertyManager("")
bool = swCustProp.Get4("famille de composant", False, val, valout)
If valout = "1" Then
Randomize
vMatProp(0) = Rnd * 0.05 + 0.95 'Red
vMatProp(1) = 0.77 * Rnd + 0.05 'Green
vMatProp(2) = (1 - 2 * Abs(0.45 - vMatProp(1))) * Rnd + 2 * Abs(0.45 - vMatProp(1)) '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
End If
Next
ElseIf swModel.GetType = swDocDRAWING Then
MsgBox ("Cette macro n'est à utiliser que sur les assemblages.")
Exit Sub
End If
swModel.GraphicsRedraw2
End Sub
To retrieve the value of the variable on the part, it must be considered as loaded in Solidworks, so in resolved mode, is this the case in your assembly?
And I think I can answer instead of @Cyril.f, he never told you to replace "ElseIf swModel.GetType = swDocASSEMBLY Then" with "If Valout = "1" then" but answered your question "But where do I specify the value "1" looking for this property?". The solution he gives is exactly the same as mine but without the concrete example...
For the remark "Val is a "non-optional argument", it means that you don't "type" your variable so put a line "Dim Val as String" before ... The same goes for the variable "valout" of course.
This means that the custom property must be called Component Family, its evaluated value must be 1 , and it must be in the Customize tab . If it is found in the custom properties specific to the configuration then it is not quite the same code that is needed because you have to specify the name of the configuration in which to look for the value, such as the configuration named Default in the following line:
Set swCustProp = swModel2.Extension.CustomPropertyManager("Default").
I just tried with the proposed code, without adding anything, and, oh joy, it works... I don't know where I screwed up on my first try. Sorry for questioning your expertise.
I'll try to add a line to change the parts to solved.
Finally, do you have an idea to apply color at the level of the part and not the assembly component?
"To retrieve the value of the variable on the part, it must be considered as loaded in Solidworks, so in solved mode, is this the case in your assembly?"
which crashes because the macro does not find the ModelDoc2 of a component for one reason or another (lite mode, deleted state, ...) which returns an error of this type. I see that the error handling has not been done :-) ... As a reminder, the macros given here are only for example and must be reworked to at least add error handling...
To improve the thing, I'm going to try to insert a line to switch everything to solved mode. I found this function: "LightweightAllResolved". I'll try to insert it, ideally with a dialog box to confirm.
And the recurring question: if I want to apply color to parts instead of components, I have a hunch that I need to replace this line vElementArr = swModel.GetComponents
by some chode of the kind Set swCompModel = swApp.ActivateDoc (or ActivateDoc2 or ActivateDoc3...?) Am I on the right track?