Zmiana koloru części w złożeniu

0

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();
        }
    }
}

0

Kompletnie nie ogarniam tematu Solidworks, ale troche ogaraniam temat null reference exceptionow. Z moich obserwacji wynika, ze tego typu excptiony pojawiaja sie czesto wtedy, kiedy chcesz sie dobrac do jakiejs skladowej obiektu, ktory jest nullem ;) Zakladajac, ze ten exception leci przy probie przeczytania elementu 0 z tablicy appearancesArray, mozna wnioskowac, ze tablica jest nullem. Podejrzenie pada, ze to co przyszlo z metody get_DisplayStateSpecMaterialPropertyValues jest nullem (byc moze dlatego, ze argument ktory do niej wysylasz jest zle zrobiony, albo po prostu dla tej konfiguracji danych metoda zwraca nulla) - ale to juz trzeba sprawdzic w debugu :)

0

Tak, zzgadza się ta linijka zwraca null.

 appearances = (object)modelExt.get_DisplayStateSpecMaterialPropertyValues(swDisplayStateSetting);

Problem w tym dlaczego tak jest i co z tym zrobić:)

0

Pojecia nie mam;) O ile nie ma zadnej dokumentacji ktora wyjasnia kiedy ta metoda zwraca nulla, to mozna wziac do lapy jakis dekompiler, np darmowy JetBrains dotPeek, zdekompilowac to assembly w ktorym ta metoda jest zdefiniowana i zobaczyc o co tam chodzi. Ewentaualnie podgladni w debugu ten obiekt ktory wysylasz, moze ma cos nieustawione czy cos

1 użytkowników online, w tym zalogowanych: 0, gości: 1