bjbsquared/SolidPy — pull request #2 by hg42 (2014-02-01 23:11:04+00:00)
Title: Functional style

changes to allow functional programming style.

example:

l = 100.0
r = 10.0
br = 30.0

rod = Cylinder(r=r, h=l)
ball = Sphere(r=br)

parts = (
rod.rotate(90, 0, 0).color([1, 0, 0])
+ rod.rotate( 0, 90, 0).color([0, 1, 0])
+ rod.rotate( 0, 0, 90).color([0, 0, 1])
+ ball.scale(r/br)
+ ~ball.scale(0.5).translate(l, 0, 0)
+ -ball.scale(0.5).translate(0, l, 0)
+ +ball.scale(0.5).translate(0, 0, l)
)

To make this possible, each modifying method has to return a modified copy of itself.
This way an transformation doesn't change the receiving object and returns the transformed copy of the object instead. Otherwise variables would be changed by transformations.

As an alternative you could also use functions instead of variables to define a part, but the variables look more natural to me.
This style would look like this:
rod = lambda: Cylinder(r=r, h=l)
ball = lambda: Sphere(r=br)
...
+ ball().scale(r/br)

Instead I would like to reserve functions for combined operations or complex or parametric constructions.

Additionally I added unary operators for 'disable' = '-' = '*', 'background' = '+' = '%' and 'debug' = '~' = '#'.
Unfortunately there are only three (single character) unary operators in python, while openscad having four modifiers, so 'root' has no corresponding unary operator, but I think it's already cleaner to use variables for this.

Btw. for functional style it would be more convenient to use methods instead of assignments (e.g. obj.debug() instead of obj.debug = True, however everyone will use the unary operators, anyway.