Improving split to explicitly handle all Keep Enum values

This commit is contained in:
gumyr 2025-12-03 10:13:09 -05:00
parent 6605b676a3
commit 3871345dcd
3 changed files with 16 additions and 7 deletions

View file

@ -963,9 +963,9 @@ def split(
for obj in object_list: for obj in object_list:
bottom = None bottom = None
if keep == Keep.BOTH: if keep == Keep.BOTH:
top, bottom = obj.split(bisect_by, keep) # type: ignore[arg-type] top, bottom = obj.split(bisect_by, keep)
else: else:
top = obj.split(bisect_by, keep) # type: ignore[arg-type] top = obj.split(bisect_by, keep)
for subpart in [top, bottom]: for subpart in [top, bottom]:
if isinstance(subpart, Iterable): if isinstance(subpart, Iterable):
new_objects.extend(subpart) new_objects.extend(subpart)

View file

@ -1805,6 +1805,12 @@ class Shape(NodeMixin, Generic[TOPODS]):
]: ]:
"""split and keep inside and outside""" """split and keep inside and outside"""
@overload
def split(
self, tool: TrimmingTool, keep: Literal[Keep.INSIDE, Keep.OUTSIDE]
) -> None:
"""invalid split"""
@overload @overload
def split(self, tool: TrimmingTool) -> Self | list[Self] | None: def split(self, tool: TrimmingTool) -> Self | list[Self] | None:
"""split and keep inside (default)""" """split and keep inside (default)"""
@ -1834,6 +1840,9 @@ class Shape(NodeMixin, Generic[TOPODS]):
if self._wrapped is None or not tool: if self._wrapped is None or not tool:
raise ValueError("Can't split an empty edge/wire/tool") raise ValueError("Can't split an empty edge/wire/tool")
if keep in [Keep.INSIDE, Keep.OUTSIDE]:
raise ValueError(f"{keep} is invalid")
shape_list = TopTools_ListOfShape() shape_list = TopTools_ListOfShape()
shape_list.Append(self.wrapped) shape_list.Append(self.wrapped)
@ -1924,7 +1933,6 @@ class Shape(NodeMixin, Generic[TOPODS]):
return top return top
if keep == Keep.BOTTOM: if keep == Keep.BOTTOM:
return bottom return bottom
return None
@overload @overload
def split_by_perimeter( def split_by_perimeter(

View file

@ -172,10 +172,11 @@ class TestShape(unittest.TestCase):
self.assertEqual(len(top), 2) self.assertEqual(len(top), 2)
self.assertAlmostEqual(top[0].length, 3, 5) self.assertAlmostEqual(top[0].length, 3, 5)
def test_split_return_none(self): def test_split_invalid_keep(self):
shape = Box(1, 1, 1) - Pos((0, 0, -0.25)) * Box(1, 0.5, 0.5) with self.assertRaises(ValueError):
split_shape = shape.split(Plane.XY, keep=Keep.INSIDE) Box(1, 1, 1).split(Plane.XY, keep=Keep.INSIDE)
self.assertIsNone(split_shape) with self.assertRaises(ValueError):
Box(1, 1, 1).split(Plane.XY, keep=Keep.OUTSIDE)
def test_split_by_perimeter(self): def test_split_by_perimeter(self):
# Test 0 - extract a spherical cap # Test 0 - extract a spherical cap