Skip to content

Commit eea5cd6

Browse files
authored
Improve text alignment (#1455)
* Add test to check text alignment options * Improve text alignment * Simplify text alignment tests in Cadquery * Refactor text alignment in shapes module * Remove unused import in test_cadquery module * Refactor alignment tests in test_cadquery * Adjust volume value in exporter test
1 parent d1a3a92 commit eea5cd6

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

cadquery/occ_impl/shapes.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@
200200
)
201201

202202
from OCP.StdPrs import StdPrs_BRepFont, StdPrs_BRepTextBuilder as Font_BRepTextBuilder
203+
from OCP.Graphic3d import (
204+
Graphic3d_HTA_LEFT,
205+
Graphic3d_HTA_CENTER,
206+
Graphic3d_HTA_RIGHT,
207+
Graphic3d_VTA_BOTTOM,
208+
Graphic3d_VTA_CENTER,
209+
Graphic3d_VTA_TOP,
210+
)
203211

204212
from OCP.NCollection import NCollection_Utf8String
205213

@@ -3665,23 +3673,28 @@ def makeText(
36653673
font_kind,
36663674
float(size),
36673675
)
3668-
text_flat = Shape(builder.Perform(font_i, NCollection_Utf8String(text)))
3669-
3670-
bb = text_flat.BoundingBox()
3671-
3672-
t = Vector()
3673-
3674-
if halign == "center":
3675-
t.x = -bb.xlen / 2
3676-
elif halign == "right":
3677-
t.x = -bb.xlen
3678-
3679-
if valign == "center":
3680-
t.y = -bb.ylen / 2
3681-
elif valign == "top":
3682-
t.y = -bb.ylen
3683-
3684-
text_flat = text_flat.translate(t)
3676+
if halign == "left":
3677+
theHAlign = Graphic3d_HTA_LEFT
3678+
elif halign == "center":
3679+
theHAlign = Graphic3d_HTA_CENTER
3680+
else: # halign == "right"
3681+
theHAlign = Graphic3d_HTA_RIGHT
3682+
3683+
if valign == "bottom":
3684+
theVAlign = Graphic3d_VTA_BOTTOM
3685+
elif valign == "center":
3686+
theVAlign = Graphic3d_VTA_CENTER
3687+
else: # valign == "top":
3688+
theVAlign = Graphic3d_VTA_TOP
3689+
3690+
text_flat = Shape(
3691+
builder.Perform(
3692+
font_i,
3693+
NCollection_Utf8String(text),
3694+
theHAlign=theHAlign,
3695+
theVAlign=theVAlign,
3696+
)
3697+
)
36853698

36863699
if height != 0:
36873700
vecNormal = text_flat.Faces()[0].normalAt() * height

tests/test_cadquery.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,6 +3875,22 @@ def testText(self):
38753875
)
38763876
)
38773877

3878+
def testTextAlignment(self):
3879+
left_bottom = Workplane().text("I", 10, 0, halign="left", valign="bottom")
3880+
lb_bb = left_bottom.val().BoundingBox()
3881+
self.assertGreaterEqual(lb_bb.xmin, 0)
3882+
self.assertGreaterEqual(lb_bb.ymin, 0)
3883+
3884+
centers = Workplane().text("I", 10, 0, halign="center", valign="center")
3885+
c_bb = centers.val().BoundingBox()
3886+
self.assertAlmostEqual(c_bb.center.x, 0, places=0)
3887+
self.assertAlmostEqual(c_bb.center.y, 0, places=0)
3888+
3889+
right_top = Workplane().text("I", 10, 0, halign="right", valign="top")
3890+
rt_bb = right_top.val().BoundingBox()
3891+
self.assertLessEqual(rt_bb.xmax, 0)
3892+
self.assertLessEqual(rt_bb.ymax, 0)
3893+
38783894
def testParametricCurve(self):
38793895

38803896
from math import sin, cos, pi

tests/test_exporters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ def test_dxf_text(tmpdir, testdatadir):
910910
s2 = Sketch().importDXF(fname)
911911
w2 = Workplane("XZ", origin=(0, -0.5, 0)).placeSketch(s2).extrude(-1)
912912

913-
assert w1.val().Volume() == approx(59.983287, 1e-2)
913+
assert w1.val().Volume() == approx(61.669465, 1e-2)
914914
assert w2.val().Volume() == approx(w1.val().Volume(), 1e-2)
915915
assert w2.intersect(w1).val().Volume() == approx(w1.val().Volume(), 1e-2)
916916

0 commit comments

Comments
 (0)