logo

Custom Exporters

Creating a custom exporter is just like writing a function, with a few small differences:
  • Exporters donโ€™t run on any change to your workflow โ€” they only run on-demand when a user clicks a download / export button.
  • Exporters donโ€™t show up in the Workflow panel. They show up as buttons when you click the Export button:
Image without caption
  • Exporters cannot add anything to the Model, such as adding new elements or geometry.

How to create an exporter

  1. Create a new function with hypar new โ€” just like any other C# function.
  1. Open hypar.json and edit the $schema property of the function to be https://prod-api.hypar.io/schemas/ExporterFunction:
json
{ "$schema": "https://prod-api.hypar.io/schemas/ExporterFunction", "id": "[SOME_GUID_HERE]", "name": "STL Export", ... }
  1. Make sure to add model_dependencies to your function, which specify the models your exporter needs access to. In this example, the exporter creates an STL file from Location and Envelope, both optionally:
json
"model_dependencies": [ { "name": "location", "optional": true }, { "name": "Envelope", "optional": true } ],
  1. Your exporter may export one or more file types. For each type of file you want to make available for export, add a FileDestination input to input_schema, like so.
๐Ÿ’ก
The $hyparFileExtension value is required โ€” without this the exporter will fail!
json
"input_schema": { "type": "object", "properties": { "Download STL": { "$ref": "https://schemas.hypar.io/FileDestination.json", "$hyparFileExtension": "stl" }, "Download Text Report": { "$ref": "https://schemas.hypar.io/FileDestination.json", "$hyparFileExtension": "txt" } } },
๐Ÿ’ก
You may also add regular inputs to the input_schema, like number or text fields, if you need to supply configurable settings for your exporter.
  1. Run hypar init to update the generated code for your function.
  1. In your function code, create the file you want the user to download. You will use the FileDestination object to pass a file back out to the user. Here are a few examples:

Simple text export

c#
var txtDestination = input.DownloadTextReport; // grab the destination string myTextFileContents = "Hello, world!"; // call SetExportTextContents to create an export text file txtDestination.SetExportTextContents(myTextFileContents);

Read a file from disk

c#
var destination = input.DownloadSTL; // grab the destination // write to a temporary local file (see note below *) var tempFilePath = Path.GetTempFileName(); stl.Save(tempFilePath); // create a stream reading from the file path var readStream = File.OpenRead(tempFilePath); // set that stream as the export stream destination.SetExportStream(readStream);
In any Hypar function, youโ€™re only allowed to write files to the system temp directory. Use Path.GetTempFileName or Path.GetTempPath.
In any Hypar function, youโ€™re only allowed to write files to the system temp directory. Use Path.GetTempFileName or Path.GetTempPath.

Stream directly to export

โ—
Donโ€™t close the stream you set as the Export stream (e.g. with a using statement) โ€” Hyparโ€™s API needs access to it after that, and weโ€™ll take care of closing it for you.
c#
var destination = input.DownloadSTL; // grab the destination from input var stream = new MemoryStream(); // create a new stream // do something with that stream // (this example uses a library which can "Save" directly to a stream) stl.Save(stream); // call SetExportStream on the destination destination.SetExportStream(stream); // This line will not be necessary in newer versions // of the `Hypar.Functions` library. stream.Seek(0, SeekOrigin.Begin);
  1. Publish your function with hypar publish to be able to use it on Hypar.

How to use an exporter

Your new exporter wonโ€™t be available in the Export menu immediately. You have to add it to the workflow, just like any other function (note: this is likely to change eventually).
Image without caption
Once added, youโ€™ll see it show up in the export menu:
Image without caption
When you click the button, youโ€™ll see the inputs/settings of your exporter, including the File Destinations as download buttons.
Image without caption