chiark / gitweb /
8b69b445499503991010b0295de2ce8ca08be2c4
[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 cutout_l_end_y = 85;
34 cutout_l_end_curve = 1;
35
36 cutout_tile01_y = 170 - 147;
37 cutout_tile11_x = 22 + cutout_l_end_curve;
38 cutout_tile11_y = cutout_l_end_curve - cutout_tile01_y;
39
40 // calculated
41
42 TEST = false;
43
44 ply_edge_hole_dist = ply_edge_min + ply_hole_dia/2;
45
46 echo(str("HOLES IN PLY ctr dist from PLY edge = ", ply_edge_hole_dist));
47
48 hole_slop = (ply_hole_dia - post_dia)/2;
49 tile_hard_edge_hole_dist = ply_edge_hole_dist + hole_slop;
50
51 echo(str("HOLES IN PLY ctr dist from TILE HARD edge = ",
52          tile_hard_edge_hole_dist));
53
54 echo(str("HOLES IN PLY ctr dist from TILE ROUND edge = ",
55          tile_hard_edge_hole_dist + round_edge_rad));
56
57 thehd = [ tile_hard_edge_hole_dist, tile_hard_edge_hole_dist ];
58 thehd_tr = thehd;
59 thehd_tl = [ -thehd_tr[0], thehd_tr[1] ];
60 thehd_bl = -thehd_tr;
61 thehd_br = -thehd_tl;
62
63 interlock_rad = interlock_dia/2;
64 interlock_negative_rad = interlock_rad + 0.125;
65
66 interlock_sq_adj = 0.2; // arbitrary
67
68 module Post(){
69   mirror([0,0,1]) {
70     difference(){
71       cylinder(r= post_dia/2, h= tile_th + ply_th - post_shorter);
72       translate([0,0, tile_th]) {
73         cylinder(r= screw_big_dia/2, h= screw_big_len);
74         cylinder(r= screw_dia/2, h= ply_th, $fn=20);
75       }
76     }
77     if (TEST) {
78       tsz = tile_hard_edge_hole_dist - test_edge + 1;
79       translate([0,0, tile_th/2]) {
80         cube([post_dia, tsz*2, tile_th], center=true);
81         cube([tsz*2, post_dia, tile_th], center=true);
82       }
83     }
84   }
85 }
86
87 module Posts(posts) {
88   for (p= posts) {
89     translate(concat(p, [0]))
90       Post();
91   }
92 }
93
94 module TileBase(botleft, topright){
95   size = topright - botleft;
96   botleft_post = botleft + thehd_tr;
97   topright_post = topright + thehd_bl;
98   difference(){
99     mirror([0,0,1])
100       translate(concat(botleft, [0]))
101       cube(concat(size, [tile_th]));
102     if (!TEST) {
103       translate( concat(botleft_post, [ -tile_th ])
104                  + 0.5 * [ post_dia, post_dia, 0 ] )
105         Commitid_BestCount_M( topright_post-botleft_post
106                               + [-post_dia,-post_dia]
107                               + [0, thehd[1]]);
108     }
109     if (TEST) {
110       translate( concat(botleft + [thehd[0], 0], [0]) )
111         Commitid_BestCount([ size[0] - thehd[0]*2, thehd[1] ]);
112       mirror([0,0,1]) {
113         translate(concat(botleft + [test_edge,test_edge], [test_tile_th]))
114           cube(concat(size - [test_edge,test_edge]*2, [tile_th]));
115         translate(concat(botleft_post, [-1]))
116           cube(concat(topright_post-botleft_post, [tile_th+2]));
117       }
118     }
119   }
120 }
121
122 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars) { body; }
123 m4_define(`INREFFRAME',`
124   length_vec = ($2) - ($1);
125   length = dist2d([0,0], length_vec);
126   length_uvec = length_vec / length;
127   ortho_uvec = [ -length_uvec[1], length_uvec[0] ];
128   m = [ [ length_uvec[0],  ortho_uvec[0], 0, ($1)[0], ],
129         [ length_uvec[1],  ortho_uvec[1], 0, ($1)[1], ],
130         [ 0,              0,              1,            0, ],
131         [ 0,              0,              0,            1, ] ];
132   $3
133   multmatrix(m)
134 ')
135
136 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars)
137 m4_dnl    INREFFRAME_EDGE { body; }
138 m4_define(`INREFFRAME_EDGE',`
139   translate([0,0, -round_edge_rad])
140 ')
141
142 module RoundEdge(left_cnr, right_cnr) {
143   INREFFRAME(left_cnr, right_cnr)
144     INREFFRAME_EDGE {
145     difference(){
146       rotate([0,90,0])
147         cylinder(r= round_edge_rad, h= length, $fn=50);
148       translate([-1, 0, -20])
149         cube([length+2, 20, 20]);
150     }
151   }
152 }
153
154 module RoundCornerCut(ci) {
155   // ci should be [this_cnr, right_cnr]
156   // where right_cnr is to the right (ie, anticlockwise)
157   this_cnr = ci[0];
158   right_cnr = ci[1];
159   offr= round_cnr_rad - round_edge_rad;
160   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
161     difference(){
162       cube(offr*2 - 0.1, center=true);
163       translate([offr, offr, 0])
164         cylinder(center=true, h=20, r= offr);
165     }
166   }
167 }
168
169 module RoundCornerAdd(ci) {
170   this_cnr = ci[0];
171   right_cnr = ci[1];
172   bigr = round_cnr_rad - round_edge_rad;
173   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
174     intersection(){
175       cube(bigr*2 + 0.1, center=true);
176       translate([bigr, bigr, 0])
177         rotate_extrude(convexity=10, $fn=50)
178         translate([bigr, 0])
179         difference(){
180           circle(r= round_edge_rad, $fn=50);
181           mirror([1,1])
182             square([20,20]);
183         }
184     }
185   }
186 }
187
188 module InterlockLobePlan(negative) {
189   r = negative ? interlock_negative_rad : interlock_rad;
190   ymir = negative ? 0 : 1;
191
192   dx = sqrt(3) * r;
193   $fn= 80;
194   translate([thehd[0], 0]){
195     mirror([0,ymir]){
196       circle(r=r);
197       difference(){
198         translate([-dx, -0.1])
199           square([ dx*2, r/2 + 0.1 ]);
200         for (xi = [-1, 1]) {
201           translate([ xi*dx, r ])
202             circle(r=r);
203         }
204       }
205     }
206   }
207 }
208
209 module InterlockEdgePlan(negative, nlobes, length, dosquare=true) {
210   for (lobei = [ 0 : nlobes-1 ]) {
211     lobex = (length - thehd[0]*2) * (lobei ? lobei / (nlobes-1) : 0);
212     translate([lobex, 0, 0]) {
213       InterlockLobePlan(negative);
214     }
215   }
216
217   if (dosquare) {
218     iadj = 0;
219     slotshorter = negative ? -0.1 : interlock_fine_lenslop;
220     mirror([0, negative])
221       translate([slotshorter, iadj])
222       square([length - slotshorter*2, interlock_fine + iadj*2]);
223   }
224 }
225
226 module InterlockEdge(left_cnr, right_cnr, negative=0, nlobes=2) {
227   plusth = negative * 1.0;
228   protr = interlock_fine + interlock_sq_adj;
229
230   z2 = -tile_th/2;
231   z1 = -tile_th/2 - protr / interlock_fine_slope;
232   z3 = -tile_th/2 + protr / interlock_fine_slope;
233
234   negsign = negative ? -1 : +1;
235   yprotr = negsign * protr;
236
237   INREFFRAME(left_cnr, right_cnr) {
238     for (vsect = [ // zs0            zs1      ys0,            ys1
239                   [ -tile_th-plusth, plusth,  0,              0],
240                   [ z1,              z2,      0, yprotr],
241                   [ z2,              z3,      yprotr, 0],
242                   ]) {
243       zs0 = vsect[0];
244       zs1 = vsect[1];
245       zsd = zs1-zs0;
246       ys0 = vsect[2];
247       ys1 = vsect[3];
248       ysd = ys1-ys0;
249       sl = ysd/zsd;
250       m = [ [ 1,0,   0,    0 ],
251             [ 0,1, -sl, -ys0 + negsign*interlock_sq_adj ],
252             [ 0,0,   1,  zs0 ],
253             [ 0,0,   0,    1 ] ];
254       multmatrix(m)
255         linear_extrude(height=zsd, convexity=10)
256         InterlockEdgePlan(negative, nlobes, length, !!ysd);
257     }
258   }
259 }
260
261 function TestPiece_holes2corners(holes) =
262   [ holes[0] + thehd_bl,
263     holes[1] + thehd_br,
264     holes[1] + thehd_tr,
265     holes[0] + thehd_tl ];
266
267 module TestPiece1(){ ////toplevel
268   holes = [ [-100, 0],
269             [   0, 0]
270             ];
271   corners = TestPiece_holes2corners(holes);
272   rcs = [corners[0], corners[1]];
273   difference(){
274     union(){
275       TileBase(corners[0], corners[2]);
276       Posts(holes);
277       RoundEdge(corners[0], corners[1]);
278       RoundEdge(corners[3], corners[0]);
279     }
280     InterlockEdge(corners[1], corners[2], 1, nlobes=1);
281     RoundCornerCut(rcs);
282   }
283   RoundCornerAdd(rcs);
284 }
285
286 module TestPiece2(){ ////toplevel
287   holes = [ [   0, 0],
288             [  50, 0]
289             ];
290   corners = TestPiece_holes2corners(holes);
291   TileBase(corners[0], corners[2]);
292   Posts(holes);
293   RoundEdge(corners[0], corners[1]);
294   InterlockEdge(corners[3], corners[0], 0, nlobes=1);
295 }
296
297 module TestDemo(){ ////toplevel
298   translate([ -thehd[0], 0 ])
299     color("blue")
300     TestPiece1();
301   translate([ +thehd[0] + demo_slop, 0 ])
302     TestPiece2();
303 }
304   
305 function Rectangle_corners(c0, sz) =
306   // returns the corners of a rectangle from c0 to c0+sz
307   // if sz is positive, the corners are anticlockwise starting with c0
308   [ c0 + [ 0,     0     ],
309     c0 + [ sz[0], 0     ],
310     c0 + [ sz[0], sz[1] ],
311     c0 + [ 0,     sz[1] ] ];
312
313 function Rectangle_corners2posts(c) =
314   [ c[0] + thehd_tr,
315     c[1] + thehd_tl,
316     c[2] + thehd_bl,
317     c[3] + thehd_br ];
318
319 module Rectangle_TileBase(c) { TileBase(c[0], c[2]); }
320
321 m4_dnl   R_EDGE(c,ix)
322 m4_dnl        c is from Rectangle_corners and
323 m4_dnl        ix is a corner number
324 m4_dnl    expands to two comma-separated corners:
325 m4_dnl    that denoted by ix, and the next one anticlockwise
326 m4_define(`R_EDGE',`$1[$2],$1[(($2)+1)%4]')
327
328 module Tile02(){ ////toplevel
329   sz = [100,170];
330   c0 = -sz;
331   c = Rectangle_corners(c0, sz);
332   posts = Rectangle_corners2posts(c);
333   rcs = [R_EDGE(c,0)];
334   difference(){
335     union(){
336       Rectangle_TileBase(c);
337       Posts(posts);
338       RoundEdge(R_EDGE(c,0));
339       RoundEdge(R_EDGE(c,3));
340       InterlockEdge(R_EDGE(c,2), 0);
341     }
342     InterlockEdge(R_EDGE(c,1), 1);
343     RoundCornerCut(rcs);
344   }
345   RoundCornerAdd(rcs);
346 }
347
348 module Tile12(){ ////toplevel
349   sz = [100,250];
350   c0 = [-sz[0], 0];
351   c = Rectangle_corners(c0, sz);
352   posts = Rectangle_corners2posts(c);
353   rcs = [R_EDGE(c,3)];
354   difference(){
355     union(){
356       Rectangle_TileBase(c);
357       Posts(posts);
358       RoundEdge(R_EDGE(c,2));
359       RoundEdge(R_EDGE(c,3));
360     }
361     InterlockEdge(R_EDGE(c,0), 1);
362     InterlockEdge(R_EDGE(c,1), 1);
363     RoundCornerCut(rcs);
364   }
365   RoundCornerAdd(rcs);
366 }
367
368 module Demo(){ ////toplevel
369   translate(demo_slop*[-2,1]) color("blue") Tile12();
370   translate(demo_slop*[-2,0]) color("red")  Tile02();
371 }
372   
373 //TestPiece1();
374 //TestPiece2();
375 //Demo();