From 9c3e62d9d7afb7f164f67cdd2f53626956aa0401 Mon Sep 17 00:00:00 2001 From: Matthias Urlichs Date: Sun, 13 Oct 2024 06:41:49 +0200 Subject: [PATCH] Add an "Align.NONE" enum member. This option means "leave me alone". It is necessary for placing text on its baseline. --- docs/cheat_sheet.rst | 2 +- docs/objects_2d.py | 10 +++++----- src/build123d/build_common.py | 4 ++++ src/build123d/build_enums.py | 1 + src/build123d/geometry.py | 2 ++ src/build123d/objects_part.py | 2 ++ src/build123d/objects_sketch.py | 4 ++++ 7 files changed, 19 insertions(+), 6 deletions(-) diff --git a/docs/cheat_sheet.rst b/docs/cheat_sheet.rst index ef6a2652..e5f68ef9 100644 --- a/docs/cheat_sheet.rst +++ b/docs/cheat_sheet.rst @@ -215,7 +215,7 @@ Cheat Sheet .. card:: Enums +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+ - | :class:`~build_enums.Align` | MIN, CENTER, MAX | + | :class:`~build_enums.Align` | MIN, CENTER, MAX, NONE | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+ | :class:`~build_enums.ApproxOption` | ARC, NONE, SPLINE | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+ diff --git a/docs/objects_2d.py b/docs/objects_2d.py index 9122fad7..e29c39aa 100644 --- a/docs/objects_2d.py +++ b/docs/objects_2d.py @@ -112,7 +112,7 @@ # [Ex. 11] with BuildSketch() as example_11: - Text("text", 1) + Text("text", 1, align=(Align.CENTER, Align.NONE)) # [Ex. 11] s = 100 / max(*example_11.sketch.bounding_box().size) svg = ExportSVG(scale=s) @@ -123,7 +123,7 @@ with BuildSketch() as example_12: t = Trapezoid(2, 1, 80) with Locations((-0.6, -0.3)): - Text("80°", 0.3, mode=Mode.SUBTRACT) + Text("80°", 0.3, mode=Mode.SUBTRACT, align=(Align.CENTER, Align.NONE)) # [Ex. 12] s = 100 / max(*example_12.sketch.bounding_box().size) svg = ExportSVG(scale=s) @@ -207,9 +207,9 @@ p1 = CenterArc(t.vertices().group_by(Axis.Y)[0].sort_by(Axis.X)[0], 8, 0, t.B) p2 = CenterArc(t.vertices().group_by(Axis.Y)[0].sort_by(Axis.X)[-1], 8, 180 - t.C, t.C) p3 = CenterArc(t.vertices().sort_by(Axis.Y)[-1], 8, 270 - t.A / 2, t.A) -t1 = Text("B", font_size=d.font_size).moved(Pos(p1 @ 0.5)) -t2 = Text("C", font_size=d.font_size).moved(Pos(p2 @ 0.5)) -t3 = Text("A", font_size=d.font_size).moved(Pos(p3 @ 0.5)) +t1 = Text("B", font_size=d.font_size).moved(Pos(p1 @ 0.5, align=(Align.CENTER, Align.NONE)) +t2 = Text("C", font_size=d.font_size).moved(Pos(p2 @ 0.5, align=(Align.CENTER, Align.NONE)) +t3 = Text("A", font_size=d.font_size).moved(Pos(p3 @ 0.5, align=(Align.CENTER, Align.NONE)) s = 100 / max(*isosceles_triangle.sketch.bounding_box().size) svg = ExportSVG(scale=s) diff --git a/src/build123d/build_common.py b/src/build123d/build_common.py index 6f538d03..46a63703 100644 --- a/src/build123d/build_common.py +++ b/src/build123d/build_common.py @@ -951,6 +951,8 @@ def __init__( align_offset.append(-size[i] / 2) elif self.align[i] == Align.MAX: align_offset.append(-size[i]) + elif self.align[i] == Align.NONE: + align_offset.append(0) # Align the points points = ShapeList( @@ -1151,6 +1153,8 @@ def __init__( align_offset.append(-size[i] / 2) elif self.align[i] == Align.MAX: align_offset.append(-size[i]) + elif self.align[i] == Align.NONE: + align_offset.append(0) self.min = Vector(*align_offset) #: bottom left corner self.max = self.min + self.size #: top right corner diff --git a/src/build123d/build_enums.py b/src/build123d/build_enums.py index b90b59fb..879368bf 100644 --- a/src/build123d/build_enums.py +++ b/src/build123d/build_enums.py @@ -36,6 +36,7 @@ class Align(Enum): MIN = auto() CENTER = auto() MAX = auto() + NONE = auto() def __repr__(self): return f"<{self.__class__.__name__}.{self.name}>" diff --git a/src/build123d/geometry.py b/src/build123d/geometry.py index 12341984..c04a4611 100644 --- a/src/build123d/geometry.py +++ b/src/build123d/geometry.py @@ -1033,6 +1033,8 @@ def to_align_offset(self, align: Tuple[float, float]) -> Tuple[float, float]: ) elif align[i] == Align.MAX: align_offset.append(-self.max.to_tuple()[i]) + elif align[i] == Align.NONE: + align_offset.append(0) return align_offset diff --git a/src/build123d/objects_part.py b/src/build123d/objects_part.py index 365683a0..ff62db14 100644 --- a/src/build123d/objects_part.py +++ b/src/build123d/objects_part.py @@ -73,6 +73,8 @@ def __init__( ) elif align[i] == Align.MAX: align_offset.append(-bbox.max.to_tuple()[i]) + elif align[i] == Align.NONE: + align_offset.append(0) part.move(Location(Vector(*align_offset))) context: BuildPart = BuildPart._get_context(self, log=False) diff --git a/src/build123d/objects_sketch.py b/src/build123d/objects_sketch.py index b48d45d8..60fb72b1 100644 --- a/src/build123d/objects_sketch.py +++ b/src/build123d/objects_sketch.py @@ -344,6 +344,8 @@ def __init__( align_offset.append(0) elif align[i] == Align.MAX: align_offset.append(-maxs[i]) + elif align[i] == Align.NONE: + align_offset.append(0) else: align_offset = [0, 0] pts = [point + Vector(*align_offset) for point in pts] @@ -531,6 +533,8 @@ class Text(BaseSketchObject): text, values must be between 0.0 and 1.0. Defaults to 0.0. rotation (float, optional): angles to rotate objects. Defaults to 0. mode (Mode, optional): combination mode. Defaults to Mode.ADD. + + "Align.NONE", used vertically, places the text on its baseline. """ # pylint: disable=too-many-instance-attributes