This
topic shows how to create a debug add-in using Visual C# in Microsoft Visual Studio
2010.
NOTE:
Because SOLIDWORKS Enterprise PDM 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
2010.
- Click File >
New > Project > Visual C# > Class Library.
- Select .NET
Framework 2.0 in the dropdown at the top of the dialog.
-
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 COM in the left-side panel, click PDMWorks Enterprise 20nn
Type
Library, and click Add.
- Click Assemblies > Framework in the left-side panel, click
System.Windows.Forms, and click Add.
- Click Close.
- Right-click the project name in the Solution Explorer and
click Properties.
- Click Application > Assembly Information.
- Click
Make assembly COM-Visible to register the add-in.
- Click
OK.
- 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 EdmLib;
-
Replace:
public class Class1
with:
public class Class1 : IEdmAddIn5
-
In the code, right-click
IEdmAddIn5 and click Implement Interface > Implement Interface.
- Implement
IEdmAddIn5::GetAddInInfo
as follows:
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 Enterprise PDM
poInfo.mlRequiredVersionMajor = 6;
poInfo.mlRequiredVersionMinor = 4;
// Register a menu command
poCmdMgr.AddCmd(1, "C# Add-in", (int)EdmMenuFlags.EdmMenu_Nothing);
}
- Implement
IEdmAddIn5::OnCmd
as follows:
public void OnCmd(ref EdmCmd poCmd, ref System.Array 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 Enterprise PDM 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 Enterprise PDM
Administration tool:
- Open the SOLIDWORKS
Enterprise PDM 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 2010, 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 Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using EdmLib;
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 System.Array 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; }
}
}
}