Merge pull request #465 from voneiden/algebra-minus-dimensions

Support multidimensional minus in algebra mode
This commit is contained in:
Roger Maitland 2024-01-07 10:34:15 -05:00 committed by GitHub
commit d3ad4e9212
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 5 deletions

View file

@ -1694,11 +1694,13 @@ class Shape(NodeMixin):
"""cut shape from self operator -"""
others = other if isinstance(other, (list, tuple)) else [other]
if not all([type(other)._dim == type(self)._dim for other in others]):
raise ValueError(
f"Only shapes with the same dimension can be subtracted "
f"not {type(self).__name__} and {type(other).__name__}"
)
for _other in others:
if type(_other)._dim < type(self)._dim:
raise ValueError(
f"Only shapes with equal or greater dimension can be subtracted: "
f"not {type(self).__name__} ({type(self)._dim}D) and "
f"{type(_other).__name__} ({type(_other)._dim}D)"
)
new_shape = None
if self.wrapped is None:

View file

@ -602,6 +602,54 @@ class AlgebraTests(unittest.TestCase):
with self.assertRaises(ValueError):
r = b & Sketch()
def test_1d_2d_minus(self):
line = Line((0, 0), (1, 1))
rectangle = Rectangle(1, 1)
r = line - rectangle
vertices = r.vertices()
self.assertEquals(len(vertices), 2)
self.assertTupleAlmostEquals(vertices[0], (0.5, 0.5, 0.0), 6)
self.assertTupleAlmostEquals(vertices[1], (1.0, 1.0, 0.0), 6)
def test_1d_3d_minus(self):
line = Line((0, 0), (1, 1))
box = Box(1, 1, 1)
r = line - box
vertices = r.vertices()
self.assertEquals(len(vertices), 2)
self.assertTupleAlmostEquals(vertices[0], (0.5, 0.5, 0.0), 6)
self.assertTupleAlmostEquals(vertices[1], (1.0, 1.0, 0.0), 6)
def test_2d_3d_minus(self):
rectangle = Pos(0.5, 0, 0) * Rectangle(1, 1)
box = Box(1, 1, 1)
r = rectangle - box
vertices = r.vertices()
self.assertEquals(len(vertices), 4)
self.assertTupleAlmostEquals(vertices[0], (0.5, -0.5, 0.0), 6)
self.assertTupleAlmostEquals(vertices[1], (0.5, 0.5, 0.0), 6)
self.assertTupleAlmostEquals(vertices[2], (1.0, 0.5, 0.0), 6)
self.assertTupleAlmostEquals(vertices[3], (1.0, -0.5, 0.0), 6)
print(vertices)
def test_3d_2d_minus(self):
box = Box(1, 1, 1)
rectangle = Rectangle(1, 1)
with self.assertRaises(ValueError):
_ = box - rectangle
def test_3d_1d_minus(self):
box = Box(1, 1, 1)
line = Line((0, 0), (1, 1))
with self.assertRaises(ValueError):
_ = box - line
def test_2d_1d_minus(self):
rectangle = Rectangle(1, 1)
line = Line((0, 0), (1, 1))
with self.assertRaises(ValueError):
_ = rectangle - line
class LocationTests(unittest.TestCase):
def test_wheel(self):