def test_stl_importer_2_boxes(): r"""Import an iges file containing 2 distinct boxes and test topology Notes ----- This shows the current limitations of the IgesImporter as 2 boxes cannot be distinguished from one another """ # binary STL importer = StlImporter(path_from_file(__file__, "./models_in/2_boxes_binary.stl")) topo = Topo(importer.shape, return_iter=False) # assert len(topo.solids) == 2 assert len(topo.shells) == 2 assert topo.shells[0].Closed() is True assert topo.shells[1].Closed() is True assert topo.number_of_faces == 108 * 2 assert topo.number_of_edges == 162 * 2 # ascii STL importer = StlImporter(path_from_file(__file__, "./models_in/2_boxes_ascii.stl")) topo = Topo(importer.shape, return_iter=False) # assert len(topo.solids()) == 2 assert len(topo.shells) == 2 assert topo.shells[0].Closed() is True assert topo.shells[1].Closed() is True assert topo.number_of_faces == 108 * 2 assert topo.number_of_edges == 162 * 2
def test_check_overwrite(): r"""check_overwrite() tests""" # file exists assert check_overwrite(path_from_file(__file__, "./models_in/box.igs")) is True # file does not exist assert check_overwrite(path_from_file(__file__, "./models_in/bo_.igs")) is False
def test_cache(): r"""Test the angle_between vectors of transformations.py""" t0 = time() _ = anchorable_part_from_stepzip( path_from_file(__file__, "./cad_files/rim.stepzip")) t1 = time() _ = anchorable_part_from_stepzip( path_from_file(__file__, "./cad_files/rim.stepzip")) t2 = time() assert 100 * (t2 - t1) < (t1 - t0)
def test_step_exporter_overwrite(box_shape): r"""Happy path with a subclass of TopoDS_Shape""" filename = path_from_file(__file__, "./models_out/box.stp") exporter = StepExporter(filename) solid = shape_to_topology(box_shape) assert isinstance(solid, TopoDS_Solid) exporter.add_shape(solid) exporter.write_file() initial_timestamp = os.path.getmtime(filename) assert os.path.isfile(filename) # read the written box.stp importer = StepImporter(filename) topo_compound = Topo(importer.compound, return_iter=False) assert topo_compound.number_of_faces == 6 assert len(topo_compound.faces) == 6 assert topo_compound.number_of_edges == 12 # add a sphere and write again with same exporter sphere = BRepPrimAPI_MakeSphere(10) exporter.add_shape(sphere.Shape()) exporter.write_file() # this creates a file with a box and a sphere intermediate_timestamp = os.path.getmtime(filename) assert intermediate_timestamp > initial_timestamp # check that the file contains the box and the sphere importer = StepImporter(filename) # 6 from box + 1 from sphere assert len(Topo(importer.compound, return_iter=False).faces) == 7 assert len(Topo(importer.compound, return_iter=False).solids) == 2 # create a new exporter and overwrite with a box only filename = path_from_file(__file__, "./models_out/box.stp") exporter = StepExporter(filename) solid = shape_to_topology(box_shape) exporter.add_shape(solid) exporter.write_file() assert os.path.isfile(filename) last_timestamp = os.path.getmtime(filename) assert last_timestamp > intermediate_timestamp # check the file only contains a box importer = StepImporter(filename) # 6 from box assert len(Topo(importer.compound, return_iter=False).faces) == 6 assert len(Topo(importer.compound, return_iter=False).solids) == 1
def test_iges_importer_happy_topology(): r"""import iges file containing a box and test topology""" importer = IgesImporter(path_from_file(__file__, "./models_in/box.igs")) topo = Topo(importer.compound, return_iter=False) assert topo.number_of_faces == 6 assert topo.number_of_edges == 24 # 12 edges * 2 possible orientations ?
def test_stl_exporter_adding_not_a_shape(box_shape): r"""Adding something to the exporter that is not a TopoDS_Shape or a subclass""" filename = path_from_file(__file__, "./models_out/box.stl") exporter = StlExporter(filename) with pytest.raises(ValueError): exporter.set_shape(gp_Pnt(1, 1, 1))
def test_stl_exporter_happy_path(box_shape): r"""Happy path""" filename = path_from_file(__file__, "./models_out/box.sTl") exporter = StlExporter(filename) exporter.set_shape(box_shape) exporter.write_file() assert os.path.isfile(filename)
def test_iges_exporter_happy_path(box_shape): r"""Happy path""" filename = path_from_file(__file__, "./models_out/box.IgS") exporter = IgesExporter(filename) exporter.add_shape(box_shape) exporter.write_file() assert os.path.isfile(filename)
def test_iges_importer_happy_path(): r"""happy path""" importer = IgesImporter( path_from_file(__file__, "./models_in/aube_pleine.iges")) assert isinstance(importer.compound, TopoDS_Compound) assert isinstance(importer.shapes, list) for shape in importer.shapes: assert isinstance(shape, TopoDS_Shape)
def test_happy_path(): r"""The file_origin of path_from_file exists""" new_f = path_from_file(__file__, "tmp.txt") with open(new_f, "w") as nf: nf.write("\n") assert isfile(new_f) is True remove(new_f) assert isfile(new_f) is False
def test_step_importer_happy_path(): r"""happy path""" importer = StepImporter( path_from_file(__file__, "./models_in/aube_pleine.stp")) assert isinstance(importer.compound, TopoDS_Compound) assert isinstance(importer.shapes, list) for shape in importer.shapes: assert isinstance(shape, TopoDS_Shape) assert len(importer.shapes) == 1
def test_import_dat_file(): r"""Test importing a foil section definition dat file using the import_dat_file() function""" pts = import_dat_file(path_from_file(__file__, "./models_in/naca0006.dat"), skip_first_line=True) assert len(pts) == 35 assert pts[0] == (1.0000, 0.00063) assert pts[-1] == (1.0000, -0.00063)
def test_stl_exporter_happy_path_shape_subclass(box_shape): r"""Happy path with a subclass of TopoDS_Shape""" filename = path_from_file(__file__, "./models_out/box.stl") exporter = StlExporter(filename) solid = shape_to_topology(box_shape) assert isinstance(solid, TopoDS_Solid) exporter.set_shape(solid) exporter.write_file() assert os.path.isfile(filename)
def test_read_dat_file(): r"""Test reading a foil section definition dat file""" importer = DatImporter(path_from_file(__file__, "./models_in/naca0006.dat"), skip_first_line=True) pts = importer.points assert len(pts) == 35 assert pts[0] == (1.0000, 0.00063) assert pts[-1] == (1.0000, -0.00063)
def test_file(): data_file_path = path_from_file(__file__, "test_masses_files/masses.csv") masses_collection, w_unit, p_unit, nb_templated = load_masses_from_file(data_file_path) assert masses_collection.mass_kg == 5 # 2500g + 2500g converted to kilos assert masses_collection.cg_m.x == 0.550 # meters !! assert masses_collection.cg_m.y == 0.0 # meters !! assert masses_collection.cg_m.z == -0.2 # meters !! assert w_unit == "g" assert p_unit == "mm" assert nb_templated == 0
def test_step_importer_2_boxes(): r"""Import an step file containing 2 distinct boxes and test topology""" importer = StepImporter( path_from_file(__file__, "./models_in/2_boxes_203.stp")) assert len(importer.shapes) == 1 assert importer.shapes[0].ShapeType() == TopAbs_COMPOUND topo = Topo(importer.shapes[0]) assert topo.number_of_compounds == 1 assert topo.number_of_comp_solids == 0 assert topo.number_of_solids == 2 assert topo.number_of_shells == 2
def test_stl_importer_happy_topology(): r"""import iges file containing a box and test topology""" # binary STL importer = StlImporter(path_from_file(__file__, "./models_in/box_binary.stl")) topo = Topo(importer.shape, return_iter=False) # assert len(topo.solids()) == 1 assert len(topo.shells) == 1 assert topo.shells[0].Closed() is True # direct method on TopoDS_Shell assert len(topo.faces) == 108 assert len(topo.edges) == 162 # ascii STL importer = StlImporter(path_from_file(__file__, "./models_in/box_ascii.stl")) topo = Topo(importer.shape, return_iter=False) # assert len(topo.solids) == 1 assert len(topo.shells) == 1 assert topo.shells[0].Closed() is True assert len(topo.faces) == 108 assert len(topo.edges) == 162
def test_stl_exporter_overwrite(box_shape): r"""Happy path with a subclass of TopoDS_Shape""" filename = path_from_file(__file__, "./models_out/box.stl") exporter = StlExporter(filename) solid = shape_to_topology(box_shape) assert isinstance(solid, TopoDS_Solid) exporter.set_shape(solid) exporter.write_file() assert os.path.isfile(filename) # read the written box.stl importer = StlImporter(filename) topo = Topo(importer.shape) assert topo.number_of_shells == 1 # set a sphere and write again with same exporter sphere = BRepPrimAPI_MakeSphere(10) exporter.set_shape(sphere.Shape()) # this creates a file with a sphere only, this is STL specific exporter.write_file() # check that the file contains the sphere only importer = StlImporter(filename) topo = Topo(importer.shape) assert topo.number_of_shells == 1 # create a new exporter and overwrite with a box only filename = path_from_file(__file__, "./models_out/box.stl") exporter = StlExporter(filename) solid = shape_to_topology(box_shape) exporter.set_shape(solid) exporter.write_file() assert os.path.isfile(filename) # check the file only contains a box importer = StlImporter(filename) topo = Topo(importer.shape) assert topo.number_of_shells == 1
def test_iges_exporter_overwrite(box_shape): r"""Happy path with a subclass of TopoDS_Shape""" filename = path_from_file(__file__, "./models_out/box.igs") exporter = IgesExporter(filename) solid = shape_to_topology(box_shape) assert isinstance(solid, TopoDS_Solid) exporter.add_shape(solid) exporter.write_file() assert os.path.isfile(filename) # read the written box.igs importer = IgesImporter(filename) topo_compound = Topo(importer.compound) assert topo_compound.number_of_faces == 6 assert topo_compound.number_of_edges == 24 # add a sphere and write again with same exporter sphere = BRepPrimAPI_MakeSphere(10) exporter.add_shape(sphere.Shape()) exporter.write_file() # this creates a file with a box and a sphere # check that the file contains the box and the sphere importer = IgesImporter(filename) topo_compound = Topo(importer.compound) assert topo_compound.number_of_faces == 7 # 6 from box + 1 from sphere # create a new exporter and overwrite with a box only filename = path_from_file(__file__, "./models_out/box.igs") exporter = IgesExporter(filename) solid = shape_to_topology(box_shape) exporter.add_shape(solid) exporter.write_file() assert os.path.isfile(filename) # check the file only contains a box importer = IgesImporter(filename) topo_compound = Topo(importer.compound) assert topo_compound.number_of_faces == 6 # 6 from box
def test_step_importer_happy_topology(): r"""import step file containing a box and test topology""" importer = StepImporter(path_from_file(__file__, "./models_in/box_203.stp")) assert len(importer.shapes) == 1 assert isinstance(importer.shapes[0], TopoDS_Shape) assert importer.shapes[0].ShapeType() == TopAbs_SOLID topo = Topo(importer.shapes[0]) assert topo.number_of_compounds == 0 assert topo.number_of_comp_solids == 0 assert topo.number_of_solids == 1 assert topo.number_of_shells == 1
def test_iges_importer_2_boxes(): r"""Import an iges file containing 2 distinct boxes and test topology Notes ----- This shows the current limitations of the IgesImporter as 2 boxes cannot be distinguished from one another """ importer = IgesImporter(path_from_file(__file__, "./models_in/2_boxes.igs")) topo = Topo(importer.compound, return_iter=False) assert topo.number_of_faces == 6 * 2 assert topo.number_of_edges == 24 * 2
def test_iges_importer_wrong_file_content(): r"""wrong file content""" with pytest.raises(IgesFileReadException): IgesImporter(path_from_file(__file__, "./models_in/empty.igs"))
def test_iges_importer_wrong_extension(): r"""wrong file format (i.e. trying to read a step file with iges importer)""" with pytest.raises(IncompatibleFileFormatException): IgesImporter(path_from_file(__file__, "./models_in/aube_pleine.stp"))
from OCC.Display.SimpleGui import init_display from aocutils.display.topology import faces from aocutils.display.defaults import backend from aocxchange.iges import IgesImporter from corelib.core.files import path_from_file logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: %(levelname)6s :: %(module)20s :: ' '%(lineno)3d :: %(message)s') backend = backend display, start_display, add_menu, add_function_to_menu = init_display(backend) filename = path_from_file(__file__, "./models_in/iges/2_boxes.igs") iges_importer = IgesImporter(filename) the_shapes = iges_importer.shapes print(iges_importer.nb_shapes) # 13 print(len(iges_importer.shapes)) # 13 # display.DisplayShape(iges_importer.compound) # there are no shells or solids in the compound (IGES specific) faces(display, iges_importer.compound) display.FitAll() display.View_Iso() start_display()
def test_stl_exporter_wrong_extension(box_shape): r"""Trying to write a step file with the IgesExporter""" filename = path_from_file(__file__, "./models_out/box.step") with pytest.raises(IncompatibleFileFormatException): StlExporter(filename)
def test_stl_exporter_wrong_filename(box_shape): r"""Trying to write to a non-existent directory""" filename = path_from_file(__file__, "./nonexistent/box.stl") with pytest.raises(DirectoryNotFoundException): StlExporter(filename)
#!/usr/bin/env python # coding: utf-8 r"""Exporting a single shape to BREP""" import logging from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox from aocxchange.brep import BrepExporter from corelib.core.files import path_from_file logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: %(levelname)6s :: %(module)20s :: ' '%(lineno)3d :: %(message)s') # First create a simple shape to export box_shape = BRepPrimAPI_MakeBox(50, 50, 50).Shape() # Export to BREP filename = path_from_file(__file__, "./models_out/box.brep") step_exporter = BrepExporter(filename) step_exporter.set_shape(box_shape) step_exporter.write_file()
from OCC.Display.SimpleGui import init_display from aocutils.display.topology import solids, edges from aocutils.display.defaults import backend from aocxchange.step import StepImporter from corelib.core.files import path_from_file logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: %(levelname)6s :: %(module)20s :: ' '%(lineno)3d :: %(message)s') backend = backend display, start_display, add_menu, add_function_to_menu = init_display(backend) filename = path_from_file(__file__, "./models_in/step/aube_pleine.stp") step_importer = StepImporter(filename) # step_importer.read_file() -> automatic read_file !! print("Nb shapes: %i" % len(step_importer.shapes)) for shape in step_importer.shapes: print(shape.ShapeType()) # 2 -> solid # print("number_of_shapes: %i" % step_importer.number_of_shapes) # 0 ?? # display.DisplayShape(step_importer.shapes) solids(display, step_importer.shapes[0], transparency=0.8) edges(display, step_importer.shapes[0]) display.FitAll() display.View_Iso() start_display()
#!/usr/bin/env python # coding: utf-8 r"""Exporting multiple shapes to IGES""" import logging import OCC.Core.BRepPrimAPI from aocxchange.iges import IgesExporter from corelib.core.files import path_from_file logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: %(levelname)6s :: %(module)20s :: ' '%(lineno)3d :: %(message)s') # First create a simple shape to export box_shape = OCC.BRepPrimAPI.BRepPrimAPI_MakeBox(50, 50, 50).Shape() sphere_shape = OCC.BRepPrimAPI.BRepPrimAPI_MakeSphere(20).Shape() # Export to IGES filename = path_from_file(__file__, "./models_out/result_export_multi.iges") my_iges_exporter = IgesExporter(filename, format_="5.3") my_iges_exporter.add_shape(box_shape) my_iges_exporter.add_shape(sphere_shape) my_iges_exporter.write_file()
from aocutils.display.topology import solids from aocutils.display.defaults import backend from aocxchange.step import StepImporter from corelib.core.files import path_from_file logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: %(levelname)6s :: %(module)20s :: ' '%(lineno)3d :: %(message)s') backend = backend display, start_display, add_menu, add_function_to_menu = init_display(backend) # filename = path_from_file(__file__, "./models_in/step/dm1-id-214.stp") # filename = path_from_file(__file__, # "./models_in/step/APB_GC.stp") # big file 50 Mb ! # filename = path_from_file(__file__, "./models_in/step/66m.stp") filename = path_from_file(__file__, "./models_in/step/ASA.STEP") # filename = path_from_file(__file__, "./models_in/step/Groupama_VO70.stp") step_importer = StepImporter(filename) the_shapes = step_importer.shapes print("Nb shapes : %i" % len(the_shapes)) # 4 # print("number_of_shapes(): %i" % step_importer.number_of_shapes) # 0 ?? # display.DisplayColoredShape(the_shapes[0], Quantity_Color(Quantity_NOC_GRAY3)) solids(display, the_shapes[0]) display.FitAll() display.View_Iso() start_display()