Notify User When File Changes State Example (C#)
This example shows how to create an add-in that pops up a message box when a file is
approved.
//--------------------------------------------------------------------------------------
// Preconditions:
// 1. Start Microsoft Visual Studio 2010.
// 2. Click File > New > Project > Visual C# > Class Library.
// 3. Select .NET Framework 2.0 in the dropdown at the top of the dialog.
// 4. Type StateChangeNotificationAddin in Name.
// 5. Click Browse and navigate to the folder where to create the project.
// 6. Click OK.
// 7. Right-click the project name in the Solution Explorer and click Add Reference.
// 8. In the Add Reference dialog:
// a. Add the
SolidWorks Enterprise PDM type library as a reference (click COM in the
// left-side panel, click PDMWorks Enterprise nnnn Type Library,
// and click Add).
// b. Add System.Windows.Forms as a reference (click Assemblies
> Framework in the
// left-side panel, click System.Windows.Forms, and click Add).
// c. Click Close.
//
9. Right-click the project name in the Solution Explorer and click
Properties.
//10. In the Properties window:
// a. Click Application > Assembly Information.
// b. Select Make assembly COM-Visible.
// c. Click OK.
//11. Copy the code below to Class1.cs.
//12. Click
Build > Build Solution.
//
// Postconditions:
// 1. Open the SolidWorks
Enterprise PDM Administration tool, expand a vault_name node,
// and log in as Admin.
// 2. Ensure that Default Workflow exists under vault_name > Workflows.
// a. Ensure that the workflow has an Approved state.
// b. Ensure that the workflow has a Change Approved
transition.
// 3. Under vault_name, right-click Add-ins and click New Add-in.
// a. Navigate to the
bin\Debug directory of your built project.
// b. Click Interop.EdmLib.dll and StateChangeNotificationAddin.dll.
// c. Click Open.
// d. Click OK.
// 4. Click OK after
reading the SolidWorks Enterprise PDM warning dialog.
// 5. Open Windows Explorer on a view of
vault_name.
// a. Log in as Admin.
// b. Create a new file in the root directory of vault_name and check it in.
// c. Right-click the file and click Change State > transition.
// d. Click OK in the Do Transition dialog.
// e. Repeat steps c and
d until the file is in a state that it can be Approved.
// g. Right-click the file and click Change State > Change Approved.
// h. Click OK in the Do Transition dialog.
// 6. A message box pops up notifying the user whether the file
changing state is a top
// node. Click OK.
// 7. A message box pops up notifying the user that the file has been approved.
Click OK.
//---------------------------------------------------------------------------------------
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 ChangeStateNotification : IEdmAddIn5
{
public void GetAddInInfo(ref EdmLib.EdmAddInInfo poInfo, EdmLib.IEdmVault5 poVault, EdmLib.IEdmCmdMgr5 poCmdMgr)
{
try
{
poInfo.mbsAddInName = "C# Add-In";
poInfo.mbsCompany = "Dassault Systemes";
poInfo.mbsDescription = "Exercise demonstrating responding to a change state event.";
poInfo.mlAddInVersion = 1;
//Minimum SolidWorks Enterprise PDM version
//needed for C# Add-Ins is 6.4
poInfo.mlRequiredVersionMajor = 6;
poInfo.mlRequiredVersionMinor = 4;
//Register to receive a notification when
//a file has changed state
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PostState);
}
catch (System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void OnCmd(ref EdmLib.EdmCmd poCmd, ref System.Array ppoData)
{
try {
string AffectedFileNames = "";
Boolean topNode;
IEdmCmdNode cmdNode;
switch (poCmd.meCmdType) {
//A file has changed state
case EdmCmdType.EdmCmd_PostState:
foreach ( EdmCmdData AffectedFile in ppoData) {
if (AffectedFile.mbsStrData2 == "Approved") {
AffectedFileNames += AffectedFile.mbsStrData1 + "";
}
cmdNode = (IEdmCmdNode)AffectedFile.mpoExtra;
topNode = (Boolean)cmdNode.GetProperty(EdmCmdNodeProp.EdmCmdNode_IsTopNode);
((EdmVault5)(poCmd.mpoVault)).MsgBox(poCmd.mlParentWnd, "File changing state is a top node? " + topNode);
}
if (AffectedFileNames.Length > 0) {
((EdmVault5)(poCmd.mpoVault)).MsgBox(poCmd.mlParentWnd, AffectedFileNames + " has been approved.");
}
break;
//The event isn't registered
default:
((EdmVault5)(poCmd.mpoVault)).MsgBox(poCmd.mlParentWnd, "An unknown command type was issued.");
break;
}
} catch (System.Runtime.InteropServices.COMException ex) {
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + ex.Message);
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}