include
;
// Fonction de création d'un embout pour puzzle
module embout_puzzle(rayon, height){
union() {
cylinder(r=rayon, h=height, center=true);
translate([0,2.0001*(rayon/3),0]){ // /!\ ancienne valeur y=2
cube([rayon,rayon,height],center=true);
}
}
}
// Fonction de création d'un hexagone
module hexagone(a, h){
r = sqrt(pow(a,2) - pow(a/2,2));
translate([-a,-r,-h/2]) linear_extrude (height = h){
polygon([[a/2,0],[0,r],[a/2,2*r],[1.5*a,2*r],
[2*a,r],[1.5*a,0],[a/2,0]]);
}
}
// Fonction créant les embouts "mâle"
module pos_emb_puzzle(s_hex,s_epuz,h){
ri = sqrt(pow(s_hex,2) - pow(s_hex/2,2));
for (i=[1:6]) {
if(i % 2 == 0){
translate([(ri+(7/6)*s_epuz)*cos(60*i+30),
(ri+(7/6)*s_epuz)*sin(60*i+30),0]){
rotate([0,0,60*i+30]) rotate([0,0,90]){
embout_puzzle(s_epuz,h);
}
}
}
}
}
// Fonction créant les embouts "femmelle"
module neg_emb_puzzle(s_hex,s_epuz,h){
ri = sqrt(pow(s_hex,2) - pow(s_hex/2,2));
for (i=[1:6]) {
if(i % 2 != 0){
translate([(ri-(7/6)*s_epuz)*cos(60*i+30),
(ri-(7/6)*s_epuz)*sin(60*i+30),0]){
rotate([0,0,60*i+30]) rotate([0,0,-90]){
embout_puzzle(s_epuz,h);
}
}
}
}
}
// Fonction créant les trous de niveau 1 (6 trous + centre)
module f_lvl1t(s_hex,height,rtrou){
// a=s_hex / 3; version 1.1
a=12*(s_hex / 30);
r = sqrt(pow(a,2) - pow(a/2,2));
lvl1t = [[a,r],[a/2,0],[0,r],[a/2,2*r],[1.5*a,2*r],[2*a,r],
[1.5*a,0]];
for (i=[0:6]) {
translate([lvl1t[i][0]-a,lvl1t[i][1]-r]){
cylinder(r=rtrou,h=height,center=true);
}
}
}
// Fonction créant les trous de niveau 2 (12 trous)
module f_lvl2t(s_hex,height,rtrou){
// a=2*(s_hex / 3); version 1.1
a=24*(s_hex / 30);
r = sqrt(pow(a,2) - pow(a/2,2));
lvl2t = [[a/2,0],[a/4,r/2],[0,r],[a/4,1.5*r],[a/2,2*r],[a,2*r],
[1.5*a,2*r],[1.75*a,1.5*r],[2*a,r],[1.75*a,r/2],
[1.5*a,0],[a,0]];
for (i=[0:11]) {
translate([lvl2t[i][0]-a,lvl2t[i][1]-r]){
cylinder(r=rtrou,h=height,center=true);
}
}
}
// Fonction créant la pièce de puzzle finale
module puzzle(size, ratio_embouts, h, r_trou){
s_epuz = size/ratio_embouts;
union(){
difference(){
hexagone(size,h);
neg_emb_puzzle(size,s_epuz,h);
f_lvl1t(size,h,r_trou);
f_lvl2t(size,h,r_trou);
}
pos_emb_puzzle(size,s_epuz,h);
}
}
// Main
puzzle(size=40, ratio_embouts=6, h=8, r_trou=2);