# OpenJSCAD

OpenJSCAD (opens new window) is a JavaScript environment to define mesh based 2D and 3D shapes using Constructive solid geometry CSG (opens new window). This simple yet powerful method lets you define complex shapes by using simple primitives (cubes, spheres, cones, cylinders, extrusions, ...) and apply boolean operations on them to either unify them together or subtract them of each other.

# Input Parameters

OpenJSCAD models can have input variables, just like customizers themselves. The variables are defined by defining following optional function.

function getParameterDefinitions() {
  return [
    { name: 'length',    type: 'float', default: 50,  caption: "Length"    },
    { name: 'width',     type: 'float', default: 40,  caption: "Width"     },
    { name: 'height',    type: 'float', default: 15,  caption: "Height"    },
    { name: 'radius',    type: 'float', default: 3,   caption: "Radius"    },
    { name: 'thickness', type: 'float', default: 1.5, caption: "Thickness" },
    ];
}

# main function

The main function is the entry point of a OpenJSCAD script and is always required, it returns the final shape. The first argument of the main function is an object containing the variables defined the parameter section. The example above would look like this.

parameters = {
  length: 50,
  width: 40,
  height: 15,
  radius: 3,
  thickness: 1.5
}

It is possible to create helper functions that can be called from the main function to simplify development and to repeat your self.


function box_helper(side_length) {
  return box(side_length) // built-in OpenJSCAD function
}

function main(parameters) {
  var final_object = box_helper(parameters.length);
  return final_object;
}


# OpenJSCAD example file

A full OpenJSCAD script could look like this.


// title: Base for {{name}}
// author: {{author}}

// define paramaters with this function
function getParameterDefinitions() {
  return [
    { name: 'length',    type: 'float', default: 50,  caption: "Length"    },
    { name: 'width',     type: 'float', default: 40,  caption: "Width"     },
    { name: 'height',    type: 'float', default: 15,  caption: "Height"    },
    { name: 'radius',    type: 'float', default: 3,   caption: "Radius"    },
    { name: 'thickness', type: 'float', default: 1.5, caption: "Thickness" },
    ];
}

// helper function to create box with round corners
function planeRoundBox(length, width, height, radius)
{
    var path = CAG.roundedRectangle({center: [0,0], radius: [length/2, width/2], roundradius: radius, resolution: 16 });
  	return linear_extrude({ height: height }, path);
}

// this is the entry point of the object definition
function main(parameters) {
	var outer_box = planeRoundBox(parameters.length, parameters.width, parameters.height, parameters.radius);
	var inner_box = planeRoundBox(parameters.length-parameters.thickness*2,
		parameters.width - parameters.thickness * 2,
		parameters.height - parameters.thickness * 2,
		parameters.radius - parameters.thickness).translate([ 0, 0, parameters.thickness ]);

	// boolean operation: subtract the inner rounded box from the outer rounded box to create a shell
	var object = difference(outer_box, inner_box);

	var rim = planeRoundBox(parameters.length-parameters.thickness,
			parameters.width - parameters.thickness,
			parameters.thickness,
			parameters.radius - parameters.thickness / 2).translate([ 0, 0, parameters.height - parameters.thickness ]);

	// boolean operation to create an inner rim
	object = difference(object, rim);
	return object;
}