This topic shows how to create a
debug add-in using Visual C# in Microsoft Visual Studio.
NOTE:
Because SOLIDWORKS PDM Professional cannot force a reload of
add-ins if they are written in .NET, all client machines must be restarted to ensure that the latest version of the add-in is used.
-
Start Microsoft Visual
Studio or another version that supports debugging.
- Click File >
New > Project > Visual C# > Windows Desktop > Class Library
(.NET Framework).
- In the .NET Framework dropdown at the top of the
New Project dialog, keep the default version (recommended) or select
.NET Framework 4.5 or later. See Using .NET
Framework for Add-in Applications for more information.
-
Type the name of your
project in Name.
- Click
Browse and navigate to the folder where to create your project.
- Click OK.
Class1.cs containing empty class, Class1, is created.
- Right-click the project name in the Solution Explorer and click
Add > Reference.
- Click Browse in the left-hand panel, navigate to and select
EPDM.Interop.epdm.dll, and click OK.
- 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.
- Click Assemblies > Framework in the left-hand panel,
select
System.Windows.Forms, and click OK.
- Click Close.
- Right-click the project name in the Solution Explorer and
click Properties.
- Click Application > Assembly Information.
- De-select
Make assembly COM-Visible.
- On the Build tab,
select AnyCPU for the target platform, de-select
Prefer 32-bit and select Register for COM
interop.
- Make this
a Debug Add-in:
- Click the Debug tab.
- Click
Start external program
and type
C:\Windows\System32\notepad.exe in the text field.
- If creating this add-in on a 64-bit computer, edit
project_path\project_name\project_name\project_name.csproj
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.
- Double-click Class1.cs in the Solution Explorer
to open the code window.
- At the top of the code window, type:
using System.Windows.Forms;
using System.Runtime.InteropServices;
using
EPDM.Interop.epdm;
-
Replace:
public class Class1
with:
[Guid(""),
ComVisible(true)]
public class Class1 : IEdmAddIn5
- To populate the GUID
attribute above, click Tools > Create GUID in the IDE, select GUID
Format 5, click Copy, and click Exit. Populate
[Guid(""),
...] with the copied string.
- Implement
IEdmAddIn5::GetAddInInfo
by adding the following to Class1:
public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
{
//Specify
information to display in the add-in's Properties dialog box
poInfo.mbsAddInName = "C# Add-in";
poInfo.mbsCompany = "My Company";
poInfo.mbsDescription = "Menu add-in that shows a message box.";
poInfo.mlAddInVersion = 1;
//Specify
the minimum required version of SolidWorks PDM
Professional
poInfo.mlRequiredVersionMajor = 6;
poInfo.mlRequiredVersionMinor = 4;
// Register a menu command
poCmdMgr.AddCmd(1, "C# Add-in", (int)EdmMenuFlags.EdmMenu_Nothing);
}
- Implement
IEdmAddIn5::OnCmd
by adding the following to Class1:
public void OnCmd(ref EdmCmd poCmd, ref EdmCmdData[] ppoData)
{
// Handle the menu command
if (poCmd.meCmdType == EdmCmdType.EdmCmd_Menu)
{
if (poCmd.mlCmdID == 1)
{
System.Windows.Forms.MessageBox.Show("C# Add-in");
}
}
}
- Implement your own window handle wrapper by
right-clicking the project name in the Solution Explorer and clicking Add > Class:
- Type WindowHandle.cs in Name.
- Click Add.
- Replace the code in the code window with the
following code.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Addin_CSharp
{
//Wrapper class to use SOLIDWORKS PDM Professional as the parent window
class WindowHandle : IWin32Window
{
private IntPtr mHwnd;
public WindowHandle(int hWnd)
{
mHwnd = new IntPtr(hWnd);
}
public IntPtr Handle
{
get { return mHwnd; }
}
}
}
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.cs.
Click Build > Build
Solution to build the add-in.
Install
the add-in for debugging through the SOLIDWORKS PDM Professional
Administration tool:
- Open the SOLIDWORKS
PDM Professional Administration tool.
- Expand the vault where
you want to install this add-in and log in as Admin.
- Right-click Add-ins and select Debug
Add-ins.
- Click Add Add-in, browse to
project_path\project_name\project_name\bin\Debug,
click project_name.dll, and click Open.
Your add-in's name, path, and class ID should appear in
the Add-ins installed
for debugging on this machine list.
- Click OK.
In Microsoft Visual Studio, click Debug > Start Debugging
or press F5.
Open Notepad and click File > Open.
In the Open dialog, click the name of the vault where you installed this add-in.
Right-click inside the vault view and click C#
Add-in.
Displays the message, C# Add-in.
Click OK to close the message box.
Click Cancel.
Close Notepad.
Complete
Source Code
//Class1.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using EPDM.Interop.epdm;
[Guid(""),
ComVisible(true)]
// See step 14 above to create the GUID
public class Class1 : IEdmAddIn5
{
public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
{
poInfo.mbsAddInName = "C# Add-in";
poInfo.mbsCompany = "My Company";
poInfo.mbsDescription = "Menu add-in that shows a message box.";
poInfo.mlAddInVersion = 1;
poInfo.mlRequiredVersionMajor = 6;
poInfo.mlRequiredVersionMinor = 4;
poCmdMgr.AddCmd(1, "C# Add-in", (int)EdmMenuFlags.EdmMenu_Nothing);
}
public void OnCmd(ref EdmCmd poCmd, ref EdmCmdData[] ppoData)
{
if (poCmd.meCmdType == EdmCmdType.EdmCmd_Menu)
{
if (poCmd.mlCmdID == 1)
{
System.Windows.Forms.MessageBox.Show("C# Add-in");
}
}
}
}
//WindowHandle.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Addin_CSharp
{
class WindowHandle : IWin32Window
{
private IntPtr mHwnd;
public WindowHandle(int hWnd)
{
mHwnd = new IntPtr(hWnd);
}
public IntPtr Handle
{
get { return mHwnd; }
}
}
}