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_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_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 path_from_file(file_origin, relative_path): r"""Builds an absolute path from a file using a relative path file_origin (absolute path) + relative_path (from file_origin) -> returned path Parameters ---------- file_origin : str Full / absolute path to the file from which the path is to be built relative_path : str The relative path from file_origin Returns ------- str Absolute file path """ # # Check file_origin exists # if not os.path.isfile(file_origin): # msg = "File %s not found." % file_origin # logger.error(msg) # raise FileNotFoundError(msg) # # dir_of_file_origin = os.path.dirname(os.path.realpath(file_origin)) # return os.path.abspath(os.path.join(dir_of_file_origin, relative_path)) warnings.warn("aocxchange.utils.path_from_file is deprecated, " "use corelibpy.path_from_file or corelibpy.p_ instead") return corelibpy.path_from_file(file_origin, relative_path)
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_iges_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.igs") exporter = IgesExporter(filename) with pytest.raises(ValueError): exporter.add_shape(gp_Pnt(1, 1, 1))
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_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_iges_exporter_happy_path_shape_subclass(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)
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_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_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_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
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 from corelibpy 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/aube_pleine.iges") iges_importer = IgesImporter(filename) print(iges_importer.nb_shapes) # 13 print(len(iges_importer.shapes)) # 13 the_compound = iges_importer.compound # display.DisplayShape(the_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_step_importer_wrong_file_content(): r"""wrong file content""" with pytest.raises(StepFileReadException): StepImporter(path_from_file(__file__, "./models_in/empty.stp"))
#!/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 from corelibpy 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()
def test_step_importer_wrong_extension(): r"""wrong file format (i.e. trying to read a iges file with step importer)""" with pytest.raises(IncompatibleFileFormatException): filename = path_from_file(__file__, "./models_in/aube_pleine.iges") StepImporter(filename)
#!/usr/bin/env python # coding: utf-8 r"""Exporting a shape to STL""" from __future__ import print_function import logging from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox from aocxchange.stl import StlExporter # from corelib.core.files import path_from_file from corelibpy 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 my_box_shape = BRepPrimAPI_MakeBox(50, 50, 50).Shape() # Export to STL. If ASCIIMode is set to False, then binary format is used. filename = path_from_file(__file__, "./models_out/result_export.stl") my_stl_exporter = StlExporter(filename, ascii_mode=True) my_stl_exporter.set_shape(my_box_shape) my_stl_exporter.write_file()
#!/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 from corelibpy 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()
def test_iges_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): IgesExporter(filename)
def test_iges_exporter_wrong_format(box_shape): r"""Format is not 5.1 or 5.3""" filename = path_from_file(__file__, "./models_out/box.igs") with pytest.raises(IgesUnknownFormatException): IgesExporter(filename, format_="48.3")
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 from corelibpy 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__, "./tubes.iges") iges_importer = IgesImporter(filename) print(iges_importer.nb_shapes) # 13 print(len(iges_importer.shapes)) # 13 the_compound = iges_importer.compound # display.DisplayShape(the_compound) # there are no shells or solids in the compound (IGES specific) faces(display, iges_importer.compound) display.FitAll() display.View_Iso() start_display()
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 from corelibpy 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()
from aocutils.display.topology import shells from aocutils.display.defaults import backend from aocxchange.stl import StlImporter # from corelib.core.files import path_from_file from corelibpy import path_from_file logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: %(levelname)6s :: %(module)20s :: ' '%(lineno)3d :: %(message)s') # open/parse STL file and get the resulting TopoDS_Shape instance # filename = path_from_file(__file__, "./models_in/sample.stl") # filename = path_from_file(__file__, "./models_in/USS_Albacore.STL") filename = path_from_file(__file__, "./models_in/stl/USS_Albacore.STL") my_stl_importer = StlImporter(filename) the_shape = my_stl_importer.shape # Then display the shape backend = backend display, start_display, add_menu, add_function_to_menu = init_display(backend) # display.DisplayShape(the_shape, color='BLUE', update=True) # 1 shell to display shells(display, the_shape) # faces # faces(display, the_shape, show_numbers=False) # super slow !! display.FitAll() display.View_Iso() start_display()
def test_iges_exporter_wrong_filename(box_shape): r"""Trying to write to a non-existent directory""" filename = path_from_file(__file__, "./nonexistent/box.igs") with pytest.raises(DirectoryNotFoundException): IgesExporter(filename)