Skip to content

Instantly share code, notes, and snippets.

@tdeck
Last active March 1, 2025 23:03
Show Gist options
  • Save tdeck/8c29dffe5429d4b1ffb05c1446a10d43 to your computer and use it in GitHub Desktop.
Save tdeck/8c29dffe5429d4b1ffb05c1446a10d43 to your computer and use it in GitHub Desktop.
Extracts a rectangular portion of an STL model
// You'll need to update all the constants until
// the line "END OF CONSTANTS".
stl_file = "TODO.stl";
// mesh_size and mesh_center info are critical; you can
// get them from 3dm info in 3dmake 0.4.1 and later
mesh_size = [1, 1, 1];
mesh_center = [1, 1, 1];
// Now define the bounds. There are some helpful functions
// you can use.
// mm_from_top / mm_from_bottom / mm_from_left / etc...
// percent_from_top / percent_from_bottom / percent_from_left / etc...
// percent functions take a number from 0 to 100
left_bound = mm_from_left(0);
right_bound = mm_from_right(0);
front_bound = mm_from_front(0);
back_bound = mm_from_back(0);
top_bound = mm_from_top(0);
bottom_bound = mm_from_bottom(0);
// END OF CONSTANTS
// You should not need to change anything below
function mm_from_left(x) = mesh_center[0] - mesh_size[0] / 2 + x;
function mm_from_right(x) = mesh_center[0] + mesh_size[0] / 2 - x;
function mm_from_front(x) = (mesh_center[1] - mesh_size[1] / 2) + x;
function mm_from_back(x) = mesh_center[1] + mesh_size[1] / 2 - x;
function mm_from_bottom(x) = mesh_center[2] - mesh_size[2] / 2 + x;
function mm_from_top(x) = mesh_center[2] + mesh_size[2] / 2 - x;
function percent_from_left(x) = mesh_center[0] - mesh_size[0] / 2 + x / 100 * mesh_size[0];
function percent_from_right(x) = mesh_center[0] + mesh_size[0] / 2 - x / 100 * mesh_size[0];
function percent_from_front(x) = mesh_center[1] - mesh_size[1] / 2 + x / 100 * mesh_size[1];
function percent_from_back(x) = mesh_center[1] + mesh_size[1] / 2 - x / 100 * mesh_size[1];
function percent_from_bottom(x) = (mesh_center[2] - mesh_size[2] / 2) + (x / 100 * mesh_size[2]);
function percent_from_top(x) = (mesh_center[2] + mesh_size[2] / 2) - (x / 100 * mesh_size[2]);
// Actual chunk cut operation
_ARBITRARY = 10000;
difference() { // You might think I could use an intersection, but that yields invalid mdels
import(stl_file);
// Left bound
translate([-_ARBITRARY/2 + left_bound, 0, 0])
cube([_ARBITRARY, _ARBITRARY, _ARBITRARY], center=true);
// Right bound
translate([_ARBITRARY/2 + right_bound, 0, 0])
cube([_ARBITRARY, _ARBITRARY, _ARBITRARY], center=true);
// Front bound
translate([0, -_ARBITRARY/2 + front_bound, 0])
cube([_ARBITRARY, _ARBITRARY, _ARBITRARY], center=true);
// Back bound
translate([0, _ARBITRARY/2 + back_bound, 0])
cube([_ARBITRARY, _ARBITRARY, _ARBITRARY], center=true);
// Bottom bound
translate([0, 0, -_ARBITRARY/2 + bottom_bound])
cube([_ARBITRARY, _ARBITRARY, _ARBITRARY], center=true);
// Top bound
translate([0, 0, _ARBITRARY/2 + top_bound])
cube([_ARBITRARY, _ARBITRARY, _ARBITRARY], center=true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment