From 5439b1ee64aff0cb997336b48c0ad3a9614d2ab8 Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Sat, 19 Mar 2022 14:48:16 -0400 Subject: [PATCH] add color_this() module --- attachments.scad | 58 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/attachments.scad b/attachments.scad index 6d29d54..be9d8b7 100644 --- a/attachments.scad +++ b/attachments.scad @@ -17,6 +17,8 @@ $tags = ""; $overlap = 0; $color = undef;//"yellow"; +$save_color = undef; // Saved color to revert back for children +_DEFAULT_COLOR = "gold"; $attach_to = undef; $attach_anchor = [CENTER, CENTER, UP, 0]; @@ -810,23 +812,59 @@ module hulling(a) // Module: recolor() // Usage: -// recolor(c) {...} +// recolor([c]) {...} // Topics: Attachments -// See Also: tags(), hide(), show(), diff(), intersect() +// See Also: color_this(), tags(), hide(), show(), diff(), intersect() // Description: -// Sets the color for children that can use the $color special variable. For a more step-by-step -// explanation of attachments, see the [[Attachments Tutorial|Tutorial-Attachments]]. +// Sets the color for children that can use the $color special variable. For this to work +// you cannot have any color() modules above it in any parents, only other recolor() or color_this() modules. +// Not ethat recolor() affects its children and all their descendants. +// For a more step-by-step explanation of attachments, see the [[Attachments Tutorial|Tutorial-Attachments]]. // Arguments: -// c = Color name or RGBA vector. +// c = Color name or RGBA vector. Default: The standard OpenSCAD yellow // Example: -// recolor("red") cyl(l=20, d=10); -module recolor(c) +// cuboid([10,10,5]) +// recolor("green")attach(TOP,BOT) cuboid([9,9,4.5]) +// attach(TOP,BOT) cuboid([8,8,4]) +// recolor("purple") attach(TOP,BOT) cuboid([7,7,3.5]) +// attach(TOP,BOT) cuboid([6,6,3]) +// recolor("cyan")attach(TOP,BOT) cuboid([5,5,2.5]) +// attach(TOP,BOT) cuboid([4,4,2]); +module recolor(c=_DEFAULT_COLOR) { $color = c; children(); } +// Module: color_this() +// Usage: +// color_this([c]) {...} +// Topics: Attachments +// See Also: tags(), recolor() +// Description: +// Sets the color for children at one level, reverting to the previous color for further descendants. +// This works only with attachables and you cannot have any color() modules above it in any parents, +// only recolor() or other color_this() modules. +// For a more step-by-step explanation of attachments, see the [[Attachments Tutorial|Tutorial-Attachments]]. +// Arguments: +// c = Color name or RGBA vector. Default: the standard OpenSCAD yellow +// Example: +// cuboid([10,10,5]) +// color_this("green")attach(TOP,BOT) cuboid([9,9,4.5]) +// attach(TOP,BOT) cuboid([8,8,4]) +// color_this("purple") attach(TOP,BOT) cuboid([7,7,3.5]) +// attach(TOP,BOT) cuboid([6,6,3]) +// color_this("cyan")attach(TOP,BOT) cuboid([5,5,2.5]) +// attach(TOP,BOT) cuboid([4,4,2]); +module color_this(c=_DEFAULT_COLOR) +{ + $save_color=default($color, _DEFAULT_COLOR); + $color=c; + children(); +} + + // Module: hide() // Usage: // hide(tags) {...} @@ -1383,11 +1421,15 @@ module attachable( children(0); } } + if (is_def($save_color)) { + $color=$save_color; + $save_color=undef; + children(1); + } else children(1); } } - // Function: reorient() // // Usage: Square/Trapezoid Geometry