def test_union_volume_addition(self): """Fuses two RotateMixedShapes and checks that their fused volume is correct""" inner_box = RotateMixedShape(points=[ (100, 100, "straight"), (100, 200, "straight"), (200, 200, "straight"), (200, 100, "straight"), ]) outer_box = RotateMixedShape(points=[ (200, 100, "straight"), (200, 200, "straight"), (500, 200, "straight"), (500, 100, "straight"), ]) outer_box_and_inner_box = RotateMixedShape( points=[ (200, 100, "straight"), (200, 200, "straight"), (500, 200, "straight"), (500, 100, "straight"), ], union=inner_box, ) assert inner_box.volume + outer_box.volume == pytest.approx( outer_box_and_inner_box.volume)
def setUp(self): self.test_shape = RotateMixedShape( points=[(50, 0, "straight"), (50, 50, "spline"), ( 60, 70, "spline"), (70, 50, "circle"), (60, 25, "circle"), (70, 0, "straight")])
def test_absolute_shape_volume(self): """creates a rotated shape using straight and spline connections and \ checks the volume is correct""" test_shape = RotateMixedShape( points=[(0, 0, "straight"), (0, 20, "spline"), (20, 20, "spline"), (20, 0, "spline")]) test_shape.rotation_angle = 360 test_shape.create_solid() assert test_shape.solid is not None assert test_shape.volume > 100 test_shape2 = RotateMixedShape( points=[(0, 0, "straight"), (0, 20, "spline"), (20, 20, "spline"), (20, 0, "spline")]) test_shape2.rotation_angle = 180 test_shape2.create_solid() assert test_shape2.solid is not None assert 2 * test_shape2.volume == test_shape.volume
def test_cut_volume(self): """creates a rotated shape using straight and spline connections with another \ shape cut out and checks the volume is correct""" inner_shape = RotateMixedShape( points=[(5, 5, "straight"), (5, 10, "spline"), (10, 10, "spline"), (10, 5, "spline")], rotation_angle=180, ) outer_shape = RotateMixedShape( points=[(3, 3, "straight"), (3, 12, "spline"), (12, 12, "spline"), (12, 3, "spline")], rotation_angle=180, ) outer_shape_cut = RotateMixedShape( points=[(3, 3, "straight"), (3, 12, "spline"), (12, 12, "spline"), (12, 3, "spline")], cut=inner_shape, rotation_angle=180, ) assert inner_shape.volume == pytest.approx(862.5354) assert outer_shape.volume == pytest.approx(2854.5969) assert outer_shape_cut.volume == pytest.approx(2854.5969 - 862.5354)
def incorrect_number_of_connections_function(): """checks ValueError is raised when an incorrect number of connections is specified""" test_shape = RotateMixedShape( points=[(0, 200, "straight"), (200, 100), (0, 0, "spline"), ] ) test_shape.create_solid()
def incorrect_number_of_connections_function(): """checks that a ValueError is raised when an incorrect \ number of connections are specified. There are 3 \ points set, so only 4 connections are needed""" test_shape = RotateMixedShape(points=[ (0, 200, "straight"), (200, 100), (0, 0, "spline"), ]) test_shape.create_solid()
def main(): blanket = RotateMixedShape(points=[ (538, 305, "straight"), (538, -305, "straight"), (322, -305, "spline"), (470, 0, "spline"), (322, 305, "straight"), ]) blanket.rotation_angle = 180 blanket.export_stp("blanket_from_points.stp")
def test_cut_volume(self): """Creates a RotateMixedShape with another RotateMixedShape cut out and checks that the volume is correct.""" outer_shape = RotateMixedShape( points=[(40, -10, "spline"), ( 35, 50, "spline"), (60, 80, "straight"), ( 80, 80, "circle"), (100, 40, "circle"), (80, 0, "straight"), (80, -10, "straight")]) outer_shape_volume = outer_shape.volume outer_shape.cut = self.test_shape assert outer_shape.volume == pytest.approx(outer_shape_volume - self.test_shape.volume)
def incorrect_string_for_connection_type(): """checks that a ValueError is raised when the an invalid connection \ type is specified""" test_shape = RotateMixedShape( points=[(0, 0, "straight"), (0, 20, "spline"), ( 20, 20, "spline"), (20, 0, "not_a_valid_entry")])
def test_hash_value_update(self): """tests that the hash_value of the shape is not updated until a new solid has been created""" test_shape = RotateMixedShape(points=[ (0, 0, "straight"), (0, 20, "spline"), (20, 20, "spline"), ], rotation_angle=360) test_shape.solid assert test_shape.hash_value is not None initial_hash_value = test_shape.hash_value test_shape.rotation_angle = 180 assert test_shape.hash_value == initial_hash_value test_shape.solid assert test_shape.hash_value != initial_hash_value
def incorrect_string_for_connection_type(): """Checks ValueError is raised when an invalid connection type is specified.""" RotateMixedShape(points=[ (0, 0, "straight"), (0, 20, "spline"), (20, 20, "spline"), (20, 0, "invalid_entry"), ])
def test_conditional_solid_reconstruction(self): """tests that a new cadquery solid with a new unique hash is constructed when .solid is called again after changes have been made to the shape""" test_shape = RotateMixedShape(points=[ (0, 0, "straight"), (0, 20, "spline"), (20, 20, "spline"), ], rotation_angle=360) assert test_shape.solid is not None assert test_shape.hash_value is not None initial_hash_value = test_shape.hash_value test_shape.rotation_angle = 180 assert test_shape.solid is not None assert test_shape.hash_value is not None assert initial_hash_value != test_shape.hash_value
def test_mixed_shape_with_straight_and_circle(self): """tests the construction of a shape with straight and circular edges""" test_shape = RotateMixedShape(points=[ (10, 20, 'straight'), (10, 10, 'straight'), (20, 10, 'circle'), (40, 15, 'circle'), (20, 20, 'straight'), ], rotation_angle=10) assert test_shape.volume > 10 * 10
def test_initial_solid_construction(self): """tests that a cadquery solid with a unique hash is constructed when .solid is called""" test_shape = RotateMixedShape(points=[ (0, 0, "straight"), (0, 20, "spline"), (20, 20, "spline"), (20, 0, "straight"), ], rotation_angle=360) assert test_shape.hash_value is None assert test_shape.solid is not None assert type(test_shape.solid).__name__ == "Workplane" assert test_shape.hash_value is not None
def test_mixed_shape_with_straight_and_circle(self): """creates a rotated shape with straight and circular connections and checks the volume is correct""" test_shape = RotateMixedShape( points=[ (10, 20, "straight"), (10, 10, "straight"), (20, 10, "circle"), (40, 15, "circle"), (20, 20, "straight"), ], rotation_angle=10, ) assert test_shape.volume > 10 * 10
def test_solid_return(self): """tests that the same cadquery solid with the same unique hash is returned when shape.solid is called again when no changes have been made to the shape""" test_shape = RotateMixedShape(points=[ (0, 0, "straight"), (0, 20, "spline"), (20, 20, "spline"), (20, 0, "straight"), ], rotation_angle=360) assert test_shape.solid is not None assert test_shape.hash_value is not None initial_hash_value = test_shape.hash_value assert test_shape.solid is not None assert test_shape.hash_value is not None assert initial_hash_value == test_shape.hash_value
def test_export_stp(self): """creates a RotateMixedShape and checks that an stp file of the shape can be exported using the export_stp method""" test_shape = RotateMixedShape( points=[ (10, 20, "straight"), (10, 10, "straight"), (20, 10, "circle"), (40, 15, "circle"), (20, 20, "straight"), ], rotation_angle=10, ) os.system("rm tests/test.stp") test_shape.export_stp("tests/test.stp") assert Path("tests/test.stp").exists() is True os.system("rm tests/test.stp") test_shape.stp_filename = "tests/test.stp" test_shape.export_stp() assert Path("tests/test.stp").exists() is True os.system("rm tests/test.stp")
def test_shape_volume_with_multiple_azimuth_placement_angles(self): """creates rotated shapes with multiple placement angles using straight and \ spline connections and checks volumes are correct""" test_shape = RotateMixedShape( points=[(1, 1, "straight"), (1, 20, "spline"), (20, 20, "spline"), (20, 1, "spline")]) test_shape.rotation_angle = 10 test_shape.azimuth_placement_angle = [0, 90, 180, 270] test_shape.create_solid() assert test_shape.solid is not None assert test_shape.volume > 100 test_shape2 = RotateMixedShape( points=[(1, 1, "straight"), (1, 20, "spline"), (20, 20, "spline"), (20, 1, "spline")]) test_shape2.rotation_angle = 5 test_shape2.azimuth_placement_angle = [0, 90, 180, 270] test_shape2.create_solid() assert test_shape2.solid is not None assert 2 * test_shape2.volume == pytest.approx(test_shape.volume) test_shape3 = RotateMixedShape( points=[(1, 1, "straight"), (1, 20, "spline"), (20, 20, "spline"), (20, 1, "spline")]) test_shape3.rotation_angle = 20 test_shape3.azimuth_placement_angle = [0, 180] test_shape3.create_solid() assert test_shape3.solid is not None assert test_shape3.volume == pytest.approx(test_shape.volume)
class TestRotateMixedShape(unittest.TestCase): def setUp(self): self.test_shape = RotateMixedShape( points=[(50, 0, "straight"), (50, 50, "spline"), ( 60, 70, "spline"), (70, 50, "circle"), (60, 25, "circle"), (70, 0, "straight")]) def test_export_2d_image(self): """Creates a RotateMixedShape object and checks that a png file of the object with the correct suffix can be exported using the export_2d_image method.""" os.system("rm filename.png") self.test_shape.export_2d_image("filename") assert Path("filename.png").exists() is True os.system("rm filename.png") self.test_shape.export_2d_image("filename.png") assert Path("filename.png").exists() is True os.system("rm filename.png") def test_default_parameters(self): """Checks that the default parameters of a RotateMixedShape are correct.""" assert self.test_shape.rotation_angle == 360 assert self.test_shape.stp_filename == "RotateMixedShape.stp" assert self.test_shape.stl_filename == "RotateMixedShape.stl" assert self.test_shape.azimuth_placement_angle == 0 def test_relative_shape_volume_rotation_angle(self): """Creates two RotateMixedShapes with different rotation_angles and checks that their relative volumes are correct.""" assert self.test_shape.volume > 100 test_volume = self.test_shape.volume self.test_shape.rotation_angle = 180 assert test_volume == pytest.approx(self.test_shape.volume * 2) def test_relative_shape_volume_azimuth_placement_angle(self): """Creates two RotateMixedShapes with different azimuth_placement_angles and checks that their relative volumes are correct.""" self.test_shape.rotation_angle = 10 self.test_shape.azimuth_placement_angle = 0 test_volume_1 = self.test_shape.volume self.test_shape.azimuth_placement_angle = [0, 90, 180, 270] assert self.test_shape.volume == pytest.approx(test_volume_1 * 4) def test_shape_face_areas(self): """Creates RotateMixedShapes and checks that the face areas are expected.""" assert len(self.test_shape.areas) == 4 assert len(set(self.test_shape.areas)) == 4 self.test_shape.rotation_angle = 180 assert len(self.test_shape.areas) == 6 assert len(set([round(i) for i in self.test_shape.areas])) == 5 def test_union_volume_addition(self): """Fuses two RotateMixedShapes and checks that their fused volume is correct""" inner_box = RotateMixedShape(points=[ (100, 100, "straight"), (100, 200, "straight"), (200, 200, "straight"), (200, 100, "straight"), ]) outer_box = RotateMixedShape(points=[ (200, 100, "straight"), (200, 200, "straight"), (500, 200, "straight"), (500, 100, "straight"), ]) outer_box_and_inner_box = RotateMixedShape( points=[ (200, 100, "straight"), (200, 200, "straight"), (500, 200, "straight"), (500, 100, "straight"), ], union=inner_box, ) assert inner_box.volume + outer_box.volume == pytest.approx( outer_box_and_inner_box.volume) def test_incorrect_connections(self): """Checks that errors are raised when invalid connection arguments are used to construct a RotateMixedShape.""" def incorrect_string_for_connection_type(): """Checks ValueError is raised when an invalid connection type is specified.""" RotateMixedShape(points=[ (0, 0, "straight"), (0, 20, "spline"), (20, 20, "spline"), (20, 0, "invalid_entry"), ]) self.assertRaises(ValueError, incorrect_string_for_connection_type) def incorrect_number_of_connections_function(): """Checks ValueError is raised when an incorrect number of connections is specified.""" test_shape = RotateMixedShape( points=[(0, 200, "straight"), (200, 100), (0, 0, "spline")]) self.assertRaises(ValueError, incorrect_number_of_connections_function) def test_cut_volume(self): """Creates a RotateMixedShape with another RotateMixedShape cut out and checks that the volume is correct.""" outer_shape = RotateMixedShape( points=[(40, -10, "spline"), ( 35, 50, "spline"), (60, 80, "straight"), ( 80, 80, "circle"), (100, 40, "circle"), (80, 0, "straight"), (80, -10, "straight")]) outer_shape_volume = outer_shape.volume outer_shape.cut = self.test_shape assert outer_shape.volume == pytest.approx(outer_shape_volume - self.test_shape.volume) def test_mixed_shape_with_straight_and_circle(self): """Creates a RotateMixedShape with straight and circular connections and checks that the volume is correct.""" test_shape = RotateMixedShape( points=[ (10, 20, "straight"), (10, 10, "straight"), (20, 10, "circle"), (40, 15, "circle"), (20, 20, "straight"), ], rotation_angle=10, ) assert test_shape.volume > 10 * 10 def test_export_stp_extension(self): """Creates a RotateMixedShape and checks that a stp file of the shape can be exported with the correct suffix using the export_stp method.""" os.system("rm filename.stp filename.step") self.test_shape.export_stp("filename.stp") self.test_shape.export_stp("filename.step") assert Path("filename.stp").exists() is True assert Path("filename.step").exists() is True os.system("rm filename.stp filename.step") self.test_shape.export_stp("filename") assert Path("filename.stp").exists() is True os.system("rm filename.stp") def test_export_stl(self): """Creates a RotateMixedShape and checks that a stl file of the shape can be exported with the correct suffix using the export_stl method.""" os.system("rm filename.stl") self.test_shape.export_stl("filename.stl") assert Path("filename.stl").exists() is True os.system("rm filename.stl") self.test_shape.export_stl("filename") assert Path("filename.stl").exists() is True os.system("rm filename.stl") def test_export_stp(self): """Exports and stp file with mode = solid and wire and checks that the outputs exist and relative file sizes are correct.""" os.system("rm test_solid.stp test_solid2.stp test_wire.stp") self.test_shape.export_stp('test_solid.stp', mode='solid') self.test_shape.export_stp('test_solid2.stp') self.test_shape.export_stp('test_wire.stp', mode='wire') assert Path("test_solid.stp").exists() is True assert Path("test_solid2.stp").exists() is True assert Path("test_wire.stp").exists() is True assert Path("test_solid.stp").stat().st_size == \ Path("test_solid2.stp").stat().st_size assert Path("test_wire.stp").stat().st_size < \ Path("test_solid2.stp").stat().st_size os.system("rm test_solid.stp test_solid2.stp test_wire.stp")