Hide Table of Contents

Change Color of Component in Specific Display State Example (C#)

This example shows how to change the color of a:

  • selected component in a specific display state at the assembly and part levels.
  • component in its part document.
//----------------------------------------------
//Preconditions: Specified assembly and part documents exist.
//
//Postconditions: 
// 1. Opens the specified assembly document.
// 2. Changes the selected component's, USB_cover1
//    assembly-level color from the default red
//    to gray in the specified display state. This
//    is the overriding color level.
// 3. To verify, examine the component in the graphics area.
// 4. Press F5 to continue.
// 5. Changes the selected component's, USB_cover1, part-level
//    color from red to green in the specified display state of
//    the component part. This is not the overriding color level.
// 6. Closes, but does not save, the assembly document.
// 7. Opens the component's part document, USB_cover1.sldprt.
// 8. Examine the graphics area.
// 9. Press F5 to continue.
//10. Changes part's color from red to green in the specified
//    display state of the part. This is the overriding color
//    level.
//11. To verify, examine the graphics area.
//12. Press F5 to continue.
//13. Closes, but does not save, the part document.
//-----------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
 
namespace AppearancesCSharp.csproj
{
 
    partial class SolidWorksMacro
    {
 
        public void Main()
        {
 
            ModelDoc2 swModelDoc = null;
            ModelDocExtension swModelDocExt = null;
            SelectionMgr swSelMgr = null;
            DisplayStateSetting swDisplayStateSetting = null;
            Component2 swComponent = null;
            Component2[] swComponents = new Component2[1];
            string[] displayStateNames = new string[1];
            object appearances = null;
            object[] appearancesArray = null;
            AppearanceSetting swAppearanceSetting = default(AppearanceSetting);
            AppearanceSetting[] newAppearanceSetting = new AppearanceSetting[1];
            ConfigurationManager swConfigMgr = default(ConfigurationManager);
            Configuration swConfig = default(Configuration);
            bool status = false;
            int errors = 0;
            int warnings = 0;
            string fileName = "";
            int red_rgb = 0;
            int green_rgb = 0;
            int blue_rgb = 0;
            int newColor = 0;
 
            //Open assembly document
            fileName = "C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\appearances\\usb_flash_drive1.sldasm";
            swModelDoc = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, ""ref errors, ref warnings);
 
            //Select component, USB_cover1
            swModelDocExt = (ModelDocExtension)swModelDoc.Extension;
            swSelMgr = (SelectionMgr)swModelDoc.SelectionManager;
            status = swModelDocExt.SelectByID2("USB_cover1-1@usb_flash_drive1""COMPONENT", 0, 0, 0, false, 0, null, 0);
            swComponent = (Component2)swSelMgr.GetSelectedObject6(1, -1);
            swComponents[0] = swComponent;
 
            swModelDoc.ClearSelection2(true);
 
            //Get display state 
            swDisplayStateSetting = (DisplayStateSetting)swModelDocExt.GetDisplayStateSetting((int)swDisplayStateOpts_e.swAllDisplayState);
            swDisplayStateSetting.Entities = swComponents;
            swDisplayStateSetting.Option = (int)swDisplayStateOpts_e.swSpecifyDisplayState;
            displayStateNames[0] = "Display State-1";
            swDisplayStateSetting.Names = displayStateNames;
            //Assembly level is default
            swDisplayStateSetting.PartLevel = false;
 
            //Change the selected component's, USB_cover1, 
            //assembly-level color from the default red
            //to gray in the specified display state; this
            //is the overriding color level
            appearances = (object)swModelDocExt.get_DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting);
            appearancesArray = (object[])appearances;
            swAppearanceSetting = (AppearanceSetting)appearancesArray[0];
            red_rgb = 50;
            green_rgb = 50;
            blue_rgb = 50;
            newColor = 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.Color = newColor;
            newAppearanceSetting[0] = (AppearanceSetting)swAppearanceSetting;
            swModelDocExt.set_DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting, newAppearanceSetting);
 
            System.Diagnostics.Debugger.Break();
            //Examine the component in the graphics area
            //Press F5 to continue
 
            swModelDoc.ClearSelection2(true);
 
            status = swModelDocExt.SelectByID2("USB_cover1-1@usb_flash_drive1""COMPONENT", 0, 0, 0, false, 0, null, 0);
            swComponent = (Component2)swSelMgr.GetSelectedObject6(1, -1);
            swComponents[0] = (Component2)swComponent;
 
            swModelDoc.ClearSelection2(true);
 
            //Get display state
            swDisplayStateSetting = swModelDocExt.GetDisplayStateSetting((int)swDisplayStateOpts_e.swAllDisplayState);
            swDisplayStateSetting.Entities = swComponents;
            swDisplayStateSetting.Option = (int)swDisplayStateOpts_e.swSpecifyDisplayState;
            displayStateNames[0] = "<Default>_Display State 1";
            swDisplayStateSetting.Names = displayStateNames;
            //Set to part level
            swDisplayStateSetting.PartLevel = true;
 
            //Change the selected component's, USB_cover1, part-level
            //color from red to green in the specified display state of
            //the component part; this is not the overriding color level
            appearances = (object)swModelDocExt.get_DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting);
            appearancesArray = (object[])appearances;
            swAppearanceSetting = (AppearanceSetting)appearancesArray[0];
            red_rgb = 0;
            green_rgb = 255;
            blue_rgb = 0;
            newColor = 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.Color = newColor;
            newAppearanceSetting[0] = swAppearanceSetting;
            swModelDocExt.set_DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting, newAppearanceSetting);
 
 
            //Close the assembly document without saving
            //changes
            swApp.CloseDoc("usb_flash_drive1");
 
            //Open the assembly component USB_cover1 as a part document
            fileName = "C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\appearances\\usb_cover1.sldprt";
            swModelDoc = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, ""ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModelDoc.Extension;
            swConfigMgr = (ConfigurationManager)swModelDoc.ConfigurationManager;
            swConfig = (Configuration)swConfigMgr.ActiveConfiguration;
 
            swComponents[0] = null;
            swComponents[0] = swConfig.GetRootComponent3(true);
 
            swModelDoc.ClearSelection2(true);
 
            //Get display state
            swDisplayStateSetting = null;
            swDisplayStateSetting = (DisplayStateSetting)swModelDocExt.GetDisplayStateSetting((int)swDisplayStateOpts_e.swAllDisplayState);
            swDisplayStateSetting.Entities = swComponents;
            swDisplayStateSetting.Option = (int)swDisplayStateOpts_e.swSpecifyDisplayState;
            displayStateNames[0] = "<Default>_Display State 1";
            swDisplayStateSetting.Names = displayStateNames;
 
            System.Diagnostics.Debugger.Break();
 
            //Change color of selected component in specified display state
            //from default red to green; this is the overriding color
            appearances = (object)swModelDocExt.get_DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting);
            appearancesArray = (object[])appearances;
            swAppearanceSetting = (AppearanceSetting)appearancesArray[0];
            red_rgb = 0;
            green_rgb = 255;
            blue_rgb = 0;
            newColor = 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.Color = newColor;
            newAppearanceSetting[0] = swAppearanceSetting;
            swModelDocExt.set_DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting, newAppearanceSetting);
 
            System.Diagnostics.Debugger.Break();
            //Examine the graphics area
            //Press F5 to continue
 
            //Close the part document without saving
            //changes
            swApp.CloseDoc("usb_cover1");
 
        }
 
        /// <summary>
        /// The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;
 
 
    }
}


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Change Color of Component in Specific Display State Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.