Passing children in recursive module

3 messages
Open this post in threaded view
|

Passing children in recursive module

 I've got some geometry that I'm wanting to slice into 8+ parts on various planes, and to add fixing holes to, and I'm wanting to make life easier than loads of nested difference() and intersection() operations. I'm using a "BinOps" module that takes an argument saying which binary to use, and then wanted to wrap this in another module that lets me specify multiple operations to apply to variable numbers of children. I first coded non-recursive with fixed depth (but null() module to let me still use it) and then struggled to code recursively as children() always seem to be unioned. Is there a better work around? module sphere1 () sphere (r=5); module slicer () translate ([-10,-10,-10]) cube ([10, 20, 20]); module sliceu () translate ([-10,-10,-10]) cube ([20, 20, 10]); module null(); // Applies operator to children(0) and children(1:last) // n=nop, d=difference, u=union, i=intersection module BinOp (Op) {          if (Op == "n") children(0);      else    if (Op == "d") difference () {children(0); children([1:\$children-1]);}      else    if (Op == "u") union () {children(0); children([1:\$children-1]);}      else    if (Op == "i") intersection () {children(0); children([1:\$children-1]);} } // Make this recursive! // Applies children(1) to children(0) first using first operator, and then works along module MultiOpOld (OpStr) {      BinOp (OpStr[3]) {          BinOp (OpStr[2]) {              BinOp (OpStr[1]) {                  BinOp (OpStr[0]) {                      children(0);                      children(1);                  }                  children(2);              }              children(3);          }          children(4);      } } // Apply all operations to children(0) with one operation per child after children(1) module MultiOp (OpStr, opnum = undef) {      opnum = opnum==undef ? len(OpStr) : opnum;      BinOp (OpStr[opnum-1]) {              if (opnum==1)                  children(0);              else                  MultiOp (OpStr, opnum=opnum-1)  {                      // Would like to pass down all children except last one, but ...                      children(0);                      children(1);                      if (\$children>3) children(2);                      if (\$children>4) children(3);                      if (\$children>5) children(4);                  }              children(opnum);          } } // Change the control string to slice in different ways. Will have 4-5 slice planes eventually. MultiOp ("ddnn") {      sphere1();      slicer();      sliceu();      null();      null(); } _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org