Witam, chce aby program, po naciśnieciu przycisku w GUI, zmieniał kolor zaznaczonej części złożenia, otworzonego w Solidworks. W oparciu o dokumentacje Solidworks Api napisałem program, który niestety wyrzuca wyjątek w linijce, którą zaznaczyłem w kodzie. Jeżeli ktoś ogarnia temat (a raczej nie jest on zbyt popularny) to prosze o pomoc.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SldWorks;
using SwCommands;
using SwConst;
using SWPublished;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SldWorks.SldWorks swApp;
ModelDoc2 swModel;
SelectionMgr swSelMgr;
DisplayStateSetting swDisplayStateSetting = null;
AppearanceSetting[] newAppearanceSetting = new AppearanceSetting[1];
string[] displayStateNames = new string[1];
object appearances = null;
object[] appearancesArray = null;
AppearanceSetting swAppearanceSetting = default(AppearanceSetting);
Component2 swComponent = null;
Component2[] swComponents = new Component2[1];
int red_rgb = 0;
int green_rgb = 0;
int blue_rgb = 0;
int newColor = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
swApp = (SldWorks.SldWorks)Marshal.GetActiveObject("SldWorks.Application");
swModel = (ModelDoc2)swApp.ActiveDoc;
swSelMgr = (SelectionMgr)swModel.SelectionManager;
if (swModel.GetType() != (int)swDocumentTypes_e.swDocASSEMBLY)
{
MessageBox.Show("Wymagany dokument złożenia!");
return;
}
for (int i = 1; i <= swSelMgr.GetSelectedObjectCount2(-1); i++)
{
//Component2 comp = (Component2)swSelMgr.GetSelectedObjectsComponent3(i, -1);
swComponent = (Component2)swSelMgr.GetSelectedObject6(i, -1);
swComponents[0] = swComponent;
if (swComponent != null)
{
ModelDoc2 model = (ModelDoc2)swComponent.GetModelDoc();
var modelExt = (ModelDocExtension)model.Extension;
if (model.GetType() == (int)swDocumentTypes_e.swDocPART)
{
swDisplayStateSetting = (DisplayStateSetting)modelExt.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)modelExt.get_DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting);
appearancesArray = (object[])appearances;
swAppearanceSetting = (AppearanceSetting)appearancesArray[0];//null reference exception
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;
modelExt.set_DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting, newAppearanceSetting);
}
}
}
swModel.EditRebuild3();
}
}
}