Autodesk Creative Platform Core Version 1.19.0
A broad and deep collection of 2D and 3D capabilities.

FileParameterJSON Overview

Inherits Classes: ParameterJSON

This is the definition of a file input parameter. This is an asychronous-only parameter which means that the Parameter Processing has to use the Asynchronous approach for this to work correctly. The parameter should return a value of type File

5 Examples:

//An example of defining the file parameter synchronously.
params = [
    {
        "id": "image",
        "displayName": "Image",
        "type": "file",
        "accept": "image/*",
        "maxSize": 1048576
    }
];
//To fetch a mask from a local resource, the asynchronous method of defining the parameters is required.
//This example also shows how a mask can be applied on a file parameter supporting images
function shapeGeneratorDefaults(callback) {
    Library.resourceText('Mask.svg', function(resourceData) {
        var sketch2D = Conversions.toSketch2DFromSVG(resourceData);
        var params = [
            {
                "id": "image",
                "displayName": "Image",
                "type": "file",
                "accept": "image/*",
                "maxSize": 1048576,
                "config" : {
                    "mask" : sketch2D
                }
            }
        ];
        callback(params);
      });
}
// The File object returned by the file Parameter can be used to
// retrieve the contents of the file as text in this manner
function shapeGeneratorEvaluate(params, callback) {

    var file = params['file'];
    if(!file) { //Always check if a file has been selected before attempting operations
        //Create the mesh when a file is not selected.
        //A good option would be to use a default file as a placeholder prompting ther user to select a file
        file = Library.resourceFile('default.txt')
    }

    file.readAsText(function (text) {
        Debug.log('The file contents are - ' + text);
        //Create mesh
        var mesh = new Mesh3D();
        var solid = Solid.make(mesh);
        callback(solid);
    });
}
// The contents can also be retrieved as a DataView
function shapeGeneratorEvaluate(params, callback) {

    var file = params['file'];
    if(!file) { //Always check if a file has been selected before attempting operations
        //Create the mesh when a file is not selected.
        //A good option would be to use a default file as a placeholder prompting ther user to select a file
        file = Library.resourceFile('default.png')
    }

    file.readAsDataView(function (dataview) {
        //Dataview operations
        //....
        //Create mesh
        var mesh = new Mesh3D();
        var solid = Solid.make(mesh);
        callback(solid);
    });
}
// If the ShapeGenerator has the Canvas dependency, the Image class can also be leveraged to acess the file contents as an Image.
function shapeGeneratorEvaluate(params, callback) {

    var file = params['image'];
    if(!file) { //Always check if a file has been selected before attempting operations
        //Create the mesh when a file is not selected.
        //A good option would be to use a default file as a placeholder prompting ther user to select a file
        file = Library.resourceFile('default.png')
    }

    var image = Canvas.Image.loadFromFile(file, function (imagedata) {
        //Image data
        //Create mesh
        var mesh = new Mesh3D();
        var solid = Solid.make(mesh);
        callback(solid);
    });
}

JSON Field Summary

  • property
      accept | Required
  • property
      config | Optional
  • property
      maxSize | Optional
  • property
      displayIcon | Optional
  • property
      displayName | Required
  • property
      id | Required
  • property
      invalid | Optional , Default: "" , Coming Soon!
  • property
      type | Required
  • property
      validate | Optional , Default: false , Coming Soon!
  • property
      visible | Optional , Default: true , Coming Soon!

JSON Field Details

String accept | Required

Allows the client to restrict the types of files that can be supplied by the user. The value must conform to rules defined by https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#attr-accept.

FileParameterConfigurationJSON config | Optional

Allows the client to specify additionl configuration basing on the "accept" type. For example, for "image/*" type a Sketch2D object is set as the "mask"

Number maxSize | Optional

Allows the client to configure the maximum file size allowed (inclusive) in bytes. For example, for a maximum size of 1MB this property would be configured to 1,048,576.