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