AnsweredAssumed Answered

Black face when setting appearance from API

Question asked by Daniel Masarin on May 21, 2018

Hello people,

 

I'm in trouble trying to use the Solidworks API to set appearances to a parts face. I followed the example here but when I execute the code I get a black face like the image below.

 

black-face-solidworks.PNG

 

Here is the code I am running:

 

ModelDoc2 model = part as ModelDoc2;
Configuration configuration = model.GetActiveConfiguration();
string[] displayStates = (string[])configuration.GetDisplayStates();
string defaultState = displayStates.First(state => state != DEFAULT_STATE_NAME);

if(!displayStates.Contains(DEFAULT_STATE_NAME))
      configuration.CreateDisplayState(DEFAULT_STATE_NAME);

ModelDocExtension swModelDocExt = model.Extension;
DisplayStateSetting swDisplayStateSetting = swModelDocExt.GetDisplayStateSetting((int)swDisplayStateOpts_e.swThisDisplayState);
swDisplayStateSetting.Entities = faces;
swDisplayStateSetting.Option = (int)swDisplayStateOpts_e.swThisDisplayState;
string[] displayStateNames = new string[1] { DEFAULT_STATE_NAME };
swDisplayStateSetting.Names = displayStateNames;
swDisplayStateSetting.PartLevel = true;

object appearances = (object)swModelDocExt.DisplayStateSpecMaterialPropertyValues[swDisplayStateSetting];
object[] appearancesArray = (object[])appearances;
AppearanceSetting swAppearanceSetting = (AppearanceSetting)appearancesArray[0];
int red_rgb = 255;
int green_rgb = 255;
int blue_rgb = 0;
swAppearanceSetting.Color = Math.Max(Math.Min(red_rgb, 255), 0) + Math.Max(Math.Min(green_rgb, 255), 0) * 16 * 16 + Math.Max(Math.Min(blue_rgb, 255), 0) * 16 * 16 * 16 * 16;
swAppearanceSetting.Diffuse = 1;

AppearanceSetting[] newAppearanceSetting = new AppearanceSetting[1];
newAppearanceSetting[0] = swAppearanceSetting;

swModelDExt.DisplayStateSpecMaterialPropertyValues[swDisplayStateSetting] = newAppearanceSetting;

 

Someone could help me understand what is happening?

 

Thanks

Outcomes