SOLIDWORKS PDM API hooks are how an add-in reacts to vault activity (PDM add-in events such as check-in, state change or a card button click). You implement IEdmAddIn5, register the events you care about with IEdmCmdMgr5.AddHook (and menu commands with AddCmd) inside GetAddInInfo, and PDM calls your OnCmd method with an EdmCmd describing the event and an EdmCmdData array describing the affected files or folders. Pre-events can be cancelled, post-events cannot. This cheat sheet covers the pieces we use on every PDM add-in project, the EdmCmdType values that matter most, and the mistakes that cost the most debugging time.
The two methods every PDM add-in implements
IEdmAddIn5 has exactly two methods.
GetAddInInfo
PDM calls this when the add-in loads. You fill in the EdmAddInInfo structure (name, company, description, version, and the minimum PDM version required) and use the IEdmCmdMgr5 argument to declare which events and commands you want. Nothing is hooked unless you ask for it here.
OnCmd
PDM calls this every time one of your registered events or menu commands fires. Its signature in C# is:
void OnCmd(ref EdmCmd poCmd, ref EdmCmdData[] ppoData)
poCmd.meCmdType tells you which event fired, poCmd.mlCmdID tells you which menu command was clicked, poCmd.mpoVault gives you the vault, and poCmd.mlParentWnd gives you a window handle for parenting dialogs. ppoData holds one entry per affected item.
AddHook vs AddCmd
| Method | What it registers | Typical use |
|---|---|---|
AddHook(EdmCmdType) |
An event notification | Validate before check-in, stamp a variable after a state change |
AddCmd(id, menuText, flags, ...) |
A right-click menu or toolbar command | “Export BOM”, “Create PDF”, admin utilities |
AddCmd takes a command ID that comes back to you in poCmd.mlCmdID with meCmdType set to EdmCmd_Menu. Backslashes in the menu text create submenus, which we cover in creating submenus with IEdmCmdMgr5.AddCmd. The menu flags parameter controls things like whether the command needs a selection or only appears for files or folders.
The EdmCmdType cheat sheet for SOLIDWORKS PDM API hooks
PDM uses “lock” for checkout and “unlock” for check-in, which trips up everyone the first time. These are the hooks we use most often.
| EdmCmdType | Fires when | Cancellable |
|---|---|---|
EdmCmd_Menu |
User clicks a command added with AddCmd |
Not applicable |
EdmCmd_PreAdd / EdmCmd_PostAdd |
Files are added to the vault | Pre only |
EdmCmd_PreLock / EdmCmd_PostLock |
Files are checked out | Pre only |
EdmCmd_PreUnlock / EdmCmd_PostUnlock |
Files are checked in | Pre only |
EdmCmd_PreUndoLock / EdmCmd_PostUndoLock |
Checkout is undone | Pre only |
EdmCmd_PreState / EdmCmd_PostState |
A workflow transition changes state | Pre only |
EdmCmd_PreRename / EdmCmd_PostRename |
Files are renamed | Pre only |
EdmCmd_PreDelete / EdmCmd_PostDelete |
Files are deleted | Pre only |
EdmCmd_PreMove / EdmCmd_PostMove |
Files are moved | Pre only |
EdmCmd_PreCopy / EdmCmd_PostCopy |
Files are copied | Pre only |
EdmCmd_PreGet / EdmCmd_PostGet |
Files are retrieved (Get) | Pre only |
EdmCmd_PreAddFolder / EdmCmd_PostAddFolder |
A folder is created | Pre only |
EdmCmd_CardButton |
A data card button with “Run add-in” is clicked | Not applicable |
EdmCmd_CardInput |
A data card control value changes | Not applicable |
EdmCmd_CardListSrc |
A card list is populated from an add-in | Not applicable |
EdmCmd_SerialNo |
A serial number is generated by an add-in | Not applicable |
EdmCmd_TaskSetup, EdmCmd_TaskRun and related |
Task configuration and execution | Task specific |
The full enumeration is longer (folder renames, labels, shares and more). Check the EdmCmdType page in the SOLIDWORKS PDM API help for the exact list your version supports, and read our note on writing add-ins with version-specific APIs before you hook something that only exists in newer releases.
Reading the EdmCmdData array
Each EdmCmdData element describes one affected item. The meaning of each field depends on the command type, which is the single most important thing to remember. The fields you will touch most are:
| Field | Usually holds (file events) |
|---|---|
mlObjectID1 |
The file ID |
mlObjectID2 |
The parent folder ID |
mlObjectID3, mlObjectID4 |
Command specific IDs (for example transition or state related IDs on state changes) |
mbsStrData1 |
Command specific text, often the file path |
mbsStrData2 |
Command specific text (for example the new name on a rename) |
mlLongData1, mlLongData2 |
Command specific numbers |
mpoExtra |
Command specific object |
For data card events, some of the interesting information lives on poCmd.mpoExtra instead of the array: card buttons and card input events give you access to the card’s variables so you can read and write values before they are saved. Always check the per-command table in the API help rather than assuming the same layout across events.
Tip: use the IDs, not the paths. Resolve the file with
IEdmVault5.GetObject(EdmObjectType.EdmObject_File, id)and work from theIEdmFile5object. Paths change on rename and move; IDs do not.
Cancelling pre-events
To stop an operation, set poCmd.mbCancel to a non-zero value in a pre-event handler. PDM then aborts the whole operation, so for a batch check-in of fifty files, one bad file cancels all fifty. Tell the user why, using mlParentWnd as the owner of your message box, otherwise they see a silent failure. Cancelling is a recurring question on CADOverflow, for example How to cancel change state by SOLIDWORKS PDM API and Preventing an event from executing using a pre delegate.
Setting mbCancel in a post-event does nothing useful: the work is already done. Not every pre-event honors cancellation in every context either, which we cover in detail in cancelling certain events in PDM.
A minimal C# skeleton
This add-in blocks check-in of files whose name contains “DRAFT” and handles a menu command.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using EPDM.Interop.epdm;
[ComVisible(true)]
[Guid("6F2C1A3E-9B7D-4E21-8C55-2D4B8A1F0E77")]
public class HooksAddIn : IEdmAddIn5
{
private const int CmdShowIds = 1000;
public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
{
poInfo.mbsAddInName = "Hooks Demo";
poInfo.mbsCompany = "Blue Byte Systems Inc.";
poInfo.mbsDescription = "Demonstrates PDM hooks";
poInfo.mlAddInVersion = 1;
poInfo.mlRequiredVersionMajor = 6;
poInfo.mlRequiredVersionMinor = 4;
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PreUnlock);
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PostState);
poCmdMgr.AddCmd(CmdShowIds, "Hooks Demo\\Show IDs",
(int)EdmMenuFlags.EdmMenu_MustHaveSelection, "", "", 0, 0);
}
public void OnCmd(ref EdmCmd poCmd, ref EdmCmdData[] ppoData)
{
try
{
switch (poCmd.meCmdType)
{
case EdmCmdType.EdmCmd_PreUnlock:
foreach (EdmCmdData item in ppoData)
{
string path = item.mbsStrData1 ?? string.Empty;
if (path.ToUpperInvariant().Contains("DRAFT"))
{
MessageBox.Show("Draft files cannot be checked in:\n" + path);
poCmd.mbCancel = 1;
return;
}
}
break;
case EdmCmdType.EdmCmd_Menu:
if (poCmd.mlCmdID == CmdShowIds)
MessageBox.Show("Selected items: " + ppoData.Length);
break;
}
}
catch (Exception ex)
{
// Never let an exception escape OnCmd.
MessageBox.Show("Hooks Demo error: " + ex.Message);
}
}
}
Two notes. The class must be COM visible with a fixed GUID, and the DLL plus its dependencies go into the add-in you load through the Administration tool. If loading fails with “class not registered”, read why class not registered happens when loading PDM add-ins. Second, in production we route the message through a proper owner window rather than a bare MessageBox.Show.
Debugging PDM add-in hooks
The add-in runs inside whatever process hosts the PDM client, most commonly explorer.exe, but also SOLIDWORKS and the Administration tool. That shapes the debugging workflow:
- In the Administration tool, right-click Add-ins and choose Debug Add-ins…. Point it at the DLL in your build output folder. PDM then loads that DLL on your machine straight from the build folder, without you having to package and deploy it to the vault for every test.
- Restart Windows Explorer so PDM loads the debug build.
- In Visual Studio, use Debug > Attach to Process and attach to
explorer.exe(managed code). Set breakpoints inOnCmd. - Trigger the event in File Explorer.
GetAddInInfo runs once at load, so changes to your hook list need another Explorer restart. Rebuilding while Explorer has the DLL loaded fails because the file is locked: restart Explorer before you build.
Performance: hooks run in the user’s face
Every hook runs synchronously inside the user’s client. A slow PreUnlock handler makes every check-in slow for everyone.
- Hook only what you need. Every registered hook means PDM calls into your code for that event across the whole vault.
- Exit early. Check file extension, folder or state before doing any expensive work.
- Batch your lookups.
ppoDatacan contain hundreds of items on a large check-in; avoid one database round trip per item. - Push heavy work out of the hook. Conversions, exports and SOLIDWORKS automation belong in a task, not a post-event handler.
- Consolidate. Several small add-ins, each hooking the same events, multiply the overhead. See our thoughts on the ideal number of add-ins per vault.
Community discussions
- EdmCmd_PostState firing twice on CADOverflow
- How to check PDM event is triggered from SOLIDWORKS on CADOverflow
- Cancel State Change via API on the SOLIDWORKS Forum
FAQ
Is checkout EdmCmd_PreLock or EdmCmd_PreCheckOut?
Checkout is a lock in PDM terms, so use EdmCmd_PreLock and EdmCmd_PostLock. Check-in is EdmCmd_PreUnlock and EdmCmd_PostUnlock.
Why does my hook fire once with many items instead of once per file?
PDM batches operations. One OnCmd call can carry every file in the operation in ppoData, so always loop over the array.
Can I cancel a post-event?
No. By the time a post-event fires the operation has completed. Validate in the matching pre-event instead.
Do I need to restart Explorer after every change?
After every rebuild, yes, because the DLL is loaded into the Explorer process. Logging out and back in to the vault also reloads hooks and menus.
Need help with this?
We build and maintain SOLIDWORKS PDM add-ins for engineering teams, from single hooks to full vault integrations. If you would rather script event automation than compile it, PDMShell exposes many of these events as trigger points. Contact us to talk through your project.
TIPS & TRICKS
51 SOLIDWORKS PDM API tips and tricks that save hours of debugging. 62 pages, with code samples in C# and VBA.
Get the free eBook →