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