BOSL2/tutorials/Attachments.md

1640 lines
51 KiB
Markdown
Raw Normal View History

2021-04-12 07:41:12 +00:00
# Attachments Tutorial
<!-- TOC -->
## Attachables
2022-11-11 02:38:41 +00:00
BOSL2 introduces the concept of attachables. You can do the following
things with attachable shapes:
2022-11-13 04:28:59 +00:00
* Control where the shape appears and how it is oriented by anchoring and specifying orientation and spin
2022-11-11 02:38:41 +00:00
* Position or attach shapes relative to parent objects
2023-03-10 01:49:18 +00:00
* Tag objects and then control boolean operations based on their tags.
* Change the color of objects so that child objects are different colors than their parents
2022-11-11 02:38:41 +00:00
The various attachment features may seem complex at first, but
attachability is one of the most important features of the BOSL2
library. It enables you to position objects relative to other objects
in your model instead of having to keep track of absolute positions.
It makes models simpler, more intuitive, and easier to maintain.
Almost all objects defined by BOSL2 are attachable. In addition,
BOSL2 overrides the built-in definitions for `cube()`, `cylinder()`,
2023-03-10 01:49:18 +00:00
`sphere()`, `square()`, `circle()` and `text()` and makes them attachable as
well. However, some basic OpenSCAD built-in definitions are not
attachable and will not work with the features described in this
tutorial. The non-attachables are `polyhedron()`, `linear_extrude()`,
`rotate_extrude()`, `surface()`, `projection()` and `polygon()`.
Some of these have attachable alternatives: `vnf_polyhedron()`,
`linear_sweep()`, `rotate_sweep()`, and `region()`.
2021-04-12 07:41:12 +00:00
## Anchoring
2022-11-11 02:38:41 +00:00
Anchoring allows you to align a specified part of an object or point
on an object with the origin. The alignment point can be the center
of a side, the center of an edge, a corner, or some other
2022-11-13 16:40:07 +00:00
distinguished point on the object. This is done by passing a vector
or text string into the `anchor=` argument. For roughly cubical
2021-04-12 07:41:12 +00:00
or prismoidal shapes, that vector points in the general direction of the side, edge, or
2021-09-30 09:04:31 +00:00
corner that will be aligned to. For example, a vector of [1,0,-1] refers to the lower-right
edge of the shape. Each vector component should be -1, 0, or 1:
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-09-30 09:04:31 +00:00
// Anchor at upper-front-left corner
2021-04-12 07:41:12 +00:00
cube([40,30,50], anchor=[-1,-1,1]);
```
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-09-30 09:04:31 +00:00
// Anchor at upper-right edge
2021-04-12 07:41:12 +00:00
cube([40,30,50], anchor=[1,0,1]);
```
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-09-30 09:04:31 +00:00
// Anchor at bottom face
2021-04-12 07:41:12 +00:00
cube([40,30,50], anchor=[0,0,-1]);
```
Since manually written vectors are not very intuitive, BOSL2 defines some standard directional
vector constants that can be added together:
Constant | Direction | Value
-------- | --------- | -----------
`LEFT` | X- | `[-1, 0, 0]`
`RIGHT` | X+ | `[ 1, 0, 0]`
2022-11-13 16:40:07 +00:00
`FRONT`/`FORWARD`/`FWD` | Y | `[ 0, 1, 0]`
2021-04-12 07:41:12 +00:00
`BACK` | Y+ | `[ 0, 1, 0]`
2022-11-13 16:40:07 +00:00
`BOTTOM`/`BOT`/`DOWN` | Z (Y in 2D) | `[ 0, 0, 1]` (`[0, 1]` in 2D.)
`TOP`/`UP` | Z+ (Y+ in 2D) | `[ 0, 0, 1]` (`[0, 1]` in 2D.)
2021-04-12 07:41:12 +00:00
`CENTER`/`CTR` | Centered | `[ 0, 0, 0]`
2022-11-13 16:40:07 +00:00
If you want a vector pointing towards the bottomleft edge, just add the `BOTTOM` and `LEFT` vector
constants together like `BOTTOM + LEFT`. This will result in a vector of `[1,0,1]`. You can pass
2022-11-13 04:28:59 +00:00
that to the `anchor=` argument for a clearly understandable anchoring:
2021-10-07 04:40:47 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube([40,30,50], anchor=BACK+TOP);
```
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube([40,30,50], anchor=FRONT);
```
2021-09-30 09:04:31 +00:00
---
2022-11-13 16:40:07 +00:00
For cylindrical type attachables, the Z component of the vector will be 1, 0, or 1, referring
2021-10-07 04:40:47 +00:00
to the bottom rim, the middle side, or the top rim of the cylindrical or conical shape.
2022-11-13 16:40:07 +00:00
The X and Y components can be any value, pointing towards the circular perimeter of the cone.
2021-10-07 04:40:47 +00:00
These combined let you point at any place on the bottom or top rims, or at an arbitrary
side wall:
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cylinder(r1=25, r2=15, h=60, anchor=TOP+LEFT);
```
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cylinder(r1=25, r2=15, h=60, anchor=BOTTOM+FRONT);
```
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cylinder(r1=25, r2=15, h=60, anchor=UP+spherical_to_xyz(1,30,90));
```
2021-09-30 09:04:31 +00:00
---
2021-10-07 04:40:47 +00:00
For Spherical type attachables, you can pass a vector that points at any arbitrary place on
the surface of the sphere:
2022-11-11 02:38:41 +00:00
p
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
sphere(r=50, anchor=TOP);
```
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
sphere(r=50, anchor=TOP+FRONT);
```
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
sphere(r=50, anchor=spherical_to_xyz(1,-30,60));
```
2021-09-30 09:04:31 +00:00
---
2021-04-12 07:41:12 +00:00
Some attachable shapes may provide specific named anchors for shape-specific anchoring. These
2022-11-13 16:40:07 +00:00
will be given as strings and will be specific to that type of
attachable. When named anchors are supported, they are listed in a
"Named Anchors" section of the documentation for the module. The
`teardrop()` attachable, for example, has a named anchor called "cap" and in 2D the
`star()` attachable has anchors labeled by tip number:
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
teardrop(d=100, l=20, anchor="cap");
```
2022-11-13 16:40:07 +00:00
```openscad-2D
include <BOSL2/std.scad>
star(n=7, od=30, id=20, anchor="tip2");
```
2021-09-30 09:04:31 +00:00
---
2022-11-13 04:28:59 +00:00
Some shapes, for backwards compatibility reasons, can take a `center=` argument. This just
2021-04-12 07:41:12 +00:00
overrides the `anchor=` argument. A `center=true` argument is the same as `anchor=CENTER`.
2022-11-13 16:40:07 +00:00
A `center=false` argument chooses the anchor to match the behavior of
the builtin version: for a cube it is the same as `anchor=[-1,-1,-1]` but for a
cylinder, it is the same as `anchor=BOTTOM`.
2021-10-07 04:40:47 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-10-07 04:40:47 +00:00
cube([50,40,30],center=true);
```
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-10-07 04:40:47 +00:00
cube([50,40,30],center=false);
```
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
---
2022-11-11 02:38:41 +00:00
Most 2D shapes provided by BOSL2 are also anchorable. The built-in `square()` and `circle()`
2022-11-13 16:40:07 +00:00
modules have been overridden to make them attachable.. The `anchor=` options for 2D
2022-11-11 02:38:41 +00:00
shapes treat 2D vectors as expected. Special handling occurs with 3D
vectors: if the Y coordinate is zero and the Z coordinate is nonzero,
then the Z coordinate is used to replace the Y coordinate. This is
done so that you can use the TOP and BOTTOM names as anchor for 2D
shapes.
2021-09-30 09:04:31 +00:00
```openscad-2D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-09-30 09:04:31 +00:00
square([40,30], anchor=BACK+LEFT);
```
```openscad-2D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-09-30 09:04:31 +00:00
circle(d=50, anchor=BACK);
```
```openscad-2D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-09-30 09:04:31 +00:00
hexagon(d=50, anchor=LEFT);
```
```openscad-2D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
ellipse(d=[50,30], anchor=FRONT);
2022-11-11 02:38:41 +00:00
This final 2D example shows using the 3D anchor, TOP, with a 2D
2022-11-13 04:28:59 +00:00
object. Also notice how the pentagon anchors to its most extreme point on
2022-11-11 02:38:41 +00:00
the Y+ axis.
```openscad-2D
include <BOSL2/std.scad>
pentagon(d=50, anchor=TOP);
```
2021-04-12 07:41:12 +00:00
## Spin
2022-11-11 02:38:41 +00:00
You can spin attachable objects around the origin using the `spin=`
argument. The spin applies **after** anchoring, so depending on how
you anchor an object, its spin may not be about its center. This
means that spin can have an effect even on rotationally symmetric
objects like spheres and cylinders. You specify the spin in degrees.
A positive number will result in a counter-clockwise spin around the Z
axis (as seen from above), and a negative number will make a clockwise
2021-10-07 04:40:47 +00:00
spin:
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube([20,20,40], center=true, spin=45);
```
2022-11-13 16:40:07 +00:00
You can also spin around other axes, or multiple axes at once, by giving 3 angles (in degrees) to
`spin=` as a vector, like [Xang,Yang,Zang]. Similarly to `rotate()`,
the rotations apply in the order given, X-axis spin, then Y-axis, then Z-axis:
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube([20,20,40], center=true, spin=[10,20,30]);
```
2022-11-13 04:28:59 +00:00
This example shows a cylinder which has been anchored at its FRONT,
with a rotated copy in gray. The rotation is performed around the
2022-11-13 16:40:07 +00:00
origin, but the cylinder is off the origin, so the rotation **does**
2022-11-13 04:28:59 +00:00
have an effect on the cylinder, even though the cylinder has
rotational symmetry.
2022-11-11 02:38:41 +00:00
```openscad-3D
include <BOSL2/std.scad>
cylinder(h=40,d=20,anchor=FRONT+BOT);
2022-11-13 16:40:07 +00:00
%cylinder(h=40.2,d=20,anchor=FRONT+BOT,spin=40);
2022-11-11 02:38:41 +00:00
```
2021-09-30 09:04:31 +00:00
You can also apply spin to 2D shapes from BOSL2, though only by scalar angle:
```openscad-2D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-09-30 09:04:31 +00:00
square([40,30], spin=30);
```
```openscad-2D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
ellipse(d=[40,30], spin=30);
```
2021-04-12 07:41:12 +00:00
## Orientation
Another way to specify a rotation for an attachable shape, is to pass a 3D vector via the
`orient=` argument. This lets you specify what direction to tilt the top of the shape towards.
For example, you can make a cone that is tilted up and to the right like this:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cylinder(h=100, r1=50, r2=20, orient=UP+RIGHT);
```
2022-11-13 04:28:59 +00:00
More precisely, the Z direction of the shape is rotated to align with
the vector you specify. Two dimensional attachables, which have no Z vector,
do not accept the `orient=` argument.
2021-04-12 07:41:12 +00:00
## Mixing Anchoring, Spin, and Orientation
When giving `anchor=`, `spin=`, and `orient=`, they are applied anchoring first, spin second,
then orient last. For example, here's a cube:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube([20,20,50]);
```
You can center it with an `anchor=CENTER` argument:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube([20,20,50], anchor=CENTER);
```
Add a 45 degree spin:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube([20,20,50], anchor=CENTER, spin=45);
```
Now tilt the top up and forward:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube([20,20,50], anchor=CENTER, spin=45, orient=UP+FWD);
```
2022-11-11 02:38:41 +00:00
For 2D shapes, you can mix `anchor=` with `spin=`, but not with `orient=`.
```openscad-2D
include <BOSL2/std.scad>
square([40,30], anchor=BACK+LEFT, spin=30);
```
## Positioning Children
Positioning is a powerful method for placing an object relative to
another object. You do this by making the second object a child of
2022-11-13 04:28:59 +00:00
the first object. By default, the child's anchor point will be
2022-11-13 16:40:07 +00:00
aligned with the center of the parent. The default anchor for `cyl()`
is CENTER, and in this case, the cylinder is centered on the cube's center
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-13 16:40:07 +00:00
up(13) cube(50)
2022-11-16 04:02:39 +00:00
cyl(d=25,l=95);
2022-11-13 04:28:59 +00:00
```
With `cylinder()` the default anchor is BOTTOM. It's hard to tell,
but the cylinder's bottom is placed at the center of the cube.
```openscad-3D
include <BOSL2/std.scad>
cube(50)
2023-01-20 12:10:57 +00:00
cylinder(d=25,h=75);
2021-04-12 07:41:12 +00:00
```
2022-11-13 04:28:59 +00:00
If you explicitly anchor the child object then the anchor you choose will be aligned
2022-11-11 02:38:41 +00:00
with the center point of the parent object. In this example the right
side of the cylinder is aligned with the center of the cube.
```openscad-3D
include <BOSL2/std.scad>
cube(50,anchor=FRONT)
2023-01-20 12:10:57 +00:00
cylinder(d=25,h=95,anchor=RIGHT);
2022-11-11 02:38:41 +00:00
```
The `position()` module enables you to specify where on the parent to
position the child object. You give `position()` an anchor point on
2022-11-13 04:28:59 +00:00
the parent, and the child's anchor point is aligned with the specified
parent anchor point. In this example the LEFT anchor of the cylinder is positioned on the
2022-11-11 02:38:41 +00:00
RIGHT anchor of the cube.
```openscad-3D
include <BOSL2/std.scad>
cube(50,anchor=FRONT)
2023-01-20 12:10:57 +00:00
position(RIGHT) cylinder(d=25,h=75,anchor=LEFT);
2022-11-11 02:38:41 +00:00
```
Using this mechanism you can position objects relative to other
objects which are in turn positioned relative to other objects without
having to keep track of the transformation math.
```openscad-3D
include <BOSL2/std.scad>
cube([50,50,30],center=true)
position(TOP+RIGHT) cube([25,40,10], anchor=RIGHT+BOT)
position(LEFT+FRONT+TOP) cube([12,12,8], anchor=LEFT+FRONT+BOT)
2023-01-20 12:10:57 +00:00
cylinder(h=10,r=3);
2022-11-11 02:38:41 +00:00
```
The positioning mechanism is not magical: it simply applies a
`translate()` operation to the child. You can still apply your own
additional translations or other transformations if you wish. For
example, you can position an object 5 units from the right edge:
```openscad-3D
2022-11-11 03:06:59 +00:00
include<BOSL2/std.scad>
2022-11-11 02:38:41 +00:00
cube([50,50,20],center=true)
2022-11-13 04:28:59 +00:00
position(TOP+RIGHT) left(5) cube([4,50,10], anchor=RIGHT+BOT);
2022-11-11 02:38:41 +00:00
```
2023-06-16 22:27:15 +00:00
2022-11-11 02:38:41 +00:00
Positioning objects works the same way in 2D.
```openscad-2D
2022-11-11 02:38:41 +00:00
include<BOSL2/std.scad>
square(10)
position(RIGHT) square(3,anchor=LEFT);
```
## Using position() with orient()
When positioning an object near an edge or corner you may wish to
orient the object relative to some face other than the TOP face that
2022-11-13 04:28:59 +00:00
meets at that edge or corner. You can always apply a `rotation()` to
change the orientation of the child object, but in order to do this,
you need to figure out the correct rotation. The `orient()` module provides a
mechanism for re-orienting the child() that eases this burden:
it can orient the child relative to the parent anchor directions. This is different
2022-11-11 02:38:41 +00:00
than giving an `orient=` argument to the child, because that orients
2022-11-16 04:02:39 +00:00
relative to the parent's global coordinate system by just using the vector
directly, instead of orienting to the parent's anchor, which takes
2022-11-16 04:02:39 +00:00
account of face orientation. A series of three
2022-11-11 02:38:41 +00:00
examples shows the different results. In the first example, we use
only `position()`. The child cube is erected pointing upwards, in the
Z direction. In the second example we use `orient=RIGHT` in the child
and the result is that the child object points in the X+ direction,
without regard for the shape of the parent object. In the final
example we apply `orient(RIGHT)` and the child is oriented
2022-11-16 04:02:39 +00:00
relative to the slanted right face of the parent using the parent
RIGHT anchor.
2022-11-11 02:38:41 +00:00
```openscad-3D
include<BOSL2/std.scad>
prismoid([50,50],[30,30],h=40)
position(RIGHT+TOP)
cube([15,15,25],anchor=RIGHT+BOT);
```
```openscad-3D
include<BOSL2/std.scad>
prismoid([50,50],[30,30],h=40)
position(RIGHT+TOP)
cube([15,15,25],orient=RIGHT,anchor=LEFT+BOT);
```
```openscad-3D
include<BOSL2/std.scad>
prismoid([50,50],[30,30],h=40)
position(RIGHT+TOP)
orient(RIGHT)
2022-11-11 02:38:41 +00:00
cube([15,15,25],anchor=BACK+BOT);
```
2022-11-16 04:02:39 +00:00
You may have noticed that the children in the above three examples
have different anchors. Why is that? The first and second examples
2022-11-13 04:28:59 +00:00
differ because anchoring up and anchoring to the right require
anchoring on opposite sides of the child. But the third case differs
because the spin has changed. The examples below show the same models
but with arrows replacing the child cube. The red flags on the arrows
mark the zero spin direction. Examine the red flags to see how the spin
changes. The Y+ direction of the child will point towards that red
flag.
```openscad-3D
include<BOSL2/std.scad>
prismoid([50,50],[30,30],h=40)
position(RIGHT+TOP)
2022-11-16 04:02:39 +00:00
anchor_arrow(40);
2022-11-13 04:28:59 +00:00
```
```openscad-3D
include<BOSL2/std.scad>
prismoid([50,50],[30,30],h=40)
position(RIGHT+TOP)
2022-11-16 04:02:39 +00:00
anchor_arrow(40, orient=RIGHT);
2022-11-13 04:28:59 +00:00
```
```openscad-3D
include<BOSL2/std.scad>
prismoid([50,50],[30,30],h=40)
position(RIGHT+TOP)
orient(RIGHT)
2022-11-16 04:02:39 +00:00
anchor_arrow(40);
2022-11-13 04:28:59 +00:00
```
2023-06-17 21:42:09 +00:00
## Aligning children with align()
You may have noticed that with position() and orient(), specifying the
child anchors to position objects flush with their parent can be
annoying, or sometimes even tricky. You can simplify this task by
using the align() module. This module positions children at specified
anchor points on the parent while picking the correct anchor points on
the children so that they line up with faces on the parent object.
In the simplest case, if you want to place a child on the RIGHT side
of its parent, you need to anchor the child to its LEFT anchor:
```openscad-3D
include<BOSL2/std.scad>
cuboid([50,40,15])
position(RIGHT)
color("lightblue")cuboid(5,anchor=LEFT);
```
Using align(), the determination of the anchor is automatic. Any
anchor you do specify is ignored.
2023-06-16 22:27:15 +00:00
```openscad-3D
2023-06-16 23:24:31 +00:00
include<BOSL2/std.scad>
2023-06-16 22:27:15 +00:00
cuboid([50,40,15])
2023-06-17 21:42:09 +00:00
align(RIGHT)
color("lightblue")cuboid(5);
2023-06-16 22:27:15 +00:00
```
2023-06-17 21:42:09 +00:00
To place the child on top of the parent in the corner you can do use
align as shown below instead of specifying the RIGHT+FRONT+BOT anchor
with position():
2023-06-16 22:27:15 +00:00
```openscad-3D
2023-06-16 23:24:31 +00:00
include<BOSL2/std.scad>
2023-06-16 22:27:15 +00:00
cuboid([50,40,15])
2023-06-17 21:42:09 +00:00
align(RIGHT+FRONT+TOP)
color("lightblue")prismoid([10,5],[7,4],height=4);
```
Both position() and align() can accept a list of anchor locations and
makes several copies of the children, but
if you want the children positioned flush, each copy
requires a different anchor, so it is impossible to do this with a
2023-08-19 05:15:15 +00:00
single call to position(), but easily done using align():
2023-06-17 21:42:09 +00:00
```openscad-3D
include<BOSL2/std.scad>
cuboid([50,40,15])
align([RIGHT+TOP,LEFT+TOP])
color("lightblue")prismoid([10,5],[7,4],height=4);
```
Align also accepts a spin argument, which lets you spin the child
while still aligning it:
```openscad-3D
include<BOSL2/std.scad>
cuboid([50,40,15])
align(RIGHT+TOP,spin=90)
color("lightblue")prismoid([10,5],[7,4],height=4);
```
Note that this is different than using the spin argument to the child
object, which will apply after alignment has been done.
```openscad-3D
include<BOSL2/std.scad>
cuboid([50,40,15])
align(RIGHT+TOP)
color("lightblue")prismoid([10,5],[7,4],height=4,spin=90);
```
If you orient the object DOWN it will be attached from its top anchor:
```openscad-3D
include<BOSL2/std.scad>
cuboid([50,40,15])
align(RIGHT+TOP,DOWN)
color("lightblue")prismoid([10,5],[7,4],height=4);
```
When placing children on the RIGHT and LEFT, there is a spin applied.
This means that setting spin=0 changes the orientation. Here we have
one object with the default and one object with zero spin:
```openscad-3D
include<BOSL2/std.scad>
prismoid(50,30,25){
align(RIGHT+TOP,RIGHT,spin=0)
color("lightblue")prismoid([10,5],[7,4],height=4);
align(RIGHT+BOT,RIGHT)
color("green")prismoid([10,5],[7,4],height=4);
}
2023-06-16 22:27:15 +00:00
```
2022-11-13 04:28:59 +00:00
2022-11-11 02:38:41 +00:00
## Attachment overview
Attachables get their name from their ability to be attached to each
other. Unlike with positioning, attaching changes the orientation of
the child object. When you attach an object, it appears on the parent
2022-11-16 04:02:39 +00:00
relative to the local coordinate system of the parent at the anchor point. To understand
2022-11-11 02:38:41 +00:00
what this means, imagine the perspective of an ant walking on a
2022-11-16 04:02:39 +00:00
sphere. The meaning of UP varies depending on where on the sphere the
ant is standing. If you **attach** a cylinder to the sphere then the cylinder will
be "up" from the ant's perspective. The first example shows a
cylinder placed with `position()` so it points up in the global parent
coordinate system. The second example shows how `attach()` points the
cylinder UP from the perspective of an ant standing at the anchor
point on the sphere.
```openscad-3D
include<BOSL2/std.scad>
sphere(40)
2023-01-20 12:10:57 +00:00
position(RIGHT+TOP) cylinder(r=8,h=20);
2022-11-16 04:02:39 +00:00
```
2022-11-11 02:38:41 +00:00
2022-11-13 04:28:59 +00:00
```openscad-3D
2022-11-11 02:38:41 +00:00
include<BOSL2/std.scad>
sphere(40)
2023-01-20 12:10:57 +00:00
attach(RIGHT+TOP) cylinder(r=8,h=20);
2022-11-11 02:38:41 +00:00
```
In the example above, the cylinder's center point is attached to the
2022-11-13 04:28:59 +00:00
sphere, pointing "up" from the perspective of the sphere's surface.
2022-11-11 02:38:41 +00:00
For a sphere, a surface normal is defined everywhere that specifies
what "up" means. But for other objects, it may not be so obvious.
Usually at edges and corners the direction is the average of the
direction of the faces that meet there.
When you specify an anchor you are actually specifying both an anchor
point but also an anchor direction. If you want to visualize this
direction you can use anchor arrows.
## Anchor Directions and Anchor Arrows
2022-11-16 04:02:39 +00:00
For the ant on the sphere it is obvious which direction is UP; that
direction corresponds to the Z+ axis. The location of the X and Y
2022-11-19 18:03:10 +00:00
axes is less clear and in fact it may be arbitrary. One way that is
useful to show the position and orientation of an anchor point is by
attaching an anchor arrow to that anchor. As noted before, the small
red flag points in the direction of the anchor's Y+ axis when the spin
is zero.
2022-11-11 02:38:41 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-13 04:28:59 +00:00
cube(18, center=true)
2022-11-11 02:38:41 +00:00
attach(LEFT+TOP)
anchor_arrow();
```
For large objects, you can change the size of the arrow with the `s=` argument.
```openscad-3D
include <BOSL2/std.scad>
sphere(d=100)
attach(LEFT+TOP)
2022-11-13 04:28:59 +00:00
anchor_arrow(s=50);
2022-11-11 02:38:41 +00:00
```
2022-11-13 04:28:59 +00:00
To show all the standard cardinal anchor points, you can use the `show_anchors()` module.
2022-11-11 02:38:41 +00:00
```openscad-3D
include <BOSL2/std.scad>
cube(40, center=true)
show_anchors();
```
```openscad-3D
include <BOSL2/std.scad>
cylinder(h=40, d=40, center=true)
show_anchors();
```
```openscad-3D
include <BOSL2/std.scad>
sphere(d=40)
show_anchors();
```
2022-11-11 02:38:41 +00:00
For large objects, you can again change the size of the arrows with the `s=` argument.
```openscad-3D
include <BOSL2/std.scad>
cylinder(h=100, d=100, center=true)
show_anchors(s=30);
2022-11-13 04:28:59 +00:00
```
2022-11-11 02:38:41 +00:00
## Basic Attachment
2021-04-12 07:41:12 +00:00
2022-11-11 02:38:41 +00:00
The simplest form of attachment is to attach using the `attach()`
2022-11-13 04:28:59 +00:00
module with a single argument, which specifies the anchor on the parent
2022-11-11 02:38:41 +00:00
where the child will attach. This will attach the bottom of the child
to the given anchor point on the parent. The child appears on the parent with its
2022-11-13 04:28:59 +00:00
Z direction aligned parallel to the parent's anchor direction, and
2022-11-19 18:03:10 +00:00
its Y direction pointing in the zero spin direction for the
2022-11-13 04:28:59 +00:00
parent anchor. The anchor direction of the child does not affect the result in this
2022-11-11 02:38:41 +00:00
case.
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube(50,center=true)
2023-01-20 21:55:56 +00:00
attach(RIGHT)cylinder(d1=30,d2=15,h=25);
2021-04-12 07:41:12 +00:00
```
2022-11-11 02:38:41 +00:00
```openscad-3D
include <BOSL2/std.scad>
cube(50,center=true)
2023-01-20 21:55:56 +00:00
attach(RIGHT+TOP)cylinder(d1=30,d2=15,h=25);
2022-11-11 02:38:41 +00:00
```
2022-11-19 18:03:10 +00:00
In the second example, the child object points diagonally away
2022-11-11 02:38:41 +00:00
from the cube. If you want the child at at edge of the parent it's
2022-11-13 04:28:59 +00:00
likely that this result will not be what you want. To get a different
result, use `position()` with `orient()`, if needed.
2022-11-11 02:38:41 +00:00
If you give an anchor point to the child object it moves the child
around (in the attached coordinate system). Or alternatively you can
think that it moves the object first, and then it gets attached.
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube(50,center=true)
2023-01-20 12:10:57 +00:00
attach(RIGHT)cylinder(d1=30,d2=15,h=25,anchor=FRONT);
2021-04-12 07:41:12 +00:00
```
2022-11-11 02:38:41 +00:00
In the above example we anchor the child to its FRONT and then attach
it to the RIGHT. An ambiguity exists regarding the spin of the
parent's coordinate system. How is this resolved? The small flags
on the anchor arrows show the position of zero spin by pointing
2022-11-19 18:03:10 +00:00
towards the local Y+ direction, which is also the BACK direction of the child. For the above
2022-11-11 02:38:41 +00:00
cube, the arrow looks like this:
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube(50,center=true)
2022-11-11 02:38:41 +00:00
attach(RIGHT)anchor_arrow(30);
2021-04-12 07:41:12 +00:00
```
2022-11-11 02:38:41 +00:00
The red flag points up, which explains why the attached cylinder
appeared above the anchor point. The CENTER anchor generally has a
direction that points upward, so an attached object will keep its
orientation if attached to the CENTER of a parent.
By default, `attach()` places the child exactly flush with the surface of the parent. Sometimes
it's useful to have the child overlap the parent by insetting a bit. You can do this with the
`overlap=` argument to `attach()`. A positive value will inset the child into the parent, and
2022-11-16 04:02:39 +00:00
a negative value will outset out from the parent, which may be helpful
when doing differences.
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
cube(50,center=true)
attach(TOP,overlap=10)
2023-01-20 12:10:57 +00:00
cylinder(d=20,h=20);
```
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube(50,center=true)
attach(TOP,overlap=-20)
2023-01-20 12:10:57 +00:00
cylinder(d=20,h=20);
2021-04-12 07:41:12 +00:00
```
2022-11-11 02:38:41 +00:00
As with `position()`, you can still apply your own translations and
2022-11-16 04:02:39 +00:00
other transformations even after attaching an object. However, the
2022-11-11 02:38:41 +00:00
order of operations now matters. If you apply a translation outside
2022-11-16 04:02:39 +00:00
of the anchor then it acts in the parent's global coordinate system, so the
2022-11-11 02:38:41 +00:00
child moves up in this example:
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-12 07:41:12 +00:00
cube(50,center=true)
2022-11-16 04:02:39 +00:00
up(13)
attach(RIGHT)
2023-01-20 12:10:57 +00:00
cylinder(d1=30,d2=15,h=25);
2021-04-12 07:41:12 +00:00
```
2022-11-11 02:38:41 +00:00
On the other hand, if you put the translation between the attach and
2022-11-19 18:03:10 +00:00
the object in your code, then it will act in the local coordinate system of
the parent at the parent's anchor, so in the example below it moves to the right.
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-11 02:38:41 +00:00
cube(50,center=true)
2022-11-16 04:02:39 +00:00
attach(RIGHT)
up(13)
2023-01-20 12:10:57 +00:00
cylinder(d1=30,d2=15,h=25);
2021-04-12 07:41:12 +00:00
```
2022-11-11 02:38:41 +00:00
## Attachment With Parent and Child Anchors
The `attach()` module can also take a second argument, the child anchor.
In this case, the attachment behavior
is quite different. The objects are still attached with their anchor
points aligned, but the child is reoriented so that its anchor
direction is the opposite of the parent anchor direction. It's like
you assemble the parts by pushing them together in the direction of
their anchor arrows. Two examples appear below, where first we show
two objects with their anchors and then we show the result of
attaching with those anchors.
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-16 04:02:39 +00:00
cube(50,anchor=BOT) attach(TOP) anchor_arrow(30);
2023-01-20 12:10:57 +00:00
right(60)cylinder(d1=30,d2=15,h=25) attach(TOP) anchor_arrow(30);
2021-04-12 07:41:12 +00:00
```
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-16 04:02:39 +00:00
cube(50,anchor=BOT)
2023-01-20 12:10:57 +00:00
attach(TOP,TOP) cylinder(d1=30,d2=15,h=25);
2021-04-12 07:41:12 +00:00
```
2022-11-11 02:38:41 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-11 02:38:41 +00:00
cube(50,center=true) attach(RIGHT) anchor_arrow(30);
2023-01-20 12:10:57 +00:00
right(80)cylinder(d1=30,d2=15,h=25) attach(LEFT) anchor_arrow(30);
```
2022-11-11 02:38:41 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-11 02:38:41 +00:00
cube(50,center=true)
2023-01-20 12:10:57 +00:00
attach(RIGHT,LEFT) cylinder(d1=30,d2=15,h=25);
2022-11-13 04:28:59 +00:00
```
2022-11-11 02:38:41 +00:00
Note that when you attach with two anchors like this, the attachment
2022-11-13 04:28:59 +00:00
operation **overrides any anchor or orientation specified in the
2022-11-16 04:02:39 +00:00
child**. That means the child's `anchor=` and `orient=` options are
2022-11-13 04:28:59 +00:00
ignored.
2022-11-11 02:38:41 +00:00
Attachment with CENTER anchors can be surprising because the anchors
point upwards, so in the example below, the child's CENTER anchor
2022-11-16 04:02:39 +00:00
points up, so it is inverted when it is attached to the parent cone.
Note that the anchors are CENTER anchors, so the bases of the anchors are
hidden in the middle of the objects.
```openscad-3D
include <BOSL2/std.scad>
2023-01-20 12:10:57 +00:00
cylinder(d1=30,d2=15,h=25) attach(CENTER) anchor_arrow(40);
right(40)cylinder(d1=30,d2=15,h=25) attach(CENTER) anchor_arrow(40);
2022-11-16 04:02:39 +00:00
```
2021-04-12 07:41:12 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2023-01-20 12:10:57 +00:00
cylinder(d1=30,d2=15,h=25)
2022-11-11 02:38:41 +00:00
attach(CENTER,CENTER)
2023-01-20 12:10:57 +00:00
cylinder(d1=30,d2=15,h=25);
```
2022-11-11 02:38:41 +00:00
## Positioning and Attaching Multiple Children
You can attach or position more than one child at a time by enclosing them all in braces:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-11 02:38:41 +00:00
cube(50, center=true) {
2023-01-20 12:10:57 +00:00
attach(TOP) cylinder(d1=50,d2=20,h=20);
position(RIGHT) cylinder(d1=50,d2=20,h=20);
2022-11-11 02:38:41 +00:00
}
```
2022-11-11 02:38:41 +00:00
If you want to attach the same shape to multiple places on the same parent, you can pass the
desired anchors as a list to the `attach()` or `position()` modules:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-11 02:38:41 +00:00
cube(50, center=true)
2023-01-20 12:10:57 +00:00
attach([RIGHT,FRONT],TOP) cylinder(d1=35,d2=20,h=25);
```
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-11 02:38:41 +00:00
cube(50, center=true)
2023-01-20 12:10:57 +00:00
position([TOP,RIGHT,FRONT]) cylinder(d1=35,d2=20,h=25);
```
2022-11-11 02:38:41 +00:00
## Attaching 2D Children
You can use attachments in 2D as well. As usual for the 2D case you
can use TOP and BOTTOM as alternative to BACK and FORWARD.
```openscad-2D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-11 02:38:41 +00:00
square(50,center=true)
attach(RIGHT,FRONT)
trapezoid(w1=30,w2=0,h=30);
```
2022-11-11 02:38:41 +00:00
```openscad-2D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2022-11-11 02:38:41 +00:00
circle(d=50)
attach(TOP,BOT,overlap=5)
trapezoid(w1=30,w2=0,h=30);
```
2022-11-11 02:38:41 +00:00
2021-04-12 07:44:45 +00:00
## Tagged Operations
2021-04-21 01:58:07 +00:00
BOSL2 introduces the concept of tags. Tags are names that can be given to attachables, so that
you can refer to them when performing `diff()`, `intersect()`, and `conv_hull()` operations.
Each object can have no more than one tag at a time.
2021-04-12 07:41:12 +00:00
### `diff([remove], [keep])`
2021-04-21 01:58:07 +00:00
The `diff()` operator is used to difference away all shapes marked with the tag(s) given to
`remove`, from the other shapes.
2021-04-12 07:41:12 +00:00
2021-04-21 01:58:07 +00:00
For example, to difference away a child cylinder from the middle of a parent cube, you can
do this:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
diff("hole")
cube(100, center=true)
2022-05-15 14:51:45 +00:00
tag("hole")cylinder(h=101, d=50, center=true);
2021-04-21 01:58:07 +00:00
```
The `keep=` argument takes tags for shapes that you want to keep in the output.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
diff("dish", keep="antenna")
2021-04-21 01:58:07 +00:00
cube(100, center=true)
attach([FRONT,TOP], overlap=33) {
2022-05-15 14:51:45 +00:00
tag("dish") cylinder(h=33.1, d1=0, d2=95);
tag("antenna") cylinder(h=33.1, d=10);
2021-04-21 01:58:07 +00:00
}
```
Remember that tags are inherited by children. In this case, we need to explicitly
untag the first cylinder (or change its tag to something else), or it
will inherit the "keep" tag and get kept.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
diff("hole", "keep")
2022-05-15 14:51:45 +00:00
tag("keep")cube(100, center=true)
attach([RIGHT,TOP]) {
2022-05-15 14:51:45 +00:00
tag("") cylinder(d=95, h=5);
tag("hole") cylinder(d=50, h=11, anchor=CTR);
2021-04-21 01:58:07 +00:00
}
```
You can of course apply `tag()` to several children.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
diff("hole")
cube(100, center=true)
attach([FRONT,TOP], overlap=20)
2022-05-15 15:47:44 +00:00
tag("hole") {
2021-04-21 01:58:07 +00:00
cylinder(h=20.1, d1=0, d2=95);
down(10) cylinder(h=30, d=30);
}
```
Many of the modules that use tags have default values for their tags. For diff the default
remove tag is "remove" and the default keep tag is "keep". In this example we rely on the
default values:
```openscad-3D
include <BOSL2/std.scad>
diff()
sphere(d=100) {
tag("keep")xcyl(d=40, l=120);
tag("remove")cuboid([40,120,100]);
}
```
2021-04-21 01:58:07 +00:00
The parent object can be differenced away from other shapes. Tags are inherited by children,
though, so you will need to set the tags of the children as well as the parent.
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
diff("hole")
tag("hole")cube([20,11,45], center=true)
tag("body")cube([40,10,90], center=true);
2021-04-21 01:58:07 +00:00
```
2021-09-30 09:04:31 +00:00
Tags (and therefore tag-based operations like `diff()`) only work correctly with attachable
children. However, a number of built-in modules for making shapes are *not* attachable.
Some notable non-attachable modules are `text()`, `linear_extrude()`, `rotate_extrude()`,
`polygon()`, `polyhedron()`, `import()`, `surface()`, `union()`, `difference()`,
`intersection()`, `offset()`, `hull()`, and `minkowski()`.
To allow you to use tags-based operations with non-attachable shapes, you can wrap them with the
2022-05-15 15:47:44 +00:00
`force_tag()` module to specify their tags. For example:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
diff("hole")
cuboid(50)
attach(TOP)
2022-05-15 15:47:44 +00:00
force_tag("hole")
rotate_extrude()
right(15)
square(10,center=true);
```
### `intersect([intersect], [keep])`
To perform an intersection of attachables, you can use the `intersect()` module. This is
specifically intended to address the situation where you want intersections involving a parent
and a child, something that is impossible with the native `intersection()` module. This module
treats the children in three groups: objects matching the `intersect` tags, objects matching
the tags listed in `keep` and the remaining objects that don't match any listed tags. The
intersection is computed between the union of the `intersect` tagged objects and the union of
2022-11-13 04:28:59 +00:00
the objects that don't match any listed tags. Finally the objects listed in `keep` are union
ed with the result.
2021-04-21 01:58:07 +00:00
In this example the parent is intersected with a conical bounding shape.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
intersect("bounds")
cube(100, center=true)
tag("bounds") cylinder(h=100, d1=120, d2=95, center=true, $fn=72);
2021-04-21 01:58:07 +00:00
```
In this example the child objects are intersected with the bounding box parent.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
intersect("pole cap")
2021-04-21 01:58:07 +00:00
cube(100, center=true)
attach([TOP,RIGHT]) {
tag("pole")cube([40,40,80],center=true);
tag("cap")sphere(d=40*sqrt(2));
2021-04-21 01:58:07 +00:00
}
```
The default `intersect` tag is "intersect" and the default `keep` tag is "keep". Here is an
example where "keep" is used to keep the pole from being removed by the intersection.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
intersect()
2021-04-21 01:58:07 +00:00
cube(100, center=true) {
tag("intersect")cylinder(h=100, d1=120, d2=95, center=true, $fn=72);
tag("keep")zrot(45) xcyl(h=140, d=20, $fn=36);
2021-04-21 01:58:07 +00:00
}
```
### `conv_hull([keep])`
You can use the `conv_hull()` module to hull shapes together. Objects
marked with the keep tags are excluded from the hull and unioned into the final result.
The default keep tag is "keep".
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
conv_hull()
cube(50, center=true) {
2021-04-21 01:58:07 +00:00
cyl(h=100, d=20);
tag("keep")xcyl(h=100, d=20);
2021-04-21 01:58:07 +00:00
}
```
2021-04-12 07:41:12 +00:00
2021-04-21 01:58:07 +00:00
## 3D Masking Attachments
To make it easier to mask away shapes from various edges of an attachable parent shape, there
are a few specialized alternatives to the `attach()` and `position()` modules.
### `edge_mask()`
If you have a 3D mask shape that you want to difference away from various edges, you can use
the `edge_mask()` module. This module will take a vertically oriented shape, and will rotate
and move it such that the BACK, RIGHT (X+,Y+) side of the shape will be aligned with the given
edges. The shape will be tagged as a "remove" so that you can use
`diff()` with its default "remove" tag. For example,
here's a shape for rounding an edge:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
module round_edge(l,r) difference() {
translate([-1,-1,-l/2])
cube([r+1,r+1,l]);
translate([r,r])
cylinder(h=l+1,r=r,center=true, $fn=quantup(segs(r),4));
}
round_edge(l=30, r=19);
```
You can use that mask to round various edges of a cube:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
module round_edge(l,r) difference() {
translate([-1,-1,-l/2])
cube([r+1,r+1,l]);
translate([r,r])
cylinder(h=l+1,r=r,center=true, $fn=quantup(segs(r),4));
}
diff()
cube([50,60,70],center=true)
edge_mask([TOP,"Z"],except=[BACK,TOP+LEFT])
round_edge(l=71,r=10);
```
### `corner_mask()`
If you have a 3D mask shape that you want to difference away from various corners, you can use
the `corner_mask()` module. This module will take a shape and rotate and move it such that the
BACK RIGHT TOP (X+,Y+,Z+) side of the shape will be aligned with the given corner. The shape
will be tagged as a "remove" so that you can use `diff()` with its
default "remove" tag. For example, here's a shape for
rounding a corner:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
module round_corner(r) difference() {
translate(-[1,1,1])
cube(r+1);
translate([r,r,r])
2023-01-20 12:10:57 +00:00
spheroid(r=r, style="aligned", $fn=quantup(segs(r),4));
}
round_corner(r=10);
```
You can use that mask to round various corners of a cube:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
module round_corner(r) difference() {
translate(-[1,1,1])
cube(r+1);
translate([r,r,r])
2023-01-20 12:10:57 +00:00
spheroid(r=r, style="aligned", $fn=quantup(segs(r),4));
}
diff()
cube([50,60,70],center=true)
corner_mask([TOP,FRONT],LEFT+FRONT+TOP)
round_corner(r=10);
```
### Mix and Match Masks
You can use `edge_mask()` and `corner_mask()` together as well:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
module round_corner(r) difference() {
translate(-[1,1,1])
cube(r+1);
translate([r,r,r])
2023-01-20 12:10:57 +00:00
spheroid(r=r, style="aligned", $fn=quantup(segs(r),4));
}
module round_edge(l,r) difference() {
translate([-1,-1,-l/2])
cube([r+1,r+1,l]);
translate([r,r])
cylinder(h=l+1,r=r,center=true, $fn=quantup(segs(r),4));
}
diff()
cube([50,60,70],center=true) {
edge_mask("ALL") round_edge(l=71,r=10);
corner_mask("ALL") round_corner(r=10);
}
```
## 2D Profile Mask Attachments
While 3D mask shapes give you a great deal of control, you need to make sure they are correctly
sized, and you need to provide separate mask shapes for corners and edges. Often, a single 2D
profile could be used to describe the edge mask shape (via `linear_extrude()`), and the corner
mask shape (via `rotate_extrude()`). This is where `edge_profile()`, `corner_profile()`, and
`face_profile()` come in.
### `edge_profile()`
Using the `edge_profile()` module, you can provide a 2D profile shape and it will be linearly
2022-11-13 04:28:59 +00:00
extruded to a mask of the appropriate length for each given edge. The resultant mask will be
tagged with "remove" so that you can difference it away with `diff()`
with the default "remove" tag. The 2D profile is
assumed to be oriented with the BACK, RIGHT (X+,Y+) quadrant as the "cutter edge" that gets
re-oriented towards the edges of the parent shape. A typical mask profile for chamfering an
edge may look like:
```openscad-2D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
mask2d_roundover(10);
```
Using that mask profile, you can mask the edges of a cube like:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
diff()
cube([50,60,70],center=true)
edge_profile("ALL")
mask2d_roundover(10);
```
### `corner_profile()`
You can use the same profile to make a rounded corner mask as well:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
diff()
cube([50,60,70],center=true)
corner_profile("ALL", r=10)
mask2d_roundover(10);
```
### `face_profile()`
As a simple shortcut to apply a profile mask to all edges and corners of a face, you can use the
`face_profile()` module:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
diff()
cube([50,60,70],center=true)
face_profile(TOP, r=10)
mask2d_roundover(10);
```
2021-04-12 07:41:12 +00:00
## Coloring Attachables
Usually, when coloring a shape with the `color()` module, the parent color overrides the colors of
all children. This is often not what you want:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
$fn = 24;
color("red") spheroid(d=3) {
attach(CENTER,BOT) color("white") cyl(h=10, d=1) {
attach(TOP,BOT) color("green") cyl(h=5, d1=3, d2=0);
}
}
```
2023-03-10 01:49:18 +00:00
If you use the `recolor()` module, however, the child's color
overrides the color of the parent. This is probably easier to understand by example:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
$fn = 24;
recolor("red") spheroid(d=3) {
attach(CENTER,BOT) recolor("white") cyl(h=10, d=1) {
attach(TOP,BOT) recolor("green") cyl(h=5, d1=3, d2=0);
}
}
```
2021-04-12 07:41:12 +00:00
2023-03-10 01:49:18 +00:00
Be aware that `recolor()` will only work if you avoid using the native
`color()` module. Also note that `recolor()` still affects all its
children. If you want to color an object without affecting the
children you can use `color_this()`. See the difference below:
```openscad-3D
include <BOSL2/std.scad>
$fn = 24;
recolor("red") spheroid(d=3) {
attach(CENTER,BOT) recolor("white") cyl(h=10, d=1) {
attach(TOP,BOT) cyl(h=5, d1=3, d2=0);
}
}
right(5)
recolor("red") spheroid(d=3) {
attach(CENTER,BOT) color_this("white") cyl(h=10, d=1) {
attach(TOP,BOT) cyl(h=5, d1=3, d2=0);
}
}
```
As with all of the attachable features, these color modules only work
on attachable objects, so they will have no effect on objects you
create using `linear_extrude()` or `rotate_extrude()`.
2021-04-12 07:41:12 +00:00
## Making Attachables
2021-04-21 01:58:07 +00:00
To make a shape attachable, you just need to wrap it with an `attachable()` module with a
basic description of the shape's geometry. By default, the shape is expected to be centered
at the origin. The `attachable()` module expects exactly two children. The first will be
the shape to make attachable, and the second will be `children()`, literally.
### Prismoidal/Cuboidal Attachables
To make a cuboidal or prismoidal shape attachable, you use the `size`, `size2`, and `offset`
arguments of `attachable()`.
In the most basic form, where the shape is fully cuboid, with top and bottom of the same size,
2021-04-21 01:58:07 +00:00
and directly over one another, you can just use `size=`.
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module cubic_barbell(s=100, anchor=CENTER, spin=0, orient=UP) {
attachable(anchor,spin,orient, size=[s*3,s,s]) {
union() {
xcopies(2*s) cube(s, center=true);
xcyl(h=2*s, d=s/4);
}
children();
}
}
cubic_barbell(100) show_anchors(30);
2021-04-21 01:58:07 +00:00
```
When the shape is prismoidal, where the top is a different size from the bottom, you can use
the `size2=` argument as well. While `size=` takes all three axes sizes, the `size2=` argument
only takes the [X,Y] sizes of the top of the shape.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module prismoidal(size=[100,100,100], scale=0.5, anchor=CENTER, spin=0, orient=UP) {
attachable(anchor,spin,orient, size=size, size2=[size.x, size.y]*scale) {
hull() {
up(size.z/2-0.005)
linear_extrude(height=0.01, center=true)
square([size.x,size.y]*scale, center=true);
down(size.z/2-0.005)
linear_extrude(height=0.01, center=true)
square([size.x,size.y], center=true);
}
children();
}
}
prismoidal([100,60,30], scale=0.5) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
When the top of the prismoid can be shifted away from directly above the bottom, you can use
the `shift=` argument. The `shift=` argument takes an [X,Y] vector of the offset of the center
of the top from the XY center of the bottom of the shape.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module prismoidal(size=[100,100,100], scale=0.5, shift=[0,0], anchor=CENTER, spin=0, orient=UP) {
attachable(anchor,spin,orient, size=size, size2=[size.x, size.y]*scale, shift=shift) {
hull() {
translate([shift.x, shift.y, size.z/2-0.005])
linear_extrude(height=0.01, center=true)
square([size.x,size.y]*scale, center=true);
down(size.z/2-0.005)
linear_extrude(height=0.01, center=true)
square([size.x,size.y], center=true);
}
children();
}
}
prismoidal([100,60,30], scale=0.5, shift=[-30,20]) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
In the case that the prismoid is not oriented vertically, (ie, where the `shift=` or `size2=`
arguments should refer to a plane other than XY) you can use the `axis=` argument. This lets
you make prismoids naturally oriented forwards/backwards or sideways.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module yprismoidal(
size=[100,100,100], scale=0.5, shift=[0,0],
anchor=CENTER, spin=0, orient=UP
) {
attachable(
anchor, spin, orient,
size=size, size2=point2d(size)*scale,
shift=shift, axis=BACK
) {
xrot(-90) hull() {
translate([shift.x, shift.y, size.z/2-0.005])
linear_extrude(height=0.01, center=true)
square([size.x,size.y]*scale, center=true);
down(size.z/2-0.005)
linear_extrude(height=0.01, center=true)
square([size.x,size.y], center=true);
}
children();
}
}
yprismoidal([100,60,30], scale=1.5, shift=[20,20]) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
### Cylindrical Attachables
To make a cylindrical shape attachable, you use the `l`, and `r`/`d`, args of `attachable()`.
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module twistar(l,r,d, anchor=CENTER, spin=0, orient=UP) {
r = get_radius(r=r,d=d,dflt=1);
attachable(anchor,spin,orient, r=r, l=l) {
linear_extrude(height=l, twist=90, slices=20, center=true, convexity=4)
star(n=20, r=r, ir=r*0.9);
children();
}
}
twistar(l=100, r=40) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
2022-11-13 04:28:59 +00:00
If the cylinder is elipsoidal in shape, you can pass the unequal X/Y sizes as a 2-item vector
2021-04-21 01:58:07 +00:00
to the `r=` or `d=` argument.
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module ovalstar(l,rx,ry, anchor=CENTER, spin=0, orient=UP) {
attachable(anchor,spin,orient, r=[rx,ry], l=l) {
linear_extrude(height=l, center=true, convexity=4)
scale([1,ry/rx,1])
star(n=20, r=rx, ir=rx*0.9);
children();
}
}
ovalstar(l=100, rx=50, ry=30) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
2022-11-13 04:28:59 +00:00
For cylindrical shapes that aren't oriented vertically, use the `axis=` argument.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module ytwistar(l,r,d, anchor=CENTER, spin=0, orient=UP) {
r = get_radius(r=r,d=d,dflt=1);
attachable(anchor,spin,orient, r=r, l=l, axis=BACK) {
2021-04-21 01:58:07 +00:00
xrot(-90)
linear_extrude(height=l, twist=90, slices=20, center=true, convexity=4)
star(n=20, r=r, ir=r*0.9);
children();
}
}
ytwistar(l=100, r=40) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
### Conical Attachables
To make a conical shape attachable, you use the `l`, `r1`/`d1`, and `r2`/`d2`, args of
`attachable()`.
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module twistar(l, r,r1,r2, d,d1,d2, anchor=CENTER, spin=0, orient=UP) {
r1 = get_radius(r1=r1,r=r,d1=d1,d=d,dflt=1);
r2 = get_radius(r1=r2,r=r,d1=d2,d=d,dflt=1);
attachable(anchor,spin,orient, r1=r1, r2=r2, l=l) {
linear_extrude(height=l, twist=90, scale=r2/r1, slices=20, center=true, convexity=4)
star(n=20, r=r1, ir=r1*0.9);
children();
}
}
twistar(l=100, r1=40, r2=20) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
2022-11-13 04:28:59 +00:00
If the cone is ellipsoidal in shape, you can pass the unequal X/Y sizes as a 2-item vectors
2021-04-21 01:58:07 +00:00
to the `r1=`/`r2=` or `d1=`/`d2=` arguments.
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module ovalish(l,rx1,ry1,rx2,ry2, anchor=CENTER, spin=0, orient=UP) {
attachable(anchor,spin,orient, r1=[rx1,ry1], r2=[rx2,ry2], l=l) {
hull() {
up(l/2-0.005)
linear_extrude(height=0.01, center=true)
ellipse([rx2,ry2]);
2021-04-21 01:58:07 +00:00
down(l/2-0.005)
linear_extrude(height=0.01, center=true)
ellipse([rx1,ry1]);
2021-04-21 01:58:07 +00:00
}
children();
}
}
ovalish(l=100, rx1=50, ry1=30, rx2=30, ry2=50) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
2021-09-30 09:04:31 +00:00
For conical shapes that are not oriented vertically, use the `axis=` argument to indicate the
direction of the primary shape axis:
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module ytwistar(l, r,r1,r2, d,d1,d2, anchor=CENTER, spin=0, orient=UP) {
r1 = get_radius(r1=r1,r=r,d1=d1,d=d,dflt=1);
r2 = get_radius(r1=r2,r=r,d1=d2,d=d,dflt=1);
attachable(anchor,spin,orient, r1=r1, r2=r2, l=l, axis=BACK) {
xrot(-90)
linear_extrude(height=l, twist=90, scale=r2/r1, slices=20, center=true, convexity=4)
star(n=20, r=r1, ir=r1*0.9);
children();
}
}
ytwistar(l=100, r1=40, r2=20) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
### Spherical Attachables
To make a spherical shape attachable, you use the `r`/`d` args of `attachable()`.
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module spikeball(r, d, anchor=CENTER, spin=0, orient=UP) {
r = get_radius(r=r,d=d,dflt=1);
attachable(anchor,spin,orient, r=r*1.1) {
union() {
2022-10-14 23:29:09 +00:00
sphere_copies(r=r, n=512, cone_ang=180) cylinder(r1=r/10, r2=0, h=r/10);
2021-04-21 01:58:07 +00:00
sphere(r=r);
}
children();
}
}
spikeball(r=50) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
2022-10-14 23:29:09 +00:00
If the shape is an ellipsoid, you can pass a 3-item vector of sizes to `r=` or `d=`.
2021-04-21 01:58:07 +00:00
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-04-21 01:58:07 +00:00
module spikeball(r, d, scale, anchor=CENTER, spin=0, orient=UP) {
r = get_radius(r=r,d=d,dflt=1);
attachable(anchor,spin,orient, r=r*1.1*scale) {
union() {
2022-10-14 23:29:09 +00:00
sphere_copies(r=r, n=512, scale=scale, cone_ang=180) cylinder(r1=r/10, r2=0, h=r/10);
2021-04-21 01:58:07 +00:00
scale(scale) sphere(r=r);
}
children();
}
}
spikeball(r=50, scale=[0.75,1,1.5]) show_anchors(20);
2021-04-21 01:58:07 +00:00
```
### VNF Attachables
If the shape just doesn't fit into any of the above categories, and you constructed it as a
[VNF](vnf.scad), you can use the VNF itself to describe the geometry with the `vnf=` argument.
2021-04-26 22:52:23 +00:00
There are two variations to how anchoring can work for VNFs. When `extent=true`, (the default)
then a plane is projected out from the origin, perpendicularly in the direction of the anchor,
2022-11-13 04:28:59 +00:00
to the furthest distance that intersects with the VNF shape. The anchor point is then the
center of the points that still intersect that plane.
```openscad-FlatSpin,VPD=500
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
module stellate_cube(s=100, anchor=CENTER, spin=0, orient=UP) {
s2 = 3 * s;
verts = [
[0,0,-s2*sqrt(2)/2],
each down(s/2, p=path3d(square(s,center=true))),
each zrot(45, p=path3d(square(s2,center=true))),
each up(s/2, p=path3d(square(s,center=true))),
[0,0,s2*sqrt(2)/2]
];
faces = [
[0,2,1], [0,3,2], [0,4,3], [0,1,4],
[1,2,6], [1,6,9], [6,10,9], [2,10,6],
[1,5,4], [1,9,5], [9,12,5], [5,12,4],
[4,8,3], [4,12,8], [12,11,8], [11,3,8],
[2,3,7], [3,11,7], [7,11,10], [2,7,10],
[9,10,13], [10,11,13], [11,12,13], [12,9,13]
];
vnf = [verts, faces];
attachable(anchor,spin,orient, vnf=vnf) {
vnf_polyhedron(vnf);
children();
}
}
stellate_cube(25) {
attach(UP+RIGHT) {
anchor_arrow(20);
%cube([100,100,0.1],center=true);
}
}
```
2022-11-13 04:28:59 +00:00
When `extent=false`, then the anchor point will be the furthest intersection of the VNF with
the anchor ray from the origin. The orientation of the anchor point will be the normal of the
face at the intersection. If the intersection is at an edge or corner, then the orientation
will bisect the angles between the faces.
```openscad-VPD=1250
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
module stellate_cube(s=100, anchor=CENTER, spin=0, orient=UP) {
s2 = 3 * s;
verts = [
[0,0,-s2*sqrt(2)/2],
each down(s/2, p=path3d(square(s,center=true))),
each zrot(45, p=path3d(square(s2,center=true))),
each up(s/2, p=path3d(square(s,center=true))),
[0,0,s2*sqrt(2)/2]
];
faces = [
[0,2,1], [0,3,2], [0,4,3], [0,1,4],
[1,2,6], [1,6,9], [6,10,9], [2,10,6],
[1,5,4], [1,9,5], [9,12,5], [5,12,4],
[4,8,3], [4,12,8], [12,11,8], [11,3,8],
[2,3,7], [3,11,7], [7,11,10], [2,7,10],
[9,10,13], [10,11,13], [11,12,13], [12,9,13]
];
vnf = [verts, faces];
2021-04-26 22:52:23 +00:00
attachable(anchor,spin,orient, vnf=vnf, extent=false) {
vnf_polyhedron(vnf);
children();
}
}
stellate_cube() show_anchors(50);
```
2021-04-12 07:41:12 +00:00
2021-10-24 03:28:40 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
2021-10-24 03:28:40 +00:00
$fn=32;
R = difference(circle(10), right(2, circle(9)));
2021-12-06 22:28:36 +00:00
linear_sweep(R,height=10,atype="hull")
2021-10-24 03:28:40 +00:00
attach(RIGHT) anchor_arrow();
```
2021-04-12 07:41:12 +00:00
## Making Named Anchors
While vector anchors are often useful, sometimes there are logically extra attachment points that
aren't on the perimeter of the shape. This is what named string anchors are for. For example,
the `teardrop()` shape uses a cylindrical geometry for it's vector anchors, but it also provides
a named anchor "cap" that is at the tip of the hat of the teardrop shape.
2021-09-30 09:04:31 +00:00
Named anchors are passed as an array of `named_anchor()`s to the `anchors=` argument of `attachable()`.
The `named_anchor()` call takes a name string, a positional point, an orientation vector, and a spin.
2022-11-13 04:28:59 +00:00
The name is the name of the anchor. The positional point is where the anchor point is at. The
orientation vector is the direction that a child attached at that anchor point should be oriented.
The spin is the number of degrees that an attached child should be rotated counter-clockwise around
the orientation vector. Spin is optional, and defaults to 0.
To make a simple attachable shape similar to a `teardrop()` that provides a "cap" anchor, you may
define it like this:
2021-09-30 09:04:31 +00:00
```openscad-3D
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
module raindrop(r, thick, anchor=CENTER, spin=0, orient=UP) {
anchors = [
2021-09-30 09:04:31 +00:00
named_anchor("cap", [0,r/sin(45),0], BACK, 0)
];
attachable(anchor,spin,orient, r=r, l=thick, anchors=anchors) {
linear_extrude(height=thick, center=true) {
circle(r=r);
back(r*sin(45)) zrot(45) square(r, center=true);
}
children();
}
}
raindrop(r=25, thick=20, anchor="cap");
```
If you want multiple named anchors, just add them to the list of anchors:
2021-05-21 08:31:16 +00:00
```openscad-FlatSpin,VPD=150
2022-02-09 02:40:58 +00:00
include <BOSL2/std.scad>
module raindrop(r, thick, anchor=CENTER, spin=0, orient=UP) {
anchors = [
2021-09-30 09:04:31 +00:00
named_anchor("captop", [0,r/sin(45), thick/2], BACK+UP, 0),
named_anchor("cap", [0,r/sin(45), 0 ], BACK, 0),
named_anchor("capbot", [0,r/sin(45),-thick/2], BACK+DOWN, 0)
];
attachable(anchor,spin,orient, r=r, l=thick, anchors=anchors) {
linear_extrude(height=thick, center=true) {
circle(r=r);
back(r*sin(45)) zrot(45) square(r, center=true);
}
children();
}
}
raindrop(r=15, thick=10) show_anchors();
```
Sometimes the named anchor you want to add may be at a point that is reached through a complicated
set of translations and rotations. One quick way to calculate that point is to reproduce those
transformations in a transformation matrix chain. This is simplified by how you can use the
function forms of almost all the transformation modules to get the transformation matrices, and
chain them together with matrix multiplication. For example, if you have:
```
scale([1.1, 1.2, 1.3]) xrot(15) zrot(25) right(20) sphere(d=1);
```
2022-11-13 04:28:59 +00:00
and you want to calculate the center point of the sphere, you can do it like:
```
sphere_pt = apply(
scale([1.1, 1.2, 1.3]) * xrot(15) * zrot(25) * right(20),
[0,0,0]
);
```
2021-04-12 07:41:12 +00:00