How to download files
Introduction
Would you like to extract information like input values and calculation results from your solution and save them locally? Then you can download files by following these steps.
Prerequisites
- You configured the frames and routes for your solution.
- You configured the relevant contents such as the model(s), control panel(s), viewer(s) and datastore.
1. Add a download button
To download a file when a button is clicked, configure a download button. This button has two states:
The initialize state: When there’s no file to download yet, the button state is initialize. In the configuration, this state is also used to trigger the file generation model to run.
The download state: When the model runs and the button is given an asset to download, its state changes to download .
- Add a download button with
kind: DownloadButton. - For the
initializestate, define thetextandcolor. - For the
downloadstate, define thetextandcolor.
The configuration should look similar to the example below:
my-download-button:
kind: DownloadButton
initialize:
text: Generate file
color: primary
download:
text: Download file
color: success
2. Create Grasshopper definition for generating files
Create a Rhino Grasshopper definition to generate the file to download.
❗ Only text-based files such as .txt, .ifc, .csv, .obj can be generated. The logic to create the file contents needs to be defined in Grasshopper. Binary files can be converted to text by encoding them in Base64.
- Define the inputs with the Packhunt
Inputcomponent. - Define a trigger input with the Packhunt
Inputcomponent. This trigger input serves as a gate for running the file generation model and it doesn’t need to be connected to the rest of the Grasshopper logic. - Add the logic for creating the file content.
- Connect the file content to the Packhunt
Create filecomponent. - Create an asset with your file.
- Place the
Create assetcomponent on the canvas. - Define the
Filewith the output of theCreate filecomponent - Define the
Pathwith the file name to download and the file extension. - Define the
project slug. This is your solution name. - Set
isprivatetotrue. - Set
randomizetotrue. This creates a folder with a unique name for your file.
- Place the
- Output the asset.
- Connect the output of the
Create assetcomponent to a PackhuntDataOutputcomponent. - Give a unique name to the
DataOutputcomponent.
- Connect the output of the
- Save this Grasshopper file to your solution folder.
See the image below for an example Grasshopper definition.
3. Configure a model
In your solution.yaml file, configure a Model for your Grasshopper file generation definition.
- Add a block with kind:Model
- Define the
modelFilewith the name of your Grasshopper file.
4. Subscribe the model to control panel
To get the inputs from a control panel:
- Subscribe the
ModeltoControlPanel. - Define the
frameandsource. - Set the
actiontoset.
The configuration should look similar to the example below:
my-file-generation-model:
kind: Model
modelFile: my-model.gh
subscribe:
- kind: ControlPanel
frame: home-frame
source: my-control-panel
action: set
5. Subscribe the model to download button
To run the file generation model only when the DownloadButton is clicked:
- Subscribe the
Modelto theDownloadButton. - Define the
frameandsource. - Set the trigger input with the
SetInputproperty. This input is only used for triggering the model execution and does not need to be connected to the other components. - Set the
actiontoupdate.
The configuration should look similar to the example below:
my-file-generation-model:
kind: Model
modelFile: my-model.gh
subscribe:
- kind: ControlPanel
frame: home-frame
source: my-control-panel
action: set
- kind: DownloadButton
frame: home-frame
source: my-download-button
setInput: my-filler-input
action: update
6. Subscribe download button to model
To define which asset to download in the solution:
- Subscribe the
DownloadButtonto theModel. - Define the
frameandsource. - Set the
outputto the name of theDataOutputcomponent which outputs the asset in the model. - Set the
actiontosetButtonSettings.
The configuration should look similar to the example below:
my-download-button:
kind: DownloadButton
initialize:
text: Generate file
color: primary
download:
text: Download file
color: success
subscribe:
- kind: Model
frame: home-frame
source: my-file-generation-model
output: my-asset-output
action: setButtonSettings
See the image below as an example.
Configuration
See the downloadButton configuration docs for more information.