Alex
Alex

Reputation: 1038

Returning Revit elements data as JSON in Forge API

Within a web client using the Forge API, I would like to get a JSON array of Revit elements in a model. Using the Design Automation API, I am creating an Activity that uses FilteredElementCollector to retrieve the elements, but once I have the elements I'm not sure the best way to retrieve those results in my web service. This Activity does not need to write back to an .rvt file.

The CountItApp tutorial writes the results to a results.txt file in cloud storage, and the web app then downloads that results.txt file and parses the results. On my web client I want to display these results, but file I/O does not seem like a very good solution for JSON data. A couple alternatives I've considered:

  1. Write results to an external database and query that database in my web application once the WorkItem completes. As far as I know this is not possible due to Forge's restrictions on network access within an Activity.
  2. Pass the results with the onComplete callback. I don't know if this is possible.

Upvotes: 2

Views: 1185

Answers (1)

Rahul Bhobe
Rahul Bhobe

Reputation: 4451

Design automation allows you to post a workitem with output arguments with POST callback. This allows you to receive the output data as application/json if your output file generated by your activity is a json file.

Design automation also allows you to specify a variable workitem.id in your output url. When your workitem completes we shall call this url with the variable expanded to the id of that workitem. This dynamic variable path allows you to determine the workitem id associated with that callback.

Here is how you could go about. First define an activity with such an output parameter (verb: post) with a hardcoded local name result.json:

"results": {
    "zip": false,
    "ondemand": false,
    "verb": "post",
    "description": "Results",
    "required": true,
    "localName": "results.json"
}

In your appbundle code save the json contents in a file with hardcoded name result.json to the current working folder.

using (StreamWriter sw = File.CreateText("result.json"))
{
    sw.WriteLine(JsonConvert.SerializeObject(data, Formatting.Indented));
    sw.Close();
}

Then you can post a workitem like so:

"result": {
    "verb": "post",
    "url": "https://www.yourserver.com/results/$(workitem.id)"
}

In your server implementation of the callback you will get the json contents as the payload. You may read the results and communicate back to the client corresponding to the workitem id, using sockets or any other means of communication you may have with your client.

Upvotes: 3

Related Questions