26 Aug 2020

Design Automation for Inventor VS template - run WorkItem

I wrote before about the Visual Studio project template for Design Automation for Inventor and how it can help with local debugging
This time I’ll show you how to use its "Interaction" project to create/update the AppBundle and Activity, plus run the WorkItem.

First of all, you'll have to provide your Forge App's credentials in the "appsettings.json" file:

Provide Forge App's credentialsSince the AppBundle project of the solution (in our case named "TestPlugin") already has a Post-Build event set up to gather and generate the necessary content of the AppBundle and zip it up, therefore you can already use the "Interaction" project's "Post app bundle" task to upload your AppBundle to Design Automation.
Note: it's worth doing a "Rebuild Solution" to compile everything before running the "Interaction" project:

Rebuild Solution

In the "Publisher.Custom.cs" file you can set all the AppBundleActivity and WorkItem properties you want to use. In this case I added the "InputJson" property that will provide the json file with parameter values for the WorkItem.

Activity and WorkItemn propertiesI will be able to use that property for both the Activity and the WorkItem and in the Activity's commandLine as well. So let's use it in the commandLine to provide the path to the downloaded json file:

private static List<string> GetActivityCommandLine()
{
    return new List<string> { 
        $"$(engine.path)\\InventorCoreConsole.exe /al \"$(appbundles[{Constants.Activity.Id}].path)\"" +
        $"/i \"$(args[{Constants.Parameters.InventorDoc}].path)\" \"$(args[{Constants.Parameters.InputJson}].path)\"" 
    };
}

Because we are not providing it as part of a known commandLine argument (like /i, which specifies the document to open) therefore the AppBundle's RunWithArguments() function will be called with the "map" object passed to it. In our case, "map" will contain the InputJson's path under key "_1" - see Run() vs RunWithArguments() 

Now lets add it to the Activity parameters as well:

private static Dictionary<string, Parameter> GetActivityParams()
{
    return new Dictionary<string, Parameter>
    {
        {
            Constants.Parameters.InventorDoc,
            new Parameter
            {
                Verb = Verb.Get,
                Description = "IPT file to process"
            }
        },
        {
            Constants.Parameters.InputJson,
            new Parameter
            {
                Verb = Verb.Get,
                LocalName = "params.json",
                Description = "Input parameters"
            }
        },
        {
            Constants.Parameters.OutputIpt,
            new Parameter
            {
                Verb = Verb.Put,
                LocalName = "result.ipt",
                Description = "Resulting IPT",
                Ondemand = false,
                Required = false
            }
        }
    };
}

Note: make sure you specify a localName for the json file in the properties of either the Activity (as done above) or in the WorkItem, otherwise Design Automation will treat the URL as an actual URL and will try to download from there, which in our case will fail because we will provide "data:application/json,{\"length\":\"10 in\"}" instead - see below. 

The Activity is ready now, so you could run the "Post activity" task of the "Interaction" project already. Now let's add the same parameter to the WorkItem as well:

private static Dictionary<string, IArgument> GetWorkItemArgs()
{
    // TODO: update the URLs below with real values
    return new Dictionary<string, IArgument>
    {
        {
            Constants.Parameters.InventorDoc,
            new XrefTreeArgument
            {
                Url = "!!! CHANGE ME !!!"
            }
        },
        {
            Constants.Parameters.InputJson,
            new XrefTreeArgument
            {
                Url = "data:application/json,{\"length\":\"10 in\"}"
            }
        },
        {
            Constants.Parameters.OutputIpt,
            new XrefTreeArgument
            {
                Verb = Verb.Put,
                Url = "!!! CHANGE ME !!!"
            }
        }
    };
}

As you can see in the above code, it's also possible to provide the content of the json file directly in the URL, instead of having to create the json file, upload it somewhere, generate a pre-signed URL for it and pass that to the WorkItem.

The easiest way to provide URL's for InventorDoc and OutputIpt is to upload the files to your bucket on OSS and also generate the pre-signed URL for them:

Use OSS for WorkItem files

You could use a utility like https://oss-manager.autodesk.io/ or the Autodesk Forge Tools VS Code Extension to achieve all this

Generate pre-signed URLWhen you filled in the URL's for all the WorkItem properties, you are ready to execute the "Run work item" task of the "Interaction" project. Once the WorkItem finished running, the Report generated for it by Design Automation will be printed to the console:

WorkItem report

Apart from having information about what Design Automation engine was used, if it succeeded or not, etc, it will also show all the Trace messages printed from your AppBundle or iLogic Rules:

Trace messages from AppBundle and iLogic Rules

You can also use the https://da-manager.autodesk.io/ or the above mentioned VS Code extension to check/modify your AppBundles, Activities and WorkItems:

Design Automation Tools

Related Article