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