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