Ejemplo n.º 1
0
        // Set COLOR
        public void SetColor(string configname, string hex, string coatingtype, string coatingclass, SldWorks swapp)
        {
            try
            {
                var colorsql = new ToSQL();

                if (hex == "")
                {
                    colorsql.SetRalSql(configname, "", "", "", false, swapp);
                    //return;
                }

                else
                {
                    if (swapp == null)
                    {
                        swapp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
                    }

                    _swmodel = swapp.ActiveDoc;

                    ModelDocExtension swModelDocExt = null;

                    DisplayStateSetting swDisplayStateSetting = null;
                    var                  swComponents         = new Component2[1];
                    object[]             displayStateNames    = null;
                    object               appearances          = null;
                    object[]             appearancesArray     = null;
                    var                  swAppearanceSetting  = default(AppearanceSetting);
                    var                  newAppearanceSetting = new AppearanceSetting[1];
                    ConfigurationManager swConfigMgr          = default(ConfigurationManager);
                    Configuration        swConfig             = default(Configuration);

                    CheckedDisplayStatesToConfig(swapp);

                    int nbrDisplayStates = 0;

                    swModelDocExt = (ModelDocExtension)_swmodel.Extension;
                    swConfigMgr   = (ConfigurationManager)_swmodel.ConfigurationManager;
                    swConfig      = (Configuration)swConfigMgr.ActiveConfiguration;

                    swComponents[0] = null;
                    swComponents[0] = swConfig.GetRootComponent3(true);

                    _swmodel.ClearSelection2(true);

                    //Get display state
                    swDisplayStateSetting          = (DisplayStateSetting)swModelDocExt.GetDisplayStateSetting((int)swDisplayStateOpts_e.swAllDisplayState);
                    swDisplayStateSetting.Entities = swComponents;
                    swDisplayStateSetting.Option   = (int)swDisplayStateOpts_e.swSpecifyDisplayState;
                    // Get the names of display states
                    displayStateNames = (object[])swConfig.GetDisplayStates();

                    nbrDisplayStates = swConfig.GetDisplayStatesCount();

                    for (var i = 0; i <= (nbrDisplayStates - 1); i++)
                    {
                        var displayStateName = (string)displayStateNames[i];

                        displayStateNames[0]        = displayStateName; //"<Default>_Состояние отображения 1";
                        swDisplayStateSetting.Names = displayStateNames;
                    }

                    //Change color of selected component in specified display state
                    //from default red to green; this is the overriding color
                    appearances         = swModelDocExt.DisplayStateSpecMaterialPropertyValues[swDisplayStateSetting];
                    appearancesArray    = (object[])appearances;
                    swAppearanceSetting = (AppearanceSetting)appearancesArray[0];

                    //Color myColor = Color.FromArgb(0xBEBD7F);
                    var myColor = ColorTranslator.FromHtml("#" + hex);

                    int redRgb   = myColor.R;
                    int greenRgb = myColor.G;
                    int blueRgb  = myColor.B;

                    int newColor = Math.Max(Math.Min(redRgb, 255), 0) + Math.Max(Math.Min(greenRgb, 255), 0) * 16 * 16 + Math.Max(Math.Min(blueRgb, 255), 0) * 16 * 16 * 16 * 16;

                    swAppearanceSetting.Color = newColor;
                    newAppearanceSetting[0]   = swAppearanceSetting;
                    _swmodel.ClearSelection2(true);
                    swModelDocExt.DisplayStateSpecMaterialPropertyValues[swDisplayStateSetting] = newAppearanceSetting;

                    colorsql.SetRalSql(configname, hex, coatingtype, coatingclass, true, swapp);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "; " + ex.StackTrace);
            }
        }