How to download files

A how-to guide for downloading text based files from your solution.

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 initialize state, define the text and color.
  • For the download state, define the text and color.

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 Input component.
  • Define a trigger input with the Packhunt Input component. 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 file component.
  • Create an asset with your file.
    • Place the Create asset component on the canvas.
    • Define the File with the output of the Create file component
    • Define the Path with the file name to download and the file extension.
    • Define the project slug. This is your solution name.
    • Set isprivate to true.
    • Set randomize to true. This creates a folder with a unique name for your file.
  • Output the asset.
    • Connect the output of the Create asset component to a Packhunt DataOutput component.
    • Give a unique name to the DataOutput component.
  • 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 modelFile with the name of your Grasshopper file.

4. Subscribe the model to control panel

To get the inputs from a control panel:

  • Subscribe the Model to ControlPanel.
  • Define the frame and source.
  • Set the action to set.

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 Model to the DownloadButton.
  • Define the frame and source.
  • Set the trigger input with the SetInput property. This input is only used for triggering the model execution and does not need to be connected to the other components.
  • Set the action to update.

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 DownloadButton to the Model.
  • Define the frame and source.
  • Set the output to the name of the DataOutput component which outputs the asset in the model.
  • Set the action to setButtonSettings.

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.