A Beginners Guide to Creating PDM Add-ins

On the heels of our earlier step-by-step guide to creating a SolidWorks add-in, I wanted to offer a similar guide to creating SolidWorks PDM add-ins from the beginning, right up to and including deploying the add-in. Although our flagship SolidWorks drawing note add-in, Versa Note, doesnโ€™t directly interact with PDM currently, it is possible to store your drawing note template file in your PDM vault in order to share with your full team.

Unlike SolidWorks add-ins, there is no need for a starter template (or SDK) to create a PDM add-in, so I will cover this topic by creating a project from scratch. I believe this is an important step to a deeper understanding of the process. There are, however, some great tools to expedite this process such as theย PDM SDKย for Visual Studio created by Blue Byte Systems. After you have followed along with this tutorial, I would encourage you to try this tool to understand the benefits it offers.

Again, we will use and Microsoft Visual Studio (reminder that although the community edition is free, itโ€™s important to read the license terms to be sure that your usage falls within the terms of service). After opening Visual Studio, select โ€˜Create a new projectโ€™ when prompted:

In the search, type โ€˜classโ€™ and select either the C# or VB class library (be sure to choose one of the options that include โ€œ.NET Frameworkโ€). For this guide, I will choose a C# Class Library project.

Give your project a name, verify the project save location and ensure the highest Framework is selected (i.e. .NET Framework 4.8)

After you click โ€˜Createโ€™, Visual Studio will generate a new class library project, and a class called โ€˜Class1โ€™. In the Solution Explorer, you may delete this class and we will create one with an appropriate name (alternately, it is possible to rename it).

Right click on the project name at the top of the Solution Explorer and select โ€˜Addโ€™ -> โ€˜Classโ€ฆโ€™ and call it whatever you like (I will call it PDM_Addin_Main)

Next, we need to add a reference to the PDM library. To do this, right click on the โ€˜Referencesโ€™ node in the Solution Explorer and select โ€˜Add Referenceโ€ฆโ€™ and browse to the file EPDM.Interop.epdm.dll in the local PDM client installation folder (likely C:\Program Files\SolidWorks\SolidWorks PDM)

To avoid unexpected behavior, SolidWorks recommends setting the โ€˜Embed Interop Typesโ€™ setting to False for the EPDM.Interop.epdm dll.

At the top of the PDM_Addin_Main.ca file, you will see a number of โ€˜usingโ€™ statements. The default using statements can be removed and replaced by a single line:

using EPDM.Interop.epdm;

Next, we need to implement the PDM addin interface. Tim Corey offers great content covering C# development, and has several great videos explaining the concept of interfaces. Assuming you donโ€™t have time to watch them, think of an interface as a contract; and by implementing the interface, your application must fulfil the terms of the contract by including the methods and properties required by that interface.

Visual Studio makes it easy to implement an interface. After the โ€˜usingโ€™ statement you added above, make the following change:

using EPDM.Interop.epdm;

namespace PDM_Addin
{
    internal class PDM_Addin_Main
    {
    }
}

Becomes:using EPDM.Interop.epdm;

using EPDM.Interop.epdm;

namespace PDM_Addin
{
    [Guid(""), ComVisible(true)]
    public class PDM_Addin_Main : IEdmAddIn5
    {
    }
}

Note that we have added the GUID ComVisible properties to the PDM_Addin_Main class, made the class public and we have implemented the interface IEdmAddIn5 using a colon after the class name.

Last, letโ€™s generate a GUID or Globally Unique Identifier, which is a randomly generated 36 character string assigned to each windows application. Itโ€™s length and complexity ensures that no two applications are assigned the same value. To generate a GUID, press the Windows key, and type PowerShell to search for and run Windows PowerShell. In PowerShell, type the following, and press Enter:

[guid]::NewGuid()

Then add the resulting GUID to your code:

using EPDM.Interop.epdm;

namespace PDM_Addin
{
    [Guid("90b7faf9-5f46-4deb-bd00-9754ac5d2e9b"), ComVisible(true)]
    public class PDM_Addin_Main : IEdmAddIn5
    {
    }
}

Visual Studio will underline IEdmAddIn5 red, indicating that the โ€˜termsโ€™ of the โ€˜contractโ€™ have not be fulfilled. Thankfully, Visual Studio offers an easy way to add the missing methods and properties. Simply hit โ€˜ctrl-.โ€™ and select the โ€˜Implement Interfaceโ€™ option:

This will add 2 empty methods: GetAddInInfo and OnCmd. The GetAddInInfo method is used to provide information about the add-in when it is loaded by PDM, and is where we will specify the triggers that our add-in will watch for. The second method will fire when any command is executed, and will be used to execute the code we provide. In the GetAddInInfo method, weโ€™ll remove the line

throw new NotImplementedException();

and replace it with few simple lines of code, so your method looks like this:

public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
{
  poInfo.mbsAddInName = "Awesome PDM AddIn";
  poInfo.mlRequiredVersionMajor = 21; // minimum supported version PDM 2021

  poCmdMgr.AddCmd(1, "Amazing Menu Command"); // menu command 
  poCmdMgr.AddHook(EdmCmdType.EdmCmd_PreState); // state change event
}

You can find a full list of PDM hooks (events that can be used to trigger code) here.

The last setup step is will allow it to load in PDM. In the Solution Explorer, double click on โ€˜Properties, select โ€˜Buildโ€™ tab, and check the option to โ€˜Register for COM interopโ€™

Now weโ€™re ready to make the add-in do something. For now, letโ€™s keep it bare bones, and simply display a message to the user. In a future blog post, we will update the add-in to perform more useful tasks.

In the OnCmd method, weโ€™ll add the code to handle the menu command and pre-state change hook we added:

public void OnCmd(ref EdmCmd poCmd, ref EdmCmdData[] ppoData)
{
    IEdmVault21 eVault = poCmd.mpoVault as IEdmVault21; // get the PDM vault object

    if ( poCmd.meCmdType == EdmCmdType.EdmCmd_Menu )
    {
        if ( poCmd.mlCmdID == 1 ) // menu command 1
        {
            eVault.MsgBox(0, 
               "Congratulations, the menu command executed successfully!");
        }
    }
    else if ( poCmd.meCmdType == EdmCmdType.EdmCmd_PreState )
    {
        EdmMBoxResult result = eVault.MsgBox(0, 
            "Congratulations, the state change method executed successfully!" +
            "\r\nPress 'Cancel' to cancel the transition", EdmMBoxType.EdmMbt_OKCancel);

        if ( result == EdmMBoxResult.EdmMbr_Cancel ) 
        {
            poCmd.mbCancel = Convert.ToInt16(true); // this will cancel the transition
        }
    }
}

Now we can build our add-in!

To load the add-in in PDM, open the Administration tool and expand the node for your vault, then right click on Add-ins and select โ€œNew Add-inโ€ฆโ€. Browse to the files in the project output folder:

Select all of the files and add to PDM. Any time you load a PDM add-in, it is necessary to restart the Explorer process to load the new add-in. The easiest way to do this is to open the Windows Task Manager (Ctrl-Shift-Esc) then right click on Explorer and select Restart (alternativelyyou can log out and back in).

Now you can browse to your vault and open the Tools menu, where you should see the menu command we added. You may also transition a file to see the state change message we added.

Thatโ€™s it! In a future post, we will talk about debugging PDM add-ins and we will implement functionality to make this add-in more useful. As always, thank you for reading, say tuned for more tutorials, if you havenโ€™t checked out our SolidWorks drawing notes add-in Versa Note, now is the time; download and start your free trial today!