def test_extruded_shape_with_overlap_volume(self): """creates two overlapping extruded shapes at different placement angles using straight connections and checks that their volume is correct""" test_shape = ExtrudeStraightShape(points=[(0, 0), (0, 20), (10, 20), (10, 0)], distance=10) test_shape.azimuth_placement_angle = [0, 90, 180, 270] assert test_shape.volume == pytest.approx((10 * 20 * 10 * 4) - (5 * 20 * 5 * 4))
def test_absolute_shape_volume(self): """creates an extruded shape at one placement angle using straight connections and checks that the volume is correct""" test_shape = ExtrudeStraightShape(points=[(0, 0), (0, 20), (20, 20), (20, 0)], distance=30) test_shape.create_solid() assert test_shape.solid is not None assert test_shape.volume == pytest.approx(20 * 20 * 30)
def test_rotation_angle(self): """creates an extruded shape with a rotation_angle < 360 and checks that the correct cut is performed and the volume is correct""" test_shape = ExtrudeStraightShape( points=[(50, 0), (50, 20), (70, 20), (70, 0)], distance=50, azimuth_placement_angle=[0, 45, 90, 135, 180, 225, 270, 315, 360]) test_volume = test_shape.volume test_shape.rotation_angle = 180 assert test_shape.volume == pytest.approx(test_volume * 0.5)
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 = ExtrudeStraightShape(points=[(0, 0), (0, 20), (20, 20)], distance=20) test_shape.solid assert test_shape.hash_value is not None initial_hash_value = test_shape.hash_value test_shape.distance = 30 assert test_shape.hash_value == initial_hash_value test_shape.solid assert test_shape.hash_value != initial_hash_value
def test_extruded_shape_relative_volume(self): """creates two extruded shapes at different placement angles using straight connections and checks that their relative volumes are correct""" test_shape = ExtrudeStraightShape(points=[(5, 0), (5, 20), (15, 20), (15, 0)], distance=10) test_shape.azimuth_placement_angle = 0 assert test_shape.volume == pytest.approx(10 * 20 * 10 * 1) test_shape.azimuth_placement_angle = [0, 90, 180, 270] assert test_shape.volume == pytest.approx(10 * 20 * 10 * 4)
def test_extruded_shape_volume(self): """creates an extruded shape with multiple placement angles using straight \ connections and checks the volume is correct""" test_shape = ExtrudeStraightShape(points=[(5, 0), (5, 20), (15, 20), (15, 0)], distance=10) test_shape.azimuth_placement_angle = 0 assert test_shape.volume == pytest.approx(10 * 20 * 10 * 1) test_shape.azimuth_placement_angle = [0, 90, 180, 270] assert test_shape.volume == pytest.approx(10 * 20 * 10 * 4)
def test_intersect_volume_2_shapes(self): """creates two extruded shapes using straight connections with the second shape intersecting the first and checks that the volume is correct""" test_shape1 = ExtrudeStraightShape(points=[(10, 10), (10, 30), (30, 30), (30, 10)], distance=30) test_shape = ExtrudeStraightShape( points=[(0, 0), (0, 20), (20, 20), (20, 0)], distance=30, intersect=test_shape1, ) assert test_shape.solid is not None assert test_shape.volume == pytest.approx(20 * 20 * 30 * 0.25)
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 = ExtrudeStraightShape(points=[(0, 0), (0, 20), (20, 20)], distance=20) assert test_shape.solid is not None assert test_shape.hash_value is not None initial_hash_value = test_shape.hash_value test_shape.distance = 30 assert test_shape.solid is not None assert test_shape.hash_value is not None assert initial_hash_value != test_shape.hash_value
def __init__(self, thickness: float, gap_size: float, height: Optional[float] = 2000., width: Optional[float] = 2000., azimuth_placement_angle=[ 0., 36., 72., 108., 144., 180., 216., 252., 288., 324. ], stp_filename: Optional[str] = "BlanketCutterParallels.stp", stl_filename: Optional[str] = "BlanketCutterParallels.stl", name: Optional[str] = "blanket_cutter_parallels", material_tag: Optional[str] = "blanket_cutter_parallels_mat", **kwargs) -> None: self.main_cutting_shape = \ ExtrudeStraightShape( distance=gap_size / 2.0, azimuth_placement_angle=azimuth_placement_angle, ) self.gap_size = gap_size self.thickness = thickness super().__init__(distance=self.distance, azimuth_placement_angle=azimuth_placement_angle, stp_filename=stp_filename, stl_filename=stl_filename, name=name, material_tag=material_tag, **kwargs) self.height = height self.width = width
def test_intersect_volume(self): """Creates an ExtrudeStraightShape with another ExtrudeStraightShape intersected and checks that the volume is correct.""" intersected_shape = ExtrudeStraightShape( points=[(0, 10), (0, 30), (20, 30), (20, 10)], distance=30, intersect=self.test_shape ) assert intersected_shape.volume == pytest.approx(10 * 20 * 30)
def test_union_volume(self): """Creates a union of two ExtrudeStraightShapes and checks that the volume is correct.""" unioned_shape = ExtrudeStraightShape( points=[(0, 10), (0, 30), (20, 30), (20, 10)], distance=30, union=self.test_shape ) assert unioned_shape.volume == pytest.approx(30 * 20 * 30)
def __init__(self, magnet, inner_offset, outer_offset, vertical_section_offset, **kwargs): self.magnet = magnet super().__init__(**kwargs) self.inner_offset = inner_offset self.outer_offset = outer_offset self.vertical_section_offset = vertical_section_offset self.leg_shape = ExtrudeStraightShape( distance=self.distance, azimuth_placement_angle=self.azimuth_placement_angle)
def test_intersect_volume_3_extruded_shapes(self): """creates three different extruded shapes using straight connections with the second and third shapes intersecting the first and checks that the volume is correct""" test_shape1 = ExtrudeStraightShape(points=[(10, 10), (10, 30), (30, 30), (30, 10)], distance=10) test_shape2 = ExtrudeStraightShape(points=[(10, 10), (10, 30), (30, 30), (30, 10)], distance=3) test_shape = ExtrudeStraightShape( points=[(0, 0), (0, 20), (20, 20), (20, 0)], distance=30, intersect=[test_shape1, test_shape2], ) assert test_shape.solid is not None assert test_shape.volume == pytest.approx(20 * 20 * 30 * 0.25 * 0.1)
def test_initial_solid_construction(self): """tests that a cadquery solid with a unique hash is constructed when .solid is called""" test_shape = ExtrudeStraightShape(points=[(0, 0), (0, 20), (20, 20), (20, 0)], distance=20) 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_cut_volume(self): """Creates an ExtrudeStraightShape with another ExtrudeStraightShape cut out and checks that the volume is correct.""" shape_with_cut = ExtrudeStraightShape( points=[(0, 0), (0, 40), (40, 40), (40, 0)], distance=40, cut=self.test_shape ) assert shape_with_cut.volume == pytest.approx( (40 * 40 * 40) - (20 * 20 * 30) )
def test_cut_volume(self): """creates an extruded shape using straight connections with another shape cut out and checks that the volume is correct""" inner_shape = ExtrudeStraightShape(points=[(5, 5), (5, 10), (10, 10), (10, 5)], distance=30) outer_shape = ExtrudeStraightShape(points=[(3, 3), (3, 12), (12, 12), (12, 3)], distance=30) outer_shape_with_cut = ExtrudeStraightShape( points=[(3, 3), (3, 12), (12, 12), (12, 3)], cut=inner_shape, distance=30, ) assert inner_shape.volume == pytest.approx(5 * 5 * 30) assert outer_shape.volume == pytest.approx(9 * 9 * 30) assert outer_shape_with_cut.volume == pytest.approx( (9 * 9 * 30) - (5 * 5 * 30), abs=0.1)
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 = ExtrudeStraightShape(points=[(0, 0), (0, 20), (20, 20), (20, 0)], distance=20) 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_conditional_solid_reconstruction_parameters(self): """tests that a new cadquery solid with a new unique hash is created when the shape properties of points, distance, workplane, name, color, material_tag, stp_filename, azimuth_placement_angle or cut are changed""" # points test_shape = ExtrudeStraightShape(points=[(0, 0), (0, 20), (20, 20)], distance=20) test_shape.solid initial_hash_value = test_shape.hash_value test_shape.points = [(0, 0), (10, 30), (15, 50), (25, 5), (15, 0)] test_shape.solid assert test_shape.solid is not None assert test_shape.hash_value != initial_hash_value # distance test_shape = ExtrudeStraightShape(points=[(0, 0), (0, 20), (20, 20)], distance=20) test_shape.solid initial_hash_value = test_shape.hash_value test_shape.distance = 30 test_shape.solid assert test_shape.solid is not None assert test_shape.hash_value != initial_hash_value # workplane test_shape = ExtrudeStraightShape( points=[(0, 0), (0, 20), (20, 20)], distance=20, workplane="XZ", ) test_shape.solid initial_hash_value = test_shape.hash_value test_shape.workplane = "YZ" test_shape.solid assert test_shape.solid is not None assert test_shape.hash_value != initial_hash_value # name test_shape = ExtrudeStraightShape( points=[(0, 0), (0, 20), (20, 20)], distance=20, name='test_name', ) test_shape.solid initial_hash_value = test_shape.hash_value test_shape.name = 'new_name' test_shape.solid assert test_shape.solid is not None assert test_shape.hash_value != initial_hash_value # color test_shape = ExtrudeStraightShape( points=[(0, 0), (0, 20), (20, 20)], distance=20, color=[0.5, 0.5, 0.5], ) test_shape.solid initial_hash_value = test_shape.hash_value test_shape.color = [0.1, 0.2, 0.8] test_shape.solid assert test_shape.solid is not None assert test_shape.hash_value != initial_hash_value # material_tag test_shape = ExtrudeStraightShape( points=[(0, 0), (0, 20), (20, 20)], distance=20, material_tag='test_material', ) test_shape.solid initial_hash_value = test_shape.hash_value test_shape.material_tag = 'new_material' test_shape.solid assert test_shape.solid is not None assert test_shape.hash_value != initial_hash_value # stp_filename test_shape = ExtrudeStraightShape( points=[(0, 0), (0, 20), (20, 20)], distance=20, stp_filename='test_filename.stp', ) test_shape.solid initial_hash_value = test_shape.hash_value test_shape.stp_filename = 'new_filename.stp' test_shape.solid assert test_shape.solid is not None assert test_shape.hash_value != initial_hash_value # azimuth_placement_angle test_shape = ExtrudeStraightShape( points=[(0, 0), (0, 20), (20, 20)], distance=20, azimuth_placement_angle=0, ) test_shape.solid initial_hash_value = test_shape.hash_value test_shape.azimuth_placement_angle = 180 test_shape.solid assert test_shape.solid is not None assert test_shape.hash_value != initial_hash_value # cut cut_shape = ExtrudeStraightShape(points=[(5, 5), (5, 15), (15, 15)], distance=5) test_shape = ExtrudeStraightShape( points=[(0, 0), (0, 20), (20, 20)], distance=20, ) test_shape.solid initial_hash_value = test_shape.hash_value test_shape.cut = cut_shape test_shape.solid assert test_shape.solid is not None assert test_shape.hash_value != initial_hash_value
def setUp(self): self.test_shape = ExtrudeStraightShape( points=[(10, 10), (10, 30), (30, 30), (30, 10)], distance=30 )
class TestExtrudeStraightShape(unittest.TestCase): def setUp(self): self.test_shape = ExtrudeStraightShape( points=[(10, 10), (10, 30), (30, 30), (30, 10)], distance=30 ) def test_default_parameters(self): """Checks that the default parameters of an ExtrudeStraightShape are correct.""" assert self.test_shape.rotation_angle == 360 assert self.test_shape.stp_filename == "ExtrudeStraightShape.stp" assert self.test_shape.stl_filename == "ExtrudeStraightShape.stl" assert self.test_shape.extrude_both def test_absolute_shape_volume(self): """Creates an ExtrudeStraightShape and checks that the volume is correct.""" assert self.test_shape.solid is not None assert self.test_shape.volume == pytest.approx(20 * 20 * 30) def test_absolute_shape_areas(self): """Creates an ExtrudeStraightShape and checks that the volume is correct.""" assert self.test_shape.area == pytest.approx( (20 * 20 * 2) + (20 * 30 * 4) ) assert len(self.test_shape.areas) == 6 assert self.test_shape.areas.count(pytest.approx(20 * 20)) == 2 assert self.test_shape.areas.count(pytest.approx(20 * 30)) == 4 def test_relative_shape_volume(self): """Creates two ExtrudeStraightShapes and checks that their relative volumes are correct.""" test_volume = self.test_shape.volume self.test_shape.azimuth_placement_angle = [0, 90, 180, 270] self.test_shape.rotation_axis = "Y" assert self.test_shape.volume == pytest.approx(test_volume * 4) def test_cut_volume(self): """Creates an ExtrudeStraightShape with another ExtrudeStraightShape cut out and checks that the volume is correct.""" shape_with_cut = ExtrudeStraightShape( points=[(0, 0), (0, 40), (40, 40), (40, 0)], distance=40, cut=self.test_shape ) assert shape_with_cut.volume == pytest.approx( (40 * 40 * 40) - (20 * 20 * 30) ) def test_union_volume(self): """Creates a union of two ExtrudeStraightShapes and checks that the volume is correct.""" unioned_shape = ExtrudeStraightShape( points=[(0, 10), (0, 30), (20, 30), (20, 10)], distance=30, union=self.test_shape ) assert unioned_shape.volume == pytest.approx(30 * 20 * 30) def test_intersect_volume(self): """Creates an ExtrudeStraightShape with another ExtrudeStraightShape intersected and checks that the volume is correct.""" intersected_shape = ExtrudeStraightShape( points=[(0, 10), (0, 30), (20, 30), (20, 10)], distance=30, intersect=self.test_shape ) assert intersected_shape.volume == pytest.approx(10 * 20 * 30) def test_rotation_angle(self): """Creates an ExtrudeStraightShape with a rotation_angle < 360 and checks that the correct cut is performed and the volume is correct.""" self.test_shape.azimuth_placement_angle = [45, 135, 225, 315] self.rotation_axis = "Y" test_volume = self.test_shape.volume self.test_shape.rotation_angle = 180 assert self.test_shape.volume == pytest.approx( test_volume * 0.5, rel=0.01) def test_extrude_both(self): """Creates an ExtrudeStraightShape with extrude_both = True and False and checks that the volumes are correct.""" test_volume_extrude_both = self.test_shape.volume self.test_shape.extrude_both = False assert self.test_shape.volume == pytest.approx( test_volume_extrude_both) 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")
class TestExtrudeStraightShape(unittest.TestCase): def setUp(self): self.test_shape = ExtrudeStraightShape( points=[(10, 10), (10, 30), (30, 30), (30, 10)], distance=30 ) def test_default_parameters(self): """Checks that the default parameters of an ExtrudeStraightShape are correct.""" assert self.test_shape.rotation_angle == 360 assert self.test_shape.stp_filename == "ExtrudeStraightShape.stp" assert self.test_shape.stl_filename == "ExtrudeStraightShape.stl" assert self.test_shape.extrude_both def test_absolute_shape_volume(self): """Creates an ExtrudeStraightShape and checks that the volume is correct.""" assert self.test_shape.solid is not None assert self.test_shape.volume == pytest.approx(20 * 20 * 30) def test_absolute_shape_areas(self): """Creates an ExtrudeStraightShape and checks that the volume is correct.""" assert self.test_shape.area == pytest.approx( (20 * 20 * 2) + (20 * 30 * 4) ) assert len(self.test_shape.areas) == 6 assert self.test_shape.areas.count(pytest.approx(20 * 20)) == 2 assert self.test_shape.areas.count(pytest.approx(20 * 30)) == 4 def test_relative_shape_volume(self): """Creates two ExtrudeStraightShapes and checks that their relative volumes are correct.""" test_volume = self.test_shape.volume self.test_shape.azimuth_placement_angle = [0, 90, 180, 270] self.test_shape.rotation_axis = "Y" assert self.test_shape.volume == pytest.approx(test_volume * 4) def test_cut_volume(self): """Creates an ExtrudeStraightShape with another ExtrudeStraightShape cut out and checks that the volume is correct.""" shape_with_cut = ExtrudeStraightShape( points=[(0, 0), (0, 40), (40, 40), (40, 0)], distance=40, cut=self.test_shape ) assert shape_with_cut.volume == pytest.approx( (40 * 40 * 40) - (20 * 20 * 30) ) def test_union_volume(self): """Creates a union of two ExtrudeStraightShapes and checks that the volume is correct.""" unioned_shape = ExtrudeStraightShape( points=[(0, 10), (0, 30), (20, 30), (20, 10)], distance=30, union=self.test_shape ) assert unioned_shape.volume == pytest.approx(30 * 20 * 30) def test_intersect_volume(self): """Creates an ExtrudeStraightShape with another ExtrudeStraightShape intersected and checks that the volume is correct.""" intersected_shape = ExtrudeStraightShape( points=[(0, 10), (0, 30), (20, 30), (20, 10)], distance=30, intersect=self.test_shape ) assert intersected_shape.volume == pytest.approx(10 * 20 * 30) def test_rotation_angle(self): """Creates an ExtrudeStraightShape with a rotation_angle < 360 and checks that the correct cut is performed and the volume is correct.""" self.test_shape.azimuth_placement_angle = [45, 135, 225, 315] self.rotation_axis = "Y" test_volume = self.test_shape.volume self.test_shape.rotation_angle = 180 assert self.test_shape.volume == pytest.approx( test_volume * 0.5, rel=0.01) def test_extrude_both(self): """Creates an ExtrudeStraightShape with extrude_both = True and False and checks that the volumes are correct.""" test_volume_extrude_both = self.test_shape.volume self.test_shape.extrude_both = False assert self.test_shape.volume == pytest.approx( test_volume_extrude_both) 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") def test_incorrect_points_input(self): """Checks that an error is raised when the points are input with the connection""" def incorrect_points_definition(): self.test_shape.points = [ (10, 10, 'straight'), (10, 30, 'straight'), (30, 30, 'straight'), (30, 10, 'straight') ] self.assertRaises( ValueError, incorrect_points_definition ) def test_export_html_with_different_workplanes(self): """Checks that all the workplanes produce an html file when using the export_html method and that the axis have the correct labels""" os.system("rm *.html") for workplane in ["XY", "YZ", "XZ", "YX", "ZY", "ZX"]: self.test_shape.workplane = workplane fig = self.test_shape.export_html(workplane + ".html") assert Path(workplane + ".html").exists() is True assert fig.layout.xaxis.title['text'] == workplane[0] assert fig.layout.yaxis.title['text'] == workplane[1]