using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
namespace test
{
class Program
{
public static SldWorks swApp;
static void Main(string[] args)
{
ConnectSw();
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;
fileName = "D:\\C# SolidWorks Tools\\tutorial\\appearances\\usb_flash_drive1.sldasm";
swModelDoc = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
System.Diagnostics.Debugger.Break();
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);
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;
swDisplayStateSetting.PartLevel = false;
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();
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);
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;
swDisplayStateSetting.PartLevel = true;
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);
swApp.CloseDoc("usb_flash_drive1");
fileName = "D:\\C# SolidWorks Tools\\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);
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();
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();
swApp.CloseDoc("usb_cover1");
}
public static void ConnectSw()
{
if (swApp == null)
{
Process[] processArr = null;
processArr = Process.GetProcessesByName("SLDWORKS");
if (processArr.Length > 0)
{
try
{
swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
}
catch (Exception)
{
}
if (swApp == null)
{
for (int i = 18; i < 28; i++)
{
try
{
swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application." + i.ToString());
if (swApp != null)
{
break;
}
}
catch (Exception)
{
}
}
}
if (swApp == null)
{
try
{
Guid myGuid1 = new Guid("6894540C-3171-484F-9E97-6A962559BA30");
object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromCLSID(myGuid1));
swApp = (SldWorks)processSW;
}
catch (Exception)
{
}
}
if (swApp == null)
{
try
{
Guid myGuid1 = new Guid("B4875E89-91F6-4124-BB63-2539727E98FA");
object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromCLSID(myGuid1));
swApp = (SldWorks)processSW;
}
catch (Exception)
{
}
}
if (swApp == null)
{
try
{
swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.ISldWorks");
}
catch (Exception)
{
}
}
}
}
if (swApp == null)
{
}
if (swApp == null)
{
try
{
swApp = new SldWorks();
}
catch (Exception)
{
}
}
if (swApp != null)
{
if (!swApp.Visible)
{
try
{
swApp.Visible = true;
}
catch
{
swApp = null;
}
}
}
if (swApp == null)
{
throw new Exception("无法连接到Solidworks,请检查!");
}
}
}
}