This topic shows how to create a
debug add-in using VB.NET in Microsoft Visual Studio.
NOTE:
Because SOLIDWORKS PDM Professional 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.
- Click File >
New > Project > Visual Basic > Windows Desktop >
Class Library (.NET Framework).
-
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 PDM Professional type library and the
Systems.Windows.Forms assembly:
- Select Add Reference.
- Click Browse in the left-hand panel, navigate to and select
EPDM.Interop.epdm.dll, and click OK.
- Select Add Reference.
- Click Assemblies > Framework in the left-hand panel, select System.Windows.Forms, and click
OK.
- Click Close.
- Type Imports System.Windows.Forms at the top of
the code window.
- Type
Imports
EPDM.Interop.epdm and
Imports System.Runtime.InteropServices
below
Imports System.Windows.Forms.
- Type before Public Class Class1:
<Guid("")> _
<ComVisible(True)> _
- To populate the GUID
attribute above, click Tools > Create GUID in the IDE, select GUID
Format
6, click Copy, and click Exit. Replace
<Guid("")>
with the copied string.
- Type
below Public Class Class1:
Implements IEdmAddIn5
- 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 PDM Professional
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 EdmCmdData[]) 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 PDM Professional 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.
- On the Application tab,
click Assembly Information.
- De-select Make assembly COM-Visible.
- On the Compile tab,
select AnyCPU
for the target CPU, de-select Prefer 32-bit and select Register for COM interop.
- 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 Epdm.Interop.epdm in the Solution Explorer,
select Properties, and set Embed Interop Types to False
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.
- On the Application tab, keep the suggested target framework or select .NET Framework
4.5 in the Target
framework dropdown.
- Click Build > Build
Solution to build the add-in.
- Install
the add-in as a Debug Add-in using the SOLIDWORKS PDM Professional
Administration tool:
- Start up the SOLIDWORKS
PDM Professional 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, 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 File 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 System.Runtime.InteropServices
Imports EPDM.Interop.epdm
<Guid("")> _ ' See step 10 above to create the GUID
<ComVisible(True)> _
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 PDM Professional 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 EdmCmdData[]) 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