Language: EN

net6-solidworks

How to connect with the SolidWorks API from NET6

New entry in the series aimed at seeing how to connect different programs through their API with a NET6 application written in C#.

We have previously seen how to connect with different Microsoft Office applications, such as Word, Excel, and Outlook. We have also seen that the ability to integrate different technologies is 1 of the most relevant points of the IoT.

Now we are going to move on to controlling CAD software like SolidWorks. Being able to interact with CAD software has many opportunities within the field of industry and 4.0.

Imagine, for example, the possibility of connecting an ESP32 with a sensor, and in the event of a specific event, being able to display, print a drawing, take a screenshot and save it, display a 3D drawing.

In general, there are a lot of very interesting examples within the field of Industry 4.0 that can be obtained from connecting CAD software with your own application in NET6.

Unfortunately, the SolidWorks .NET API is really bad. Precisely, it is one of the reasons why it was chosen to see the differences between a well-formed API and a poorly formed API.

In any case, even though it is going to be a bit more painful on the way, it is perfectly possible to connect with SolidWorks from an application in .NET6.

How to connect SolidWorks with a NET6 application

As usual for this example, we want a simple console application. Now we must add the COM references of SolidWorks, which are:

  • SldWorks 20xx Type Library
  • SOLIDWORKS 20xx Commands type library
  • SOLIDWORKS 20xx Constant type library

where 20xx is the version of SolidWorks you have installed. As we see, right from the start, we start because the names of the libraries are not clear (SldWorks? What is the abbreviation for if the rest are SOLIDWORKS)

On the other hand, to access the SolidWorks API, we will need to do Marshaling, something that is not available in NET6. Therefore, I have added a Marshal2 code in the Github example to replace this deficiency.

With our references added and our Marshal2 object, we copy the following code,

using DemoSolidWorksApi;
using SldWorks;
using System.Runtime.InteropServices;

var progId = "SldWorks.Application";
var progType = System.Type.GetTypeFromProgID(progId);

var app = Marshal2.GetActiveObject(progId) as ISldWorks;

var part = app.NewPart() as PartDoc;

var box = CreateBox(app, 0.2, 0.2);
box.Name = "MyBox";

var cyl = CreateCylinder(app, 1, 1);
cyl.Name = "MyCylinder";

Console.ReadLine();

static IFeature CreateBox(ISldWorks app, double diam, double height)
{
    var part = app.ActiveDoc as IPartDoc;

    var modeler = app.IGetModeler();

    var boxBody = modeler.CreateBodyFromBox(new double[]
    {
                0, 0, 0,
                1, 0, 0,
                1, 1, 1
    }) as Body;
    if (boxBody != null)
    {
        var feat = part.CreateFeatureFromBody3(boxBody, false, 1) as IFeature;
        return feat;
    }
    else
    {
        throw new NullReferenceException("Failed to create body. Make sure that the parameters are valid");
    }
}

static IFeature CreateCylinder(ISldWorks app, double diam, double height)
{
    var part = app.ActiveDoc as IPartDoc;

    var modeler = app.IGetModeler();

    var cylBody = modeler.CreateBodyFromCyl(new double[]
   {
                2, 0, 0,
                0, 1, 0,
                diam / 2, height
   }) as Body;

    if (cylBody != null)
    {
        var feat = part.CreateFeatureFromBody3(cylBody, false, 1) as IFeature;
        return feat;
    }
    else
    {
        throw new NullReferenceException("Failed to create body. Make sure that the parameters are valid");
    }
}

Where we are simply creating a cube and a cylinder respectively. Enough for this example, and to see how horrible the .NET API of SolidWorks is.

The object model is poorly defined, the classes do not offer information about the parameters they receive, nor the interfaces they need. The order of the parameters is not very intuitive, there are deprecated functions everywhere, and the documentation is almost non-existent.

I am not criticizing the CAD software itself. In fact, there are many people who are great defenders of SolidWorks. But, objectively, its .NET API is really bad.

In any case, the example is there, it works, and it is a good example of an API that we will have to suffer if we have to use it.

In the next entry, we will see how to connect with another CAD software, Autodesk Inventor, and I’ll give you a spoiler that its API is much better developed. Until next time!

Download the code

All the code in this post is available for download on Github. github-full