Skip to content

Commit 1102dfa

Browse files
authored
Merge pull request gumyr#1003 from jwagenet/slot-fixes
Bugfixes: Slot objects
2 parents 08a9014 + 82f65f7 commit 1102dfa

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

src/build123d/objects_sketch.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def __init__(
387387
self.slot_height = height
388388

389389
arc = arc if isinstance(arc, Wire) else Wire([arc])
390-
face = Face(arc.offset_2d(height / 2)).rotate(Axis.Z, rotation)
390+
face = Face(arc.offset_2d(height / 2))
391391
super().__init__(face, rotation, None, mode)
392392

393393

@@ -426,10 +426,10 @@ def __init__(
426426

427427
half_line = point_v - center_v
428428

429-
if half_line.length * 2 <= height:
429+
if half_line.length <= 0:
430430
raise ValueError(
431-
f"Slots must have width > height. "
432-
"Got: {height=} width={half_line.length * 2} (computed)"
431+
"Distance between center and point must be greater than 0 "
432+
f"Got: distance = {half_line.length} (computed)"
433433
)
434434

435435
face = Face(
@@ -464,7 +464,7 @@ def __init__(
464464
rotation: float = 0,
465465
mode: Mode = Mode.ADD,
466466
):
467-
if center_separation <= 0:
467+
if center_separation < 0:
468468
raise ValueError(
469469
f"Requires center_separation > 0. Got: {center_separation=}"
470470
)
@@ -475,14 +475,18 @@ def __init__(
475475
self.center_separation = center_separation
476476
self.slot_height = height
477477

478-
face = Face(
479-
Wire(
480-
[
481-
Edge.make_line(Vector(-center_separation / 2, 0, 0), Vector()),
482-
Edge.make_line(Vector(), Vector(+center_separation / 2, 0, 0)),
483-
]
484-
).offset_2d(height / 2)
485-
)
478+
if center_separation > 0:
479+
face = Face(
480+
Wire(
481+
[
482+
Edge.make_line(Vector(-center_separation / 2, 0, 0), Vector()),
483+
Edge.make_line(Vector(), Vector(+center_separation / 2, 0, 0)),
484+
]
485+
).offset_2d(height / 2)
486+
)
487+
else:
488+
face = cast(Face, Circle(height / 2, mode=mode).face())
489+
486490
super().__init__(face, rotation, None, mode)
487491

488492

@@ -510,7 +514,7 @@ def __init__(
510514
align: Align | tuple[Align, Align] | None = (Align.CENTER, Align.CENTER),
511515
mode: Mode = Mode.ADD,
512516
):
513-
if width <= height:
517+
if width < height:
514518
raise ValueError(
515519
f"Slot requires that width > height. Got: {width=}, {height=}"
516520
)
@@ -521,7 +525,7 @@ def __init__(
521525
self.width = width
522526
self.slot_height = height
523527

524-
if width != height:
528+
if width > height:
525529
face = Face(
526530
Wire(
527531
[
@@ -532,6 +536,7 @@ def __init__(
532536
)
533537
else:
534538
face = cast(Face, Circle(width / 2, mode=mode).face())
539+
535540
super().__init__(face, rotation, align, mode)
536541

537542

tests/test_build_sketch.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,25 +328,40 @@ def test_slot_center_point(self):
328328
self.assertEqual(s.faces()[0].normal_at(), Vector(0, 0, 1))
329329

330330
def test_slot_center_to_center(self):
331+
height = 2
331332
with BuildSketch() as test:
332-
s = SlotCenterToCenter(4, 2)
333+
s = SlotCenterToCenter(4, height)
333334
self.assertEqual(s.center_separation, 4)
334-
self.assertEqual(s.slot_height, 2)
335+
self.assertEqual(s.slot_height, height)
335336
self.assertEqual(s.rotation, 0)
336337
self.assertEqual(s.mode, Mode.ADD)
337-
self.assertAlmostEqual(test.sketch.area, pi + 4 * 2, 5)
338+
self.assertAlmostEqual(test.sketch.area, pi + 4 * height, 5)
338339
self.assertEqual(s.faces()[0].normal_at(), Vector(0, 0, 1))
339340

341+
# Circle degenerate
342+
s1 = SlotCenterToCenter(0, height)
343+
self.assertTrue(len(s1.edges()) == 1)
344+
self.assertEqual(s1.edge().geom_type, GeomType.CIRCLE)
345+
self.assertAlmostEqual(s1.edge().radius, height / 2)
346+
347+
340348
def test_slot_overall(self):
349+
height = 2
341350
with BuildSketch() as test:
342-
s = SlotOverall(6, 2)
351+
s = SlotOverall(6, height)
343352
self.assertEqual(s.width, 6)
344-
self.assertEqual(s.slot_height, 2)
353+
self.assertEqual(s.slot_height, height)
345354
self.assertEqual(s.rotation, 0)
346355
self.assertEqual(s.mode, Mode.ADD)
347-
self.assertAlmostEqual(test.sketch.area, pi + 4 * 2, 5)
356+
self.assertAlmostEqual(test.sketch.area, pi + 4 * height, 5)
348357
self.assertEqual(s.faces()[0].normal_at(), Vector(0, 0, 1))
349358

359+
# Circle degenerat
360+
s1 = SlotOverall(2, height)
361+
self.assertTrue(len(s1.edges()) == 1)
362+
self.assertEqual(s1.edge().geom_type, GeomType.CIRCLE)
363+
self.assertAlmostEqual(s1.edge().radius, height / 2)
364+
350365
def test_text(self):
351366
with BuildSketch() as test:
352367
t = Text("test", 2)
@@ -530,9 +545,9 @@ def test_full_round(self):
530545
@pytest.mark.parametrize(
531546
"slot,args",
532547
[
533-
(SlotOverall, (5, 10)),
548+
(SlotOverall, (9, 10)),
534549
(SlotCenterToCenter, (-1, 10)),
535-
(SlotCenterPoint, ((0, 0, 0), (2, 0, 0), 10)),
550+
(SlotCenterPoint, ((0, 0, 0), (0, 0, 0), 10)),
536551
],
537552
)
538553
def test_invalid_slots(slot, args):

0 commit comments

Comments
 (0)