25 Apr 2019

Design Automation for Inventor VS template

As mentioned in this blog post there is a Visual Studio Project Template you can use to create a new Inventor add-in that can be used with Design Automation API to drive Inventor on the cloud.

One benefit of this, apart from creating the necessary add-in that will drive Inventor Server, is that it also generates a helper project (see debugPluginLocally) which enables you to test the add-in locally. 

Inside Visual Studio just go to "Tools >> Extensions and Updates..." then search for "Design Automation for Inventor" and click "Download"

Visual Studio Template

Now when creating a new project you'll have the option to select the "Design Automation for Inventor" template

DA4I template

This will create a solution with two projects: one is the actual add-in that you can use with Inventor Server on Forge, the other is the helper project for testing the add-in locally:

DA4I add-in

When compiling the solution it also creates a zip for the add-in bundle in the Output folder that can be uploaded to Design Automation as an AppBundle:

Output folder

Just to show the various parts together. The "Bundle" folder is one level up from the Build Output folder, which is set to "bin\Debug" by default.
All the contents of that folder are then zipped up and placed in the "Output" folder, which will be a subfolder of the Solution's root folder:  

Folder structure

In case of using Visual Studio 2019 to create the project leave the option "Place solution and project in the same directory" unticked otherwise the relative path set for "PackagePathname" in "appsettings.json" of the "Interaction" project will not be correct. If you do prefer this option then just modify the value in "appsettings.json" accordingly.

Leave option unticked

The source code of SampleBundlePlugin will also reference the Autodesk.Forge.DesignAutomation.Inventor.Utils NuGet package which, among other things, also includes the HeartBeat class. You can use that to make sure that your app bundle code does not get shut down prematurely. When a process is started on the Design Automation server (inc InventorCoreConsole.exe), it needs to signal that it is still running fine. If it produces no sign of life (print something to the console) for more than a minute then it will get shut down.
Some operations in Inventor (e.g. making a time-consuming update) might take longer and cause your app bundle to get ended. 
An instance of HeartBeat class will keep writing an update from a separate thread every 50 seconds to the console in order to avoid that problem.
The sample code provided will use that object as well:

public void RunWithArguments(Document doc, NameValueMap map)
{
    LogTrace("Processing " + doc.FullFileName);

    try
    {
        // ...

        if (doc.DocumentType == DocumentTypeEnum.kPartDocumentObject)
        {
            using (new HeartBeat()) // <--
            {
                // TODO: handle the Inventor part here
            }
        }
        else if (doc.DocumentType == DocumentTypeEnum.kAssemblyDocumentObject) // Assembly.
        {
            using (new HeartBeat()) // <--
            {
                // TODO: handle the Inventor assembly here
            }
        }
    }
 
    // etc.
}

Only use a single instance of HeartBeat in the outermost scope of your code, as done in the code above as well.

Related Article