This
topic shows how to create a debug add-in using VB.NET in Microsoft Visual Studio
2010.
NOTE:
Because SOLIDWORKS Enterprise PDM cannot force a reload of
add-ins if they are written in VB.NET, all client machines must be restarted to ensure that the latest version of the add-in is used.
-
Start Microsoft Visual Studio
2010.
- Click File >
New > Project > Visual Basic > Class Library.
-
Type the name of your
project in Name.
- Click
Browse and navigate to the folder where to create your project.
- Click OK.
Class1.vb containing an empty class called Class1 is created.
- Right-click the name of your project in the Solution
Explorer to add the SOLIDWORKS Enterprise PDM type library and the
Systems.Windows.Forms assembly:
- Select Add Reference.
- Click COM, select PDMWorks Enterprise 20nn Type
Library, and click Add.
- Select Add Reference.
- Click .NET, select System.Windows.Forms, and click Add.
- Click Close.
- Type Imports System.Windows.Forms at the top of
the code window.
- Type
Imports EdmLib below
Imports System.Windows.Forms.
- Type
Implements IEdmAddIn5
below Public Class Class1, the class declaration.
- Implement
IEdmAddIn5::GetAddInInfo
by pasting the following code between Implements IEdmAddIn5 and
End Class:
Public Sub GetAddInInfo(ByRef poInfo As
EdmAddInInfo, ByVal poVault As IEdmVault5, ByVal poCmdMgr As IEdmCmdMgr5)
Implements IEdmAddIn5.GetAddInInfo
'
Specify add-in
information
poInfo.mbsAddInName = "VB.NET Add-in"
poInfo.mbsCompany = "My Company"
poInfo.mbsDescription = "Menu add-in that shows a message box."
poInfo.mlAddInVersion = 1
' Specify minimum version of SOLIDWORKS Enterprise PDM
poInfo.mlRequiredVersionMajor
= 6
poInfo.mlRequiredVersionMinor = 4
' Register a menu command
poCmdMgr.AddCmd(1, "VB.NET Add-in", EdmMenuFlags.EdmMenu_Nothing)
End Sub
- Implement
IEdmAddIn5::OnCmd
by pasting the following code between End Sub and End Class:
NOTE:
IEdmVault8::GetWin32Window
is used to convert the parent window handle to a window handle that .NET can
use.
Public Sub OnCmd(ByRef poCmd As EdmCmd, ByRef
ppoData As System.Array) Implements IEdmAddIn5.OnCmd
' Handle the menu command
If
poCmd.meCmdType = EdmCmdType.EdmCmd_Menu Then
If
poCmd.mlCmdID = 1 Then
Dim v8 as IEdmVault8
V8 = poCmd.mpoVault
System.Windows.Forms.MessageBox.Show(v8.GetWin32Window(poCmd.mlParentWnd),
"VB.NET Add-in")
End
If
End If
End Sub
- Implement your own window handle wrapper
by right-clicking the name of your project in the Solution Explorer and
selecting Add > New Item > Class:
- Type WindowHandle in Name.
- Click Add.
- Replace the code in the code window with the
following code.
Imports
System.Windows.Forms
' Wrapper class to use SOLIDWORKS Enterprise PDM as a parent
window to VB forms
Public Class WindowHandle
Implements
IWin32Window
Private
mHwnd As IntPtr
Public Sub
New(ByVal hWnd As Integer)
mHwnd =
New IntPtr(hWnd)
End Sub
Public ReadOnly
Property Handle() As IntPtr Implements
System.Windows.Forms.IWin32Window.Handle
Get
Return mHwnd
End Get
End
Property
End
Class
Your add-in uses the new wrapper in the menu
command handler to
show the message box by calling
System.Windows.Forms.MessageBox.Show in Class1::OnCmd in Class1.vb.
- Right-click the name of
your project in the Solution Explorer and select Properties.
- Click Assembly Information
on the Application tab.
- Click
Make assembly COM-Visible to register the add-in.
- Click OK.
- Change your add-in to
be a Debug Add-in:
- Right-click the name of
your project in the Solution Explorer and select Properties.
- Click the Debug tab.
- Click Start external program
and type
C:\Windows\System32\notepad.exe in the text field.
- Right-click EdmLib in the Solution Explorer,
select Properties, and set Embed Interop Types to False
in the Properties window to handle methods that pass arrays of structures.
- If creating this add-in on a 64-bit computer, edit
project_path\project_name\project_name\project_name.vbproj
in Notepad:
- Insert the following line below <PropertyGroup
Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> and
below <PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
'Release|AnyCPU' ">.
<PlatformTarget>AnyCPU</PlatformTarget>
- Save the file and exit Notepad.
- Right-click the name of
your project in the Solution Explorer and select Properties.
- Click Advanced Compile Options on the Compile
tab.
- Select .NET Framework 2.0 in the Target
framework (all configurations) dropdown.
- Click Build > Build
Solution to build the add-in.
- Install
the add-in as a Debug Add-in using the SOLIDWORKS Enterprise PDM
Administration tool:
- Start up the SOLIDWORKS
Enterprise PDM Administration tool.
- Expand the vault where
you want to install this add-in. Log in if prompted.
- Right-click Add-ins and select Debug
Add-ins.
- Click Add Add-in, browse to
project_path\project_name\project_name\bin\Debug,
select project_name.dll, and click Open.
Your add-in's name, path, and class ID should appear in Add-ins installed
for debugging on this machine.
- Click OK.
- In Microsoft Visual Studio 2010, click Debug > Start Debugging
or press F5.
- Click File > Open in Notepad.
- Click the name of the vault where you installed your add-in.
- Right-click inside the vault in Windows Explorer and select VB.NET
Add-in.
A message box is displayed with the message VB.NET Add-in.
- Click OK to close the message box.
Complete
Source Code
Class1.vb
Imports System.Windows.Forms
Imports EdmLib
Public Class Class1
Implements IEdmAddIn5
Public Sub GetAddInInfo(ByRef poInfo As EdmAddInInfo, ByVal poVault As IEdmVault5, ByVal poCmdMgr As IEdmCmdMgr5) Implements IEdmAddIn5.GetAddInInfo
' Fill in the add-in's description
poInfo.mbsAddInName = "VB.NET Add-in"
poInfo.mbsCompany = "My Company"
poInfo.mbsDescription = "Menu add-in that shows a message box."
poInfo.mlAddInVersion = 1
' Minimum SOLIDWORKS Enterprise PDM version needed for VB.NET add-ins is 6.4
poInfo.mlRequiredVersionMajor = 6
poInfo.mlRequiredVersionMinor = 4
' Register a menu command
poCmdMgr.AddCmd(1, "VB.NET Add-in", EdmMenuFlags.EdmMenu_Nothing)
End Sub
Public Sub OnCmd(ByRef poCmd As EdmCmd, ByRef ppoData As System.Array) Implements IEdmAddIn5.OnCmd
' Handle the menu command
If poCmd.meCmdType = EdmCmdType.EdmCmd_Menu Then
If poCmd.mlCmdID = 1 Then
Dim v8 As IEdmVault8
v8 = poCmd.mpoVault
System.Windows.Forms.MessageBox.Show(v8.GetWin32Window(poCmd.mlParentWnd), "VB.NET Add-in")
End If
End If
End Sub
End Class
WindowHandle.vb
Imports System.Windows.Forms
Public Class WindowHandle
Implements IWin32Window
Private mHwnd As IntPtr
Public Sub New(ByVal hWnd As Integer)
mHwnd = New IntPtr(hWnd)
End Sub
Public ReadOnly Property Handle() As IntPtr Implements System.Windows.Forms.IWin32Window.Handle
Get
Return mHwnd
End Get
End Property
End Class