chiark / gitweb /
maglite-holder: wip holder
[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   this_cnr = ci[0];
147   right_cnr = ci[1];
148   offr= round_cnr_rad - round_edge_rad;
149   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
150     difference(){
151       cube(offr*2 - 0.1, center=true);
152       translate([offr, offr, 0])
153         cylinder(center=true, h=20, r= offr);
154     }
155   }
156 }
157
158 module RoundCornerAdd(ci) {
159   this_cnr = ci[0];
160   right_cnr = ci[1];
161   bigr = round_cnr_rad - round_edge_rad;
162   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
163     intersection(){
164       cube(bigr*2 + 0.1, center=true);
165       translate([bigr, bigr, 0])
166         rotate_extrude(convexity=10, $fn=50)
167         translate([bigr, 0])
168         difference(){
169           circle(r= round_edge_rad, $fn=50);
170           mirror([1,1])
171             square([20,20]);
172         }
173     }
174   }
175 }
176
177 module InterlockLobePlan(negative) {
178   r = negative ? interlock_negative_rad : interlock_rad;
179   ymir = negative ? 0 : 1;
180
181   dx = sqrt(3) * r;
182   $fn= 80;
183   translate([thehd[0], 0]){
184     mirror([0,ymir]){
185       circle(r=r);
186       difference(){
187         translate([-dx, -0.1])
188           square([ dx*2, r/2 + 0.1 ]);
189         for (xi = [-1, 1]) {
190           translate([ xi*dx, r ])
191             circle(r=r);
192         }
193       }
194     }
195   }
196 }
197
198 module InterlockEdgePlan(negative, nlobes, length, dosquare=true) {
199   for (lobei = [ 0 : nlobes-1 ]) {
200     lobex = (length - thehd[0]*2) * (lobei ? lobei / (nlobes-1) : 0);
201     translate([lobex, 0, 0]) {
202       InterlockLobePlan(negative);
203     }
204   }
205
206   if (dosquare) {
207     iadj = 0;
208     slotshorter = negative ? -0.1 : interlock_fine_lenslop;
209     mirror([0, negative])
210       translate([slotshorter, iadj])
211       square([length - slotshorter*2, interlock_fine + iadj*2]);
212   }
213 }
214
215 module InterlockEdge(left_cnr, right_cnr, negative=0, nlobes=2) {
216   plusth = negative * 1.0;
217   protr = interlock_fine + interlock_sq_adj;
218
219   z2 = -tile_th/2;
220   z1 = -tile_th/2 - protr / interlock_fine_slope;
221   z3 = -tile_th/2 + protr / interlock_fine_slope;
222
223   negsign = negative ? -1 : +1;
224   yprotr = negsign * protr;
225
226   INREFFRAME(left_cnr, right_cnr) {
227     for (vsect = [ // zs0            zs1      ys0,            ys1
228                   [ -tile_th-plusth, plusth,  0,              0],
229                   [ z1,              z2,      0, yprotr],
230                   [ z2,              z3,      yprotr, 0],
231                   ]) {
232       zs0 = vsect[0];
233       zs1 = vsect[1];
234       zsd = zs1-zs0;
235       ys0 = vsect[2];
236       ys1 = vsect[3];
237       ysd = ys1-ys0;
238       sl = ysd/zsd;
239       m = [ [ 1,0,   0,    0 ],
240             [ 0,1, -sl, -ys0 + negsign*interlock_sq_adj ],
241             [ 0,0,   1,  zs0 ],
242             [ 0,0,   0,    1 ] ];
243       multmatrix(m)
244         linear_extrude(height=zsd, convexity=10)
245         InterlockEdgePlan(negative, nlobes, length, !!ysd);
246     }
247   }
248 }
249
250 function TestPiece_holes2corners(holes) =
251   [ holes[0] + thehd_bl,
252     holes[1] + thehd_br,
253     holes[1] + thehd_tr,
254     holes[0] + thehd_tl ];
255
256 module TestPiece1(){ ////toplevel
257   holes = [ [-100, 0],
258             [   0, 0]
259             ];
260   corners = TestPiece_holes2corners(holes);
261   rcs = [corners[0], corners[1]];
262   difference(){
263     union(){
264       TileBase(corners[0], corners[2]);
265       Posts(holes);
266       RoundEdge(corners[0], corners[1]);
267       RoundEdge(corners[3], corners[0]);
268     }
269     InterlockEdge(corners[1], corners[2], 1, nlobes=1);
270     RoundCornerCut(rcs);
271   }
272   RoundCornerAdd(rcs);
273 }
274
275 module TestPiece2(){ ////toplevel
276   holes = [ [   0, 0],
277             [  50, 0]
278             ];
279   corners = TestPiece_holes2corners(holes);
280   TileBase(corners[0], corners[2]);
281   Posts(holes);
282   RoundEdge(corners[0], corners[1]);
283   InterlockEdge(corners[3], corners[0], 0, nlobes=1);
284 }
285
286 module TestDemo(){ ////toplevel
287   translate([ -thehd[0], 0 ])
288     color("blue")
289     TestPiece1();
290   translate([ +thehd[0] + demo_slop, 0 ])
291     TestPiece2();
292 }
293   
294 function Rectangle_corners(c0, sz) =
295   [ c0 + [ 0,     0     ],
296     c0 + [ sz[0], 0     ],
297     c0 + [ sz[0], sz[1] ],
298     c0 + [ 0,     sz[1] ] ];
299
300 function Rectangle_corners2posts(c) =
301   [ c[0] + thehd_tr,
302     c[1] + thehd_tl,
303     c[2] + thehd_bl,
304     c[3] + thehd_br ];
305
306 module Tile02(){ ////toplevel
307   sz = [100,170];
308   c0 = -sz;
309   c = Rectangle_corners(c0, sz);
310   posts = Rectangle_corners2posts(c);
311   rcs = [c[0], c[1]];
312   difference(){
313     union(){
314       TileBase(c[0], c[2]);
315       Posts(posts);
316       RoundEdge(c[0], c[1]);
317       RoundEdge(c[3], c[0]);
318       InterlockEdge(c[2], c[3], 0);
319     }
320     InterlockEdge(c[1], c[2], 1);
321     RoundCornerCut(rcs);
322   }
323   RoundCornerAdd(rcs);
324 }
325
326 module Tile12(){ ////toplevel
327   sz = [100,250];
328   c0 = [-sz[0], 0];
329   c = Rectangle_corners(c0, sz);
330   posts = Rectangle_corners2posts(c);
331   rcs = [c[2], c[3]];
332   difference(){
333     union(){
334       TileBase(c[0], c[2]);
335       RoundEdge(c[2], c[3]);
336       RoundEdge(c[3], c[0]);
337       Posts(posts);
338     }
339     InterlockEdge(c[0], c[1], 1);
340     RoundCornerCut(rcs);
341   }
342   RoundCornerAdd(rcs);
343 }
344
345 module Demo(){ ////toplevel
346   translate(demo_slop*[-2,1]) color("blue") Tile12();
347   translate(demo_slop*[-2,0]) color("red")  Tile02();
348 }
349   
350 //TestPiece1();
351 //TestPiece2();
352 //Demo();