Skip to content

Commit

Permalink
Improved the section operator
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Sep 6, 2023
1 parent 3d32c14 commit 8209624
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/build123d/operations_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def section(
section_by: Union[Plane, Iterable[Plane]] = Plane.XZ,
height: float = 0.0,
clean: bool = True,
mode: Mode = Mode.INTERSECT,
mode: Mode = Mode.PRIVATE,
) -> Sketch:
"""Part Operation: section
Expand Down Expand Up @@ -501,16 +501,23 @@ def section(
)
for plane in section_planes
]
if obj is None:
if context is not None and context._obj is not None:
obj = context.part
else:
raise ValueError("obj must be provided")

new_objects = [obj.intersect(plane) for plane in planes]

if context is not None:
context._add_to_context(*planes, faces_to_pending=False, clean=clean, mode=mode)
result = planes
context._add_to_context(
*new_objects, faces_to_pending=False, clean=clean, mode=mode
)
else:
result = [obj.intersect(plane) for plane in planes]
if clean:
result = [r.clean() for r in result]
new_objects = [r.clean() for r in new_objects]

return Sketch(Compound.make_compound(result).wrapped)
return Sketch(Compound.make_compound(new_objects).wrapped)


def thicken(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_build_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,14 @@ class TestSection(unittest.TestCase):
def test_circle(self):
with BuildPart() as test:
Sphere(10)
section()
self.assertAlmostEqual(test.faces()[-1].area, 100 * pi, 5)
s = section()
self.assertAlmostEqual(s.area, 100 * pi, 5)

def test_custom_plane(self):
with BuildPart() as test:
Sphere(10)
section(section_by=Plane.XZ)
self.assertAlmostEqual(test.faces().filter_by(Axis.Y)[-1].area, 100 * pi, 5)
s = section(section_by=Plane.XZ)
self.assertAlmostEqual(s.area, 100 * pi, 5)


class TestSplit(unittest.TestCase):
Expand Down

0 comments on commit 8209624

Please sign in to comment.