farrellm/scad-clj — pull request #14 by EricGebhart (2014-06-04 05:19:13+00:00)
Title: Added include and call functions to enable the use of non-clojure scad libraries.

This seems to work pretty well. This adds 'include' and 'call' functions to call into scad library functions. Call seems like a good name, but maybe there is something better.

Include was easy. it looks like this.

(defn includes []
[(include "MCAD/nuts_and_bolts.scad")
(include "MCAD/motors.scad")])

(spit "test.scad" (write-scad (includes))

turns into this.

include
include

Call got complicated because of the arguments but seems to be working well. The arguments are specified as a vector. I used keywords to specify the function name and the named arguments.
It's all recursive and handles everything I've ever come across in my limited scad experience.
The recursion causes the named attributes to get mixed up, I guess it could be reversed, but it shouldn't really matter.

(def showmotor true)
(def hole-tolerance 0.01)
(def nut-size 3)
(def x 10)
(def y 20)
(def z 20)

(call :nutHole [{:size nut-size}])
(call :foo [[x y 15] 17 hole-tolerance false true nut-size
{:size nut-size, :showmotor showmotor, :foo hole-tolerance}])
(call :bar [[x y z] nut-size {:square [1 0 1 0] :showmotor showmotor}])

Turns into this:

nutHole(size=3);
foo([10, 20, 15], 17, 0.01, false, true, 3, showmotor=true, foo=0.01, size=3);
bar([10, 20, 20], 3, square=[1, 0, 1, 0], showmotor=true);

My entire example looks like this

(def showmotor true)
(def hole-tolerance 0.01)
(def nut-size 3)
(def x 10)
(def y 20)
(def z 20)

(def primitives
(union
(call :nutHole [{:size nut-size}])
(call :foo [[10 20 15] 17 hole-tolerance false true nut-size {:size 3, :showmotor true, :foo 0.01}])
(call :bar [[x y z] nut-size {:square [1 0 1 0] :showmotor showmotor}])
(translate [50 0 0]
(cube 100 100 100)
(sphere 110)
(cylinder 10 150))))

(defn motor-plate []
(call :stepper_motor_mount [17 hole_tolerance showmotor] ))

(defn includes []
[(include "MCAD/nuts_and_bolts.scad")
(include "MCAD/motors.scad")])

(spit "test.scad"
(write-scad
(includes)
(motor-plate)
primitives))

And it generated this:

include
include
stepper_motor_mount(17, 0.01, true);
union () {
nutHole(size=3);
foo([10, 20, 15], 17, 0.01, false, true, 3, showmotor=true, foo=0.01, size=3);
bar([10, 20, 20], 3, square=[1, 0, 1, 0], showmotor=true);
translate ([50, 0, 0]) {
cube ([100, 100, 100], center=true);
sphere (r=110, center=true);
cylinder (h=150, r=10, center=true);
}
}