Macro for Component Appearance Showing Up Black

I am running the exact code posted here: 

2019 SOLIDWORKS API Help - Change Color of Component in Specific Display State Example (VBA) 

This changes the color of a part at the assembly level. However, when I run this, the component color shows up black regardless of the color I change it to. After the macro runs, if I edit the component appearance manually, don't change anything, and press apply...then the correct color appears.

Here is proof of the color it should be:

But here is how it appears in the graphics window:

Note that I am using the usb case part rather than the usb cover part from the tutorial. Otherwise, the code is the exact same. Once I apply a part appearance manually the part is able to change colors as normal using the macro. I have tried both "rebuild" and "redraw" and neither work. I have also tried to record a macro of me editing the part appearance manually and pressing apply but the code for this does not appear in the recorded macro.

I am curious if this happens with other users as well. If not, maybe it is related to a system or document setting. The way I intend to use this macro would involve thousands of parts so I cannot manually edit each one. I am looking for a way to have the color application work the first time.

SolidworksApi/macros
Comments 6
LL
LL 2021-12-21

same pb for me impossible to change the color, the solution is to save the file and the color is ok​

NL
NL 2022-09-13

I'm having the same issue. In the sample assembly, the only part that changes color without having to close and open the assembly is the cover. I've compared the document property settings for the components and there doesn't seem to be anything to account for this.
​

comment imagecomment image
AM
AM 2023-04-01

I also have the same issue…​

BM
BM 2023-04-21

Same issue here

CF
CF 2023-07-26

Same issue, I have add an undo and redo​ like this and it works.

        swModelDocExt.DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting) = newAppearances
        swModelDoc.EditUndo2 (1)
        swModelDoc.EditRedo2 (1)

It's not quick on a large assembly but it seems to be the only way to get the desired result

SF
SF 2024-01-20

The Undo / Redo option is clever, unfortunately I could not get it to work for me and since saving the document(s) is not an option I have resorted to adding a Texture to the documents - which is clumsy and takes ages in assemblies with 1000+ components.


However..

After coming back to the method I've figured out how to get it to work correctly. It is as simple as setting the rest of the color properties for the AppearanceSetting object. This works without any issues on the first go and is very fast.


Between the 2 lines from the example:

     swAppearanceSetting.Color = newColor

     Set newAppearances(0) = swAppearanceSetting


you need to define the rest of the color properties

(Note that my code example is for C#, adjust the values for VBA notation as required)


                swAppearanceSetting.Diffuse = 0.8d;
                swAppearanceSetting.Specular = 0.5d;
                swAppearanceSetting.SpecularColor = newColor;
                swAppearanceSetting.Luminous = 0.1d;
                swAppearanceSetting.Transparent = 0d;


Good luck.