Language: EN

net6-inventor

How to connect to the Autodesk Inventor API from NET6

We continue and, for the moment at least, we finish with the series of entries aimed at seeing how to connect an application through its API with a NET6 application written in C#.

Previously, we have commented that the integration of different technologies is one of the important points of IoT applications. So we have seen how to connect with Microsoft Word, Excel, and Outlook.

In the previous entry, we saw how to connect them with CAD software like SolidWorks. We commented that this offers us a great number of possibilities, for example, within the scope of industry 4.0.

We also saw that the SolidWorks API was not particularly well formed or documented, and in general, working with it was a little hell. Today we are going to see the API of another CAD software, Autodesk Inventor.

Regardless of whether we like it more or less as CAD, the truth is that the Autodesk Inventor API is much better than that of Solid Works.

So we are going to see an example of how we could connect to the Autodesk Inventor API from a NET6 application.

How to connect Autodesk Inventor with a NET6 application

Just like in all the examples in the series, we start by creating a NET6 console application. Next, we add the following COM reference ‘Autodesk Inventor Object Library’.

As in the SolidWorks example, the Autodesk Inventor API needs Marshaling, which is not available in NET6. Therefore, I am attaching the Marshal2 object in the code repo to cover this deficiency.

Next, we copy the following code,

var app = (Application)Marshal2.GetActiveObject("Inventor.Application");
var path = "your_inventor_file_path";
loadExitingPart(app, path);

createNewPart(app);

Console.ReadLine();

static void loadExitingPart(Application app, string path)
{
    var doc = app.Documents.Open(path);
    System.Threading.Thread.Sleep(200);
    app.CommandManager.ControlDefinitions["AppIsometricViewCmd"].Execute();
    app.ActiveView.DisplayMode = DisplayModeEnum.kTechnicalIllustrationRendering;

    doc.SaveAs("C:\\temp\\thumbnail.png", true);
}

static void createNewPart(Application app)
{
    var doc = app.Documents.Add(DocumentTypeEnum.kPartDocumentObject);
    
    var trans = app.TransactionManager.StartTransaction((_Document)doc, "My Command");
    drawCube(app, (PartDocument)trans.Document, 5, 5, 5);
    Thread.Sleep(200);
    app.CommandManager.ControlDefinitions["AppIsometricViewCmd"].Execute();
    trans.End();

    app.ActiveView.DisplayMode = DisplayModeEnum.kTechnicalIllustrationRendering;
}

static void drawCube(Application app, PartDocument doc, int x, int y, int z)
{
    var comp = doc.ComponentDefinition;

    var sketch = comp.Sketches.Add(comp.WorkPlanes[3]);

    var geo = app.TransientGeometry;
    var lines = sketch.SketchLines.AddAsTwoPointRectangle(geo.CreatePoint2d(0, 0), geo.CreatePoint2d(3, 3));

    var profile = sketch.Profiles.AddForSolid();

    var extrudeDef = comp.Features.ExtrudeFeatures.CreateExtrudeDefinition(profile, PartFeatureOperationEnum.kJoinOperation);
    extrudeDef.SetDistanceExtent(3, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection);

    var extrude = comp.Features.ExtrudeFeatures.Add(extrudeDef);

    WorkAxis XAxis = comp.WorkAxes[1];
    WorkAxis YAxis = comp.WorkAxes[2];
    WorkAxis ZAxis = comp.WorkAxes[3];

    ObjectCollection objCol = app.TransientObjects.CreateObjectCollection();
    objCol.Add(extrude);

    var pattern = comp.Features.RectangularPatternFeatures.Add(objCol, XAxis, true, 5, 4, YDirectionEntity: YAxis, YCount: 5, YSpacing: 4);

    ObjectCollection objCol2 = app.TransientObjects.CreateObjectCollection();
    objCol2.Add(extrude);
    objCol2.Add(pattern);

    var pattern2 = comp.Features.RectangularPatternFeatures.Add(objCol2, ZAxis, true, 5, 4);
}

This code provides example functions for opening a file, and saving a screenshot of the model. And, on the other hand, to create a new file and create a 3D array of cubes.

Even with these simple examples, it is already possible to see that the API object model is much better. In addition, the functions provide information about the parameters they receive and return.

And so far, at least for now, this series dedicated to seeing how to connect with different applications from a program made by us in .NET. We have seen examples of very well-formed APIs (those of Microsoft) and others not so well-formed.

In any case, the process of working with a pencil is always more or less similar to finding the name of the references that we have to add to our project and suffering with the object model and the documentation until we manage to do the actions that we want.

Working with APIs is usually tough, but it leads to very interesting projects. It is a very common requirement in IoT and industry 4.0 applications, and it is one of the points where Microsoft Windows stands out the most.

For now, we leave this series here. If at some point we feel like making an entry about another API, we will expand the series. See you in the next entry!

Download the code

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