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) assert len([i for i in topo.shells()]) == 2 assert next(topo.shells()).Closed() is True assert [i for i in 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) assert len([i for i in topo.shells()]) == 2 assert next(topo.shells()).Closed() is True assert [i for i in topo.shells()][1].Closed() is True assert topo.number_of_faces() == 108 * 2 assert topo.number_of_edges() == 162 * 2
def file_to_shape(pth): '''get a Shape from an .iges or .step file''' from OCCDataExchange.brep import BrepImporter from OCCDataExchange.iges import IgesImporter from OCCDataExchange.step import StepImporter from OCCDataExchange.stl import StlImporter assert os.path.isfile(pth), '%s is not a valid file' % (pth) ext = os.path.splitext(pth)[1].lower() assert ext in ['.iges', '.igs', '.stp', '.step', '.brep', '.stl'], '%s is not an readable format' % (ext) if ext in ['.iges', '.igs']: reader = IgesImporter(pth) return reader.compound elif ext in ['.step', '.stp']: reader = StepImporter(pth) return reader.compound elif ext == '.brep': reader = BrepImporter(pth) return reader.compound elif ext == '.stl': reader = StlImporter(pth) return reader.shape
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) # assert len(topo.solids()) == 1 assert len([i for i in topo.shells()]) == 1 assert next(topo.shells()).Closed() is True # direct method on TopoDS_Shell assert len([i for i in topo.faces()]) == 108 assert len([i for i in topo.edges()]) == 162 # ascii STL importer = StlImporter(path_from_file(__file__, "./models_in/box_ascii.stl")) topo = Topo(importer.shape) # assert len(topo.solids) == 1 assert len([i for i in topo.shells()]) == 1 assert next(topo.shells()).Closed() is True assert len([i for i in topo.faces()]) == 108 assert len([i for i in 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.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.BRepPrimAPI_MakeSphere(10) exporter.set_shape(sphere.Shape()) exporter.write_file( ) # this creates a file with a sphere only, this is STL specific # 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_stl_importer_happy_path(): r"""happy path""" importer = StlImporter(path_from_file(__file__, "./models_in/box_binary.stl")) assert isinstance(importer.shape, TopoDS.TopoDS_Shape)
def test_stl_importer_wrong_extension(): r"""wrong file format (i.e. trying to read a step file with iges importer)""" with pytest.raises(AssertionError): StlImporter(path_from_file(__file__, "./models_in/aube_pleine.stp"))
def test_iges_importer_wrong_path(): r"""Wrong filename""" with pytest.raises(AssertionError): StlImporter("C:/stupid-filename.bad_extension")
from OCCUtils.Topology import Topo from OCC.Display import SimpleGui from OCCDataExchange.stl import StlImporter from OCCDataExchange.utils 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 = OCCDataExchange.path_from_file(__file__, "./models_in/sample.stl") # filename = OCCDataExchange.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 display, start_display, add_menu, add_function_to_menu = SimpleGui.init_display( ) # display.DisplayShape(the_shape, color='BLUE', update=True) # 1 shell to display for sh in Topo(the_shape).shells(): display.DisplayShape(sh) # faces # OCCUtils.display.topology.faces(display, the_shape, show_numbers=False) # super slow !! display.FitAll()