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