Merge pull request #186 from snoyer/relocate-shape

add `Shape.relocate`
This commit is contained in:
gumyr 2023-04-19 14:04:19 -04:00 committed by GitHub
commit f8ad67cd5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 0 deletions

View file

@ -1063,6 +1063,13 @@ class Location:
def __pow__(self, exponent: int) -> Location:
return Location(self.wrapped.Powered(exponent))
def __eq__(self, other: Location):
return (
isinstance(other, Location)
and self.position == other.position
and self.orientation == other.orientation
)
def to_axis(self) -> Axis:
"""Convert the location into an Axis"""
return Axis.Z.located(self)

View file

@ -2191,6 +2191,26 @@ class Shape(NodeMixin):
shape_copy.wrapped = downcast(shape_copy.wrapped.Moved(loc.wrapped))
return shape_copy
def relocate(self, loc: Location):
"""Change the location of self while keeping it geometrically similar
Args:
loc (Location): new location to set for self
"""
if self.location != loc:
old_ax = gp_Ax3()
old_ax.Transform(self.location.wrapped.Transformation())
new_ax = gp_Ax3()
new_ax.Transform(loc.wrapped.Transformation())
trsf = gp_Trsf()
trsf.SetDisplacement(new_ax, old_ax)
builder = BRepBuilderAPI_Transform(self.wrapped, trsf, True, True)
self.wrapped = builder.Shape()
self.wrapped.Location(loc.wrapped)
def distance_to_with_closest_points(
self, other: Union[Shape, VectorLike]
) -> tuple[float, Vector, Vector]:

View file

@ -1407,6 +1407,16 @@ class TestLocation(DirectApiTestCase):
self.assertVectorAlmostEquals(axis.position, (1, 2, 3), 6)
self.assertVectorAlmostEquals(axis.direction, (0, 1, 0), 6)
def test_eq(self):
loc = Location((1, 2, 3), (4, 5, 6))
diff_posistion = Location((10, 20, 30), (4, 5, 6))
diff_orientation = Location((1, 2, 3), (40, 50, 60))
same = Location((1, 2, 3), (4, 5, 6))
self.assertEqual(loc, same)
self.assertNotEqual(loc, diff_posistion)
self.assertNotEqual(loc, diff_orientation)
class TestMatrix(DirectApiTestCase):
def test_matrix_creation_and_access(self):
@ -2266,6 +2276,20 @@ class TestShape(DirectApiTestCase):
positive_half, negative_half = [s.clean() for s in sphere.cut(divider).solids()]
self.assertGreater(abs(positive_half.volume - negative_half.volume), 0, 1)
def test_relocate(self):
box = Solid.make_box(10, 10, 10).move(Location((20, -5, -5)))
cylinder = Solid.make_cylinder(2, 50).move(Location((0, 0, 0), (0, 90, 0)))
box_with_hole = box.cut(cylinder)
box_with_hole.relocate(box.location)
self.assertEqual(box.location, box_with_hole.location)
bbox1 = box.bounding_box()
bbox2 = box_with_hole.bounding_box()
self.assertVectorAlmostEquals(bbox1.min, bbox2.min, 5)
self.assertVectorAlmostEquals(bbox1.max, bbox2.max, 5)
class TestShapeList(DirectApiTestCase):
"""Test ShapeList functionality"""