Skip to content

Commit

Permalink
Add an "Align.NONE" enum member.
Browse files Browse the repository at this point in the history
This option means "leave me alone".
It is necessary for placing text on its baseline.
  • Loading branch information
smurfix committed Oct 13, 2024
1 parent e2434d7 commit f54698d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/cheat_sheet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
+----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
Expand Down
10 changes: 5 additions & 5 deletions docs/objects_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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, align=(Align.CENTER, Align.NONE)).moved(Pos(p1 @ 0.5)
t2 = Text("C", font_size=d.font_size, align=(Align.CENTER, Align.NONE)).moved(Pos(p2 @ 0.5)
t3 = Text("A", font_size=d.font_size, align=(Align.CENTER, Align.NONE)).moved(Pos(p3 @ 0.5)

s = 100 / max(*isosceles_triangle.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
Expand Down
4 changes: 4 additions & 0 deletions src/build123d/build_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 955 in src/build123d/build_common.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/build_common.py#L954-L955

Added lines #L954 - L955 were not covered by tests

# Align the points
points = ShapeList(
Expand Down Expand Up @@ -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)

Check warning on line 1157 in src/build123d/build_common.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/build_common.py#L1156-L1157

Added lines #L1156 - L1157 were not covered by tests

self.min = Vector(*align_offset) #: bottom left corner
self.max = self.min + self.size #: top right corner
Expand Down
1 change: 1 addition & 0 deletions src/build123d/build_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}>"
Expand Down
2 changes: 2 additions & 0 deletions src/build123d/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 1037 in src/build123d/geometry.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/geometry.py#L1036-L1037

Added lines #L1036 - L1037 were not covered by tests
return align_offset


Expand Down
2 changes: 2 additions & 0 deletions src/build123d/objects_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 77 in src/build123d/objects_part.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/objects_part.py#L76-L77

Added lines #L76 - L77 were not covered by tests
part.move(Location(Vector(*align_offset)))

context: BuildPart = BuildPart._get_context(self, log=False)
Expand Down
4 changes: 4 additions & 0 deletions src/build123d/objects_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 348 in src/build123d/objects_sketch.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/objects_sketch.py#L347-L348

Added lines #L347 - L348 were not covered by tests
else:
align_offset = [0, 0]
pts = [point + Vector(*align_offset) for point in pts]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f54698d

Please sign in to comment.