RubyScad
Description
Important
To get the latest code please either download from github or better yet use the rubygem.
Github: github.com/cjbissonnette/RubyScad
RubyGem (thanks knewter):
rubygems.org/gems/rubyscad
A ruby module that can easily be used to create openscad scripts. Think of it has haml for html or jade for javascript. When used, ruby code is translated into the openscad language which can be used like any other openscad script. I love openscad, but always long for modern language features (classes, iterators, lambda functions). I know efforts are being made to make javascript driven scripts, but until these are more mature, at least for me, this will fill the gap.
Benefits:
Use the full power of ruby / rubygems to create more powerful objects. Classes can be made that are subclassed to add functionality, mixin modules can be made to expand the language, and command line arguments can be used to make scripts customizable from the command line. I think having a full OOD language can make for some interesting objects.
Goals:
-Use ruby syntax to create compatible openscad scripts...done
-Include all the functionality of openscad...done except dxf_dim and dxf_cross (if somebody knows how to extract these pieces of information using ruby please let me know)
-be able to continue to use existing openscad libraries/ arbitrary existing openscad code...done
-lightweight...done (~300 lines, 1 file)
-match openscad syntax as much as possible for easy transition... done (very few exceptions, see instructions)
To get the latest code please either download from github or better yet use the rubygem.
Github: github.com/cjbissonnette/RubyScad
RubyGem (thanks knewter):
rubygems.org/gems/rubyscad
A ruby module that can easily be used to create openscad scripts. Think of it has haml for html or jade for javascript. When used, ruby code is translated into the openscad language which can be used like any other openscad script. I love openscad, but always long for modern language features (classes, iterators, lambda functions). I know efforts are being made to make javascript driven scripts, but until these are more mature, at least for me, this will fill the gap.
Benefits:
Use the full power of ruby / rubygems to create more powerful objects. Classes can be made that are subclassed to add functionality, mixin modules can be made to expand the language, and command line arguments can be used to make scripts customizable from the command line. I think having a full OOD language can make for some interesting objects.
Goals:
-Use ruby syntax to create compatible openscad scripts...done
-Include all the functionality of openscad...done except dxf_dim and dxf_cross (if somebody knows how to extract these pieces of information using ruby please let me know)
-be able to continue to use existing openscad libraries/ arbitrary existing openscad code...done
-lightweight...done (~300 lines, 1 file)
-match openscad syntax as much as possible for easy transition... done (very few exceptions, see instructions)
Instructions
Update:
I added a quick language def which outlines all the functions, and a new example "knob.rb" which is another example to use.
I have converted the 22 examples included with openscad into RubyScad scripts. These use just about every command in the openscad language and will detail the syntax. I will highlight some of the features below / some of the common pitfalls when migrating.
Basic Use:
Install Ruby 1.9.3 > (in ubuntu repos, easy windows and mac auto install): ruby-lang.org/en/
Install RubyGems: rubygems.org/
Run: gem install rubyscad
Super Simple:
in a blank ruby document i.e. test.rb:
then simple run this from command line:
ruby test.rb
this will print the output to the command line which can be piped to a file OR
run the file with a string as the first argument::
ruby test.rb "output.scad"
which will create "output.scad" file in the same directory
Using A Module
Using A Class
To use an existing library:
To output any string directly to openscad use:
format_output("string")
Common Pitfalls:
-When using a function it is general named the same as the openscad function i.e. cube, sphere... with the exception of the include function which is "scad_include" so as not to interfere with ruby’s function of the same name
-Functions always accept a hash for their arguments so all calls will look like: cube(size: 6, center:true) which means the following is ILLEGAL: translate([1,2,3]) and must be converted to translate(v:[1,2,3] or optionally translate(x:1,y:2,z:3). Exceptions to this are "echo" which takes any number of arguments, and create an echo statement in openscad i.e. echo 4, 5, "test". "use", and "scad_include" take one string i.e. use "write.scad".
-To set global '$' variables use the associated function "$fa = 4" becomes "fa 4", "$fs = 6" becomes "fs 6", and "$fn = 12" becomes "fn 12"
That’s the high level overview, look at the examples for more details, or leave a comment if I need to clarify something.
I added a quick language def which outlines all the functions, and a new example "knob.rb" which is another example to use.
I have converted the 22 examples included with openscad into RubyScad scripts. These use just about every command in the openscad language and will detail the syntax. I will highlight some of the features below / some of the common pitfalls when migrating.
Basic Use:
Install Ruby 1.9.3 > (in ubuntu repos, easy windows and mac auto install): ruby-lang.org/en/
Install RubyGems: rubygems.org/
Run: gem install rubyscad
Super Simple:
in a blank ruby document i.e. test.rb:
require "rubyscad"
include RubyScad
translate(x: 5)
cube(size: 6)
then simple run this from command line:
ruby test.rb
this will print the output to the command line which can be piped to a file OR
run the file with a string as the first argument::
ruby test.rb "output.scad"
which will create "output.scad" file in the same directory
Using A Module
require "rubyscad"
module Test
extend RubyScad
translate(v:[1,2,3]) {
union {
sphere(r: 3, center:true)
cube(size:3, center:true)
}
}
end
Using A Class
require "rubyscad"
class TestClass
include RubyScad
def initialize(args={})
@size = args.fetch(:size, 1)
end
def render()
cube(size: @size)
end
end
TestClass.new.render
To use an existing library:
require "rubyscad"
module WriteTest
extend RubyScad
use "Write.scad"
def self.writecylinder(args={})
format_command "writecylinder(%s);", args
end
writecylinder(text: "test", where: [0,0,0])
end
To output any string directly to openscad use:
format_output("string")
Common Pitfalls:
-When using a function it is general named the same as the openscad function i.e. cube, sphere... with the exception of the include function which is "scad_include" so as not to interfere with ruby’s function of the same name
-Functions always accept a hash for their arguments so all calls will look like: cube(size: 6, center:true) which means the following is ILLEGAL: translate([1,2,3]) and must be converted to translate(v:[1,2,3] or optionally translate(x:1,y:2,z:3). Exceptions to this are "echo" which takes any number of arguments, and create an echo statement in openscad i.e. echo 4, 5, "test". "use", and "scad_include" take one string i.e. use "write.scad".
-To set global '$' variables use the associated function "$fa = 4" becomes "fa 4", "$fs = 6" becomes "fs 6", and "$fn = 12" becomes "fn 12"
That’s the high level overview, look at the examples for more details, or leave a comment if I need to clarify something.
You must be logged in to post a comment.
laird
on
January 18, 2013
said:
Seriously, seriously cool. I love the _idea_ of OpenSCAD, but would rather use a real language. This looks perfect.
gooberdlx
on
January 18, 2013
said:
make a gem out of it? (p.s. I can help with that)
cjbissonnette
on
January 18, 2013
said:
I have never made a gem and would greatly appreciate the help. I was going to see if anybody else found this useful before putting in to much work. I think one of the benefits to using ruby is people could make libraries as gems so they are more accessible than the current system (or lack there of).
License

Your instructions could use: 'Some HTML is allowed (<, , <ol>, etc', for syntax highlighting.</ol>
did some clean up work, hope its a bit more clear now.