2021-08-04 14:00:11 +03:00
|
|
|
---
|
|
|
|
language: openscad
|
2022-07-24 00:53:11 +03:00
|
|
|
filename: learnopenscad.scad
|
2021-08-04 14:00:11 +03:00
|
|
|
contributors:
|
2022-07-24 01:05:01 +03:00
|
|
|
- ["Tom Preston", "https://github.com/tompreston/"]
|
2021-08-04 14:00:11 +03:00
|
|
|
---
|
|
|
|
|
|
|
|
Draw 3D models with code using [OpenSCAD](https://openscad.org/).
|
|
|
|
|
2023-03-01 03:00:37 +03:00
|
|
|
```
|
2022-07-24 01:05:01 +03:00
|
|
|
// Comments look like this
|
2021-08-04 14:00:11 +03:00
|
|
|
|
2022-07-24 01:05:01 +03:00
|
|
|
// 3D Primitives
|
2021-08-04 14:00:11 +03:00
|
|
|
cube(10);
|
|
|
|
cube([5, 10, 20]);
|
|
|
|
sphere(10);
|
|
|
|
|
2022-07-24 01:05:01 +03:00
|
|
|
// Transformations
|
2021-08-04 14:00:11 +03:00
|
|
|
translate([20, 0, 0]) cube(10);
|
|
|
|
rotate([0, 20, 30]) cube(10);
|
|
|
|
|
|
|
|
translate([20, 0, 0]) rotate([0, 20, 30]) cube(10);
|
|
|
|
rotate([0, 20, 30]) translate([20, 0, 0]) cube(10);
|
|
|
|
|
2022-07-24 01:05:01 +03:00
|
|
|
// Modifiers
|
|
|
|
//
|
|
|
|
// * disable
|
|
|
|
// ! show only
|
|
|
|
// # highlight / debug
|
|
|
|
// % transparent / background
|
|
|
|
//
|
|
|
|
// For example, show only the rotated cube at the origin, before we translate it.
|
2021-08-04 14:00:11 +03:00
|
|
|
translate([20, 0, 0]) !rotate([0, 20, 30]) cube(10);
|
|
|
|
|
2022-07-24 01:05:01 +03:00
|
|
|
// Formatting
|
|
|
|
// The following models are the same. The official docs prefer the second.
|
2021-08-04 14:00:11 +03:00
|
|
|
rotate([0, 20, 30]) translate([20, 0, 0]) cube(10);
|
|
|
|
|
|
|
|
rotate([0, 20, 30])
|
|
|
|
translate([20, 0, 0])
|
|
|
|
cube(10);
|
|
|
|
|
|
|
|
rotate([0, 20, 30]) {
|
|
|
|
translate([20, 0, 0]) {
|
|
|
|
cube(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 01:05:01 +03:00
|
|
|
// Loops
|
2021-08-04 14:00:11 +03:00
|
|
|
num_cubes = 5;
|
|
|
|
r = 20;
|
|
|
|
cube_len = 5;
|
|
|
|
|
|
|
|
for (i = [0:num_cubes]) {
|
|
|
|
echo(str("Plot cube ", i));
|
|
|
|
rotate([0, i * 360 / num_cubes, 0])
|
|
|
|
translate([r, 0, 0])
|
|
|
|
cube(cube_len, center=true);
|
|
|
|
}
|
|
|
|
|
2022-07-24 01:05:01 +03:00
|
|
|
// Boolean operations
|
|
|
|
//
|
|
|
|
// union() - the sum of both shapes
|
|
|
|
// difference() - the first shape, minus the second shape
|
|
|
|
// intersection() - only parts of both shapes which intersect
|
|
|
|
//
|
2021-08-04 14:00:11 +03:00
|
|
|
cube_l = 20;
|
|
|
|
cube_w = 10;
|
|
|
|
cube_h = 10;
|
|
|
|
|
|
|
|
hole_pos_l = 10;
|
|
|
|
hole_pos_h = 5;
|
|
|
|
hole_r = 3;
|
|
|
|
|
|
|
|
difference() {
|
|
|
|
cube([cube_l, cube_w, cube_h]);
|
|
|
|
translate([hole_pos_l, 0, hole_pos_h])
|
|
|
|
rotate([-90, 0, 0])
|
|
|
|
cylinder(cube_w, r=hole_r);
|
|
|
|
}
|
|
|
|
|
2022-07-24 01:05:01 +03:00
|
|
|
// Functions calculate values
|
2021-08-04 14:00:11 +03:00
|
|
|
function inch2mm(i) = i * 25.4;
|
|
|
|
|
|
|
|
cube(inch2mm(2));
|
|
|
|
|
2022-07-24 01:05:01 +03:00
|
|
|
// Modules create objects you want to use later
|
2021-08-04 14:00:11 +03:00
|
|
|
module house(roof="flat", paint=[1,0,0]) {
|
|
|
|
color(paint)
|
|
|
|
if (roof=="flat") {
|
|
|
|
translate([0,-1,0]) cube();
|
|
|
|
} else if (roof=="pitched") {
|
|
|
|
rotate([90,0,0])
|
|
|
|
linear_extrude(height=1)
|
|
|
|
polygon(points=[[0,0],[0,1],[0.5,1.5],[1,1],[1,0]]);
|
|
|
|
}
|
|
|
|
else if (roof=="domical") {
|
|
|
|
translate([0,-1,0]) {
|
|
|
|
translate([0.5,0.5,1])
|
|
|
|
sphere(r=0.5,$fn=20);
|
2022-07-24 01:05:01 +03:00
|
|
|
cube();
|
2021-08-04 14:00:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
house("pitched");
|
|
|
|
translate([2, 0, 0]) house("domical");
|
|
|
|
|
2022-07-24 01:05:01 +03:00
|
|
|
// Import modules and function from other files
|
|
|
|
include <filename> // Import the content of the file as if they were written in this file
|
|
|
|
use <filename> // Import modules and functions, but do not execute any commands
|
2021-08-04 14:00:11 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
## Further Reading
|
2024-06-03 22:31:20 +03:00
|
|
|
|
|
|
|
* Official docs [openscad.org/documentation.html](https://openscad.org/documentation.html)
|
|
|
|
* Cheat sheet [openscad.org/cheatsheet/index.html](https://openscad.org/cheatsheet/index.html)
|
|
|
|
* Vim bindings [github.com/sirtaj/vim-openscad](https://github.com/sirtaj/vim-openscad)
|