o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Write an OpenSCAD Script that creates a rectangle shape with 4mm thickness and rounded…
- User
Write an OpenSCAD Script that creates a rectangle shape with 4mm thickness and rounded edges.
- Assistant
Write an OpenSCAD Script that creates a rectangle shape with 4mm thickness and rounded edges.
Sure! Below is an OpenSCAD script that creates a 3D rectangular frame with rounded edges and a 4mm thickness. You can customize the dimensions and corner radius as needed.
// =============================
// Parameters
// =============================
width = 100; // Total width of the rectangle (mm)
height = 60; // Total height of the rectangle (mm)
corner_radius = 10; // Radius of the rounded corners (mm)
frame_thickness = 4; // Thickness of the frame (mm)
extrude_height = 5; // Height to extrude the 2D shape into 3D (mm)
// =============================
// Module: Rounded Rectangle
// =============================
/*
* Creates a 2D rounded rectangle centered at the origin.
* Parameters:
* w - width of the rectangle
* h - height of the rectangle
* r - radius of the corners
*/
module rounded_rect(w, h, r) {
// Ensure the corner radius does not exceed half of width or height
r = min(r, w/2, h/2);
// Create the rounded rectangle using hull of circles at corners
hull() {
for (x = [-w/2 + r, w/2 - r])
for (y = [-h/2 + r, h/2 - r])
translate([x, y])
circle(r=r, $fn=32);
}
}
// =============================
// Main 3D Object
// =============================
difference() {
// Extrude the outer rounded rectangle
linear_extrude(height = extrude_height)
rounded_rect(width, height, corner_radius);
// Extrude the inner rounded rectangle to subtract and create the frame
linear_extrude(height = extrude_height)
rounded_rect(width - 2*frame_thickness, height - 2*frame_thickness, max(corner_radius - frame_thickness, 0));
}
// =============================
// (Optional) Preview Settings
// =============================
// Uncomment the following lines to see a wireframe and a colored model
/*
color([0.8, 0.2, 0.2])
difference() {
linear_extrude(height = extrude_height)
rounded_rect(width, height, corner_radius);
linear_extrude(height = extrude_height)
rounded_rect(width - 2*frame_thickness, height - 2*frame_thickness, max(corner_radius - frame_thickness, 0));
}
translate([150, 0, 0])
color([0.2, 0.8, 0.2])
difference() {
linear_extrude(height = extrude_height)
rounded_rect(width, height, corner_radius);
linear_extrude(height = extrude_height)
rounded_rect(width - 2*frame_thickness, height - 2*frame_thickness, max(corner_radius - frame_thickness, 0));
}
*/
Parameters Section:
Module rounded_rect:
hull() function to create smooth, rounded corners by enclosing circles at each corner.Main 3D Object:
difference(): This operation subtracts the inner shape from the outer shape to create a frame.Optional Preview Settings:
width, height, and corner_radius variables to fit your specific needs.frame_thickness to make the frame thicker or thinner. Ensure that corner_radius remains larger than half of frame_thickness to maintain rounded edges.extrude_height determines how "tall" the frame is. For a flat 2D model, set this to a minimal value like 1 mm.When you open this script in OpenSCAD and render it (by pressing F5 for preview or F6 for a full render), you'll see a 3D rectangular frame with smoothly rounded edges and a uniform thickness of 4mm.
$fn=32 parameter in the circle() function determines the smoothness of the rounded corners. Increasing this number will make the corners smoother but may increase render time.$fn) to balance between visual quality and performance.Feel free to modify and experiment with the parameters to achieve the exact design you need!
Not yet included in a published release.