chiark / gitweb /
sewing-table: Leg: introduce tubules
[reprap-play.git] / sewing-table.scad.m4
1 // -*- C -*-
2
3 include <funcs.scad>
4 include <commitid.scad>
5
6 ply_th = 18;
7 ply_hole_dia = 15;
8 ply_edge_min = 10;
9
10 ply_hole_dia_real = 12;
11
12 tile_th = 3;
13 post_dia = 8;
14
15 post_shorter = 1;
16
17 screw_dia = 2.2;
18 screw_big_dia = 3.6;
19 screw_big_len = 4.0;
20
21 round_edge_rad = 2.0;
22
23 round_cnr_rad = 10;
24
25 interlock_dia = 10;
26 interlock_fine = 0.66;
27
28 interlock_fine_slope = 1.0;
29 interlock_fine_lenslop = 1.0;
30
31 demo_slop = 0.1;
32
33 leg_height = 50;
34
35 leg_hole_dia = 5 + 0.75;
36 leg_big_dia = 37;
37 leg_bot_dia = 15;
38 leg_top_flat_z = 5;
39
40 leg_fin_top_w = 5;
41 leg_fin_bot_w = 5;
42 leg_fin_bot_rad = 30;
43 leg_fin_bot_flat_z = 5;
44
45 leg_n_fins = 4;
46 leg_n_tubules = 4;
47 leg_tubule_dia = 4;
48
49 // cutout
50
51 machine_rear_to_front = 84 + 0.25 - 1.4;
52
53 cutout_l_end_y_front_slop = 0.5;
54 cutout_l_end_y_rear_slop = 0.5;
55
56 cutout_l_end_x = 22;
57 cutout_l_end_y = machine_rear_to_front;
58 cutout_l_end_new_x_slop = 1.4 - 1.95;
59 cutout_l_end_y_total = cutout_l_end_y
60   + cutout_l_end_y_front_slop + cutout_l_end_y_rear_slop;
61
62 tile02_tr = [-250, 0];
63 tile01_tr = [  0, 0];
64
65 cutout_tile01_y = 170 - 147 + cutout_l_end_y_front_slop;
66 cutout_tile11_y = cutout_l_end_y_total - cutout_tile01_y;
67
68 // front and rear curves
69
70 rearedge_len = 170 + 0.70;
71 frontedge_len = 170;
72
73 rearcurve_strt_len = 52;
74
75 rearcurve_z_slop = -0.50;
76
77 rearcurve_avoid_y = 35;
78
79 rearcurve_double_inrad = 26.10 + 8.04;
80
81 reartablet_z = 2.54;
82 reartablet_x = 5 + 1;
83 reartablet_y = 8;
84
85 frontcurve_side_skew = 3.5 / 72;
86 frontcurve_avoid_y = 70;
87 frontcurve_z_slop = 0.75;
88
89 frontcurve_strt_len = 50;
90 frontcurve_dualcurve_angle = 30;
91
92 teststrapslots_at = [ [ 110, 70 ], [ 110, -35 ],
93                       [ 180, 90 ],
94                       [ 190, -80 ], // do not change index of this one
95                       [   0, 70 ],  [  0, -35 ],
96                       ];
97
98 teststrap = [ 3, 5 ];
99 teststrap_peg = [7.5, 3.5];
100
101 ply_edge_hole_dist_real = 14;
102
103 // calculated
104
105 TEST = false;
106 JIG = false;
107
108 ply_edge_hole_dist = ply_edge_min + ply_hole_dia/2;
109
110 echo(str("HOLES IN PLY ctr dist from PLY edge = ", ply_edge_hole_dist));
111
112 hole_slop = (ply_hole_dia - post_dia)/2;
113 tile_hard_edge_hole_dist = ply_edge_hole_dist + hole_slop;
114
115 echo(str("HOLES IN PLY ctr dist from TILE HARD edge = ",
116          tile_hard_edge_hole_dist));
117
118 echo(str("HOLES IN PLY ctr dist from TILE ROUND edge = ",
119          tile_hard_edge_hole_dist + round_edge_rad));
120
121 thehd = [ tile_hard_edge_hole_dist, tile_hard_edge_hole_dist ];
122 thehd_tr = thehd;
123 thehd_tl = [ -thehd_tr[0], thehd_tr[1] ];
124 thehd_bl = -thehd_tr;
125 thehd_br = -thehd_tl;
126
127 tablet_z_slop = 1.00;
128
129 interlock_rad = interlock_dia/2;
130 interlock_negative_rad = interlock_rad + 0.125;
131
132 interlock_sq_adj = 0.2; // arbitrary
133
134 leg_fin_top_rad = sqrt( pow(leg_big_dia/2,2) -
135                         pow(leg_fin_top_w/2,2) );
136
137 leg_tubule_pos_rad = leg_big_dia/2 * 0.6;
138
139 m4_define(`POST_TCROSSSZ',
140           `2*( tile_hard_edge_hole_dist - test_edge + 1 )')
141
142 module Post(){
143   mirror([0,0,1]) {
144     if (!JIG) {
145       difference(){
146         cylinder(r= post_dia/2, h= tile_th + ply_th - post_shorter);
147         translate([0,0, tile_th]) {
148           cylinder(r= screw_big_dia/2, h= screw_big_len);
149           cylinder(r= screw_dia/2, h= ply_th, $fn=20);
150         }
151       }
152     }
153     if (TEST) {
154       translate([0,0, tile_th/2]) {
155         cube([post_dia,      POST_TCROSSSZ, tile_th], center=true);
156         cube([POST_TCROSSSZ, post_dia,      tile_th], center=true);
157       }
158     }
159     if (JIG) {
160       translate([0,0, tile_th/2]) {
161         cube([POST_TCROSSSZ, POST_TCROSSSZ, tile_th], center=true);
162       }
163     }
164   }
165 }
166
167 module PostHole(){
168   if (JIG) {
169     translate([0,0,-5])
170       cylinder(r= post_dia/2 + jig_post_hole_slop, h=10);
171     translate([0,0, -jig_min_th])
172       cylinder(r= ply_hole_dia_real/2, h = 5);
173     for (rot=[0:90:270]) rotate(rot) {
174         translate([ ply_edge_hole_dist_real, 0, 0 ])
175           cube([ jig_pencil_rad*2, jig_pencil_slotlen, 20 ], center=true);
176       }
177   }
178 }
179
180 module Posts(posts) {
181   for (p= posts) {
182     translate(concat(p, [0]))
183       Post();
184   }
185 }
186 module PostHoles(posts) {
187   for (p= posts)  {
188     translate(concat(p, [0]))
189       PostHole();
190   }
191 }
192
193 module TileBase(botleft, topright){
194   size = topright - botleft;
195   botleft_post = botleft + thehd_tr;
196   topright_post = topright + thehd_bl;
197   difference(){
198     mirror([0,0,1])
199       translate(concat(botleft, [0]))
200       cube(concat(size, [tile_th]));
201     if (!(TEST || JIG)) {
202       cidsz = topright_post-botleft_post
203         + [-post_dia,-post_dia]
204         + [0, thehd[1]];
205       cidszr = [ min(cidsz[0],50), min(cidsz[1],50) ];
206       echo("CID",cidsz,cidszr);
207       translate( concat(botleft_post, [ -tile_th ])
208                  + 0.5 * [ post_dia, post_dia, 0 ]
209                  + 0.5 * concat( cidsz - cidszr, [ 0 ]) )
210         Commitid_BestCount_M(cidszr);
211     }
212     if ((TEST || JIG)) {
213       crossoff = tile_hard_edge_hole_dist + POST_TCROSSSZ/2;
214       cidsz = [ thehd[0], size[1] - 2*crossoff ];
215       cidszr = [ cidsz[0], min(cidsz[1], 50) ];
216       if (TEST)
217         translate( concat(botleft + [0, crossoff] + (cidsz-cidszr)/2, [0]) )
218           Commitid_BestCount(cidszr);
219       difference(){
220         mirror([0,0,1]) {
221           translate(concat(botleft + [test_edge,test_edge], [test_tile_th]))
222             cube(concat(size - [test_edge,test_edge]*2, [tile_th*2]));
223           translate(concat(botleft_post, [-1]))
224             cube(concat(topright_post-botleft_post, [tile_th+2]));
225         }
226         shufflesz = max(test_edge, tile_hard_edge_hole_dist)*2;
227         minkowski(){
228           MachineEnvelope();
229           cube(shufflesz, center=true);
230         }
231         if (JIG) {
232           translate([0,0,-20]) linear_extrude(height=20) {
233             for (diag=[[ +1, botleft                   ],
234                        [ -1, [topright[0], botleft[1]] ]]) {
235               translate(diag[1])
236                 rotate(atan2(size[1], diag[0] * size[0]))
237                 translate([0, -test_edge/2])
238                 square([vectorlen2d(size), test_edge]);
239             }
240           }
241         }
242       }
243     }
244   }
245 }
246
247 m4_dnl   R_EDGE(c,ix)
248 m4_dnl        c is from Rectangle_corners and
249 m4_dnl        ix is a corner number
250 m4_dnl    expands to two comma-separated corners:
251 m4_dnl    that denoted by ix, and the next one anticlockwise
252 m4_define(`R_EDGE',`$1[$2], $1[(($2)+1)%4]')
253
254 m4_dnl   R_CNR(c,ix)
255 m4_dnl        c is from Rectangle_corners and
256 m4_dnl        ix is a corner number
257 m4_dnl    expands to an array of corners as for RoundCorner
258 m4_define(`R_CNR',`[ $1[$2], $1[(($2)+1)%4], $1[(($2)+3)%4] ]')
259
260 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars) { body; }
261 m4_define(`INREFFRAME',`
262   length_vec = ($2) - ($1);
263   length = dist2d([0,0], length_vec);
264   length_uvec = length_vec / length;
265   ortho_uvec = [ -length_uvec[1], length_uvec[0] ];
266   m = [ [ length_uvec[0],  ortho_uvec[0], 0, ($1)[0], ],
267         [ length_uvec[1],  ortho_uvec[1], 0, ($1)[1], ],
268         [ 0,              0,              1,            0, ],
269         [ 0,              0,              0,            1, ] ];
270   $3
271   multmatrix(m)
272 ')
273
274 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars)
275 m4_dnl    INREFFRAME_EDGE { body; }
276 m4_define(`INREFFRAME_EDGE',`
277   translate([0,0, -round_edge_rad])
278 ')
279
280 module RoundEdge(left_cnr, right_cnr) {
281   INREFFRAME(left_cnr, right_cnr)
282     INREFFRAME_EDGE {
283     difference(){
284       rotate([0,90,0])
285         cylinder(r= round_edge_rad, h= length, $fn=50);
286       translate([-1, 0, -20])
287         cube([length+2, 20, 20]);
288     }
289   }
290 }
291
292 m4_define(`ROUNDCORNER_VARS',`
293   this_cnr = ci[0];
294   right_cnr = ci[1];
295   left_cnr = ci[2];
296   bigr= round_cnr_rad - round_edge_rad;
297   l_uvec = unitvector2d(left_cnr - this_cnr);
298   r_uvec = unitvector2d(right_cnr - this_cnr);
299   lp1 = left_cnr  + clockwise2d(l_uvec) * bigr;
300   lp2 = this_cnr  + clockwise2d(l_uvec) * bigr;
301   lp3 = this_cnr  - clockwise2d(r_uvec) * bigr;
302   lp4 = right_cnr - clockwise2d(r_uvec) * bigr;
303   ctr = line_intersection_2d(lp1,lp2,lp3,lp4);
304   ctr3 = concat(ctr,[0])
305 ')
306
307 module RoundCorner_selector(ci, adj) {
308   ROUNDCORNER_VARS;
309   intersection(){
310     union(){
311       INREFFRAME(ctr3,concat(lp1,[4])){
312         translate([0,0,-bigr]) linear_extrude(height=bigr*2) {
313           translate([-bigr*2 + adj, -bigr])
314             square([bigr*2, bigr*3]);
315         }
316       }
317     }
318     union(){
319       INREFFRAME(ctr3,concat(lp4,[0])){
320         translate([0,0,-bigr]) linear_extrude(height=bigr*2) {
321           translate([-bigr*2, -bigr*2])
322             square([bigr*2 + adj, bigr*3]);
323         }
324       }
325     }
326   }
327 }
328
329 module RoundCornerCut(ci) {
330   // ci should be [this_cnr, right_cnr, left_cnr]
331   // where right_cnr is to the right (ie, anticlockwise)
332   ROUNDCORNER_VARS;
333   difference(){
334     RoundCorner_selector(ci, -0.1);
335     translate(ctr3)
336       cylinder(center=true, h=20, r= bigr);
337   }
338 }
339
340 module RoundCornerAdd(ci) {
341   ROUNDCORNER_VARS;
342   intersection(){
343     RoundCorner_selector(ci, +0.1);
344     INREFFRAME_EDGE {
345       translate(ctr3){
346         rotate_extrude(convexity=10, $fn=50)
347           translate([bigr, 0])
348           difference(){
349           circle(r= round_edge_rad, $fn=50);
350           mirror([1,1])
351             square([20,20]);
352         }
353       }
354     }
355   }
356 }
357
358 module InterlockLobePlan(negative) {
359   r = negative ? interlock_negative_rad : interlock_rad;
360   ymir = negative ? 0 : 1;
361
362   dx = sqrt(3) * r;
363   $fn= 80;
364   translate([thehd[0], 0]){
365     mirror([0,ymir]){
366       circle(r=r);
367       difference(){
368         translate([-dx, -0.1])
369           square([ dx*2, r/2 + 0.1 ]);
370         for (xi = [-1, 1]) {
371           translate([ xi*dx, r ])
372             circle(r=r);
373         }
374       }
375     }
376   }
377 }
378
379 module InterlockEdgePlan(negative, nlobes, length, dosquare=true) {
380   for (lobei = [ 0 : nlobes-1 ]) {
381     lobex = (length - thehd[0]*2) * (lobei ? lobei / (nlobes-1) : 0);
382     translate([lobex, 0, 0]) {
383       InterlockLobePlan(negative);
384     }
385   }
386
387   if (dosquare) {
388     iadj = 0;
389     slotshorter = negative ? -0.1 : interlock_fine_lenslop;
390     mirror([0, negative])
391       translate([slotshorter, iadj])
392       square([length - slotshorter*2, interlock_fine + iadj*2]);
393   }
394 }
395
396 module InterlockEdge(left_cnr, right_cnr, negative=0, nlobes=2) {
397   plusth = negative * 1.0;
398   protr = interlock_fine + interlock_sq_adj;
399
400   z2 = -tile_th/2;
401   z1 = -tile_th/2 - protr / interlock_fine_slope;
402   z3 = -tile_th/2 + protr / interlock_fine_slope;
403
404   negsign = negative ? -1 : +1;
405   yprotr = negsign * protr;
406
407   INREFFRAME(left_cnr, right_cnr) {
408     for (vsect = [ // zs0            zs1      ys0,            ys1
409                   [ -tile_th-plusth, plusth,  0,              0],
410                   [ z1,              z2,      0, yprotr],
411                   [ z2,              z3,      yprotr, 0],
412                   ]) {
413       zs0 = vsect[0];
414       zs1 = vsect[1];
415       zsd = zs1-zs0;
416       ys0 = vsect[2];
417       ys1 = vsect[3];
418       ysd = ys1-ys0;
419       sl = ysd/zsd;
420       m = [ [ 1,0,   0,    0 ],
421             [ 0,1, -sl, -ys0 + negsign*interlock_sq_adj ],
422             [ 0,0,   1,  zs0 ],
423             [ 0,0,   0,    1 ] ];
424       multmatrix(m)
425         linear_extrude(height=zsd, convexity=10)
426         InterlockEdgePlan(negative, nlobes, length, !!ysd);
427     }
428   }
429 }
430
431 function TestPiece_holes2corners(holes) =
432   [ holes[0] + thehd_bl,
433     holes[1] + thehd_br,
434     holes[1] + thehd_tr,
435     holes[0] + thehd_tl ];
436
437 module TestPiece1(){ ////toplevel
438   holes = [ [-100, 0],
439             [   0, 0]
440             ];
441   corners = TestPiece_holes2corners(holes);
442   rcs = R_CNR(corners,0);
443   difference(){
444     union(){
445       TileBase(corners[0], corners[2]);
446       Posts(holes);
447       RoundEdge(corners[0], corners[1]);
448       RoundEdge(corners[3], corners[0]);
449     }
450     InterlockEdge(corners[1], corners[2], 1, nlobes=1);
451     RoundCornerCut(rcs);
452     PostHoles(holes);
453   }
454   RoundCornerAdd(rcs);
455 }
456
457 module TestPiece2(){ ////toplevel
458   holes = [ [   0, 0],
459             [  50, 0]
460             ];
461   corners = TestPiece_holes2corners(holes);
462   difference(){
463     union(){
464       TileBase(corners[0], corners[2]);
465       Posts(holes);
466       RoundEdge(corners[0], corners[1]);
467       InterlockEdge(corners[3], corners[0], 0, nlobes=1);
468     }
469     PostHoles(holes);
470   }
471 }
472
473 module TestDemo(){ ////toplevel
474   translate([ -thehd[0], 0 ])
475     color("blue")
476     TestPiece1();
477   translate([ +thehd[0] + demo_slop, 0 ])
478     TestPiece2();
479 }
480
481 module Machine_NewRearProfile(){
482   // figures copied out of xfig edit boxes
483   // best not to edit the posbox size if poss - just move it
484   posbox = 10 * ([7.2333,-14.1267] - [-16.2289,40.0289]); // box, Green
485   sideline = -10 * ([-6.2400,13.5600] - [-2.4467,28.2556]); // line, Blue
486   scaleline = 10 * dist2d([-1.1911,-20.4800], [-11.2600,4.0578]); // Green2
487   scaleline_mm = 12+5+10+5+3;
488   sh = -[abs(posbox[0]), abs(posbox[1])];
489   rot = atan2(-sideline[0], sideline[1]);
490   sc = scaleline_mm / scaleline;
491   //echo("SH",sh,rot,sc);
492   scale(sc) rotate(rot) translate(sh){
493     import("sewing-table-rear-profile.dxf", convexity=10); // spline, Pink3
494   }
495 }
496
497 module Machine_NewFrontProfile(){
498   // figures copied out of xfig edit boxes
499   // best not to edit the posbox size if poss - just move it
500   posbox = 10 * ([11.8022,8.0600] - [4.2044,19.1867]); // box, Green
501   refline = 10 * ([7.6778,16.7222] - [27.8689,17.6578]); // line, Blue
502   refline_mm = (11-1)*10;
503   sh = -[abs(posbox[0]), abs(posbox[1])];
504   rot = atan2(-refline[0], refline[1]);
505   sc = refline_mm / vectorlen2d(refline);
506   //echo("SH",sh,rot,sc);
507   mirror([1,0]) scale(sc) rotate(rot+90) translate(sh){
508     import("sewing-table-front-profile.dxf", convexity=10); // spline, Pink3
509   }
510 }
511
512 module Machine_NewEndProfile(){
513   // figures copied out of xfig edit boxes
514   // NB that y coords are reversed - xfig origin is at bottom left
515   posboxs = 10 * [[4.0400,17.7956], [11.6622,32.5511]]; // box, Pink3
516   refline = 10 * ([8.4000,22.6000] - [50.3000,22.2000]); // line, Blue
517   refline_mm = 10 * (11 - 2.5);
518   sidelines = 10 * [[[9.0889,20.6178], [8.9644,14.6889]],
519                     [[50.3800,21.9578], [50.1933,14.4933]]]; // lines, Blue3
520   baseline = 10 * [[8.4000,18.0822], [50.3000,17.6822]]; // line, Green2
521
522   rot_adj = -0.38;
523
524   posbox = [min(posboxs[0][0],posboxs[1][0]),
525             max(posboxs[0][1],posboxs[1][1])];
526
527   m4_define(`MNEP_ELP',
528      `line_intersection_2d(baseline[0],baseline[1],
529                            sidelines[$1][0],sidelines[$1][1])')
530   endline = [MNEP_ELP(0),MNEP_ELP(1)];
531
532   rot = atan2(-refline[1], -refline[0]);
533   sc = refline_mm / vectorlen2d(refline);
534   sh = (0.5 * (endline[0] + endline[1])) - posbox;
535
536   ellen = sc * dist2d(endline[0],endline[1]);
537   scy = cutout_l_end_y_total / ellen;
538
539   scale([scy,1]) scale(sc) rotate(rot + rot_adj) translate(-[sh[0],-sh[1]]){
540
541     mirror([0,1]){
542   //%translate(1 * (posboxs[0] - posbox)) square(50);
543   //%translate(1 * (posboxs[1] - posbox)) square(50);
544 //  %translate(1 * (baseline[0] - posbox)) square([50,10]);
545
546 //  %translate(1 * (endline[0] - posbox)) square([50,10]);
547 //  %translate(1 * (endline[1] - posbox)) square([50,10]);
548
549 //  %translate(1 * (sidelines[0][0] - posbox)) square([10,50]);
550 //  %translate(1 * (sidelines[0][1] - posbox)) square([10,50]);
551 //  %translate(1 * (sidelines[1][0] - posbox)) square([10,50]);
552 //  %translate(1 * (sidelines[1][1] - posbox)) square([10,50]);
553     }
554
555     import("sewing-table-end-profile.dxf", convexity=10); // spline, Pink3
556   }
557 }
558
559 module Machine_NewEndProfileDemo(){ ////toplevel
560     translate([0,5,0])                             Machine_NewEndProfile();
561     translate([0,5,1]) color("blue") mirror([1,0]) Machine_NewEndProfile();
562   mirror([0,1,0]){
563     translate([0,5, 0])                             Machine_NewEndProfile();
564     translate([0,5,-1]) color("blue") mirror([1,0]) Machine_NewEndProfile();
565   }
566 }
567
568 module Machine_NewArm(){
569   translate([0,0,-30]) linear_extrude(height=60) {
570     translate(tile01_tr + [ -cutout_l_end_x - cutout_l_end_new_x_slop,
571                             (-cutout_tile01_y + cutout_tile11_y)/2 ]){
572       rotate(-90){
573         hull(){
574           for (d=[0,400]) 
575             translate([0,d]) Machine_NewEndProfile();
576         }
577       }
578     }
579   }
580 }
581
582 module Machine_NewRearCurve(){
583   slant = atan2(4,210-10);
584   //echo("SL",slant);
585   translate([0,0, rearcurve_double_inrad]) rotate([slant,0,0]){
586     translate([ rearcurve_double_inrad,
587                 0,
588                 -rearcurve_double_inrad + 10 ]){
589       rotate([180,0,0]) rotate([0,0,90]) linear_extrude(height=30){
590         hull(){
591           Machine_NewRearProfile();
592           translate([0,-100]) Machine_NewRearProfile();
593         }
594       }
595     }
596     rotate([0,90,0]) rotate([90,0,0]) {
597       intersection(){
598         rotate_extrude(convexity=10, $fn=64)
599           rotate(90)
600           translate([ 0, -rearcurve_double_inrad ])
601           Machine_NewRearProfile();
602         translate([0,0,-500])
603           cube([500,500,1000]);
604       }
605     }
606     translate([1,0,-rearcurve_double_inrad])
607       rotate([0,-90,0]) rotate([0,0,-90])
608       linear_extrude(height= rearcurve_strt_len + 1)
609       Machine_NewRearProfile();
610   }
611 }
612
613 module Machine_Curves(){ ////toplevel
614   translate([ tile01_tr[0] - cutout_l_end_x + rearedge_len,
615               cutout_tile11_y,
616               0 ]){
617     //%cube([20,20,20]);
618     translate([ -reartablet_x,
619                 -1,
620                 -reartablet_z + tablet_z_slop])
621       mirror([0,0,1])
622       cube([ reartablet_x+1,
623              reartablet_y+1,
624              20 ]);
625   }
626   translate([ tile01_tr[0] - cutout_l_end_x + frontedge_len,
627               cutout_tile11_y,
628               frontcurve_z_slop ]){
629     translate([0, -machine_rear_to_front, 0])
630       multmatrix([[1, -frontcurve_side_skew, 0, 0],
631                   [0,  1,   0, 0],
632                   [0,  0,   1, 0],
633                   [0,  0,   0, 1]])
634       mirror([1,0,0]) rotate([0,-90,0])rotate([0,0,-90])
635       linear_extrude(height= 200)
636       Machine_NewFrontProfile();
637   }
638   translate([ tile01_tr[0] - cutout_l_end_x + rearedge_len,
639               cutout_tile11_y,
640               frontcurve_z_slop ]){
641     translate([ rearcurve_strt_len,
642                 0,
643                 rearcurve_z_slop ]){
644       Machine_NewRearCurve();
645     }
646   }
647 }
648
649 module TestStrapSlots(){
650   pegwidth = teststrap_peg[0];
651   for (pos = teststrapslots_at) {
652     echo("TSS",pos);
653     translate(concat(pos,[0]))
654       for (mx = [0,1]) mirror([mx,0,0]) {
655           translate([ pegwidth/2, -teststrap[1]/2, -20 ])
656             cube(concat(teststrap,[40]));
657         }
658   }
659 }
660
661 module TestStrapPeg_any(l){ cube(concat([l],teststrap_peg)); }
662
663 module TestStrapPeg_Short(){ ////toplevel
664   TestStrapPeg_any(35);
665 }
666
667 module TestStrapPeg_Long(){ ////toplevel
668   TestStrapPeg_any(60);
669 }
670
671 module Machine(){ ////toplevel
672   Machine_NewArm();
673   Machine_Curves();
674   if (TEST)
675     TestStrapSlots();
676 }
677
678 module MachineEnvelope(){
679   // used for testing
680   p_arm_bl = [-cutout_l_end_x, -cutout_tile01_y];
681   y_arm_t  = cutout_tile11_y;
682   p_crv_fl = p_arm_bl + [frontedge_len, -frontcurve_avoid_y];
683   y_crv_b  = y_arm_t + rearcurve_avoid_y;
684
685   translate([0,0,-50]) linear_extrude(height= 100){
686     translate(p_arm_bl) square([400, y_arm_t] - p_arm_bl);
687     translate(p_crv_fl) square([400, y_crv_b] - p_crv_fl);
688   }
689 }
690
691 module Leg(){ ////toplevel
692   difference(){
693     union(){
694       hull(){
695         mirror([0,0,1])
696           cylinder(r= leg_big_dia/2, height=leg_top_flat_z, $fn=100);
697         translate([0,0, -leg_height])
698           cylinder(r= leg_bot_dia/2, height=1, $fn=100);
699       }
700       for (rot=[0: 360/leg_n_fins : 359]) rotate(rot) {
701           hull(){
702             mirror([0,0,1]) translate([0, -leg_fin_top_w/2, 0])
703               cube([ leg_fin_top_rad - 0.1,
704                      leg_fin_top_w,
705                      1 ])
706               ;
707             translate([0, -leg_fin_bot_w/2, -leg_height])
708               cube([ leg_fin_bot_rad,
709                      leg_fin_bot_w,
710                      leg_fin_bot_flat_z ]);
711           }
712         }
713     }
714     mirror([0,0,1]) translate([0,0,-1])
715       cylinder(r= leg_hole_dia/2, h=leg_height+2, $fn=30);
716     for (rot=[45: 360/leg_n_tubules : 359]) rotate(rot) {
717         mirror([0,0,1]) translate([ leg_tubule_pos_rad, 0, -1])
718           cylinder(r= leg_tubule_dia/2, h=leg_height+2, $fn=20);
719       }
720   }
721 }
722
723 function Rectangle_corners(c0, sz) =
724   // returns the corners of a rectangle from c0 to c0+sz
725   // if sz is positive, the corners are anticlockwise starting with c0
726   [ c0 + [ 0,     0     ],
727     c0 + [ sz[0], 0     ],
728     c0 + [ sz[0], sz[1] ],
729     c0 + [ 0,     sz[1] ] ];
730
731 function Rectangle_corners2posts(c) =
732   [ c[0] + thehd_tr,
733     c[1] + thehd_tl,
734     c[2] + thehd_bl,
735     c[3] + thehd_br ];
736
737 module Rectangle_TileBase(c) { TileBase(c[0], c[2]); }
738
739 function Posts_interpolate_one(c0,c1) = [c0, (c0+c1)/2, c1];
740
741 module Tile02(){ ////toplevel
742   sz = [100,170];
743   c0 = tile02_tr + -sz;
744   c = Rectangle_corners(c0, sz);
745   posts = Rectangle_corners2posts(c);
746   rcs = R_CNR(c,0);
747   difference(){
748     union(){
749       Rectangle_TileBase(c);
750       Posts(posts);
751       RoundEdge(R_EDGE(c,0));
752       RoundEdge(R_EDGE(c,3));
753       InterlockEdge(R_EDGE(c,2), 0);
754     }
755     InterlockEdge(R_EDGE(c,1), 1);
756     RoundCornerCut(rcs);
757     PostHoles(posts);
758   }
759   RoundCornerAdd(rcs);
760 }
761
762 module Tile12(){ ////toplevel
763   sz = [100,250];
764   c0 = tile02_tr + [-sz[0], 0];
765   c = Rectangle_corners(c0, sz);
766   posts = Rectangle_corners2posts(c);
767   rcs = R_CNR(c,3);
768   difference(){
769     union(){
770       Rectangle_TileBase(c);
771       Posts(posts);
772       RoundEdge(R_EDGE(c,2));
773       RoundEdge(R_EDGE(c,3));
774     }
775     InterlockEdge(R_EDGE(c,0), 1);
776     InterlockEdge(R_EDGE(c,1), 1);
777     RoundCornerCut(rcs);
778     PostHoles(posts);
779   }
780   RoundCornerAdd(rcs);
781 }
782
783 tile_01_11_cnr = tile01_tr + [-cutout_l_end_x, 0];
784 tile_11_10_cnr = tile01_tr + [0, cutout_tile11_y];
785 tile_01_00_cnr = tile01_tr - [0, cutout_tile01_y];
786
787 module Tile11(){ ////toplevel
788   sz = [250,250];
789   c0 = tile01_tr + [-sz[0],0];
790   c = Rectangle_corners(c0, sz);
791   cnr_posts = Rectangle_corners2posts(c);
792   posts = concat(
793                  Posts_interpolate_one(cnr_posts[0],
794                                        cnr_posts[1] - [cutout_l_end_x, 0]),
795                  [ cnr_posts[1] + [0, cutout_tile11_y],
796                    cnr_posts[2],
797                    cnr_posts[3]
798                    ]);
799   difference(){
800     union(){
801       Rectangle_TileBase(c);
802       Posts(posts);
803       RoundEdge(R_EDGE(c,2));
804       InterlockEdge(R_EDGE(c,3));
805     }
806     InterlockEdge(c[0], tile_01_11_cnr, 1);
807     InterlockEdge(tile_11_10_cnr, c[2], 1);
808     PostHoles(posts);
809     Machine();
810   }
811 }    
812
813 module Tile01(){ ////toplevel
814   sz = [250,170];
815   c0 = tile01_tr + -sz;
816   c = Rectangle_corners(c0, sz);
817   cnr_posts = Rectangle_corners2posts(c);
818   posts = concat(
819                  Posts_interpolate_one(R_EDGE(cnr_posts,0)),
820                  [ cnr_posts[2] + [0, -cutout_tile01_y] ],
821                  Posts_interpolate_one(cnr_posts[2] - [cutout_l_end_x, 0],
822                                        cnr_posts[3])
823                  );
824   difference(){
825     union(){
826       Rectangle_TileBase(c);
827       Posts(posts);
828       RoundEdge(R_EDGE(c,0));
829       InterlockEdge(tile_01_11_cnr, c[3]);
830       InterlockEdge(R_EDGE(c,3));
831     }
832     PostHoles(posts);
833     InterlockEdge(c[1], tile_01_00_cnr, 1);
834     Machine();
835   }
836 }    
837
838 module Tile10(){ ////toplevel
839   sz = [250,250];
840   c0 = tile01_tr + [0,0];
841   c = Rectangle_corners(c0, sz);
842   cnr_posts = Rectangle_corners2posts(c);
843   cty = cutout_tile11_y;
844   rcy = cty + rearcurve_avoid_y;
845   posts = [ cnr_posts[0] + [ 0,                             cty ],
846             cnr_posts[1] + [ -sz[0] + rearedge_len - cutout_l_end_x, cty ],
847             cnr_posts[1] + [ 0,                             rcy ],
848             cnr_posts[2],
849             cnr_posts[3] ];
850   rcs = R_CNR(c,2);
851   difference(){
852     union(){
853       Rectangle_TileBase(c);
854       Posts(posts);
855       RoundEdge(R_EDGE(c,1));
856       RoundEdge(R_EDGE(c,2));
857       InterlockEdge(c[3], tile_11_10_cnr);
858     }
859     PostHoles(posts);
860     RoundCornerCut(rcs);
861     Machine();
862   }
863   RoundCornerAdd(rcs);
864 }
865
866 module Tile00(){ ////toplevel
867   sz = [250,170];
868   c0 = tile01_tr + [0,-sz[1]];
869   c = Rectangle_corners(c0, sz);
870
871   // the edge c[1]..c[2] needs a diagonal chunk, from c1bis to c2bis
872   c2bis = [ -cutout_l_end_x + frontedge_len + frontcurve_strt_len, c[2][1] ];
873   c1bis = [ c[1][0],
874             c[2][1] -
875             (c[2][0] - c2bis[0]) * tan(90 - frontcurve_dualcurve_angle) ];
876
877   cnr_posts = Rectangle_corners2posts(c);
878   cty = cutout_tile01_y;
879   rcy = cty + frontcurve_avoid_y;
880   posts = [ cnr_posts[0],
881             cnr_posts[1],
882             0.5 * (cnr_posts[0] + cnr_posts[1]),
883             cnr_posts[2] + [ 0,                             -rcy ],
884             cnr_posts[2] + [ -sz[0] + frontedge_len - cutout_l_end_x, -cty ],
885             cnr_posts[3] + [ 0,                             -cty ]
886             ];
887   rcs = R_CNR(c,1);
888   rc2 = [c1bis,c2bis,c[1]];
889   difference(){
890     union(){
891       difference(){
892         union(){
893           Rectangle_TileBase(c);
894           Posts(posts);
895           RoundEdge(R_EDGE(c,0));
896           RoundEdge(c[1], c1bis);
897           InterlockEdge(tile_01_00_cnr, c[0]);
898         }
899         RoundCornerCut(rcs);
900         translate([0,0,-20]) linear_extrude(height=40) {
901           polygon([ c1bis, c1bis + [50,0], c2bis + [50,0], c2bis ]);
902         }
903       }
904       RoundEdge(c1bis, c2bis);
905     }
906     Machine();
907     PostHoles(posts);
908     RoundCornerCut(rc2);
909   }
910   RoundCornerAdd(rcs);
911   RoundCornerAdd(rc2);
912 }
913
914 module FitTest_general(c0,sz, dobrace=false, bracexx=0){
915   c = Rectangle_corners(c0, sz);
916   brace = [7,7,9];
917   bsz = sz + [bracexx,0,0];
918   difference(){
919     union(){
920       Rectangle_TileBase(c);
921       if (dobrace) {
922         translate(concat(c0, [-brace[2] + 0.1])){
923           difference(){
924             cube(concat(bsz, [brace[2]]) - [5,0,0]);
925             translate(brace + [0,0, -25])
926               cube(concat(bsz, [50]) - brace*2 + [10,0,0]);
927           }
928         }
929       }
930       RoundEdge(R_EDGE(c,1));
931     }
932     Machine();
933   }
934 }
935
936 module FitTest_PairLink(cut=false){ ////toplevel
937   cy0=-55; cy1=85; cx=132;
938   bar = [10,10];
939   legrad = 12;
940   footrad_min = 1; footrad_max = 4; footrad_depth = 5;
941   strap = [3,5];
942   adj_neg_slop = 1.0;
943   bar_z_slop = 1.75;
944
945   // calculated
946   straphole_x_max = legrad/sqrt(2) + footrad_max;
947   dz = cut ? adj_neg_slop : 0;
948
949   translate([cx - bar[0]/2, cy0, dz + bar_z_slop])
950     cube([bar[0], cy1-cy0, bar[1] - bar_z_slop]);
951
952   for (endy=[cy0,cy1]) {
953     $fn=32;
954     translate([cx,endy,dz]){
955       // feet
956       for (rot=[45:90:315]) rotate(rot) {
957         translate([legrad,0,0]){
958           hull(){
959             cylinder(r= footrad_max, h=1);
960             translate([0,0,-footrad_depth])
961               cylinder(r= footrad_min, h=1);
962           }
963           if (cut)
964             translate([0,0,-10])
965             cylinder(r= footrad_min +
966                      adj_neg_slop * (footrad_max-footrad_min)/footrad_depth,
967                      h=20);
968         }
969       }
970       // legs
971       for (rot=[45,135]) rotate(rot) {
972         hull(){
973           for (s=[-1,+1]){
974             translate([s*legrad,0,0])
975               cylinder(r= footrad_max, h=bar[1]);
976           }
977         }
978       }
979       // strap holes
980       if (cut) {
981         for (rot=[0,180]) rotate(rot) {
982             translate([ straphole_x_max - strap[0]/2, 0,0 ])
983               cube(concat(strap,[20]), center=true);
984           }
985       }
986     }
987   }
988 }
989
990 module FitTest_RearCurve(){ ////toplevel
991   difference(){
992     FitTest_general([100,0], [180,100]);
993     FitTest_PairLink(true);
994     TestStrapSlots();
995   }
996 }
997
998 module FitTest_FrontCurve(){ ////toplevel
999   p0 = [100,-80];
1000   sz = [180,80];
1001   difference(){
1002     intersection() {
1003       Tile00();
1004       translate([0,0,-8]) linear_extrude(height=18) {
1005         translate(p0) square(sz);
1006         translate(teststrapslots_at[3])
1007           scale(2* [ teststrap_peg[0], teststrap[1] ])
1008           circle(r=1, $fn=20);
1009       }
1010     }
1011     FitTest_PairLink(true);
1012     TestStrapSlots();
1013   }
1014 }
1015
1016 module FitTest_Entire(){ ////toplevel
1017   p0 = [-33,-80];
1018   szrear = [263,180];
1019   szfront = [243,szrear[1]];
1020   difference(){
1021     FitTest_general(p0, szrear, dobrace=true, bracexx=0);
1022     FitTest_PairLink(true);
1023     translate(concat(p0,[0]) + [szfront[0],-10,-40])
1024       cube([100, -p0[1], 80]);
1025     TestStrapSlots();
1026   }
1027   intersection(){
1028     FitTest_RearCurve();
1029     translate(concat(p0,[-20])) cube(concat(szrear,[40]));
1030   }
1031   FitTest_FrontCurve();
1032 }
1033
1034 module FitTest_EntireDemo(){ ////toplevel
1035   FitTest_Entire();
1036   //%Tile00();
1037 }
1038
1039 module FitTest_EndEnd(){ ////toplevel
1040   p0 = [-30,-32];
1041   sz = [156,81] - p0;
1042   sz2 = [136,68] - p0;
1043   difference(){
1044     FitTest_general(p0, sz);
1045     translate([ p0[0] -1, p0[1]+sz2[1], -10])
1046       cube([ sz2[0] +1, 50, 20 ]);
1047   }
1048 }
1049
1050 module FitTest_PairDemo(){ ////toplevel
1051   sh=[-90,-15,0];
1052   translate(sh){
1053     FitTest_PairLink();
1054     %FitTest_FrontCurve();
1055     %FitTest_RearCurve();
1056   }
1057   rotate([0,0,180]){
1058     translate(sh){
1059       difference(){
1060         union(){
1061           FitTest_FrontCurve();
1062           FitTest_RearCurve();
1063         }
1064         #FitTest_PairLink(true);
1065       }
1066     }
1067   }
1068 }
1069
1070 module RoundCornerDemo_plat(cnr){
1071   mirror([0,0,1]) linear_extrude(height=1) polygon(cnr);
1072 }
1073
1074 module RoundCornerDemo(){ ////toplevel
1075   cnr = [ [-2,-3], [13,-3], [-12,9] ];
1076   translate([0,25,0]) RoundCornerDemo_plat(cnr);
1077   translate([25,0,0]) RoundCornerAdd(cnr);
1078   translate([-25,0,0]) RoundCornerCut(cnr);
1079   translate([0,-25,0]) RoundCorner_selector(cnr, 0);
1080   difference(){
1081     RoundCornerDemo_plat(cnr);
1082     RoundCornerCut(cnr);
1083   }
1084   RoundCornerAdd(cnr);
1085 }
1086
1087 module Demo(){ ////toplevel
1088   translate(demo_slop*[-2,1]) color("blue") Tile12();
1089   translate(demo_slop*[-2,0]) color("red")  Tile02();
1090   translate(demo_slop*[-2,1]) color("orange") Tile11();
1091   translate(demo_slop*[-2,0]) color("purple") Tile01();
1092   translate(demo_slop*[-3,1]) color("blue")   Tile10();
1093   translate(demo_slop*[-3,0]) color("red")    Tile00();
1094   %Machine();
1095   // Can also do this, to print reference sheet:
1096   //  load this into openscad
1097   //  select Ctrl-4 view, view all, scale appropriately
1098   //  import sewing-table,Demo-flat.png
1099   //  pngtopnm <sewing-table,Demo-flat.png | ppmbrighten -s -50 -v +100 >t.pnm
1100   //  lpr t.pnm
1101 }
1102   
1103 //TestPiece1();
1104 //TestPiece2();
1105 //Demo();
1106
1107 //Machine_NewRearProfile();
1108 //Machine_NewRearCurve();
1109 //Machine_NewFrontProfile();
1110 //Machine_NewEndProfile();
1111 //Machine_NewEndProfileDemo();
1112 //Machine_Curves();
1113 //Machine();
1114 //FitTest();
1115 //MachineEnvelope();