Skip to content

Small wrapper for converting OpenSCAD .scad files to OpenJsCad .jscad files, openjscad (CLI) .jscad to .stl on command-line.

Notifications You must be signed in to change notification settings

Spiritdude/OpenSCAD.jscad

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenSCAD.js(cad) & openjscad (CLI)

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.

History

  • 2013/03/04: 0.005: intersect() -> intersection(), sin, cos, asin, acos included, more examples
  • 2013/03/02: 0.004: better install, examples/, etc refinements (working on 2d primitives)
  • 2013/03/01: 0.003: example.jscad vs example.scad, openscad.js/.jscad split up, and openjscad cli in nodejs implemented
  • 2013/02/28: 0.002: center:false default
  • 2013/02/27: 0.001: first version, center: true|false support

Requirements

Installation

% make install

What Works

  • openjscad CLI (command-line interface) .jscad to .stl (like "openscad test.scad -o test.st")
  • 3d primitives: sphere(), cube(), cylinder()
  • 3d transformations: translate(), rotate(), scale()
  • CSG operations: union(), difference(), intersect()

  • what does not (yet) work:
    • cylinder(fs=angle)
    • linear_extrude()
    • rotate_extrude()
    • 2d primitives

Purpose

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

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/20360,0]) translate([i,0,0]) rotate([0,i/2090,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:

  • .scad: translate([0,0,2]) sphere(size=2,$fn=50);
  • .jscad: translate([0,0,2], sphere({size:2,fn:50}));

Also:

   cube();                  // 1x1x1
   cube(2);                 // 2x2x2
   cube([1,2,3]);           // 1x2x3
   cube({size: [1,2,3]});   // dito
   cube({size:1, center: true});

Example

Go to http://joostn.github.com/OpenJsCad/processfile.html and paste openscad.jscad in there.

openjscad (CLI)

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).

Files

  • openjscad: command-line nodejs script (renders .jscad to .stl)
  • openscad.js: openscad wrapper function in javascript (in conjunction with csg.js)
  • openscad.jscad: same as above with a working example (main() defined)
  • csg.js: the core of OpenJsCad
  • examples/:
    • example000.jscad: working example (without openscad.js library)
    • example000.scad: original openscad .scad file
    • example001.jscad: converted from example001.scad (from OpenSCAD/examples/)
    • example001.scad: original openscad .scad file
    • example002.jscad: converted from example002.scad (from OpenSCAD/examples/)
    • example002.scad: original openscad .scad file
    • etc.

.JS vs .JSCAD

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).

See Also

  • OpenSCAD.net which provides a .scad to .js translator direct.
  • CoffeeSCad which provides CoffeeScript CAD development like OpenJsCad.
  • RepRapCloud, distribution of tasks (Open[J]SCAD, Slic3r etc) on several servers

About

Small wrapper for converting OpenSCAD .scad files to OpenJsCad .jscad files, openjscad (CLI) .jscad to .stl on command-line.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages