TaskpaneView.AddControl question

Hello!

I'm writing an add-in for SolidWorks 2010 (C#).

Previously I've made a (working) plugin for SolidWorks 2008, here is a part of is's code:

...

namespace SWBPlugin
{
      public class SWIntegration : ISwAddin
      {
      public SldWorks mSWApplication;
      private int mSWCookie;

      private TaskpaneView mTaskpaneView;
     private SWTaskpaneHost mTaskpaneHost;

....

private void UISetup()
      {
      try
      {
          mTaskpaneView = (TaskpaneView)mSWApplication.CreateTaskpaneView2("", "Plugin Title");
          mTaskpaneHost = (SWTaskpaneHost)mTaskpaneView.AddControl(SWTaskpaneHost.SWTASKPANE_PROGID, "");
           mTaskpaneHost.pSWApplication = mSWApplication;
      }

...

The problem I'm facing is the following:   mTaskpaneHost is null, so there is nothing but the title (Plugin Title) in the right panel.

The decaration of the TaskpaneHost class:

public partial class SWTaskpaneHost : System.Windows.Forms.UserControl

They say, SolidWorks 2010 doesn't support System.Windows.Forms.UserControl.

So, what should I do to make my plugin to work in SW 2010?

Thanks to everyone!

SolidworksApi macros
Answers 14
SF
SF 2011-01-17 Best Answer

I don't belive than noone knows...

SF
SF 2011-01-17

It would be nice to look at the entire class.  I don't think you are creating a "new" reference.

SF
SF 2011-01-17

Actually,  I would try getting rid of this line:

mTaskpaneHost.pSWApplication = mSWApplication;

SF
SF 2011-01-17

Jesse, here it is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO

using Microsoft.Win32;

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swcommands;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;
using SolidWorksTools;
using System.Runtime.InteropServices;

namespace SWBPlugin
{
    [ComVisible(true)]
    [ProgId(SWTASKPANE_PROGID)]
    public partial class SWTaskpaneHost : System.Windows.Forms.UserControl
    {
        public const string SWTASKPANE_PROGID = "Clever-Soft.SWBPluginTaskpaneHost";
        public SldWorks pSWApplication;
        private string sUid;
        private string sLogin;
        private string sUrl;
        private string sPassword;
        private string sExtensions;
        private string sTmpDir;
        private string cur_file_name;
        private Form waitfrm;
        private bool bConnected = false;
        private List aUploadFiles = new List();
        private const string sRegistryKey = "Software\\Clever-Soft\\Basalta\\Integration\\SolidWorks";

        public SWTaskpaneHost()
        {
            InitializeComponent();
           // some buttons

            this.ttToolTip.SetToolTip(this.bFind, "Нажмите, чтобы осуществить поиск объекта");
            this.ttToolTip.SetToolTip(this.bSettings, "Настройки интеграции");
            this.ttToolTip.SetToolTip(this.bOpen, "Открыть сборку или деталь на редактирование");
            this.ttToolTip.SetToolTip(this.tbCode, "Введите начало обозначения объекта для поиска");
            this.ttToolTip.SetToolTip(this.tvObjects, "Список найденных объектов");
            this.ttToolTip.SetToolTip(this.bParents, "Получить список изделий");
           
     }

...

The orther part of the code is just some plugin logic.

Jesse
SF
SF 2011-01-17

That doesn't work.

In fact mTaskpaneHost appears to be null after this:

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

SF
SF 2011-01-17

Not that class.  I think the problem lies in the addin class.  If not, I do remember something about user controls being a problem in earlier versions.  Hmmm... I'll see if I can find out.

SF
SF 2011-01-17

Here is the addin class:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.ComponentModel;

using Microsoft.Win32;

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swcommands;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;
using SolidWorksTools;

namespace SWBPlugin
{
public class SWIntegration : ISwAddin
{
public SldWorks mSWApplication;
private int mSWCookie;

// Правая панель
private TaskpaneView mTaskpaneView;

private SWTaskpaneHost mTaskpaneHost;


public bool ConnectToSW(object ThisSW, int Cookie)
{
try
{
// Получение ссылки на солид
mSWApplication = (SldWorks)ThisSW;
mSWCookie = Cookie;
// Set-up add-in call back info
bool result = mSWApplication.SetAddinCallbackInfo(0, this, Cookie);
this.UISetup();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}

return true;
}

public bool DisconnectFromSW()
{
try
{
this.UITeardown();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}

return true;
}
// Установка панели
private void UISetup()
{
try
{           
// Установка контейнера - иконка и заголовок
mTaskpaneView = (TaskpaneView)mSWApplication.CreateTaskpaneView2("d:\\projects\\devII\\SW\\SWBPlugin\\basalta.bmp", "Интеграция с Базальтой");
// Установка управляющего элемента - taskpanehost - содержимое онтейнера
mTaskpaneHost = (SWTaskpaneHost)mTaskpaneView.AddControl(SWTaskpaneHost.SWTASKPANE_PROGID, "");
// Объект - приложние SW.
mTaskpaneHost.pSWApplication = mSWApplication;
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}

}

private void UITeardown()
{
try
{
mTaskpaneElement = null;
mTaskpaneView.DeleteView();
Marshal.ReleaseComObject(mTaskpaneView);
mTaskpaneView = null;
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}

[ComRegisterFunctionAttribute()]
// Регистрация в COM
private static void ComRegister(Type t)
{
// Добавление ветки реестра
string keyPath = String.Format(@"SOFTWARE\SolidWorks\AddIns\{0:b}", t.GUID);
using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(keyPath))
{
// Добавление данных в созданную ветку реестра
rk.SetValue(null, 1); // Load at startup
rk.SetValue("Title", "Базальта"); // Title
rk.SetValue("Description", "Интерграция с Базальтой"); // Description
}
}

[ComUnregisterFunctionAttribute()]
// Разрегистрация в com
private static void ComUnregister(Type t)
{
string keyPath = String.Format(@"SOFTWARE\SolidWorks\AddIns\{0:b}", t.GUID);
Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(keyPath);
}
}
}

Sorry for comments in russian.

SF
SF 2011-01-19

Another thought: can this issue be cused by the distributive? I've tested my plugin on another machine and it was working fine there.

SF
SF 2011-01-19

Not sure, but you got me thinking.

SF
SF 2011-01-27

Jesse, thanks for you help!

My issue was in logic of my plugin, but your advice was helpfull too.

SF
SF 2011-01-31

Well I'm glad you got it to work.

SF
SF 2012-08-10

Hi!

I have absolutely the same problem!

Can you explain me how did you resolve it?

2018-05-23

I'm having the same problem too. I found an working example and compared with my. Turns out the example work on .net 4.0 but not in .net 4.5 or above.

I'm using the CSharp Tamplate provided by SolidWorks.

Hope it helps.

LX
LX 2018-08-18

right. I try AngelSix's MenusToolbars example. It is run correctly. it use .net frameworks 4.

I repeat it with VS 2017,and meet "null" misktake.

I use C# by solidworks.