OpenSCAD

Личный сайт Go-разработчика из Казани

Draw 3D models with code using OpenSCAD.

1// Comments look like this 2 3// 3D Primitives 4cube(10); 5cube([5, 10, 20]); 6sphere(10); 7 8// Transformations 9translate([20, 0, 0]) cube(10); 10rotate([0, 20, 30]) cube(10); 11 12translate([20, 0, 0]) rotate([0, 20, 30]) cube(10); 13rotate([0, 20, 30]) translate([20, 0, 0]) cube(10); 14 15// Modifiers 16// 17// * disable 18// ! show only 19// # highlight / debug 20// % transparent / background 21// 22// For example, show only the rotated cube at the origin, before we translate it. 23translate([20, 0, 0]) !rotate([0, 20, 30]) cube(10); 24 25// Formatting 26// The following models are the same. The official docs prefer the second. 27rotate([0, 20, 30]) translate([20, 0, 0]) cube(10); 28 29rotate([0, 20, 30]) 30 translate([20, 0, 0]) 31 cube(10); 32 33rotate([0, 20, 30]) { 34 translate([20, 0, 0]) { 35 cube(10); 36 } 37} 38 39// Loops 40num_cubes = 5; 41r = 20; 42cube_len = 5; 43 44for (i = [0:num_cubes]) { 45 echo(str("Plot cube ", i)); 46 rotate([0, i * 360 / num_cubes, 0]) 47 translate([r, 0, 0]) 48 cube(cube_len, center=true); 49} 50 51// Boolean operations 52// 53// union() - the sum of both shapes 54// difference() - the first shape, minus the second shape 55// intersection() - only parts of both shapes which intersect 56// 57cube_l = 20; 58cube_w = 10; 59cube_h = 10; 60 61hole_pos_l = 10; 62hole_pos_h = 5; 63hole_r = 3; 64 65difference() { 66 cube([cube_l, cube_w, cube_h]); 67 translate([hole_pos_l, 0, hole_pos_h]) 68 rotate([-90, 0, 0]) 69 cylinder(cube_w, r=hole_r); 70} 71 72// Functions calculate values 73function inch2mm(i) = i * 25.4; 74 75cube(inch2mm(2)); 76 77// Modules create objects you want to use later 78module house(roof="flat", paint=[1,0,0]) { 79 color(paint) 80 if (roof=="flat") { 81 translate([0,-1,0]) cube(); 82 } else if (roof=="pitched") { 83 rotate([90,0,0]) 84 linear_extrude(height=1) 85 polygon(points=[[0,0],[0,1],[0.5,1.5],[1,1],[1,0]]); 86 } 87 else if (roof=="domical") { 88 translate([0,-1,0]) { 89 translate([0.5,0.5,1]) 90 sphere(r=0.5,$fn=20); 91 cube(); 92 } 93 } 94} 95 96house("pitched"); 97translate([2, 0, 0]) house("domical"); 98 99// Import modules and function from other files 100include <filename> // Import the content of the file as if they were written in this file 101use <filename> // Import modules and functions, but do not execute any commands

Further Reading