def read_stl_file(filename): """ opens a stl file, reads the content, and returns a BRep topods_shape object """ assert os.path.isfile(filename) stl_reader = StlAPI_Reader() the_shape = TopoDS_Shape() stl_reader.Read(the_shape, filename) assert not the_shape.IsNull() return the_shape
def write_2_stl(occtopology, stl_filepath, linear_deflection = 0.8, angle_deflection = 0.5): """ This function writes a 3D model into STL format. Parameters ---------- occtopology : OCCtopology Geometries to be written into STL. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex stl_filepath : str The file path of the STL file. mesh_incremental_float : float, optional Default = 0.8. Returns ------- None : None The geometries are written to a STL file. """ from OCC.StlAPI import StlAPI_Writer from OCC.BRepMesh import BRepMesh_IncrementalMesh from OCC.TopoDS import TopoDS_Shape # Export to STL stl_writer = StlAPI_Writer() stl_writer.SetASCIIMode(True) occtopology = TopoDS_Shape(occtopology) mesh = BRepMesh_IncrementalMesh(occtopology, linear_deflection, True, angle_deflection, True) assert mesh.IsDone() stl_writer.Write(occtopology,stl_filepath)
def simple_mesh(occshape, mesh_incremental_float=0.8): #TODO: figure out why is it that some surfaces do not work occshape = TopoDS_Shape(occshape) bt = BRep_Tool() BRepMesh_IncrementalMesh(occshape, mesh_incremental_float) occshape_face_list = fetch.geom_explorer(occshape, "face") occface_list = [] for occshape_face in occshape_face_list: location = TopLoc_Location() #occshape_face = modify.fix_face(occshape_face) facing = bt.Triangulation(occshape_face, location).GetObject() if facing: tab = facing.Nodes() tri = facing.Triangles() for i in range(1, facing.NbTriangles() + 1): trian = tri.Value(i) index1, index2, index3 = trian.Get() #print index1, index2, index3 pypt1 = fetch.occpt2pypt(tab.Value(index1)) pypt2 = fetch.occpt2pypt(tab.Value(index2)) pypt3 = fetch.occpt2pypt(tab.Value(index3)) #print pypt1, pypt2, pypt3 occface = make_polygon([pypt1, pypt2, pypt3]) occface_list.append(occface) return occface_list
def get_front_surface(stl_file, p1, p2): show_log("get_front_surface", "001") stl_reader = StlAPI_Reader() stl_shape = TopoDS_Shape() show_log("get_front_surface", "005") # Read takes 5 minutes stl_reader.Read(stl_shape, stl_file) #pickle.dump(stl_shape, open( "get_front_surface 001.tmp", "wb" ) ) # stl_shape = pickle.load( open( "./get_front_surface 001.tmp", "rb" ) ) # t = Topo(stl_shape) # print("number of faces: %i" % (t.number_of_faces())) #OCC.RWStl.rwstl().ReadFile(stl_file) #time.sleep(10) #cos theta should be less than 0.5, theta is the angle between 1,0,0 and the face normal show_log("get_front_surface", "010") box = BRepPrimAPI_MakeBox(p1, p2).Shape() show_log("get_front_surface", "012") # BRepAlgoAPI_Common takes 3 minutes CommonSurface = BRepAlgoAPI_Common(box, stl_shape).Shape() ais_stl_shape = display.DisplayShape(stl_shape) # ais_box = display.DisplayShape(box) # ais_common = display.DisplayShape(CommonSurface) # display.Context.SetTransparency(ais_box, 0.8) display.Context.SetTransparency(ais_stl_shape, 0.8) # orig = OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex(gp_Pnt(0,0,0)) # display.DisplayShape(orig.Shape()) return CommonSurface
def read_file(self): """ Read the STEP file and stores the result in a TopoDS_Shape """ stl_reader = StlAPI() shp = TopoDS_Shape() stl_reader.Read(shp, self._filename) self._shape = shp
def get_brep(): from OCC.BRep import BRep_Builder from OCC.BRepTools import breptools_Read from OCC.TopoDS import TopoDS_Shape cylinder_head = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cylinder_head, './models/cylinder_head.brep', builder) return cylinder_head
def make_reference(self, **parameters): filename = os.path.join(template_directory, "needles", "sphere.stl") if OCCVersion == "0.16": shape = TopoDS_Shape() stl_importer = StlAPI_Reader() stl_importer.Read(shape, filename) return shape else: stl_importer = STL.STLImporter(filename) stl_importer.read_file() shape = stl_importer.get_shape() return shape
def stl_file(name, shape): stl_filename = "./" + name + "_low_resolution.stl" stl_exporter = StlAPI_Writer() stl_exporter.SetASCIIMode( True) # change to False if you need binary export stl_exporter.Write(shape, stl_filename) # then we change the mesh resolution #mesh.SetDeflection(0.05) stl_reader = StlAPI_Reader() fan_shp = TopoDS_Shape() stl_reader.Read(fan_shp, stl_filename) exproted = DisplayShapeFunc(fan_shp) display(exproted) return stl_filename
def read_stl(stl_filepath): """ This function reads STL format. Parameters ---------- stl_filepath : str The file path of the STL file. Returns ------- occtopology : OCCtopology The geometries from an STL file. """ from OCC.StlAPI import StlAPI_Reader from OCC.TopoDS import TopoDS_Shape stl_reader = StlAPI_Reader() the_shape = TopoDS_Shape() stl_reader.Read(the_shape, stl_filepath) assert not the_shape.IsNull() return the_shape
def read_brep(filename): """ Reads in a brep file :return: The shape """ if not Path(filename).is_file(): print("File not found: " + filename) raise FileNotFoundError(filename) b = BRep_Builder() shape = TopoDS_Shape() if not breptools_Read(shape, filename, b): raise RuntimeError("Cannot read brep file: " + filename) return shape
def read_brep(brep_filepath): """ This function writes a 3D model into brep format. Parameters ---------- brep_filepath : str The file path of the brep file. Returns ------- occtopology : OCCtopology Geometries read from the brep. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex """ from OCC.BRepTools import breptools_Read from OCC.TopoDS import TopoDS_Shape from OCC.BRep import BRep_Builder shape = TopoDS_Shape() builder = BRep_Builder() breptools_Read(shape, brep_filepath, builder) return shape
##Copyright 2010-2014 Thomas Paviot ([email protected]) ## ##This file is part of pythonOCC. ## ##pythonOCC is free software: you can redistribute it and/or modify ##it under the terms of the GNU Lesser General Public License as published by ##the Free Software Foundation, either version 3 of the License, or ##(at your option) any later version. ## ##pythonOCC is distributed in the hope that it will be useful, ##but WITHOUT ANY WARRANTY; without even the implied warranty of ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##GNU Lesser General Public License for more details. ## ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. from OCC.Display.SimpleGui import init_display from OCC.BRepTools import breptools_Read from OCC.TopoDS import TopoDS_Shape from OCC.BRep import BRep_Builder cylinder_head = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cylinder_head, '../assets/models/cylinder_head.brep', builder) display, start_display, add_menu, add_function_to_menu = init_display() display.DisplayShape(cylinder_head, update=True) start_display()
def load_brep(BRepFile): BRepShape = TopoDS_Shape() builder = BRep_Builder() breptools_Read(BRepShape, BRepFile, builder) return BRepShape
import OCCUtils import time from OCC.BRepOffsetAPI import BRepOffsetAPI_MakePipe from OCCUtils.Construct import make_closed_polygon p1 = gp_Pnt(0, 0, 0) p2 = gp_Pnt(0, 10, 0) p3 = gp_Pnt(10, 10, 0) p4 = gp_Pnt(10, 0, 0) rect = make_closed_polygon(p1, p2, p3, p3) display, start_display, add_menu, add_function_to_menu = init_display() #my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape() stl_reader = StlAPI_Reader() stl_box = TopoDS_Shape() stl_reader.Read(stl_box, './models/box.stl') axe = gp_Ax2(gp_Pnt(-10, 1, 1), gp_Dir(0, 0, 1)) box = BRepPrimAPI_MakeBox(axe, 50, 15, 15).Shape() CommonSurface = BRepAlgoAPI_Common(box, stl_box).Shape() topo = Topo(CommonSurface) display.EraseAll() x_mid_max = -100 front_face = None for face in topo.faces(): bbox = Bnd_Box() OCC.BRepBndLib.brepbndlib_Add(face, bbox) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() x_mid = (xmin + xmax) / 2
def load_stl(self, path): """ Load a stl model """ reader = StlAPI_Reader() shape = TopoDS_Shape() reader.Read(shape, path) return shape
def load_brep(self, path): """ Load a brep model """ shape = TopoDS_Shape() builder = BRep_Builder() breptools_Read(shape, path, builder) return shape
##GNU Lesser General Public License for more details. ## ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. import sys from OCC.Display.SimpleGui import init_display from OCC.Graphic3d import Graphic3d_RenderingParams from OCC.BRepTools import breptools_Read from OCC.TopoDS import TopoDS_Shape from OCC.BRep import BRep_Builder display, start_display, add_menu, add_function_to_menu = init_display() # loads the motor model motor_c = TopoDS_Shape() builder = BRep_Builder() breptools_Read(motor_c, '../assets/models/Motor-c.brep', builder) display.DisplayShape(motor_c, update=True) def perspective(event=None): display.SetPerspectiveProjection() display.FitAll() def orthographic(event=None): display.SetOrthographicProjection() display.FitAll() def anaglyph_red_cyan(event=None): display.SetAnaglyphMode(Graphic3d_RenderingParams.Anaglyph_RedCyan_Simple) display.FitAll()
#!/usr/bin/python # coding: utf-8 r""" """ from OCC.Display.SimpleGui import init_display from OCC.TopoDS import TopoDS_Shape from OCC.StlAPI import StlAPI_Reader stl_reader = StlAPI_Reader() fan_shp = TopoDS_Shape() stl_reader.Read(fan_shp, './models/fan.stl') display, start_display, add_menu, add_function_to_menu = init_display('wx') display.DisplayShape(fan_shp, update=True) start_display()
##it under the terms of the GNU Lesser General Public License as published by ##the Free Software Foundation, either version 3 of the License, or ##(at your option) any later version. ## ##pythonOCC is distributed in the hope that it will be useful, ##but WITHOUT ANY WARRANTY; without even the implied warranty of ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##GNU Lesser General Public License for more details. ## ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. from OCC.Display.SimpleGui import init_display from OCC.BRepTools import breptools_Read from OCC.TopoDS import TopoDS_Shape from OCC.BRep import BRep_Builder Line_1 = TopoDS_Shape() Vertex_1 = TopoDS_Shape() Vertex_2 = TopoDS_Shape() builder = BRep_Builder() breptools_Read(Line_1, 'Line_1.brep', builder) breptools_Read(Vertex_1, 'Vertex_1.brep', builder) breptools_Read(Vertex_2, 'Vertex_2.brep', builder) display, start_display, add_menu, add_function_to_menu = init_display() display.DisplayShape(Line_1, update=True) display.DisplayShape(Vertex_1, update=True) display.DisplayShape(Vertex_2, update=True) start_display()
def loadBRep(filename): builder = BRep_Builder() shape = TopoDS_Shape() breptools_Read(shape, filename, builder) return shape
def get_brep(): cylinder_head = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cylinder_head, './models/cylinder_head.brep', builder) return cylinder_head
def write_shape(self, l_shells, filename, tol): """ Method to recreate a TopoDS_Shape associated to a geometric shape after the modification of points of each Face. It returns a TopoDS_Shape (Shape). :param l_shells: the list of shells after initial parsing :param filename: the output filename :param tol: tolerance on the surface creation after modification :return: None """ self.outfile = filename # global compound containing multiple shells global_compound_builder = BRep_Builder() global_comp = TopoDS_Compound() global_compound_builder.MakeCompound(global_comp) if self.check_topo == 0: # cycle on shells (multiple objects) shape_shells_explorer = TopExp_Explorer( self.shape.Oriented(TopAbs_FORWARD), TopAbs_SHELL) ishell = 0 while shape_shells_explorer.More(): per_shell = topods_Shell(shape_shells_explorer.Current()) # a local compound containing a shell compound_builder = BRep_Builder() comp = TopoDS_Compound() compound_builder.MakeCompound(comp) # cycle on faces faces_explorer = TopExp_Explorer( per_shell.Oriented(TopAbs_FORWARD), TopAbs_FACE) iface = 0 while faces_explorer.More(): topoface = topods.Face(faces_explorer.Current()) newface = self.write_face(l_shells[ishell][iface][0], l_shells[ishell][iface][1], topoface, tol) # add face to compound compound_builder.Add(comp, newface) iface += 1 faces_explorer.Next() new_shell = self.combine_faces(comp, 0.01) itype = TopoDS_Shape.ShapeType(new_shell) # add the new shell to the global compound global_compound_builder.Add(global_comp, new_shell) print("Shell {0} of type {1} Processed ".format(ishell, itype)) print "==============================================" ishell += 1 shape_shells_explorer.Next() else: # cycle on faces # a local compound containing a shell compound_builder = BRep_Builder() comp = TopoDS_Compound() compound_builder.MakeCompound(comp) # cycle on faces faces_explorer = TopExp_Explorer( self.shape.Oriented(TopAbs_FORWARD), TopAbs_FACE) iface = 0 while faces_explorer.More(): topoface = topods.Face(faces_explorer.Current()) newface = self.write_face(l_shells[0][iface][0], l_shells[0][iface][1], topoface, tol) # add face to compound compound_builder.Add(comp, newface) iface += 1 faces_explorer.Next() new_shell = self.combine_faces(comp, 0.01) itype = TopoDS_Shape.ShapeType(new_shell) # add the new shell to the global compound global_compound_builder.Add(global_comp, new_shell) print("Shell {0} of type {1} Processed ".format(0, itype)) print "==============================================" self.write_shape_to_file(global_comp, self.outfile)