Add-in Series 3 – Create Task Pane [VIDEO]

Article by Adam Bridgman, CSWE created/updated January 8, 2014

A Task pane is a panel that resides on the right hand side of the SolidWorks graphics area by default.  It is a great place to provide information to users regardless of their environment (part, assembly, or drawing).  Follow the steps below to create a task pane, later videos in this series will add functionality to it.

  1. Now that we have an empty add-in we are ready to customize it for whatever purpose we want to use it for. In this example I will set up a simple task pane to demonstrate how it can be setup.  To set up a task pane you should add a host control.  To do this right click on your project and select, add>user control.
  2. Expand the control, this represents the task pane, you should resize it to be the size and shape you expect it to be used.
  3. Add a button and size it to the width of the user control and set the anchor properties to top left and right.

Task Pane Host

(Because of these anchors the width of the button will match the width of the task pane)

 

4. Double click the button and add the following code

Imports System.Runtime.InteropServices

<ComVisible(True)> _

<ProgId(“Javelin.SWTaskPane_SwAddin”)> _

Public Class UserControl1

    Dim swApp As SldWorks.SldWorks

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        MsgBox(“Success”)

    End Sub

    Friend Sub getSwApp(ByRef swAppIn As SldWorks.SldWorks)

        swApp = swAppIn

    End Sub

    Public Sub New()

        InitializeComponent()

    End Sub

End Class

5. Now we just have to make the new user control into a task pane.  Expand the Local Variables region and add:

Dim myTaskPaneView As SldWorks.TaskpaneView

Dim myTaskpaneHost As UserControl1

 

6.  Expand the region UI Method, add code to create the task pane.

    Public Sub AddTaskPane()

        myTaskPaneView = SwApp.CreateTaskpaneView2(“”, “Sample Task Pane”)

        myTaskpaneHost = myTaskPaneView.AddControl(“Javelin.SWTaskPane_SwAddin”, “”)

        myTaskpaneHost.getSwApp(SwApp)

    End Sub

    Public Sub RemoveTaskPane()

        Try

            myTaskpaneHost = Nothing

            myTaskPaneView.DeleteView()

            Marshal.ReleaseComObject(myTaskPaneView)

            myTaskPaneView = Nothing

        Catch ex As Exception

        End Try

    End Sub

 

7. Add our new methods under the ConnectToSW and DisconnectFromSW methods.

    Function ConnectToSW(ByVal ThisSW As Object, ByVal Cookie As Integer) As Boolean Implements ISwAddin.ConnectToSW

        iSwApp = ThisSW

        addinID = Cookie

        ‘ Setup callbacks

        ‘iSwApp.SetAddinCallbackInfo(0, Me, addinID)

        ‘ Setup the Command Manager

        ‘iCmdMgr = iSwApp.GetCommandManager(Cookie)

        ‘AddCommandMgr()

        ‘Setup the Event Handlers

        ‘SwEventPtr = iSwApp

        ‘openDocs = New Hashtable

        ‘AttachEventHandlers()

        ‘Setup Sample Property Manager

        ‘AddPMP()

        AddTaskPane()

        ConnectToSW = True

    End Function

    Function DisconnectFromSW() As Boolean Implements ISwAddin.DisconnectFromSW

        RemoveTaskPane()

        ‘RemoveCommandMgr()

        ‘RemovePMP()

        ‘DetachEventHandlers()

        ‘System.Runtime.InteropServices.Marshal.ReleaseComObject(iCmdMgr)

        ‘iCmdMgr = Nothing

        ‘System.Runtime.InteropServices.Marshal.ReleaseComObject(iSwApp)

        iSwApp = Nothing

        ‘The addin _must_ call GC.Collect() here in order to retrieve all managed code pointers

        GC.Collect()

        GC.WaitForPendingFinalizers()

        GC.Collect()

        GC.WaitForPendingFinalizers()

        DisconnectFromSW = True

    End Function

 

8.  Now you are ready to test.  Run your program in debug mode, you should see the task pane with a standard windows icon, if you select it you will see a button and clicking on the button will bring up a form.  If you resize the task pane the button will resize as required.

Posts related to 'Add-in Series 3 – Create Task Pane [VIDEO]'

Adam Bridgman, CSWE

Want to learn SOLIDWORKS?

Take a training course from our team of Certified SOLIDWORKS Experts