8 Replies Latest reply on Nov 22, 2012 6:14 AM by Robin Staes

    C# Add-In TaskPane Button displays Windows Form, when Form Button pressed code does not execute, displays error

    Keith Condon

      Hello,

      I am have been able to add a task pane successfully into SolidWorks, however the windows form I call on with the taskpane, which has been created by a user control, appears but when the button is selected the code found in the Form5 button will not run and displays this error.  Does anyone know or have a solution to this error?

      Thank you for your time and efforts in this matter!!!!!!!!!!

      I have a user control cs file named SWTaskpaneHost.cs With code as follows

       

      using System;

      using System.Collections.Generic;

      using System.ComponentModel;

      using System.Drawing;

      using System.Data;

      using System.Linq;

      using System.Text;

      using System.Windows.Forms;

       

      using SolidWorks.Interop.sldworks;

      using SolidWorks.Interop.swcommands;

      using SolidWorks.Interop.swconst;

      using SolidWorks.Interop.swpublished;

       

      using SolidWorksTools;

       

      using System.Runtime.InteropServices;

       

       

      namespace RaceWork.V1._09192012

      {

          [ComVisible(true)]

          [ProgId(SWTASKPANE_PROGID)]

          public partial class SWTaskpaneHost : UserControl

          {

             

              Form5 form5_BP = new Form5();

              public const string SWTASKPANE_PROGID = "RaceWork.SWTaskPane_SwAddin";

       

              public SWTaskpaneHost()

              {

                  InitializeComponent();

              }

       

              private void button1_Click(object sender, EventArgs e)

              {

                  form5_BP.Show();

              }

       

      Form5.cs file as

       

      using System;

      using System.Collections.Generic;

      using System.ComponentModel;

      using System.Data;

      using System.Drawing;

      using System.Linq;

      using System.Text;

      using System.Windows.Forms;

      using System.IO;

      using System.Runtime.InteropServices;

      using SldWorks;

      using SwConst;

      using System.Diagnostics;

       

      namespace RaceWork.V1._09192012

      {

          public partial class Form5 : Form

          {

              SldWorks.SldWorks swApp;

              ModelDoc2 swModel;

             

              public Form5()

              {

                  InitializeComponent();

              }

       

              private void button1_Click(object sender, EventArgs e)

              {  

                  ModelDoc2 swModel = default(ModelDoc2);

                  ModelDocExtension swModelDocExt = default(ModelDocExtension);

                  bool status = false;

                 

                  double F = 18;

       

                  swModel = (ModelDoc2)swApp.ActiveDoc;

                  swModelDocExt = (ModelDocExtension)swModel.Extension;

       

                  //F

                  status = swModelDocExt.SelectByID2("3DSketch3", "SKETCH", 0, 0, 0, true, 0, null, 0);

                  status = swModelDocExt.SelectByID2("D2@3DSketch3", "DIMENSION", 0, 0, 0, true, 0, null, 0);

                  Dimension swDimension1 = default(Dimension);

                  swDimension1 = (Dimension)swModel.Parameter("D2@3DSketch3");

                  swDimension1.SystemValue = F * 0.0254;

                  swModel.ClearSelection2(true);

       

                  status = swModel.EditRebuild3();

        

              }

          }

      }

        • Re: C# Add-In TaskPane Button displays Windows Form, when Form Button pressed code does not execute, displays error
          Artem Taturevych

          Your swApp is not initialized. Either pass the pointer from add-in or create an instance via Activator::CreateInstance. First is better.

           

          ______________________

          Regards,

          Artem Taturevych

          Application Engineer at Intercad

          http://intercad.com.au/

          Tel: +61 2 9454 4444

          • Re: C# Add-In TaskPane Button displays Windows Form, when Form Button pressed code does not execute, displays error
            Robin Staes

            Can you tell me how you solved this problem in code? I have the same problem but I don't know how to 'pass the pointer' or how to create an instance.

             

            Where can I find such information?

             

            Dear regards,

             

            Robin

              • Re: C# Add-In TaskPane Button displays Windows Form, when Form Button pressed code does not execute, displays error
                Oleg Bezyaev

                        SDAddin m_userAddin; //UserAddin Class

                        public ISldWorks m_swApp;

                        IModelDoc2 swModel;

                 

                        public Form5(SDAddin addin)

                        {

                            m_userAddin = addin; // Get the Addin

                            m_swApp = addin.SwApp; // Get the SldWorks from Addin

                            InitializeComponent();

                        }

                • Re: C# Add-In TaskPane Button displays Windows Form, when Form Button pressed code does not execute, displays error
                  Artem Taturevych

                  Just create any public method in the class you are going to pass any object. I'd prefer the Init name. This method should have a parameter of type of the object you like to pass. The pseudo code is

                   

                  class YourAddInClass

                  {

                      VarType m_Var;

                     

                      FunctionWhichCreatesYourTargetClass()

                      {

                           YourTargetClass instance = new YourTargetClass();

                            instance.Init(m_Var);

                      }

                  }

                   

                  class YourTargetClass

                  {

                      VarType m_Pointer;

                   

                      public Init(VarType pointer)

                      {

                          m_Pointer = pointer;

                      }

                      DoSomething()

                      {

                           m_Pointer.CallSomeMethod();

                      }

                  }

                   

                  ______________________

                  Regards,

                  Artem Taturevych

                  Application Engineer at Intercad

                  http://intercad.com.au/

                  Tel: +61 2 9454 4444


                    • Re: C# Add-In TaskPane Button displays Windows Form, when Form Button pressed code does not execute, displays error
                      Robin Staes

                      Screenshot.png

                      Screenshot.png

                      Thank you but I stil don't know how...

                       

                      I have a class which makes my add-in. This add-in creates a taskpane in solidworks

                       

                      MakeAddIn.cs

                       

                      using System;

                      using System.Collections.Generic;

                      using System.Linq;

                      using System.Text;

                      using SolidWorks.Interop.sldworks;

                      using SolidWorks.Interop.swcommands;

                      using SolidWorks.Interop.swconst;

                      using SolidWorks.Interop.swpublished;

                      using SolidWorksTools;

                      using System.Runtime.InteropServices;

                      using Microsoft.Win32;

                      using System.Windows.Forms;

                      using SwCSharpAddin1;

                       

                       

                      namespace AddInRobin

                      {

                          public class MakeAddIn : ISwAddin

                          {

                              public SldWorks mSWApplication;

                              private int mSWCookie;

                              private TaskpaneView mTaskpaneView;

                              private KoppelingTaskpane mTaskpaneHost;

                              ISldWorks iSwApp = null;

                              ICommandManager iCmdMgr = null;

                              int addinID = 0;

                              //BitmapHandler iBmp;

                       

                       

                       

                       

                              [ComRegisterFunction()]

                              private static void ComRegister(Type t)

                              {

                                  RegistryKey localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);

                                  localKey = localKey.CreateSubKey(@"SOFTWARE\SolidWorks\AddIns\{" + t.GUID.ToString() + "}");

                                  {

                                      localKey.SetValue(null, 1); // Load at startup

                                      localKey.SetValue("Title", "Isah Add In"); // Title

                                      localKey.SetValue("Description", "SolidWorks <-> Isah smart koppeling"); // Description

                                  }

                       

                              }

                              [ComUnregisterFunction()]

                              private static void ComUnregister(Type t)

                              {

                                  Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(@"SOFTWARE\SolidWorks\AddIns\{" + t.GUID.ToString() + "}");

                              }

                       

                       

                              public bool ConnectToSW(object ThisSW, int Cookie)

                              {

                                  mSWApplication = (SldWorks)ThisSW;

                                  mSWCookie = Cookie;

                       

                                  // Set-up add-in call back info

                                  bool result = mSWApplication.SetAddinCallbackInfo(0, this, Cookie);

                                  this.UISetup();

                                  return true;

                              }

                       

                       

                              public bool DisconnectFromSW()

                              {

                                  this.UITeardown();

                                  return true;

                              }

                              private void UISetup()

                              {

                                  mTaskpaneView = mSWApplication.CreateTaskpaneView2(string.Empty, "Solidworks <-> Isah Koppeling");

                                  mTaskpaneHost = (KoppelingTaskpane)mTaskpaneView.AddControl(KoppelingTaskpane.SWTASKPANE_PROGID, "");

                              }

                              private void UITeardown()

                              {

                                  mTaskpaneHost = null;

                                  mTaskpaneView.DeleteView();

                                  Marshal.ReleaseComObject(mTaskpaneView);

                                  mTaskpaneView = null;

                              }

                              public void CreateCube()

                              {

                                  //make sure we have a part open

                                  string partTemplate = iSwApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);

                                  if ((partTemplate != null) && (partTemplate != ""))

                                  {

                                      IModelDoc2 modDoc = (IModelDoc2)iSwApp.NewDocument(partTemplate, (int)swDwgPaperSizes_e.swDwgPaperA2size, 0.0, 0.0);

                       

                       

                                      modDoc.InsertSketch2(true);

                                      modDoc.SketchRectangle(0, 0, 0, .1, .1, .1, false);

                                      //Extrude the sketch

                                      IFeatureManager featMan = modDoc.FeatureManager;

                                      featMan.FeatureExtrusion(true,

                                          false, false,

                                          (int)swEndConditions_e.swEndCondBlind, (int)swEndConditions_e.swEndCondBlind,

                                          0.1, 0.0,

                                          false, false,

                                          false, false,

                                          0.0, 0.0,

                                          false, false,

                                          false, false,

                                          true,

                                          false, false);

                                  }

                                  else

                                  {

                                      System.Windows.Forms.MessageBox.Show("There is no part template available. Please check your options and make sure there is a part template selected, or select a new part template.");

                                  }

                              }

                          }

                      }

                       

                      I have a class which makes my taskpane.

                       

                      KoppelingTaskpane.cs

                       

                      using System;

                      using System.Collections.Generic;

                      using System.ComponentModel;

                      using System.Drawing;

                      using System.Data;

                      using System.Text;

                      using System.Linq;

                      using System.Windows.Forms;

                      using SolidWorks.Interop.sldworks;

                      using SolidWorks.Interop.swcommands;

                      using SolidWorks.Interop.swconst;

                      using SolidWorks.Interop.swpublished;

                      using SolidWorksTools;

                      using System.Runtime.InteropServices;

                      using System.Diagnostics;

                       

                       

                      namespace AddInRobin

                      {

                          [ComVisible(true)]

                          [ProgId(SWTASKPANE_PROGID)]

                          public partial class KoppelingTaskpane : UserControl

                          {

                              private SldWorks mSWApplication;

                              private int mCookie;

                              private CommandManager mCommandManager;

                              public ModelDoc2 swModel;

                              public SketchManager swSketchMgr;

                              public Sketch swSketch;

                              public SldWorks swApp;

                              ISldWorks iSwApp;

                       

                       

                              public const string SWTASKPANE_PROGID = "Robin.KoppelingTaskpane_AddInRobin";

                              public KoppelingTaskpane()

                              {

                                  InitializeComponent();

                              }

                       

                       

                              private void button1_Click_1(object sender, EventArgs e)

                              {

                       

                       

                              }

                          }

                      }

                      The form has a button. When I press this button I want the following to happen:

                       

                      When no document is opened: messagebox("No document opened") Then the solidworks fileopendialog.

                      When a part is opened: Put "PART" to custom property "DocumentType" of the part.

                      When a drawing is opened: Put "DRAWING" to custom property "DocumentType" of the drawing.

                      When an assembly is opened: Put "ASSEMBLY" to custom property "DocumentType" of the assembly.

                       

                      I can't even get the first one right.

                       

                      Please help me out.

                       

                      Kind regards

                       

                      Added screenshots

                        • Re: C# Add-In TaskPane Button displays Windows Form, when Form Button pressed code does not execute, displays error
                          Artem Taturevych

                                  private void UISetup()

                                  {

                                      mTaskpaneView = mSWApplication.CreateTaskpaneView2(string.Empty, "Solidworks <-> Isah Koppeling");

                                      mTaskpaneHost = (KoppelingTaskpane)mTaskpaneView.AddControl(KoppelingTaskpane.SWTASKPANE_PROGID, "");

                                      mTaskpaneHost.Init(mSWApplication);

                                  }

                           

                           

                          namespace AddInRobin

                          {

                              [ComVisible(true)]

                              [ProgId(SWTASKPANE_PROGID)]

                              public partial class KoppelingTaskpane : UserControl

                              {

                                  private SldWorks mSWApplication;

                                  private int mCookie;

                                  private CommandManager mCommandManager;

                                  public ModelDoc2 swModel;

                                  public SketchManager swSketchMgr;

                                  public Sketch swSketch;

                                  public SldWorks swApp;

                                  ISldWorks iSwApp;

                           

                           

                                  public const string SWTASKPANE_PROGID = "Robin.KoppelingTaskpane_AddInRobin";

                                  public KoppelingTaskpane()

                                  {

                                      InitializeComponent();

                                  }

                           

                                  public void Init(SldWorks app)

                                  {

                                    mSWApplication = app;

                                  }

                           

                                  private void button1_Click_1(object sender, EventArgs e)

                                  {

                                     swModel = mSWApplication.IActiveDoc2;

                                    //TODO: your code here

                                  }

                              }

                          }

                           

                          ______________________

                          Regards,

                          Artem Taturevych

                          Application Engineer at Intercad

                          http://intercad.com.au/

                          Tel: +61 2 9454 4444