Version 0.005 (ALPHA)
Some wrapper functions to ease the translation from OpenSCAD (.scad) to OpenJsCad (.jscad): OpenSCAD.js(cad) (openscad.js / openscad.jscad).
-- UPDATE --: Consider to get OpenJSCAD.org right away instead, which also includes this library.
OpenJsCad CLI (openjscad) written on nodejs (server-side javascript) converting .jscad to .stl.
% make install
OpenJsCad is object oriented, and usually this imposes more verbosity of the source-code, whereas OpenSCAD has a simple syntax many developers are familiar with already, but unfortunately OpenJsCad introduced non-intuitive equivalents (essentially one has to memorize a new set of arguments), therefore a few brief wrapping functions (openscad.jscad) provide a much easier translation of existing .scad to .jscad files:
example.scad
union() { //cube(size=[30,30,0.1],center=true); translate([3,0,0]) cube(); difference() { rotate([0,-45,0]) cube(size=[8,7,3],center=true); sphere(r=3,$fn=20,center=true); } translate([10,5,5]) scale([0.5,1,2]) sphere(r=5,$fn=50); translate([-15,0,0]) cylinder(r1=2,r2=0,h=10,$fn=20); for(i=[0:19]) { rotate([0,i/20*360,0]) translate([i,0,0]) rotate([0,i/20*90,i/20*90,0]) cube(size=[1,1.2,.5],center=true); } } |
example.jscad
function main() { var cubes = new Array(); for(i=0; i<20; i++) { cubes[i] = rotate([0,i/20*360,0], translate([i,0,0], rotate([0,i/20*90,i/20*90,0], cube({size:[1,1.2,.5],center:true})))); } return union( //cube({size:[30,30,0.1],center:true}), translate([3,0,0],cube()), difference( rotate([0,-45,0], cube({size:[8,7,3],center:true})), sphere({r:3,fn:20,center:true}) ), translate([10,5,5], scale([0.5,1,2], sphere({r:5,fn:50}))), translate([-15,0,0], cylinder({r1:2,r2:0,h:10,fn:20})), cubes ); } |
Essentially whenever named arguments in .scad appear func(a=1), translate it into func({a:1}), for example:
Also:
cube(); // 1x1x1 cube(2); // 2x2x2 cube([1,2,3]); // 1x2x3 cube({size: [1,2,3]}); // dito cube({size:1, center: true});
Go to http://joostn.github.com/OpenJsCad/processfile.html and paste openscad.jscad in there.
openjscad is a nodejs script, which renders .jscad to .stl on command-line level:
% ./openjscad example.jscad % ./openjscad example.jscad -o test.stl
by default creates filename.('jscad' → 'stl'), optionally -o filename.stl can be defined (alike with openscad).
Important: by default openjscad (CLI) supports the OpenSCAD.jscad extension (unlike the web-platform).
In general .js and .jscad are both written in JavaScript, yet .jscad must have a function main():
function main() { var csg = union( ... ); return csg; }
returning an object of CSG (from csg.js).