Skip to content

Commit c6d2676

Browse files
tutorials updates and bugfix
1 parent 76c8f18 commit c6d2676

3 files changed

Lines changed: 335 additions & 111 deletions

File tree

‎attachments.scad‎

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -735,17 +735,17 @@ function _make_anchor_legal(anchor,geom) =
735735
// Topics: Attachments
736736
// See Also: attachable(), position(), align(), face_profile(), edge_profile(), corner_profile()
737737
// Usage:
738-
// PARENT() attach(from, to, [align=], [spin=], [overlap=]) CHILDREN;
739-
// PARENT() attach(from, [overlap=], [spin=]) CHILDREN;
738+
// PARENT() attach(parent, child, [align=], [spin=], [overlap=]) CHILDREN;
739+
// PARENT() attach(parent, [overlap=], [spin=]) CHILDREN;
740740
// Description:
741741
// Attaches children to a parent object at an anchor point or points, oriented in the anchor direction.
742742
// This module differs from {{position()}} and {{align()}} in that it rotates the children to
743743
// the anchor direction, which generally means it places the children on the surface of a parent.
744744
// There are two modes of operation, single argument and double argument.
745745
// .
746746
// The double argument version is usually easier to use, and it is more powerful because it supports
747-
// alignment. You provide an anchor on the parent `from` and an anchor on the child `to`.
748-
// This connects the `to` anchor on the child to the `from` anchor on the parent.
747+
// alignment. You provide an anchor on the parent `parent` and an anchor on the child `child`.
748+
// This connects the `child` anchor on the child to the `parent` anchor on the parent.
749749
// They are connected to the parent by pointing their anchor arrows at each other. The most basic case
750750
// is `attach(TOP,BOT)` which puts the bottom of the child onto the top of the parent. If you
751751
// do `attach(RIGHT,BOT)` this puts the bottom of the child onto the right anchor of the parent.
@@ -767,7 +767,7 @@ function _make_anchor_legal(anchor,geom) =
767767
// ignored with the **double argument** version of `attach()`. As noted above, you can give `spin=` to the
768768
// child but using the `spin=` parameter to `attach()` is more likely to be useful.
769769
// .
770-
// For the single parameter version of `attach()` you give only the `from` anchor. The `align` direction
770+
// For the single parameter version of `attach()` you give only the `parent` anchor. The `align` direction
771771
// is not permitted. In this case the child is placed at the specified parent anchor point
772772
// and rotated to the anchor direction. For example, `attach(TOP) cuboid(2);` will place a small
773773
// cube **with its center** located at the TOP anchor of the parent, so just half the cube will project
@@ -786,10 +786,10 @@ function _make_anchor_legal(anchor,geom) =
786786
// For a step-by-step explanation of
787787
// attachments, see the [Attachments Tutorial](Tutorial-Attachments).
788788
// Arguments:
789-
// from = The parent anchor point to attach to or a list of parent anchor points.
790-
// to = Optional name of the child anchor point. If given, orients the child to connect this anchor point to the parent anchor.
789+
// parent = The parent anchor point to attach to or a list of parent anchor points.
790+
// child = Optional child anchor point. If given, orients the child to connect this anchor point to the parent anchor.
791791
// ---
792-
// align = If `to` is given you can specify alignment to shift the child to an edge or corner of the parent.
792+
// align = If `child` is given you can specify alignment to shift the child to an edge or corner of the parent.
793793
// overlap = Amount to sink child into the parent. Equivalent to `down(X)` after the attach. This defaults to the value in `$overlap`, which is `0` by default.
794794
// spin = Amount to rotate the parent around the axis of the parent anchor.
795795
// Side Effects:
@@ -803,45 +803,57 @@ function _make_anchor_legal(anchor,geom) =
803803
// attach(FRONT, BOTTOM, overlap=1.5) cyl(l=11.5, d1=10, d2=5);
804804
// }
805805

806-
module attach(from, to, overlap, align, spin=0, norot)
806+
module attach(parent, child, overlap, align, spin=0, norot, from, to)
807807
{
808-
req_children($children);
808+
dummy3=
809+
assert(num_defined([to,child])<2, "Cannot combine deprecated 'to' argument with 'child' parameter")
810+
assert(num_defined([from,parent])<2, "Cannot combine deprecated 'from' argument with 'parent' parameter");
811+
if (is_def(to))
812+
echo("The 'to' option to attach() is deprecated and will be removed in the future. Use 'child' instead.");
813+
if (is_def(from))
814+
echo("The 'from' option to attach(0 is deprecated and will be removed in the future. Use 'parent' instead");
809815
if (norot)
810816
echo("The 'norot' option to attach() is deprecated and will be removed in the future. Use position() instead.");
817+
req_children($children);
818+
811819
dummy=assert($parent_geom != undef, "No object to attach to!")
812820
assert(is_undef(align) || (is_vector(align) && (len(align)==2 || len(align)==3)), "align must be a 2-vector or 3-vector")
813-
assert(is_undef(to) || is_string(to) || (is_vector(to) && (len(to)==2 || len(to)==3)), "to must be a named anchor (a string) or a 2-vector or 3-vector")
814-
assert(is_undef(align) || !is_string(to), "to is a named anchor. Named anchors are not supported with align=");
821+
assert(is_undef(child) || is_string(child) || (is_vector(child) && (len(child)==2 || len(child)==3)), "child must be a named anchor (a string) or a 2-vector or 3-vector")
822+
assert(is_undef(align) || !is_string(child), "child is a named anchor. Named anchors are not supported with align=");
815823
overlap = (overlap!=undef)? overlap : $overlap;
816-
anchors = (is_vector(from)||is_string(from))? [from] : from;
824+
anchors = (is_vector(parent)||is_string(parent))? [parent] : parent;
817825
two_d = _attach_geom_2d($parent_geom);
818-
to = two_d ? _force_anchor_2d(to) : to;
826+
parent = one_defined([parent,from],"parent,from");
827+
dummy4 = assert(is_string(parent) || is_list(parent), "Invalid parent anchor or anchor list");
828+
child = two_d ? _force_anchor_2d(child) : child;
819829
align = is_undef(align) ? undef
820830
: two_d ? _force_anchor_2d(align) : point3d(align);
821-
dummy2=assert(is_undef(align) || is_def(to), "Cannot use 'align' without 'to'");
831+
dummy2=assert(is_undef(align) || is_def(child), "Cannot use 'align' without 'child'");
822832
for ($idx = idx(anchors)) {
823833
dummy2=
824834
assert(is_string(anchors[$idx]) || (is_vector(anchors[$idx]) && (len(anchors[$idx])==2 || len(anchors[$idx])==3)),
825-
str("from[",$idx,"] is ",anchors[$idx]," but it must be a named anchor (string) or a 2-vector or 3-vector"))
835+
str("parent[",$idx,"] is ",anchors[$idx]," but it must be a named anchor (string) or a 2-vector or 3-vector"))
826836
assert(is_undef(align) || !is_string(anchors[$idx]),
827-
str("from[",$idx,"] is a named anchor (",anchors[$idx],"), but named anchors are not wupported with align="));
837+
str("parent[",$idx,"] is a named anchor (",anchors[$idx],"), but named anchors are not wupported with align="));
828838
anchr = is_string(anchors[$idx])? anchors[$idx]
829839
: two_d?_force_anchor_2d(anchors[$idx])
830840
:anchors[$idx];
831841
dummy=assert(is_undef(align) || all_zero(v_mul(anchr,align)),
832-
str("align (",align,") cannot include component parallel to anchor (",anchr,")"));
842+
str("align (",align,") cannot include component parallel to parent anchor (",anchr,")"));
833843
anch = _find_anchor(anchr, $parent_geom);
834844
pos = is_undef(align) ? anch[1] : _find_anchor(anchr+align, $parent_geom)[1];
835-
$attach_to = to;
845+
$attach_to = child;
836846
$attach_anchor = list_set(anch, 1, pos); ///
837847
startdir = anchr==UP || anchr==DOWN ? BACK : UP;
838-
enddir = is_undef(to) || to.z==0 ? UP : BACK;
848+
enddir = is_undef(child) || child.z==0 ? UP : BACK;
839849
anchor_adjustment = is_undef(align)? CTR
840-
: two_d ? zrot(spin, rot(to=to,from=-anchr,p=align))
841-
: zrot(spin,frame_map(x=to, z=enddir,p=frame_map(x=-anchr, z=startdir, reverse=true, p=align)));
850+
: two_d ? zrot(spin, rot(to=child,from=-anchr,p=align))
851+
: apply( frame_map(x=child, z=enddir)
852+
*frame_map(x=-anchr, z=startdir, reverse=true)
853+
*rot(v=parent,-spin), align);
842854

843855
$anchor_override=all_zero(anchor_adjustment)?undef
844-
:to+anchor_adjustment;
856+
:child+anchor_adjustment;
845857
olap = two_d? [0,-overlap,0] : [0,0,-overlap];
846858
anchrvec = two_d? BACK : UP;
847859
spinaxis = two_d? UP : anch[2];

‎shapes3d.scad‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2935,7 +2935,7 @@ function onion(r, ang=45, cap_h, d, anchor=CENTER, spin=0, orient=UP) =
29352935
// Usage:
29362936
// text3d(text, [h], [size], [font], [language=], [script=], [direction=], [atype=], [anchor=], [spin=], [orient=]);
29372937
// Description:
2938-
// Creates a 3D text block that supports anchoring and attachment to attachable objects. You cannot attach children to text.
2938+
// Creates a 3D text block that supports anchoring and single-parameter attachment to attachable objects. You cannot attach children to text.
29392939
// .
29402940
// Historically fonts were specified by their "body size", the height of the metal body
29412941
// on which the glyphs were cast. This means the size was an upper bound on the size
@@ -2988,6 +2988,7 @@ module text3d(text, h, size=10, font="Helvetica", spacing=1.0, direction="ltr",
29882988
h = one_defined([h,height,thickness],"h,height,thickness",dflt=1);
29892989
assert(is_undef(atype) || in_list(atype,["ycenter","baseline"]), "atype must be \"ycenter\" or \"baseline\"");
29902990
assert(is_bool(center));
2991+
assert(is_undef($attach_to),"text3d() does not support parent-child anchor attachment with two parameters");
29912992
atype = default(atype, center?"ycenter":"baseline");
29922993
anchor = default(anchor, center?CENTER:LEFT);
29932994
geom = attach_geom(size=[size,size,h]);

0 commit comments

Comments
 (0)