by cjbissonnette, published

RubyScad by cjbissonnette Jan 18, 2013



RubyScad by cjbissonnette is licensed under the Creative Commons - Attribution license.

Liked By

View All

Give a Shout Out

If you print this Thing and display it in public proudly give attribution by printing and displaying this tag.

Print Thing Tag

Thing Statistics

2360Views 1042Downloads


To get the latest code please either download from github or better yet use the rubygem.

Github: https://github.com/cjbissonnette/RubyScad
RubyGem (thanks knewter):

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.

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.

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


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): http://www.ruby-lang.org/en/
Install RubyGems: https://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)

Using A Class

require "rubyscad"
class TestClass
  include RubyScad
  def initialize(args={})
     @size = args.fetch(:size, 1)
   def render()
      cube(size: @size)

To use an existing library:

require "rubyscad"
module WriteTest
  extend RubyScad
  use "Write.scad"
  def self.writecylinder(args={})
     format_command "writecylinder(%s);", args
  writecylinder(text: "test", where: [0,0,0])

To output any string directly to openscad use:

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.

All Apps

Upgrade this Thing with Thingiverse Apps

Tools and Utilities

Repair, slice, or enhance this Thing

Auto-magically prepare your 3D models for 3D printing. A cloud based 3D models Preparing and Healing solution for 3D Printing, MakePrintable provides features for model repairing, wall thickness...

App Info Launch App

I've been recently working on a more in depth version of this idea called Oozby. http://creativepony.com/oozby/http://creativepony.com/oozby/ little homepage, and on github at http://github.com/bluebie/oozby/http://github.com/bluebie/oozb... - More than just using ruby grammar, my goal with oozby is to use ruby as a preprocessor to add more features like rounded cubes, squares, cylinders (like border-radius in css!), be able to make classes which generate objects, have the language detect bugs and give you much more useful error messages than OpenSCAD, and have it much more tollerant of typos and try to understand programmer's intent, so you don't need to keep a reference open at all times to look up exact argument names. Everything that takes a height can take it as h: or height: and everything which takes a radius can take it as radius: or r: or you can specify it as diameter: or d: and have it automatically halved. You can specify radius/diameter as a Range to cylinder instead of r1 and r2 if you like, or create a scope where everything inside defaults to center: true instead of center: false. Lots of neat stuff. If you play with it at all I'd love some feedback ^_^

Oozby is very much a work in progress but I'm using it for modelling almost every day now and so I'm personally pretty invested in not breaking my own code. I think stuff built on oozby today will almost certainly work fine in future versions. :)

I am definitely going to check this out. When I first started this project it was my intent to add a lot of the improvements that you are referring to. I added some small things like being able to supply diameter as a parameter to cylinders and circles, but never found the time to add more features. I'm excited you are taking this idea further, I think being able to harness the power of ruby would push openscad's usefulness greatly.

Cool! All the features I listed are already operational. Make sure you have ruby 2.0 installed and gem install oozby to have a play with it. There's a little 'oozby' command line app which you run as 'oozby compile some_directory' and it'll watch for file changes and make corrosponding .scad files from .oozby files in there, and report any errors in that terminal window. Lots of demos in the examples folder on github to play with. I'm working on making some pretty documentation which will hopefully be online in the next few weeks :)

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.

Seriously, seriously cool. I love the idea of OpenSCAD, but would rather use a real language. This looks perfect.

make a gem out of it? (p.s. I can help with that)

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

I made a gem for you, sent a pull request. Let me know what you think. Thanks for this, I just got into OpenScad tonight and I'm a rubyist, hope to make good use of this lib.

Just pushed this to http://RubyGems.orgRubyGems.org, thank again for your help

You are awesome thank you so much for this

Also, is this on github?

I am already using git to control this, so I can easily make a github page. I'll try to do this in the near future

This is the best thing EVER