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