SolidWorks Enterprise PDM (EPDM) ships with an extensive API that provides one with various methods and properties to interact with a vault. With a little knowledge and vision there isn’t much that can’t be done as far as customising your installation.
Most API examples start off with a “Hello World” example, but I am going to do something a little different and provide a working add-in that may prove useful. The add-in sets a variable called “FileHyperlink” to the ‘explore ‘ link. There are other commands one can use for the link, but explore is best suited here.
There are two methods that must be implemented when creating an add-in because your add-in implements the IEdmAddin5 interface. The first being GetAddInInfo() and the second being OnCmd().
GetAddInInfo(): EPDM Calls this method when the add-in is loaded. This is where you’ll set the add-in information as well as add any hooks or commands that you wish EPDM to call. (Commands are for menu or tool bar commands. Hooks are for events.) Take note that the AddInVersion is an integer value, so values of 1.1 or 2.3 are not supported.
public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 vault, IEdmCmdMgr5 cmdMgr)
{
poInfo.mbsAddInName = "EPDMSetHyperLinkVariable";
poInfo.mbsCompany = "Your Company";
poInfo.mbsDescription = "Sets a file variable called FileHyperlink to the explore link.";
poInfo.mlAddInVersion = 1;
poInfo.mlRequiredVersionMajor = 7;
poInfo.mlRequiredVersionMinor = 0;
cmdMgr.AddHook(EdmCmdType.EdmCmd_PreUnlock, null);
cmdMgr.AddHook(EdmCmdType.EdmCmd_PreUndoLock, null);
}
OnCmd(): EPDM calls this method whenever one of your commands or hooks you registered in the GetAddInInfo method is executed. The data array is an array of EdmCmdData structs, which contains information about the files or folders passed to the callback.
public void OnCmd(ref EdmCmd edmCmd, ref Array data)
{
if (data == null)
return;
if (edmCmd.meCmdType == EdmCmdType.EdmCmd_PreUnlock || edmCmd.meCmdType == EdmCmdType.EdmCmd_PreUndoLock)
{
IEdmVault8 vault = (IEdmVault8)edmCmd.mpoVault;
foreach (EdmCmdData cmdData in data)
{
IEdmFile5 file = (IEdmFile5)vault.GetObject(EdmObjectType.EdmObject_File, cmdData.mlObjectID1);
IEdmFolder5 folder = (IEdmFolder5)vault.GetObject(EdmObjectType.EdmObject_Folder, cmdData.mlObjectID2);
object hyperlink = String.Format(conisioString, vault.Name, folder.ID.ToString(), file.ID.ToString(), file.Name);
if (file != null && folder != null)
{
if (file.IsLocked)
{
IEdmEnumeratorVariable8 EnumVar = (IEdmEnumeratorVariable8)file.GetEnumeratorVariable(file.GetLocalPath(folder.ID));
if (EnumVar != null)
{
try
{
EnumVar.SetVar("FileHyperlink", String.Empty, ref hyperlink, false);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(String.Format("An error occured setting hyperlink variable: {0}.", ex.Message), "Set hyperlink variable", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
}
finally
{
EnumVar.CloseFile(true);
}
}
}
}
}
}
}
There are a couple of lines in the code above that are not required but I generally include them because they are in my Visual Studio template, and it saves me having to remember to do these checks.
First I check if the array of files is null, and if it is, I don’t do anything. This is redundant because the add-in isn’t calling a command; it’s hooked into an event which will always have data associated with it.
The other is the first if statement; I check to see if the command type (event) is the one we want. Again, this is redundant because we added the hooks for this event and we’re running the same code for both events. If we were executing different code for each event we would need to check which event is being called.
IEdmEnumeratorVariable8
The IEdmEnumeratorVariable8 interface is used to update file and folder variables. Generally the interface is used to update single files and using IEdmBatchUpdate is more efficient. (I’ll follow up with a post updating this code to use batch update.) I wanted to make a point on CloseFile().
Always call CloseFile() in a try… catch… finally block. The IEdmEnumeratorVariable interfaces ‘open’ the file when creating the type. If you don’t close the file when you’re done with it, you could (and probably will) run into problems.
Summing it up
I realise this isn’t much of a tutorial, but there isn’t a lot to teach here. The only requirement when creating an add-in for EPDM is that you reference the latest version of EdmLib, and you implement the IEdmAddin5 interface correctly.
I’ll do my best to provide examples of the new features of the EPDM 2010 API but in the meantime if you have any suggestions of specific areas of the API you’d like me to post on, feel free to leave a comment.
You can find the source code for this add-in and others on the EPDM page.