Add Buttons to Task Pane (VB.NET)
This example shows how to add standard SolidWorks and custom buttons to the
Task Pane.
'----------------------------------------------------------------
' Preconditions:
' 1. SolidWorks is running.
' 2. Copy the custom button images, localHelp.bmp and save.png, from
' <SolidWorks_install_folder>\samples\tutorial\api to this
macro's
' swMacro folder.
' 3. Clear the Stop VSTA debugger on macro exit checkbox, if selected
' (click Tools >
Options to display the
System Options - General
' dialog), in SolidWorks.
'
' Postconditions:
' 1. The Task Pane, with Microsoft Calendar control loaded, opens
' with the specified SolidWorks standard and custom
buttons at the
' top of the pane.
' 2. Click each button from left to right. A message box is
displayed
' after each button click. The message box might appear on the
taskbar,
' so examine the taskbar and click the message box to open it.
' Additionally clicking the Close button prompts you to
remove
' the control from the Task Pane.
' 3. Click the Stop Debugging button in the IDE.
' 4. Select the Stop VSTA debugger on macro exit checkbox in SolidWorks,
' if you want to turn it back on.
'-----------------------------------------------------------------
Imports
SolidWorks.Interop.sldworks
Imports
SolidWorks.Interop.swconst
Imports System
Imports System.Collections
Imports System.Diagnostics
Partial
Class SolidWorksMacro
Public
WithEvents swTaskPane
As TaskpaneView
Public buttonIdx
As
Integer
Public
Sub Main()
Dim result
As
Boolean
Dim
folder As
String
Dim
bitmap As
String
Dim
toolTip As
String
Dim
ctrlName As
String
Dim
ctrlLicKey As
String
folder = swApp.GetCurrentMacroPathFolder
+ "\"
' Use default
image for Task Pane tab
bitmap = ""
toolTip =
"Microsoft Calendar"
ctrlName = "MSCAL.Calendar"
ctrlLicKey = ""
swTaskPane = swApp.CreateTaskpaneView2(bitmap,
toolTip)
' Add standard and custom buttons to Task
Pane
result = swTaskPane.AddCustomButton(folder
& "localHelp.bmp",
"Help (custom bmp)")
result = swTaskPane.AddCustomButton(folder &
"save.png",
"Save (custom png)")
result = swTaskPane.AddStandardButton(swTaskPaneBitmapsOptions_e.swTaskPaneBitmapsOptions_Next,
"Next (standard)")
result = swTaskPane.AddStandardButton(swTaskPaneBitmapsOptions_e.swTaskPaneBitmapsOptions_Back,
"Back (standard)")
result = swTaskPane.AddStandardButton(swTaskPaneBitmapsOptions_e.swTaskPaneBitmapsOptions_Ok,
"OK (standard)")
result = swTaskPane.AddStandardButton(swTaskPaneBitmapsOptions_e.swTaskPaneBitmapsOptions_Close,
"Close (standard)")
' Add control to Task Pane for the buttons
swTaskPane.AddControl(ctrlName,
ctrlLicKey)
' Set up events
AttachEventHandlers()
End
Sub
Sub
AttachEventHandlers()
AttachSWEvents()
End
Sub
Sub
AttachSWEvents()
AddHandler
swTaskPane.TaskPaneActivateNotify, AddressOf
Me.swTaskPane_TaskPaneActivateNotify
AddHandler
swTaskPane.TaskPaneDestroyNotify, AddressOf
Me.swTaskPane_TaskPaneDestroyNotify
AddHandler
swTaskPane.TaskPaneToolbarButtonClicked, AddressOf
Me.swTaskPane_TaskPaneToolbarButtonClicked
End
Sub
Public
Function swTaskPane_TaskPaneActivateNotify()
As
Integer
If
Not
(swTaskPane.GetButtonState(0))
Then
For
buttonIdx = 0 To 20
swTaskPane.SetButtonState(buttonIdx,
True)
Next
Else
For
buttonIdx = 0 To 20
swTaskPane.SetButtonState(buttonIdx,
False)
Next
End
If
End
Function
Public
Function swTaskPane_TaskPaneDestroyNotify()
As
Integer
MsgBox("Remove
control from Task Pane?")
End
Function
Public
Function swTaskPane_TaskPaneToolbarButtonClicked(ByVal
ButtonIndex As
Integer)
As
Integer
Select
Case (ButtonIndex + 1)
Case 1
MsgBox("Help (custom bmp) button
clicked.")
Case 2
MsgBox("Save (custom png) button
clicked.")
Case 3
MsgBox("Next button clicked.")
Case 4
MsgBox("Back button clicked.")
Case 5
MsgBox("Okay button clicked.")
Case 6
MsgBox("Close button clicked.")
swTaskPane.DeleteView()
End
Select
End
Function
'''
<summary>
''' The SldWorks swApp
variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End
Class