vibe-openscad

tier-2-offset-handle-mug / gpt-5-2025-08-07

7 run / 累計 944.2s / 累計 $0.5865

openai gpt 5 2025-08-07 bare

bare (provider default effort)

run detail →
render
bare render of tier-2-offset-handle-mug
loading STL…
success 190.7s · 273/12031t · $0.1207
SCAD (61 lines)
// Parameters
$fn = 96;

mug_od = 80;
mug_id = 70;
mug_h  = 90;
mug_bottom = 6;

handle_inner_w = 25; // inner D width (X) = 25 mm
handle_inner_h = 30; // inner D height (Z) = 30 mm
handle_wall_t  = 5;  // 2D (XZ) wall thickness around inner D
handle_depth_y = 36; // thickness along Y (tangential)
attach_eps     = 0.2; // small overlap into mug for robust union

// Mug body: outer cylinder minus inner cavity (leaving 6mm bottom)
module mug_body(od, id, h, bottom) {
    difference() {
        cylinder(d = od, h = h);
        translate([0, 0, bottom])
            cylinder(d = id, h = h - bottom + 0.02); // +tiny to ensure open top
    }
}

// 2D inner D shape in XY-plane: X=radial, Y=vertical (will map to Z after rotation)
// Width (X) = handle_inner_w, Height (Y) = handle_inner_h
module d2d_inner(iw, ih) {
    r = ih/2;          // semicircle radius
    d = iw - r;        // rectangle width to make D profile
    intersection() {
        // Clip to half-plane x >= 0 using a large square
        translate([250, 0]) square([500, 500], center = true);
        union() {
            // rectangle from x=0..d, y=-r..r
            translate([d/2, 0]) square([d, ih], center = true);
            // semicircle (circle) on the right
            translate([d, 0]) circle(r = r);
        }
    }
}

// Handle: create 3D by extruding the 2D D profile along Z, then rotate to make extrusion along Y
module handle(iw, ih, wall_t, depth_y, mug_outer_r, z_center, eps) {
    // Place so outer flat of the OUTER D (which is at x = -wall_t) sinks eps into mug at x = mug_outer_r - eps
    dx = mug_outer_r - eps + wall_t;

    translate([dx, 0, z_center])
        rotate([-90, 0, 0]) // Z(extrude) -> Y, Y(2D) -> -Z
            difference() {
                // Outer D: offset the inner D by wall thickness in 2D, then extrude along Z
                linear_extrude(height = depth_y, center = true)
                    offset(delta = wall_t) d2d_inner(iw, ih);
                // Subtract inner D to form the handle hole
                linear_extrude(height = depth_y + 0.4, center = true) // a bit taller for clean subtraction
                    d2d_inner(iw, ih);
            }
}

union() {
    mug_body(mug_od, mug_id, mug_h, mug_bottom);
    handle(handle_inner_w, handle_inner_h, handle_wall_t, handle_depth_y, mug_od/2, mug_h/2, attach_eps);
}
パラメータ (9)
スライダを動かすと openscad-wasm で再レンダリング

prompt

OpenSCAD で、向きが指定されたマグカップを作ってください。

本体:
- 外径 80mm、内径 70mm(肉厚 5mm)、高さ 90mm の円筒形マグ
- 底面の厚みは 6mm
- 内側は飲み物を入れられるよう中空

取手(handle):
- マグの **+X 軸方向の側面のみ** に付ける(他の方向には付けない)
- 取手は D 字形状(外側が半円、本体側が直線)で、本体外周にしっかり接続する
- 取手の内側の空間は高さ 30mm × 幅 25mm(指 3 本ぐらい入る)
- 取手はマグの高さ方向の中央付近に付ける
- 本体と取手は確実に union され、マニフォールド(隙間や宙吊りなし)を保つ

完成したコード全体を ```openscad ... ``` のフェンスで囲んで出力してください。コードのみで、追加の説明は不要です。

variant: effort

low success
low
70.3s · 273/4236t · $0.0427
SCAD (58 lines)
// Parameters
outer_d = 80;        // outer diameter (mm)
inner_d = 70;        // inner diameter (mm)
height  = 90;        // mug height (mm)
bottom_thick = 6;    // bottom thickness (mm)
r_outer = outer_d/2;
r_inner = inner_d/2;

// Handle parameters
handle_inner_w = 25; // inner space width (mm)
handle_inner_h = 30; // inner space height (mm)
handle_thick   = 5;  // ring thickness around inner space (mm)
handle_depth   = 16; // extrusion depth along Y (mm)
z_mid = height/2;    // center height

// Construct mug with handle and inner cavity
difference() {
  // Solid: body + handle
  union() {
    // Mug outer solid
    cylinder(h=height, r=r_outer, $fn=128);

    // D-shaped handle on +X side
    // Build 2D profile in X-Y (Y used as Z), then extrude along Z and rotate to Y
    // Ensure strong union by intruding flat side slightly into the mug (x0 < r_outer)
    handle_2d_zmid(z_mid);
  }

  // Hollow interior (leaves 6mm bottom)
  translate([0,0,bottom_thick])
    cylinder(h=height - bottom_thick, r=r_inner, $fn=128);
}

// Handle module
module handle_2d_zmid(zc) {
  H  = handle_inner_h + 2*handle_thick;  // outer D height
  R  = H/2;                               // radius for semicircle
  x0 = r_outer - 6;                       // flat side position (slightly inside mug to ensure union)
  rectW = handle_inner_w + 2*handle_thick;

  // 2D D-shape in X-Y (Y corresponds to Z), then extrude along Z and rotate to Y
  rotate([90,0,0])  // Z -> -Y
    linear_extrude(height=handle_depth, center=true, convexity=10)
      difference() {
        union() {
          // Flat section
          square([rectW, H], center=false)
            translate([x0, zc - H/2]);

          // Semicircular outer end
          translate([x0 + rectW + R, zc])
            circle(r=R, $fn=96);
        }
        // Inner opening (rectangular void)
        translate([x0 + handle_thick, zc - handle_inner_h/2])
          square([handle_inner_w, handle_inner_h], center=false);
      }
}
medium (default) success
medium
190.7s · 273/12031t · $0.1207
SCAD (61 lines)
// Parameters
$fn = 96;

mug_od = 80;
mug_id = 70;
mug_h  = 90;
mug_bottom = 6;

handle_inner_w = 25; // inner D width (X) = 25 mm
handle_inner_h = 30; // inner D height (Z) = 30 mm
handle_wall_t  = 5;  // 2D (XZ) wall thickness around inner D
handle_depth_y = 36; // thickness along Y (tangential)
attach_eps     = 0.2; // small overlap into mug for robust union

// Mug body: outer cylinder minus inner cavity (leaving 6mm bottom)
module mug_body(od, id, h, bottom) {
    difference() {
        cylinder(d = od, h = h);
        translate([0, 0, bottom])
            cylinder(d = id, h = h - bottom + 0.02); // +tiny to ensure open top
    }
}

// 2D inner D shape in XY-plane: X=radial, Y=vertical (will map to Z after rotation)
// Width (X) = handle_inner_w, Height (Y) = handle_inner_h
module d2d_inner(iw, ih) {
    r = ih/2;          // semicircle radius
    d = iw - r;        // rectangle width to make D profile
    intersection() {
        // Clip to half-plane x >= 0 using a large square
        translate([250, 0]) square([500, 500], center = true);
        union() {
            // rectangle from x=0..d, y=-r..r
            translate([d/2, 0]) square([d, ih], center = true);
            // semicircle (circle) on the right
            translate([d, 0]) circle(r = r);
        }
    }
}

// Handle: create 3D by extruding the 2D D profile along Z, then rotate to make extrusion along Y
module handle(iw, ih, wall_t, depth_y, mug_outer_r, z_center, eps) {
    // Place so outer flat of the OUTER D (which is at x = -wall_t) sinks eps into mug at x = mug_outer_r - eps
    dx = mug_outer_r - eps + wall_t;

    translate([dx, 0, z_center])
        rotate([-90, 0, 0]) // Z(extrude) -> Y, Y(2D) -> -Z
            difference() {
                // Outer D: offset the inner D by wall thickness in 2D, then extrude along Z
                linear_extrude(height = depth_y, center = true)
                    offset(delta = wall_t) d2d_inner(iw, ih);
                // Subtract inner D to form the handle hole
                linear_extrude(height = depth_y + 0.4, center = true) // a bit taller for clean subtraction
                    d2d_inner(iw, ih);
            }
}

union() {
    mug_body(mug_od, mug_id, mug_h, mug_bottom);
    handle(handle_inner_w, handle_inner_h, handle_wall_t, handle_depth_y, mug_od/2, mug_h/2, attach_eps);
}
high success
high
161.3s · 273/10683t · $0.1072
SCAD (53 lines)
// Parameters
$fn = 128;

outer_d = 80;
inner_d = 70;
height = 90;
bottom_thickness = 6;
outer_r = outer_d/2;
inner_r = inner_d/2;

handle_inner_h = 30;   // inner void height
handle_inner_w = 25;   // inner void width
handle_t       = 6;    // handle ring thickness
handle_depth   = 16;   // handle thickness along Y
handle_embed   = 1.0;  // embed into mug wall for solid union

eps = 0.2;

// 2D D-shape in XY (flat side at x=0, centered in Y)
module d_shape2d(w, h) {
    R = h/2;
    union() {
        translate([0, -R]) square([w - R, h], center = false);
        translate([w - R, 0]) circle(r = R);
    }
}

// 3D handle placed on +X side, centered in Z
module handle3d() {
    w_o = handle_inner_w + 2*handle_t;
    h_o = handle_inner_h + 2*handle_t;

    translate([outer_r - handle_embed, 0, height/2])
        rotate([-90, 0, 0])  // extrude along Y
            linear_extrude(height = handle_depth, center = true, convexity = 10)
                difference() {
                    d_shape2d(w_o, h_o);
                    translate([handle_t, 0]) d_shape2d(handle_inner_w, handle_inner_h);
                }
}

// Mug with handle
difference() {
    union() {
        // Outer mug body
        cylinder(r = outer_r, h = height);
        // D-shaped handle on +X side
        handle3d();
    }
    // Hollow interior, leaving 6mm bottom
    translate([0, 0, bottom_thickness])
        cylinder(r = inner_r, h = height - bottom_thickness + eps);
}
maxnot run
xhighnot run

iteration: iter-png

i1 success
iter 1
88.6s · 1706/4653t · $0.0487
diff vs bare: +0/-0
  // Parameters
  $fn = 96;
  
  mug_od = 80;
  mug_id = 70;
  mug_h  = 90;
  mug_bottom = 6;
  
  handle_inner_w = 25; // inner D width (X) = 25 mm
  handle_inner_h = 30; // inner D height (Z) = 30 mm
  handle_wall_t  = 5;  // 2D (XZ) wall thickness around inner D
  handle_depth_y = 36; // thickness along Y (tangential)
  attach_eps     = 0.2; // small overlap into mug for robust union
  
  // Mug body: outer cylinder minus inner cavity (leaving 6mm bottom)
  module mug_body(od, id, h, bottom) {
      difference() {
          cylinder(d = od, h = h);
          translate([0, 0, bottom])
              cylinder(d = id, h = h - bottom + 0.02); // +tiny to ensure open top
      }
  }
  
  // 2D inner D shape in XY-plane: X=radial, Y=vertical (will map to Z after rotation)
  // Width (X) = handle_inner_w, Height (Y) = handle_inner_h
  module d2d_inner(iw, ih) {
      r = ih/2;          // semicircle radius
      d = iw - r;        // rectangle width to make D profile
      intersection() {
          // Clip to half-plane x >= 0 using a large square
          translate([250, 0]) square([500, 500], center = true);
          union() {
              // rectangle from x=0..d, y=-r..r
              translate([d/2, 0]) square([d, ih], center = true);
              // semicircle (circle) on the right
              translate([d, 0]) circle(r = r);
          }
      }
  }
  
  // Handle: create 3D by extruding the 2D D profile along Z, then rotate to make extrusion along Y
  module handle(iw, ih, wall_t, depth_y, mug_outer_r, z_center, eps) {
      // Place so outer flat of the OUTER D (which is at x = -wall_t) sinks eps into mug at x = mug_outer_r - eps
      dx = mug_outer_r - eps + wall_t;
  
      translate([dx, 0, z_center])
          rotate([-90, 0, 0]) // Z(extrude) -> Y, Y(2D) -> -Z
              difference() {
                  // Outer D: offset the inner D by wall thickness in 2D, then extrude along Z
                  linear_extrude(height = depth_y, center = true)
                      offset(delta = wall_t) d2d_inner(iw, ih);
                  // Subtract inner D to form the handle hole
                  linear_extrude(height = depth_y + 0.4, center = true) // a bit taller for clean subtraction
                      d2d_inner(iw, ih);
              }
  }
  
  union() {
      mug_body(mug_od, mug_id, mug_h, mug_bottom);
      handle(handle_inner_w, handle_inner_h, handle_wall_t, handle_depth_y, mug_od/2, mug_h/2, attach_eps);
  }
SCAD (61 lines)
// Parameters
$fn = 96;

mug_od = 80;
mug_id = 70;
mug_h  = 90;
mug_bottom = 6;

handle_inner_w = 25; // inner D width (X) = 25 mm
handle_inner_h = 30; // inner D height (Z) = 30 mm
handle_wall_t  = 5;  // 2D (XZ) wall thickness around inner D
handle_depth_y = 36; // thickness along Y (tangential)
attach_eps     = 0.2; // small overlap into mug for robust union

// Mug body: outer cylinder minus inner cavity (leaving 6mm bottom)
module mug_body(od, id, h, bottom) {
    difference() {
        cylinder(d = od, h = h);
        translate([0, 0, bottom])
            cylinder(d = id, h = h - bottom + 0.02); // +tiny to ensure open top
    }
}

// 2D inner D shape in XY-plane: X=radial, Y=vertical (will map to Z after rotation)
// Width (X) = handle_inner_w, Height (Y) = handle_inner_h
module d2d_inner(iw, ih) {
    r = ih/2;          // semicircle radius
    d = iw - r;        // rectangle width to make D profile
    intersection() {
        // Clip to half-plane x >= 0 using a large square
        translate([250, 0]) square([500, 500], center = true);
        union() {
            // rectangle from x=0..d, y=-r..r
            translate([d/2, 0]) square([d, ih], center = true);
            // semicircle (circle) on the right
            translate([d, 0]) circle(r = r);
        }
    }
}

// Handle: create 3D by extruding the 2D D profile along Z, then rotate to make extrusion along Y
module handle(iw, ih, wall_t, depth_y, mug_outer_r, z_center, eps) {
    // Place so outer flat of the OUTER D (which is at x = -wall_t) sinks eps into mug at x = mug_outer_r - eps
    dx = mug_outer_r - eps + wall_t;

    translate([dx, 0, z_center])
        rotate([-90, 0, 0]) // Z(extrude) -> Y, Y(2D) -> -Z
            difference() {
                // Outer D: offset the inner D by wall thickness in 2D, then extrude along Z
                linear_extrude(height = depth_y, center = true)
                    offset(delta = wall_t) d2d_inner(iw, ih);
                // Subtract inner D to form the handle hole
                linear_extrude(height = depth_y + 0.4, center = true) // a bit taller for clean subtraction
                    d2d_inner(iw, ih);
            }
}

union() {
    mug_body(mug_od, mug_id, mug_h, mug_bottom);
    handle(handle_inner_w, handle_inner_h, handle_wall_t, handle_depth_y, mug_od/2, mug_h/2, attach_eps);
}
i2 success
iter 2
136.9s · 1706/7989t · $0.0820
diff vs i1: +9/-9
  // Parameters
  $fn = 96;
  
  mug_od = 80;
  mug_id = 70;
  mug_h  = 90;
  mug_bottom = 6;
  
  handle_inner_w = 25; // inner D width (X) = 25 mm
  handle_inner_h = 30; // inner D height (Z) = 30 mm
- handle_wall_t  = 5;  // 2D (XZ) wall thickness around inner D
+ handle_wall_t  = 5;  // uniform wall thickness around inner D
  handle_depth_y = 36; // thickness along Y (tangential)
- attach_eps     = 0.2; // small overlap into mug for robust union
+ handle_gap     = 0.5; // clearance between mug outer surface (R=40) and handle inner flat
  
  // Mug body: outer cylinder minus inner cavity (leaving 6mm bottom)
  module mug_body(od, id, h, bottom) {
      difference() {
          cylinder(d = od, h = h);
          translate([0, 0, bottom])
-             cylinder(d = id, h = h - bottom + 0.02); // +tiny to ensure open top
+             cylinder(d = id, h = h - bottom + 0.02); // tiny extra to ensure open top
      }
  }
  
  // 2D inner D shape in XY-plane: X=radial, Y=vertical (will map to Z after rotation)
- // Width (X) = handle_inner_w, Height (Y) = handle_inner_h
  module d2d_inner(iw, ih) {
      r = ih/2;          // semicircle radius
      d = iw - r;        // rectangle width to make D profile
      intersection() {
-         // Clip to half-plane x >= 0 using a large square
+         // Keep half-plane x >= 0
          translate([250, 0]) square([500, 500], center = true);
          union() {
              // rectangle from x=0..d, y=-r..r
              translate([d/2, 0]) square([d, ih], center = true);
              // semicircle (circle) on the right
              translate([d, 0]) circle(r = r);
          }
      }
  }
  
  // Handle: create 3D by extruding the 2D D profile along Z, then rotate to make extrusion along Y
- module handle(iw, ih, wall_t, depth_y, mug_outer_r, z_center, eps) {
-     // Place so outer flat of the OUTER D (which is at x = -wall_t) sinks eps into mug at x = mug_outer_r - eps
-     dx = mug_outer_r - eps + wall_t;
+ module handle(iw, ih, wall_t, depth_y, mug_outer_r, z_center, gap) {
+     // Place so INNER flat of the D sits at mug_outer_r + gap (clearance),
+     // and OUTER flat embeds into the mug by wall_t - gap (<= wall_t), ensuring a solid union.
+     dx = mug_outer_r + gap;
  
      translate([dx, 0, z_center])
          rotate([-90, 0, 0]) // Z(extrude) -> Y, Y(2D) -> -Z
              difference() {
                  // Outer D: offset the inner D by wall thickness in 2D, then extrude along Z
                  linear_extrude(height = depth_y, center = true)
                      offset(delta = wall_t) d2d_inner(iw, ih);
                  // Subtract inner D to form the handle hole
                  linear_extrude(height = depth_y + 0.4, center = true) // a bit taller for clean subtraction
                      d2d_inner(iw, ih);
              }
  }
  
  union() {
      mug_body(mug_od, mug_id, mug_h, mug_bottom);
-     handle(handle_inner_w, handle_inner_h, handle_wall_t, handle_depth_y, mug_od/2, mug_h/2, attach_eps);
+     handle(handle_inner_w, handle_inner_h, handle_wall_t, handle_depth_y, mug_od/2, mug_h/2, handle_gap);
  }
SCAD (61 lines)
// Parameters
$fn = 96;

mug_od = 80;
mug_id = 70;
mug_h  = 90;
mug_bottom = 6;

handle_inner_w = 25; // inner D width (X) = 25 mm
handle_inner_h = 30; // inner D height (Z) = 30 mm
handle_wall_t  = 5;  // uniform wall thickness around inner D
handle_depth_y = 36; // thickness along Y (tangential)
handle_gap     = 0.5; // clearance between mug outer surface (R=40) and handle inner flat

// Mug body: outer cylinder minus inner cavity (leaving 6mm bottom)
module mug_body(od, id, h, bottom) {
    difference() {
        cylinder(d = od, h = h);
        translate([0, 0, bottom])
            cylinder(d = id, h = h - bottom + 0.02); // tiny extra to ensure open top
    }
}

// 2D inner D shape in XY-plane: X=radial, Y=vertical (will map to Z after rotation)
module d2d_inner(iw, ih) {
    r = ih/2;          // semicircle radius
    d = iw - r;        // rectangle width to make D profile
    intersection() {
        // Keep half-plane x >= 0
        translate([250, 0]) square([500, 500], center = true);
        union() {
            // rectangle from x=0..d, y=-r..r
            translate([d/2, 0]) square([d, ih], center = true);
            // semicircle (circle) on the right
            translate([d, 0]) circle(r = r);
        }
    }
}

// Handle: create 3D by extruding the 2D D profile along Z, then rotate to make extrusion along Y
module handle(iw, ih, wall_t, depth_y, mug_outer_r, z_center, gap) {
    // Place so INNER flat of the D sits at mug_outer_r + gap (clearance),
    // and OUTER flat embeds into the mug by wall_t - gap (<= wall_t), ensuring a solid union.
    dx = mug_outer_r + gap;

    translate([dx, 0, z_center])
        rotate([-90, 0, 0]) // Z(extrude) -> Y, Y(2D) -> -Z
            difference() {
                // Outer D: offset the inner D by wall thickness in 2D, then extrude along Z
                linear_extrude(height = depth_y, center = true)
                    offset(delta = wall_t) d2d_inner(iw, ih);
                // Subtract inner D to form the handle hole
                linear_extrude(height = depth_y + 0.4, center = true) // a bit taller for clean subtraction
                    d2d_inner(iw, ih);
            }
}

union() {
    mug_body(mug_od, mug_id, mug_h, mug_bottom);
    handle(handle_inner_w, handle_inner_h, handle_wall_t, handle_depth_y, mug_od/2, mug_h/2, handle_gap);
}
i3 success
iter 3
105.7s · 1693/6256t · $0.0647
diff vs i2: +0/-0
  // Parameters
  $fn = 96;
  
  mug_od = 80;
  mug_id = 70;
  mug_h  = 90;
  mug_bottom = 6;
  
  handle_inner_w = 25; // inner D width (X) = 25 mm
  handle_inner_h = 30; // inner D height (Z) = 30 mm
  handle_wall_t  = 5;  // uniform wall thickness around inner D
  handle_depth_y = 36; // thickness along Y (tangential)
  handle_gap     = 0.5; // clearance between mug outer surface (R=40) and handle inner flat
  
  // Mug body: outer cylinder minus inner cavity (leaving 6mm bottom)
  module mug_body(od, id, h, bottom) {
      difference() {
          cylinder(d = od, h = h);
          translate([0, 0, bottom])
              cylinder(d = id, h = h - bottom + 0.02); // tiny extra to ensure open top
      }
  }
  
  // 2D inner D shape in XY-plane: X=radial, Y=vertical (will map to Z after rotation)
  module d2d_inner(iw, ih) {
      r = ih/2;          // semicircle radius
      d = iw - r;        // rectangle width to make D profile
      intersection() {
          // Keep half-plane x >= 0
          translate([250, 0]) square([500, 500], center = true);
          union() {
              // rectangle from x=0..d, y=-r..r
              translate([d/2, 0]) square([d, ih], center = true);
              // semicircle (circle) on the right
              translate([d, 0]) circle(r = r);
          }
      }
  }
  
  // Handle: create 3D by extruding the 2D D profile along Z, then rotate to make extrusion along Y
  module handle(iw, ih, wall_t, depth_y, mug_outer_r, z_center, gap) {
      // Place so INNER flat of the D sits at mug_outer_r + gap (clearance),
      // and OUTER flat embeds into the mug by wall_t - gap (<= wall_t), ensuring a solid union.
      dx = mug_outer_r + gap;
  
      translate([dx, 0, z_center])
          rotate([-90, 0, 0]) // Z(extrude) -> Y, Y(2D) -> -Z
              difference() {
                  // Outer D: offset the inner D by wall thickness in 2D, then extrude along Z
                  linear_extrude(height = depth_y, center = true)
                      offset(delta = wall_t) d2d_inner(iw, ih);
                  // Subtract inner D to form the handle hole
                  linear_extrude(height = depth_y + 0.4, center = true) // a bit taller for clean subtraction
                      d2d_inner(iw, ih);
              }
  }
  
  union() {
      mug_body(mug_od, mug_id, mug_h, mug_bottom);
      handle(handle_inner_w, handle_inner_h, handle_wall_t, handle_depth_y, mug_od/2, mug_h/2, handle_gap);
  }
SCAD (61 lines)
// Parameters
$fn = 96;

mug_od = 80;
mug_id = 70;
mug_h  = 90;
mug_bottom = 6;

handle_inner_w = 25; // inner D width (X) = 25 mm
handle_inner_h = 30; // inner D height (Z) = 30 mm
handle_wall_t  = 5;  // uniform wall thickness around inner D
handle_depth_y = 36; // thickness along Y (tangential)
handle_gap     = 0.5; // clearance between mug outer surface (R=40) and handle inner flat

// Mug body: outer cylinder minus inner cavity (leaving 6mm bottom)
module mug_body(od, id, h, bottom) {
    difference() {
        cylinder(d = od, h = h);
        translate([0, 0, bottom])
            cylinder(d = id, h = h - bottom + 0.02); // tiny extra to ensure open top
    }
}

// 2D inner D shape in XY-plane: X=radial, Y=vertical (will map to Z after rotation)
module d2d_inner(iw, ih) {
    r = ih/2;          // semicircle radius
    d = iw - r;        // rectangle width to make D profile
    intersection() {
        // Keep half-plane x >= 0
        translate([250, 0]) square([500, 500], center = true);
        union() {
            // rectangle from x=0..d, y=-r..r
            translate([d/2, 0]) square([d, ih], center = true);
            // semicircle (circle) on the right
            translate([d, 0]) circle(r = r);
        }
    }
}

// Handle: create 3D by extruding the 2D D profile along Z, then rotate to make extrusion along Y
module handle(iw, ih, wall_t, depth_y, mug_outer_r, z_center, gap) {
    // Place so INNER flat of the D sits at mug_outer_r + gap (clearance),
    // and OUTER flat embeds into the mug by wall_t - gap (<= wall_t), ensuring a solid union.
    dx = mug_outer_r + gap;

    translate([dx, 0, z_center])
        rotate([-90, 0, 0]) // Z(extrude) -> Y, Y(2D) -> -Z
            difference() {
                // Outer D: offset the inner D by wall thickness in 2D, then extrude along Z
                linear_extrude(height = depth_y, center = true)
                    offset(delta = wall_t) d2d_inner(iw, ih);
                // Subtract inner D to form the handle hole
                linear_extrude(height = depth_y + 0.4, center = true) // a bit taller for clean subtraction
                    d2d_inner(iw, ih);
            }
}

union() {
    mug_body(mug_od, mug_id, mug_h, mug_bottom);
    handle(handle_inner_w, handle_inner_h, handle_wall_t, handle_depth_y, mug_od/2, mug_h/2, handle_gap);
}