New entry in the series aimed at seeing how to connect different programs through their API with a NET6 application written in C#.
Previously we have seen how to connect with different Microsoft Office applications, such as Word, Excel, and Outlook. We have also seen that the ability to integrate between different technologies is one of the most relevant points of IoT.
Now we are going to move on to controlling CAD software like SolidWorks. Being able to interact with CAD software has many applications within the field of Industry and 4.0.
Imagine, for example, the possibility of connecting an ESP32 with a sensor, and upon a certain event being able to display/print a blueprint, take a screenshot and save it, or display a 3D drawing.
In general, there are many very interesting examples within the field of Industry 4.0 that can be achieved by connecting CAD software with your own NET6 application.
Unfortunately, the .NET API for SolidWorks is really bad. Precisely, this is one of the reasons I chose it to see the differences between a well-formed API and a poorly formed one.
In any case, although we will have to suffer a bit more along the way, it is perfectly possible to connect to SolidWorks from a .NET6 application.
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 for 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 can see, right from the start, we begin with the fact that the library names are not clear (SldWorks? Why the abbreviation if the others 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 compensate for this lack.
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 for SolidWorks is.
The object model is poorly defined, the classes do not provide information about the parameters they receive, nor the interfaces they need. The order of parameters is unintuitive, there are obsolete functions everywhere, and the documentation is almost non-existent.
I am not criticizing the CAD software itself. In fact, many people 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 with if we have to use it.
In the next entry, we will see how to connect to another CAD software, Autodesk Inventor, and I’ll give you a spoiler: its API is much better crafted. See you next time!
Download the Code
All the code for this entry is available for download on Github.

