1 Reply Latest reply on Aug 8, 2018 6:37 PM by Christian Chu

    C# code issue in add in

    Josh Hixon

      Okay, so I am pretty brand new to programming in C# I have gotten pretty good in vb.net, but the best tutorial I could find for creating a task pane add-in was in C#. I figured it was time to make the jump. I am learning on the fly, but am struggling with what should be a simple bit of code. Could someone point me in the right direction? Here is what I have.

       

      
      using SolidWorks.Interop.sldworks;
      using SolidWorks.Interop.swconst;
      using System.Runtime.InteropServices;
      using System.Windows.Forms;
      
      
      namespace Template
      {
          [ProgId(Taskpaneintegration.SWTASKPANE_PROGID)]
          public partial class TaskpaneUI : UserControl
          {
              public TaskpaneUI()
              {
                  InitializeComponent();
              }
      
      
              private void TabPage2_Click(object sender, System.EventArgs e)
              {
      
      
              }
      
      
              private void Button1_Click(object sender, System.EventArgs e)
                  
              {
                  ModelDoc2 doc;
                  doc = swapp.OpenDoc(@"C:\SPECTRUM\spectrum aquatics\engineering\working folders\cole\1830238.sldprt", (int)swDocumentTypes_e.swDocPART);
      
      
                  ModelDoc2 swmodel = (ModelDoc2)swapp.ActiveDoc;
      
      
                  string pname = swmodel.GetPathName();
      
      
                  swapp.SendMsgToUser(pname);
                  
                 
              }
              public SldWorks swapp;
              
          }
      }
      

      the error I keep getting is as follows:

       

       

       

      Message was edited by: Josh Hixon

        • Re: C# code issue in add in
          Christian Chu

          @Josh,

          Not sure if you already declared/set the swapp and doc in somewhere  the top

          try this to see if it works

           {
                       //            Dim doc As ModelDoc2
                       //            doc = swapp.OpenDoc("C:\SPECTRUM\spectrum aquatics\engineering\working folders\cole\1830238.sldprt", CInt(swDocumentTypes_e.swDocPART))
                       //            
                       //
                       //
                       //            Dim swmodel As ModelDoc2 = DirectCast(swapp.ActiveDoc, ModelDoc2)
                       //
                       //
                       //            Dim pname As String = swmodel.GetPathName()
                       //
                       //
                       //            swapp.SendMsgToUser(pname)
          
                       
          
                       ModelDoc2 doc = default(ModelDoc2);
          
                       doc = swapp.OpenDoc("C:\\SPECTRUM\\spectrum aquatics\\engineering\\working folders\\cole\\1830238.sldprt", swDocumentTypes_e.swDocPART);
                       doc = swapp.ActiveDoc;
          
          
                       //Dim swmodel As ModelDoc2 = DirectCast(swApp.ActiveDoc, ModelDoc2)
          
                       string pname = doc.GetPathName;
          
          
                       swapp.SendMsgToUser(pname);
          
          
                   }
                           Public swapp As SldWork