Hello everyone,
I need to change the color of an ASM with a macro, without touching the color of the parts, so really at the ASM level, visually in the build tree it's the appearance right next to the ASM name.
After several hours of research I finally came across a macro, which I adapted to my sauce, but when I run it, it just happens... Nothing :
Sub main()
Dim swApp As SldWorks.SldWorks
Dim Modeldoc2 As SldWorks.Modeldoc2
Dim swAss As SldWorks.AssemblyDoc
Dim vMatProp As Variant
Set swApp = Application.SldWorks
Set Modeldoc2 = swApp.ActiveDoc
vMatProp = Modeldoc2.MaterialPropertyValues
If Modeldoc2.GetType = swDocASSEMBLY Then
Set swAss = Modeldoc2
vMatProp = Modeldoc2.MaterialPropertyValues
vMatProp(0) = 2 'Red
vMatProp(1) = 0 'Green
vMatProp(2) = 0 'Blue
vMatProp(3) = 1 / 2 + 0.5 'Ambient
vMatProp(4) = 1 / 2 + 0.5 'Diffuse
vMatProp(5) = 1 'Specular
vMatProp(6) = 1 * 0.9 + 0.1 'Shininess
Modeldoc2.MaterialPropertyValues = vMatProp
Else
MsgBox ("Use this macro on an assembly")
Exit Sub
End If
'Redraw to see new color
Modeldoc2.GraphicsRedraw2
End Sub
If I replace Modeldoc2.GetType = swDocASSEMBLY with Modeldoc2.GetType = swDocPART it works, but on the PART (logical), I concluded that the problem could come from Modeldoc2.MaterialPropertyValues = vMatProp which may not be suitable for use on an ASM.
Does anyone have a solution please?
Hello;
Looking to replace materials or appearances (colors)?
If you want to create groups of appearances, it is best to use the Evaluate/Visualize Assembly tool...
-> see 3dexperience.3ds.com/ search visualization assembly (Solidworks Forum)
Kind regards.
1 Like
Hello
Thank you for the link but unfortunately the assembly visualization will not be of much help to me in this case.
I need to simply replace the appearance, like this:
In the active ASM, regardless of its file name.
Kind regards.
To change this color, the only way I found was to create a look with the desired color and then change the look at the assembly level:
Debug.Print "Changement de couleur de l'assemblage"
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swAppearance As SldWorks.RenderMaterial
Dim vMat(8) As Double
Dim nDecalID As Long
Set swModelDocExt = swModel.Extension
swModel.ShowConfiguration2 (V(i))
Set swAssembly = swModel
vMat(0) = 0 * 255#
vMat(1) = 128 * 255#
vMat(2) = 0 * 255#
vMat(3) = 1
vMat(4) = 1
vMat(5) = 0.5
vMat(6) = 0.4
vMat(7) = 0
vMat(8) = 0
Set swAppearance = swModelDocExt.CreateRenderMaterial("U:\Entreprise\Service BE\1-Commun service\Solidworks\Macros\Améliorations\Fichiers en lien avec macro\indice-abc.p2m")
bRet = swAppearance.AddEntity(swModel)
bRet = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)
swModel.MaterialPropertyValues = vMat
swModel.GraphicsRedraw2
If you find better, I'll eventually take it!
Hello sbadenis,
After several attempts I managed to adapt your solution and here is what it looks like for me:
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.Modeldoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swAppearance As SldWorks.RenderMaterial
Dim nDecalID As Long
Dim vMatProp As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
vMatProp = swModel.MaterialPropertyValues
If swModel.GetType = swDocASSEMBLY Then
Set swModelDocExt = swModel.Extension
Set swAssembly = swModel
vMatProp = swModel.MaterialPropertyValues
vMatProp(0) = 2 'Red
vMatProp(1) = 0 'Green
vMatProp(2) = 0 'Blue
vMatProp(3) = 1 / 2 + 0.5 'Ambient
vMatProp(4) = 1 / 2 + 0.5 'Diffuse
vMatProp(5) = 1 'Specular
vMatProp(6) = 1 * 0.9 + 0.1 'Shininess
Set swAppearance = swModelDocExt.CreateRenderMaterial("")
bRet = swAppearance.AddEntity(swModel)
bRet = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)
swModel.MaterialPropertyValues = vMatProp
swModel.GraphicsRedraw2
Else
MsgBox ("Utiliser cette macro sur un assemblage")
Exit Sub
End If
'Redraw to see new color
swModel.GraphicsRedraw2
End Sub
Apparently the command Modeldoc2.MaterialPropertyValues = vMatProp (which is now swModel.MaterialPropertyValues = vMatProp), is only used to apply parameters to an already existing appearance, which works on any part that, by default, must already have an appearance, but this is not the case for ASMs. That's why with an ASM, before assigning it a color, you must first create the "blank" appearance using these lines (and of course variables related to them (Dim As, Set...)):
Set swAppearance = swModelDocExt.CreateRenderMaterial("")
bRet = swAppearance.AddEntity(swModel)
bRet = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)
It should also be noted that Set swAppearance = swModelDocExt.CreateRenderMaterial("") no longer points to a file, obviously it's not necessary in my case.
Anyway now it works, thank you very much for your help.
1 Like
@acapulco24 thank you for your answer, if you have a solution to remove it I'm also interested ;-), but thank you for your solution, perfectly functional indeed.