Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add intersection operator, &, to ShapeList #360

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/build123d/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -3650,6 +3650,10 @@ def __or__(self, filter_by: Union[Axis, GeomType] = Axis.Z):
"""Filter by axis or geomtype operator |"""
return self.filter_by(filter_by)

def __eq__(self, other: ShapeList):
"""ShapeLists equality operator =="""
return set(self) == set(other)

def __add__(self, other: ShapeList):
"""Combine two ShapeLists together operator +"""
return ShapeList(list(self) + list(other))
Expand All @@ -3661,6 +3665,10 @@ def __sub__(self, other: ShapeList) -> ShapeList:
# return ShapeList(hash_set.values())
return ShapeList(set(self) - set(other))

def __and__(self, other: ShapeList):
"""Intersect two ShapeLists operator &"""
return ShapeList(set(self) & set(other))

@overload
def __getitem__(self, key: int) -> T:
...
Expand Down
8 changes: 8 additions & 0 deletions tests/test_build_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,14 @@ def test_shapes(self):
Box(1, 1, 1)
self.assertIsNone(test._shapes(Compound))

def test_operators(self):
with BuildPart() as test:
Box(1, 1, 1)
self.assertEqual(
(test.faces() | Axis.Z).edges() & (test.faces() | Axis.Y).edges(),
(test.edges() | Axis.X)
)


class TestValidateInputs(unittest.TestCase):
def test_wrong_builder(self):
Expand Down
Loading