Using COM automation to test SolidWorks

I’m building a test harness for testing CAD software plugins.  Right now, I’m working on figuring out how to use COM automation with SolidWorks and the SolidWorks API.

One of the first steps is to be able to launch Solidworks and open a document. It took a bit of searching (and trial and error), but I finally found the necessary references and steps:

  1. Create a new C# project in Visual Studio
  2. Add the SolidWorks references
    • c:\Program Files\SolidWorks Corp\SolidWorks\api\redist\SolidWorks.Interop.sldworks.dll
    • c:\Program Files\SolidWorks Corp\SolidWorks\api\redist\SolidWorks.Interop.swconst.dll
  3. Instantiate a SldWorks object
  4. set visible
  5. Open your design document
  6. Test it
  7. Close your design document
  8. Exit SolidWorks

The simplest code looks something like this:

using System;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;

namespace SolidWorksExample
{
    class Program
    {
        SldWorks swApp;

        static void Main(string[] args)
        {
            var filename = @"C:\temp\pressure plate.SLDPRT";
            
            var program = new Program();
            program.StartSolidWorks();
            program.openPart(filename);
            program.verify();
            program.closeAllDocuments();
            program.StopSolidWorks();
        }

        public void StartSolidWorks()
        {
            swApp = new SldWorks();
            swApp.Visible = true;
        }

        public void openPart(String filename)
        {
            var swDocType = (int)swDocumentTypes_e.swDocPART;
            var swDocOptions = (int)swOpenDocOptions_e.swOpenDocOptions_Silent;
            var swDocConfig = "";
            int fileerror = 0;
            int filewarning = 0;
            swApp.OpenDoc6(filename, swDocType, swDocOptions, swDocConfig, ref fileerror, ref filewarning);
        }

        public void verify()
        {
            Console.WriteLine("This is where you test it");
        }

        public void closeAllDocuments()
        {
            bool IncludeUnsaved = true;
            swApp.CloseAllDocuments(IncludeUnsaved);
        }

        public void StopSolidWorks()
        {
            swApp.ExitApp();
        }
    }
}


Advertisements

2 thoughts on “Using COM automation to test SolidWorks

  1. You can probably do it in pure COM with VBScript/JScript (or Python/Perl/PHP on Windows) w/o having to go through the COM Interop DLLs on .NET too, but I figure you’re a .NET shop.

    • Yes, you can use COM directly without the .NET interop, and I have an example using COM with Python for other DLLs (for HP Quality Center) but in this case, yeah, C# and .NET is a requirement.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s