Skip to content

Instantly share code, notes, and snippets.

@t-paul
Created September 21, 2015 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-paul/58a497fdea61ce4ca568 to your computer and use it in GitHub Desktop.
Save t-paul/58a497fdea61ce4ca568 to your computer and use it in GitHub Desktop.
// Enable animation, e.g. FPS = 20, Steps = 500
$fa = 1;
$fs = 1;
$vpd = 600; // fixed distance, comment to scale the viewport
size = 20;
// --- helper functions for animation ----------------------------------------
/*
* Make object visible only in a given animation
* time frame, the default is always visible.
*
* d(s = 0.2) - show from 0.2 to 1
* d(e = 0.8) - show from 0 to 0.8
* d(s = 0.2, e = 0.8) - show from 0.2 to 0.8
*
* Example:
*
* show cube only for the first half of the animation
* d(e = 0.5) cube(10);
*/
module d(s = 0, e = 1) {
if (($t >= s) && ($t <= e)) {
children();
}
}
/*
* Function to define the movement, using cos() the
* object starts fast and slows down before reaching
* the final location.
*/
function f(x) = 1 - cos(90 * (1 - x));
/*
* Move an object from a given offset to it's final location
* using the animation feature (variable $t).
*
* Before and while moving, the object is highlighted using the
* # modifier.
*
* The parameters are:
* start_time: before this time the object is at the initial location
* end_time: after this time the object is at the final location
* offset: initial location defined as offset from the final location
*/
module m(start_time, end_time, offset) {
if ($t < start_time) {
#translate(offset) children();
} else if ($t >= end_time) {
children();
} else {
factor = ($t - start_time) / (end_time - start_time);
#translate(f(factor) * offset) children();
}
}
/*
* Show some text rotated to align with the viewport.
*/
module t(t) {
%translate([0, 0, -100])
rotate($vpr)
linear_extrude(0.1)
text(text = t, halign = "center");
}
// --- model base modules ----------------------------------------------------
module cub() {
cube(size, center = true);
}
module cyl() {
cylinder(r = 0.4 * size, h = 1.5 * size, center = true);
}
// --- model with animation --------------------------------------------------
module cube_with_holes() {
difference() {
cub();
d(s = 0.2) m(0.2, 0.4, [0, 0, 100]) cyl();
d(s = 0.4) m(0.4, 0.6, [0, 100, 0]) rotate([90, 0, 0]) cyl();
d(s = 0.6) m(0.6, 0.8, [100, 0, 0]) rotate([0, 90, 0]) cyl();
}
}
module model() {
intersection() {
cube_with_holes();
m(0, 0.2, [-100, 0, 0]) sphere(0.75 * size);
}
}
model();
// --- additional helper stuff -----------------------------------------------
d(e = 0.19) { %cub(); } // make cube visible
d(e = 0.2) { t("intersection"); }
d(s = 0.2, e = 0.8) { t("difference"); }
d(s = 0.8) { t("final assembly"); }
echo(version=version());
// Written in 2015 by Torsten Paul <Torsten.Paul@gmx.de>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any
// warranty.
//
// You should have received a copy of the CC0 Public Domain
// Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment