openscad
使用OpenSCAD(基于脚本的实体CAD建模器)创建参数化3D模型。当用户要求创建3D打印对象、参数化设计、机械部件、CAD模型或请求.scad文件时使用。OpenSCAD使用构造实体几何(CSG)和2D形状的挤压通过代码创建3D模型。
📖 中文导读:使用OpenSCAD(基于脚本的实体CAD建模器)创建参数化3D模型。当用户要求创建3D打印对象、参数化设计、机械部件、CAD模型或请求.scad文件时使用。OpenSCAD使用构造实体几何(CSG)和2D形状的挤压通过代码创建3D模型。 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。
name: openscad description: Create parametric 3D models using OpenSCAD, a script-based solid CAD modeler. Use when the user asks to create 3D printable objects, parametric designs, mechanical parts, CAD models, or requests .scad files. OpenSCAD uses constructive solid geometry (CSG) and extrusion of 2D shapes to create 3D models through code.
OpenSCAD Skill
Generate OpenSCAD (.scad) files for parametric 3D modeling. OpenSCAD is a functional programming language for creating solid 3D CAD objects using CSG (Constructive Solid Geometry).
Core Concepts
OpenSCAD scripts describe geometry through:
- Primitives - Basic 2D/3D shapes (cube, sphere, cylinder, circle, square, polygon)
- Transformations - Position/modify objects (translate, rotate, scale, mirror, color)
- Boolean Operations - Combine shapes (union, difference, intersection)
- Modules - Reusable parametric components
- Extrusion - Convert 2D to 3D (linear_extrude, rotate_extrude)
Workflow
- Define parameters as variables at the top for easy customization
- Create modules for reusable components
- Build geometry using primitives and boolean operations
- Use comments to explain complex sections
- Set
$fnfor curved surface resolution (higher = smoother but slower)
Quick Reference
3D Primitives
cube([x, y, z], center=false);
cube(size, center=false);
sphere(r=radius); // or d=diameter
cylinder(h=height, r=radius, center=false);
cylinder(h=height, r1=bottom_r, r2=top_r); // cone
polyhedron(points=[[x,y,z],...], faces=[[p0,p1,p2],...]);
2D Primitives
square([x, y], center=false);
circle(r=radius); // or d=diameter
polygon(points=[[x,y],...]);
text("string", size=10, font="Liberation Sans");
Transformations
translate([x, y, z]) object();
rotate([x_deg, y_deg, z_deg]) object();
rotate(a=degrees, v=[x,y,z]) object(); // rotate around axis
scale([x, y, z]) object();
mirror([x, y, z]) object(); // mirror across plane
color("red") object();
color([r, g, b, a]) object(); // 0-1 values
Boolean Operations
union() { obj1(); obj2(); } // combine (implicit if no operator)
difference() { base(); cut1(); } // subtract from first child
intersection() { obj1(); obj2(); } // keep only overlap
Extrusion (2D to 3D)
linear_extrude(height=h, twist=deg, scale=s, slices=n, center=false)
2d_shape();
rotate_extrude(angle=360, $fn=n)
2d_shape(); // shape must be on positive X side
Control Structures
// Loops
for (i = [0:10]) translate([i*5, 0, 0]) cube(3);
for (i = [0:2:10]) ...; // start:step:end
for (pos = [[0,0], [10,5], [5,10]]) translate(pos) sphere(2);
// Conditionals
if (condition) { ... } else { ... }
variable = condition ? value_if_true : value_if_false;
Modules and Functions
// Module (creates geometry)
module my_part(size=10, holes=true) {
difference() {
cube(size);
if (holes) cylinder(h=size+1, r=size/4, center=true);
}
}
my_part(20, holes=false);
// Function (returns value)
function circumference(r) = 2 * PI * r;
Special Variables
$fn = 100; // fragments for full circle (overrides $fa/$fs)
$fa = 12; // minimum angle per fragment
$fs = 2; // minimum size per fragment
$preview // true during F5 preview, false during F6 render
Best Practices
Parametric Design
// Define all dimensions as variables
wall_thickness = 2;
inner_diameter = 20;
height = 30;
// Derive other values
outer_diameter = inner_diameter + 2*wall_thickness;
Avoiding Rendering Issues
// Use epsilon to prevent z-fighting in boolean operations
eps = 0.01;
difference() {
cube([10, 10, 10]);
translate([2, 2, -eps])
cylinder(h=10 + 2*eps, r=3); // slightly taller than parent
}
Manifold Geometry for 3D Printing
- Ensure all geometry is watertight (no gaps)
- Holes must fully penetrate surfaces
- Use
render()to verify complex geometry - Check face orientation with F12 (Thrown Together view)
Detailed References
- For complete syntax: See
references/language_reference.md - For example patterns: See
references/examples.md
Output
Save generated code as .scad files. Users can open in OpenSCAD for:
- F5: Quick preview
- F6: Full render (required before export)
- Export to STL/AMF/3MF for 3D printing