Merge pull request #763 from revarbat/revarbat_dev

atext() to text3d().  Overrode text() for 2D attachability.
This commit is contained in:
Revar Desmera 2022-01-18 23:43:49 -08:00 committed by GitHub
commit 2ed5b02c52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 147 additions and 41 deletions

View file

@ -100,30 +100,30 @@ _ANCHOR_TYPES = ["intersect","hull"];
// Figure(3D,Big,VPD=6): Named constants for direction vectors. Some directions have more than one name.
// $fn=12;
// stroke([[0,0,0],RIGHT], endcap2="arrow2", width=.05);
// color("black")right(.05)up(.05)move(RIGHT)atext("RIGHT",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// color("black")right(.05)up(.05)move(RIGHT) text3d("RIGHT",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// stroke([[0,0,0],LEFT], endcap2="arrow2", width=.05);
// color("black")left(.05)up(.05)move(LEFT)atext("LEFT",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// color("black")left(.05)up(.05)move(LEFT) text3d("LEFT",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// stroke([[0,0,0],FRONT], endcap2="arrow2", width=.05);
// color("black")
// left(.1){
// up(.12)move(FRONT)atext("FRONT",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// move(FRONT)atext("FWD",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// down(.12)move(FRONT)atext("FORWARD",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// up(.12)move(FRONT) text3d("FRONT",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// move(FRONT) text3d("FWD",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// down(.12)move(FRONT) text3d("FORWARD",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// }
// stroke([[0,0,0],BACK], endcap2="arrow2", width=.05);
// right(.05)
// color("black")move(BACK)atext("BACK",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// color("black")move(BACK) text3d("BACK",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// stroke([[0,0,0],DOWN], endcap2="arrow2", width=.05);
// color("black")
// right(.1){
// up(.12)move(BOT)atext("DOWN",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// move(BOT)atext("BOTTOM",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// down(.12)move(BOT)atext("BOT",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// up(.12)move(BOT) text3d("DOWN",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// move(BOT) text3d("BOTTOM",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// down(.12)move(BOT) text3d("BOT",size=.1,h=.01,anchor=LEFT,orient=FRONT);
// }
// stroke([[0,0,0],TOP], endcap2="arrow2", width=.05);
// color("black")left(.05){
// up(.12)move(TOP)atext("TOP",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// move(TOP)atext("UP",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// up(.12)move(TOP) text3d("TOP",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// move(TOP) text3d("UP",size=.1,h=.01,anchor=RIGHT,orient=FRONT);
// }
// Subsection: Specifying Faces
// Modules operating on faces accept a list of faces to describe the faces to operate on. Each
@ -2457,7 +2457,7 @@ module _show_edges(edges="ALL", size=20, text, txtsize=3,toplabel) {
vpr = [55,0,25];
color("black")
if (is_def(toplabel))
for(h=idx(toplabel)) up(21+6*h)rot(vpr)atext(select(toplabel,-h-1),size=3.3,h=0.1,orient=UP,anchor=FRONT);
for(h=idx(toplabel)) up(21+6*h)rot(vpr) text3d(select(toplabel,-h-1),size=3.3,h=0.1,orient=UP,anchor=FRONT);
}
@ -2630,7 +2630,7 @@ module _show_corners(corners="ALL", size=20, text, txtsize=3,toplabel) {
vpr = [55,0,25];
color("black")
if (is_def(toplabel))
for(h=idx(toplabel)) up(21+6*h)rot(vpr)atext(select(toplabel,-h-1),size=3.3,h=.1,orient=UP,anchor=FRONT);
for(h=idx(toplabel)) up(21+6*h)rot(vpr) text3d(select(toplabel,-h-1),size=3.3,h=.1,orient=UP,anchor=FRONT);
}
module _show_cube_faces(faces, size=20, toplabel,botlabel) {
@ -2643,9 +2643,9 @@ module _show_cube_faces(faces, size=20, toplabel,botlabel) {
vpr = [55,0,25];
color("black"){
if (is_def(toplabel))
for(h=idx(toplabel)) up(21+6*h)rot(vpr)atext(select(toplabel,-h-1),size=3.3,h=.1,orient=UP,anchor=FRONT);
for(h=idx(toplabel)) up(21+6*h)rot(vpr) text3d(select(toplabel,-h-1),size=3.3,h=.1,orient=UP,anchor=FRONT);
if (is_def(botlabel))
for(h=idx(botlabel)) down(26+6*h)rot(vpr)atext(botlabel[h],size=3.3,h=.1,orient=UP,anchor=FRONT);
for(h=idx(botlabel)) down(26+6*h)rot(vpr) text3d(botlabel[h],size=3.3,h=.1,orient=UP,anchor=FRONT);
}
color("yellow",0.7) cuboid(size=size);
}

View file

@ -12,13 +12,15 @@ module _square(size,center=false) square(size,center=center);
module _circle(r,d) circle(r=r,d=d);
module _text(t,size,font,halign,valign,spacing,direction,language,script)
text(t, size=size, font=font,
module _text(text,size,font,halign,valign,spacing,direction,language,script)
text(text, size=size, font=font,
halign=halign, valign=valign,
spacing=spacing, direction=direction,
language=language, script=script
);
module _color(color) if (color==undef) children(); else color(color) children();
module _cube(size,center) cube(size,center=center);
module _cylinder(h,r1,r2,center,r,d,d1,d2) cylinder(h,r=r,d=d,r1=r1,r2=r2,d1=d1,d2=d2,center=center);

View file

@ -9,6 +9,9 @@
//////////////////////////////////////////////////////////////////////
use <builtins.scad>
// Section: Coloring Objects
// Module: rainbow()
@ -116,3 +119,5 @@ function HSV(h,s=1,v=1) =
module HSV(h,s=1,v=1,a=1) color(HSV(h,s,v),a) children();
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View file

@ -1204,7 +1204,7 @@ function teardrop2d(r, ang=45, cap_h, d, anchor=CENTER, spin=0) =
// "right" = center of the right circle
// Example(2D,NoAxes): This first example shows how the egg is constructed from two circles and two joining arcs.
// $fn=100;
// color("red")stroke(egg(78,25,12, 60),closed=true);
// color("red") stroke(egg(78,25,12, 60),closed=true);
// stroke([left(14,circle(25)),
// right(27,circle(12))]);
// Example(2D,Anim,VPD=250,VPR=[0,0,0]): Varying length between circles
@ -1481,6 +1481,98 @@ function reuleaux_polygon(N=3, r, d, anchor=CENTER, spin=0) =
// Section: Text
// Module: text()
// Topics: Attachments, Text
// Usage:
// text(text, [size], [font], ...);
// Description:
// Creates a 3D text block that can be attached to other attachable objects.
// NOTE: This cannot have children attached to it.
// Arguments:
// text = The text string to instantiate as an object.
// size = The font size used to create the text block. Default: 10
// font = The name of the font used to create the text block. Default: "Helvetica"
// ---
// halign = If given, specifies the horizontal alignment of the text. `"left"`, `"center"`, or `"right"`. Overrides `anchor=`.
// valign = If given, specifies the vertical alignment of the text. `"top"`, `"center"`, `"baseline"` or `"bottom"`. Overrides `anchor=`.
// spacing = The relative spacing multiplier between characters. Default: `1.0`
// direction = The text direction. `"ltr"` for left to right. `"rtl"` for right to left. `"ttb"` for top to bottom. `"btt"` for bottom to top. Default: `"ltr"`
// language = The language the text is in. Default: `"en"`
// script = The script the text is in. Default: `"latin"`
// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `"baseline"`
// spin = Rotate this many degrees around the Z axis. See [spin](attachments.scad#subsection-spin). Default: `0`
// See Also: attachable()
// Extra Anchors:
// "baseline" = Anchors at the baseline of the text, at the start of the string.
// str("baseline",VECTOR) = Anchors at the baseline of the text, modified by the X and Z components of the appended vector.
// Examples(2D):
// text("Foobar", size=10);
// text("Foobar", size=12, font="Helvetica");
// text("Foobar", anchor=CENTER);
// text("Foobar", anchor=str("baseline",CENTER));
// Example: Using line_of() distributor
// txt = "This is the string.";
// line_of(spacing=[10,-5],n=len(txt))
// text(txt[$idx], size=10, anchor=CENTER);
// Example: Using arc_of() distributor
// txt = "This is the string";
// arc_of(r=50, n=len(txt), sa=0, ea=180)
// text(select(txt,-1-$idx), size=10, anchor=str("baseline",CENTER), spin=-90);
module text(text, size=10, font="Helvetica", halign, valign, spacing=1.0, direction="ltr", language="en", script="latin", anchor="baseline", spin=0) {
no_children($children);
dummy1 =
assert(is_undef(anchor) || is_vector(anchor) || is_string(anchor), str("Got: ",anchor))
assert(is_undef(spin) || is_vector(spin,3) || is_num(spin), str("Got: ",spin));
anchor = default(anchor, CENTER);
spin = default(spin, 0);
geom = _attach_geom(size=[size,size],two_d=true);
anch = !any([for (c=anchor) c=="["])? anchor :
let(
parts = str_split(str_split(str_split(anchor,"]")[0],"[")[1],","),
vec = [for (p=parts) parse_float(str_strip(p," ",start=true))]
) vec;
ha = halign!=undef? halign :
anchor=="baseline"? "left" :
anchor==anch && is_string(anchor)? "center" :
anch.x<0? "left" :
anch.x>0? "right" :
"center";
va = valign != undef? valign :
starts_with(anchor,"baseline")? "baseline" :
anchor==anch && is_string(anchor)? "center" :
anch.y<0? "bottom" :
anch.y>0? "top" :
"center";
base = anchor=="baseline"? CENTER :
anchor==anch && is_string(anchor)? CENTER :
anch.z<0? BOTTOM :
anch.z>0? TOP :
CENTER;
m = _attach_transform(base,spin,undef,geom);
multmatrix(m) {
$parent_anchor = anchor;
$parent_spin = spin;
$parent_orient = undef;
$parent_geom = geom;
$parent_size = _attach_geom_size(geom);
$attach_to = undef;
do_show = _attachment_is_shown($tags);
if (do_show) {
_color($color) {
_text(
text=text, size=size, font=font,
halign=ha, valign=va, spacing=spacing,
direction=direction, language=language,
script=script
);
}
}
}
}
// Section: Rounding 2D shapes
// Module: round2d()

View file

@ -1835,7 +1835,6 @@ function spheroid(r, style="aligned", d, circum=false, anchor=CENTER, spin=0, or
// Creates a torus shape.
//
// Figure(2D,Med):
// module text3d(t,size=8) text(text=t,size=size,font="Helvetica", halign="center",valign="center");
// module dashcirc(r,start=0,angle=359.9,dashlen=5) let(step=360*dashlen/(2*r*PI)) for(a=[start:step:start+angle]) stroke(arc(r=r,start=a,angle=step/2));
// r = 75; r2 = 30;
// down(r2+0.1) #torus(r_maj=r, r_min=r2, $fn=72);
@ -1847,19 +1846,19 @@ function spheroid(r, style="aligned", d, circum=false, anchor=CENTER, spin=0, or
// }
// rot(240) color("blue") linear_extrude(height=0.01) {
// stroke([[0,0],[r+r2,0]], endcaps="arrow2",width=2);
// right(r) fwd(9) rot(-240) text3d("or",size=10);
// right(r) fwd(9) rot(-240) text("or",size=10,anchor=CENTER);
// }
// rot(135) color("blue") linear_extrude(height=0.01) {
// stroke([[0,0],[r-r2,0]], endcaps="arrow2",width=2);
// right((r-r2)/2) back(8) rot(-135) text3d("ir",size=10);
// right((r-r2)/2) back(8) rot(-135) text("ir",size=10,anchor=CENTER);
// }
// rot(45) color("blue") linear_extrude(height=0.01) {
// stroke([[0,0],[r,0]], endcaps="arrow2",width=2);
// right(r/2) back(8) text3d("r_maj",size=9);
// right(r/2) back(8) text("r_maj",size=9,anchor=CENTER);
// }
// rot(30) color("blue") linear_extrude(height=0.01) {
// stroke([[r,0],[r+r2,0]], endcaps="arrow2",width=2);
// right(r+r2/2) fwd(8) text3d("r_min",size=7);
// right(r+r2/2) fwd(8) text("r_min",size=7,anchor=CENTER);
// }
//
// Arguments:
@ -2071,10 +2070,10 @@ module onion(r, ang=45, cap_h, d, anchor=CENTER, spin=0, orient=UP)
// Section: Text
// Module: atext()
// Module: text3d()
// Topics: Attachments, Text
// Usage:
// atext(text, [h], [size], [font]);
// text3d(text, [h], [size], [font], ...);
// Description:
// Creates a 3D text block that can be attached to other attachable objects.
// NOTE: This cannot have children attached to it.
@ -2082,8 +2081,14 @@ module onion(r, ang=45, cap_h, d, anchor=CENTER, spin=0, orient=UP)
// text = The text string to instantiate as an object.
// h = The height to which the text should be extruded. Default: 1
// size = The font size used to create the text block. Default: 10
// font = The name of the font used to create the text block. Default: "Courier"
// font = The name of the font used to create the text block. Default: "Helvetica"
// ---
// halign = If given, specifies the horizontal alignment of the text. `"left"`, `"center"`, or `"right"`. Overrides `anchor=`.
// valign = If given, specifies the vertical alignment of the text. `"top"`, `"center"`, `"baseline"` or `"bottom"`. Overrides `anchor=`.
// spacing = The relative spacing multiplier between characters. Default: `1.0`
// direction = The text direction. `"ltr"` for left to right. `"rtl"` for right to left. `"ttb"` for top to bottom. `"btt"` for bottom to top. Default: `"ltr"`
// language = The language the text is in. Default: `"en"`
// script = The script the text is in. Default: `"latin"`
// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `"baseline"`
// spin = Rotate this many degrees around the Z axis. See [spin](attachments.scad#subsection-spin). Default: `0`
// orient = Vector to rotate top towards. See [orient](attachments.scad#subsection-orient). Default: `UP`
@ -2092,20 +2097,20 @@ module onion(r, ang=45, cap_h, d, anchor=CENTER, spin=0, orient=UP)
// "baseline" = Anchors at the baseline of the text, at the start of the string.
// str("baseline",VECTOR) = Anchors at the baseline of the text, modified by the X and Z components of the appended vector.
// Examples:
// atext("Foobar", h=3, size=10);
// atext("Foobar", h=2, size=12, font="Helvetica");
// atext("Foobar", h=2, anchor=CENTER);
// atext("Foobar", h=2, anchor=str("baseline",CENTER));
// atext("Foobar", h=2, anchor=str("baseline",BOTTOM+RIGHT));
// text3d("Foobar", h=3, size=10);
// text3d("Foobar", h=2, size=12, font="Helvetica");
// text3d("Foobar", h=2, anchor=CENTER);
// text3d("Foobar", h=2, anchor=str("baseline",CENTER));
// text3d("Foobar", h=2, anchor=str("baseline",BOTTOM+RIGHT));
// Example: Using line_of() distributor
// txt = "This is the string.";
// line_of(spacing=[10,-5],n=len(txt))
// atext(txt[$idx], size=10, anchor=CENTER);
// text3d(txt[$idx], size=10, anchor=CENTER);
// Example: Using arc_of() distributor
// txt = "This is the string";
// arc_of(r=50, n=len(txt), sa=0, ea=180)
// atext(select(txt,-1-$idx), size=10, anchor=str("baseline",CENTER), spin=-90);
module atext(text, h=1, size=9, font="Courier", anchor="baseline", spin=0, orient=UP) {
// text3d(select(txt,-1-$idx), size=10, anchor=str("baseline",CENTER), spin=-90);
module text3d(text, h=1, size=10, font="Helvetica", halign, valign, spacing=1.0, direction="ltr", language="em", script="latin", anchor="baseline[-1,0,-1]", spin=0, orient=UP) {
no_children($children);
dummy1 =
assert(is_undef(anchor) || is_vector(anchor) || is_string(anchor), str("Got: ",anchor))
@ -2136,6 +2141,7 @@ module atext(text, h=1, size=9, font="Courier", anchor="baseline", spin=0, orien
anch.z>0? TOP :
CENTER;
m = _attach_transform(base,spin,orient,geom);
echo(anchor=anchor, anch=anch, base=base);
multmatrix(m) {
$parent_anchor = anchor;
$parent_spin = spin;
@ -2145,18 +2151,20 @@ module atext(text, h=1, size=9, font="Courier", anchor="baseline", spin=0, orien
$attach_to = undef;
do_show = _attachment_is_shown($tags);
if (do_show) {
if (is_undef($color)) {
_color($color) {
linear_extrude(height=h, center=true)
text(text=text, size=size, halign=ha, valign=va, font=font);
} else color($color) {
$color = undef;
linear_extrude(height=h, center=true)
text(text=text, size=size, halign=ha, valign=va, font=font);
_text(
text=text, size=size, font=font,
halign=ha, valign=va, spacing=spacing,
direction=direction, language=language,
script=script
);
}
}
}
}
// This could be replaced with _cut_to_seg_u_form
function _cut_interp(pathcut, path, data) =
[for(entry=pathcut)
@ -2171,7 +2179,6 @@ function _cut_interp(pathcut, path, data) =
];
// Module: path_text()
// Usage:
// path_text(path, text, [size], [thickness], [font], [lettersize], [offset], [reverse], [normal], [top], [textmetrics])