Create a Task that Finds Approved Files Example (C#)
This example shows how to create a task add-in that notifies the logged-in
user as to which files in
the vault are in the Approved state.
//--------------------------------------------------------------------------------------
// 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 TaskAddinExample 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 with an Approved state exists
// under vault_name > Workflows.
// 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 TaskAddinExample.dll.
// c. Click Open.
// d. Click OK.
// 4. Click OK after reading the SolidWorks Enterprise PDM warning dialog.
// 5. In the taskbar notification area:
// a. Click the SolidWorks Enterprise PDM icon.
// b. Click Task Host Configuration.
// c. Select vault_name in the File Vault dropdown.
// d. Select Permit next to the add-in you installed in step 3.
// e. Click OK.
// 6. In the Administration tool
under the vault_name, right-click Tasks and click
New Task.
// a. Type Approved Files Task in the upper-left text box.
// b. Select the C# Task Add-in from the Add-in dropdown.
// c. When the attach debugger message box appears:
// 1. In the IDE, click Debug > Attach to Process > ConisioAdmin.exe.
// 2. Click Attach.
// 3. Click OK to close the message box.
// d. Click Next.
// e. On the Execution Method properties page, select the local computer.
// f. Click Next.
// g. On the Scheduling properties page, select This task is not scheduled.
// h. Click Next.
// i. On the Permissions property page, select Admin.
// j. Click Next.
// k. On the Success Notification property page:
// 1. Select Notify the user who launched the task.
// 2. In the Subject field, type Success.
// 3. In the Text field, type The task successfully completed at, type
a space,
// click >, and click Current Time.
// l. Click OK to close the New Task dialog.
// 7. Expand vault_name > Tasks and double-click Task List.
// 8. Click Add Task.
// 9. In the Add Task dialog:
// a. Select Approved Files Task from the dropdown.
// b. Click OK.
// 10. When the attach debugger message box appears:
// a. In the IDE, click Debug > Attach to Process > TaskExecutor.exe.
// b. Click Attach.
// c. Click OK to close the message box.
// 11. The task appears in the Pending tasks list. Observe the task status.
// 12. When the task completes, the task moves to the Completed tasks
// list. The logged-in user receives a notification with the list of approved files.
//---------------------------------------------------------------------------------------
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 TaskAddinExample : IEdmAddIn5
{
public void GetAddInInfo(ref EdmLib.EdmAddInInfo poInfo, EdmLib.IEdmVault5 poVault, EdmLib.IEdmCmdMgr5 poCmdMgr)
{
try
{
poInfo.mbsAddInName = "C# Task Add-In";
poInfo.mbsCompany = "Dassault Systemes";
poInfo.mbsDescription = "Exercise demonstrating a task that lists files in the Approved state.";
poInfo.mlAddInVersion = 1;
//Minimum SolidWorks Enterprise PDM version
//needed for C# Task Add-Ins is 10.0
poInfo.mlRequiredVersionMajor = 10;
poInfo.mlRequiredVersionMinor = 0;
//Register this add-in as a task add-in
poCmdMgr.AddHook(EdmCmdType.EdmCmd_TaskRun);
//Register this add-in to be called when
//selected as a task in the Administration tool
poCmdMgr.AddHook(EdmCmdType.EdmCmd_TaskSetup);
}
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
{
PauseToAttachProcess(poCmd.meCmdType.ToString());
switch (poCmd.meCmdType)
{
case EdmCmdType.EdmCmd_TaskRun:
OnTaskRun(ref poCmd, ref ppoData);
break;
case EdmCmdType.EdmCmd_TaskSetup:
OnTaskSetup(ref poCmd, ref ppoData);
break;
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void OnTaskRun(ref EdmCmd poCmd, ref System.Array ppoData)
{
//Get the task instance interface
IEdmTaskInstance inst = default(IEdmTaskInstance);
inst = (IEdmTaskInstance)poCmd.mpoExtra;
try
{
//Keep the Task List status up to date
inst.SetStatus(EdmTaskStatus.EdmTaskStat_Running);
//Format a message that will be displayed
//in the task list
inst.SetProgressRange(10, 1, "Task is running.");
List<EdmSelItem2> Items = new List<EdmSelItem2>();
inst.SetProgressPos(4, "Task is searching.");
IEdmSearch6 Search = (IEdmSearch6)((IEdmVault5)(poCmd.mpoVault)).CreateSearch();
if (Search == null)
return;
Search.SetToken(EdmSearchToken.Edmstok_FindFiles, true);
Search.SetToken(EdmSearchToken.Edmstok_WorkflowName, "Default Workflow");
Search.SetToken(EdmSearchToken.Edmstok_StateName, "Approved");
IEdmSearchResult5 SearchResult = Search.GetFirstResult();
while ((SearchResult != null))
{
EdmSelItem2 SelItem = new EdmSelItem2();
SelItem.mlID = SearchResult.ID;
SelItem.mlParentID = SearchResult.ParentFolderID;
SelItem.meType = SearchResult.ObjectType;
SelItem.mlVersion = SearchResult.Version;
Items.Add(SelItem);
SearchResult = Search.GetNextResult();
}
inst.SetProgressPos(7, "Task finished searching.");
EdmSelItem2[] NotificationArray = new EdmSelItem2[Items.Count];
Items.CopyTo(NotificationArray);
string ProgresssMsg = null;
if ((Items.Count > 0))
{
ProgresssMsg = "Found " + Items.Count.ToString() + " files.";
}
else
{
ProgresssMsg = ("No files found.");
}
inst.SetProgressPos(10, ProgresssMsg);
inst.SetStatus(EdmTaskStatus.EdmTaskStat_DoneOK, 0, "", NotificationArray, ProgresssMsg);
}
catch (System.Runtime.InteropServices.COMException ex)
{
inst.SetStatus(EdmTaskStatus.EdmTaskStat_DoneFailed, ex.ErrorCode, "The test task failed!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void OnTaskSetup(ref EdmCmd poCmd, ref System.Array ppoData)
{
try
{
//Get the property interface used to
//access the framework
IEdmTaskProperties props = default(IEdmTaskProperties);
props = (IEdmTaskProperties)poCmd.mpoExtra;
//Set the property flag that says you want a
//menu item for the user to launch the task
//and a flag to support scheduling
props.TaskFlags = (int)EdmTaskFlag.EdmTask_SupportsInitExec + (int)EdmTaskFlag.EdmTask_SupportsScheduling;
//Set up the menu commands to launch this task
EdmTaskMenuCmd[] cmds = new EdmTaskMenuCmd[1];
cmds[0].mbsMenuString = "List Approved files task";
cmds[0].mbsStatusBarHelp = "This command runs the task add-in to get the names of the files in the Approved state.";
cmds[0].mlCmdID = 1;
cmds[0].mlEdmMenuFlags = (int)EdmMenuFlags.EdmMenu_Nothing;
props.SetMenuCmds(cmds);
}
catch (System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void PauseToAttachProcess(string callbackType)
{
try
{
//If the debugger isn't already attached to a
//process,
if (!Debugger.IsAttached)
{
//Launch the debug dialog
//Debugger.Launch()
//or
//use a MsgBox dialog to pause execution
//and allow the user time to attach it
MessageBox.Show("Attach debugger to process \"" + Process.GetCurrentProcess().ProcessName + "\" for callback \"" + callbackType + "\" before clicking OK.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}