Search code examples
c#loadingadd-insolidworks

How to activate the SolidWorks add-in "FeatureWorks" with C#?


I am building a program in C# that uses the SolidWorks API.

When I create a new instance of SolidWorks with the command:

sldWrks = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application"));

the application is loaded but the add-ins are not.
Specifically, the add-in I am interested in for this program is FeatureWorks.

How can I get SolidWorks to start with the add-ins active?
Do I need to set the options in some text file in the SolidWorks installation folder or perhaps modify the registry keys?


Solution

  • You have to load it with the call:

    swApp.LoadAddIn(FileName);
    

    Here's the documentation from SolidWorks.

    Here's how I turned on the PDM Add-in. I'm sure it would be similar to FeatureWorks. Here was using a C# Windows Forms app (hence the MessageBox calls). Hopefully this gives you enough for you to figure out the rest.

    private SldWorks.SldWorks ConnectToSW()
        {
            SldWorks.SldWorks swApp = null;
    
            try
            {
                //try to connect to an existing open instance of SolidWorks:
                swApp = (SldWorks.SldWorks)Marshal.GetActiveObject("SldWorks.Application");
            }
            catch (Exception)
            {
                //try to open a new instance:
                try
                {
                    swApp = new SldWorks.SldWorks();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Can't connect to SolidWorks and can't open a new SolidWorks instance.\n" + ex.Message, "Error connecting to SolidWorks", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return null;
                }
            }
            swApp.Visible = true;
    
            //now also try to make sure PDM add-in is enabled:
            try
            {
                string appPath = swApp.GetExecutablePath();
                appPath = appPath.Substring(0, appPath.LastIndexOf('\\')) + "\\SOLIDWORKS PDM";    //e.g. C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS -> C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS PDM
    
                string pdmAddinPath1 = $"{appPath}\\PDMSW.dll";
                string pdmAddinPath2 = $"{appPath}\\epdmlib.dll";
    
                //try the first addin dll
                if (!File.Exists(pdmAddinPath1))
                {
                    DialogResult dlgRes = MessageBox.Show($"Can't load the PDM add-in:\n\n{pdmAddinPath1}\n\nPlease manually turn on the PDM Add-in in SolidWorks and click OK to continue", "Enable PDM Manually", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dlgRes == DialogResult.Cancel) throw new Exception();
                }
                try
                {
                    swApp.LoadAddIn(pdmAddinPath1);
                }
                catch (Exception)
                {
                    throw;
                }
    
                // try the second addin dll
                if (!File.Exists(pdmAddinPath2))
                {
                    DialogResult dlgRes = MessageBox.Show($"Can't load the PDM add-in:\n\n{pdmAddinPath2}\n\nPlease manually turn on the PDM Add-in in SolidWorks and click OK to continue", "Enable PDM Manually", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dlgRes == DialogResult.Cancel) throw new Exception();
                }
                Cursor.Current = Cursors.WaitCursor;
                try
                {
                    swApp.LoadAddIn(pdmAddinPath2);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Couldn't load the PDM addin.  Nothing about this is going to work right.\n\n{ex.Message}", "PDM Add-in not found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //return null;
            }
    
            return swApp;
        }