Share
 
 

JavaScript Imports

Buildstyle projects may consist of several thousand lines of JavaScript source code. Putting all code lines into one file would make it complicated to navigate and work with it. That’s why you can split up the code into several files and create links by importing source code from one file into another source code file. These imports are called script includes and you’ll find them all across the buildstyle project.

You can also include predefined source code from the script core of the Advanced Toolpath Utility. You won’t find any JavaScript files for these includes, as they are part of the container application. These imports are called internal datatype includes.

Have a look at the following example. It includes several core functionalities and JavaScript code from a printer settings file:

/** INTERNAL DATATYPE INCLUDES */
var PARAM = requireBuiltin('bsParam');
var HATCH_INFO = requireBuiltin('bsHatchInfo');
var HATCH = requireBuiltin('bsHatch');
var ISLAND = requireBuiltin('bsIsland');
var POLY_ITERATOR = requireBuiltin('bsPolylineIterator');
var VEC2 = requireBuiltin('vec2');
/** SCRIPT INCLUDES */
var PRINTSETTINGS = require('../declaration/printsettings.js');

For importing JavaScript functionality you’ll have to declare a variable at your script (see PARAM, HATCH_INFO, ISLAND above as examples). Then you call require (when importing from another JavaScript file) or requireBuiltin (when importing core functionality) to get the requested code, which will be stored in your local variable.

Now let’s have a look at the other side. The printsettings JavaScript file might contain this:

'use strict';
/** PRINT SETTINGS */
exports.Settings = {
  JobName : '' /** The name of the print job displayed on the front panel. Set
 ˓→when print data is loaded and cleared when print data is cleared. */
}
/** get printsettings string for given material
 * @param material material name
 * @param thickness layer thickness
 * @param job_name Job name (will be added to string)
 */
exports.getPrintSettings = function()
{
  return exports.Settings;
}

The import procedure requires files to have an object called exports for every “library” file. This object will be imported by files, using the “library” function. The example above defines a JavaScript property at the exports object called Settings. It also defines a JavaScript function called getPrintSettings. This function returns the Settings object.

Importing this JavaScript file returns an object on which you can call the function getPrintSettings or access the Settings object.

var PRINTSETTINGS = require('../declaration/printsettings.js');
var myPrintSettings = PRINTSETTINGS.getPrintSettings();

Was this information helpful?