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:
- Create a new C# project in Visual Studio
- 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
- Instantiate a SldWorks object
- set visible
- Open your design document
- Test it
- Close your design document
- 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(); } } }
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.