def write_file(self): r"""Write file""" logger.info( "Writing brep : {cad_file}".format(cad_file=self._filename)) builder = Handle_Message_ProgressIndicator() breptools_Write(self._shape, self._filename, builder) logger.info("Wrote BREP file")
def mesh_shape(a_topods_shape): """ Takes a BRep filename (extension .brep) and returns a topods_shp ready to be displayed """ # dump the geometry to a brep file breptools_Write(a_topods_shape, "core_mesh_gmsh.brep") # create the gmesh file gmsh_geo_file_content = """SetFactory("OpenCASCADE"); Mesh.CharacteristicLengthMin = 1; Mesh.CharacteristicLengthMax = 5; a() = ShapeFromFile("core_mesh_gmsh.brep"); """ gmsh_geo_file = open("core_mesh_gmsh.geo", "w") gmsh_geo_file.write(gmsh_geo_file_content) gmsh_geo_file.close() # call gmsh gmsh_success = os.system( "gmsh core_mesh_gmsh.geo -2 -o core_mesh_gmsh.stl -format stl") # load the stl file if gmsh_success != 0 and os.path.isfile("core_mesh_gmsh.stl"): return read_stl_file("core_mesh_gmsh.stl") else: print("Be sure gmsh is in your PATH") sys.exit()
def write_target_edges(self,filename): comp = TopoDS_Compound() builder = BRep_Builder() builder.MakeCompound(comp) for shape in self.target_edges: builder.Add(comp, shape) breptools_Write(comp,filename)
def test_adaptive(self): gcode = "G21 G94\n" gcode += "G54\n" gcode += "M6 T1 G43 H1\n" hex_flats = TopoDS_Shape() builder = BRep_Builder() assert breptools_Read(hex_flats, './inputs/hex_flats.brep', builder) t = OCCUtils.Topo(hex_flats) num_faces = 0 for face_i in t.faces(): print("face") a2d = HackerCAD.Adaptive2d(face_i) a2d.tool_diameter = 25.4 / 32.0 a2d.helix_diameter = a2d.tool_diameter * 0.35 a2d.depth = 5.0 a2d.helix_angle = 0.5 a2d.flip_ax_dir() wire = a2d.compute() breptools_Write(wire, "output/hex_flat_{}.brep".format(num_faces)) num_faces += 1 pp = HackerCAD.PostProcessor() gcode += pp.process(a2d.motions) gcode += "M2\n" f = open("./output/bla.ngc", "w") f.write(gcode) f.close()
def test_001(self): shape = TopoDS_Shape() builder = BRep_Builder() assert breptools_Read(shape, "./input/cylinder.brep", builder) face = OCCUtils.Topo(shape).faces().__next__() ct = HackerCAD.CylindricalTurning(face) edge = ct.compute() breptools_Write(edge, "output/path.brep")
def move_to_box(iname, cname, wname, visualize=False): """Move foam to periodic box. Works on BREP files. Information about physical volumes is lost. Args: iname (str): input filename cname (str): output filename with cells wname (str): output filename with walls visualize (bool): show picture of foam morphology in box if True """ cells = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cells, iname, builder) texp = TopologyExplorer(cells) solids = list(texp.solids()) cells = TopoDS_Compound() builder.MakeCompound(cells) box = BRepPrimAPI_MakeBox(gp_Pnt(1, -1, -1), 3, 3, 3).Shape() vec = gp_Vec(-1, 0, 0) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-3, -1, -1), 3, 3, 3).Shape() vec = gp_Vec(1, 0, 0) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-1, 1, -1), 3, 3, 3).Shape() vec = gp_Vec(0, -1, 0) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-1, -3, -1), 3, 3, 3).Shape() vec = gp_Vec(0, 1, 0) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-1, -1, 1), 3, 3, 3).Shape() vec = gp_Vec(0, 0, -1) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-1, -1, -3), 3, 3, 3).Shape() vec = gp_Vec(0, 0, 1) solids = slice_and_move(solids, box, vec) create_compound(solids, cells, builder) breptools_Write(cells, cname) if visualize: display, start_display, _, _ = init_display() display.DisplayShape(cells, update=True) start_display() box = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), 1, 1, 1).Shape() walls = BRepAlgoAPI_Cut(box, cells).Shape() breptools_Write(walls, wname) if visualize: display, start_display, _, _ = init_display() display.DisplayShape(walls, update=True) start_display()
def test_adaptive(self): square_pacman = TopoDS_Shape() builder = BRep_Builder() assert breptools_Read(square_pacman, './inputs/square_pacman.brep', builder) t = OCCUtils.Topo(square_pacman) square_pacman = t.faces().__next__() hacker_a2d = HackerCAD.Adaptive2d(square_pacman) hacker_a2d.step_over_factor = 0.9 hacker_a2d.z_lift_distance = 0.0 wire = hacker_a2d.compute() breptools_Write(wire, "output/adaptive.brep")
def write_brep(occtopology, brep_filepath): """ This function writes a 3D model into brep format. Parameters ---------- occtopology : OCCtopology Geometries to be written into STL. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex brep_filepath : str The file path of the brep file. Returns ------- None : None The geometries are written to a brep file. """ from OCC.Core.BRepTools import breptools_Write breptools_Write(occtopology, brep_filepath)
def write_2_stl_gmsh(occtopology, stl_filepath, mesh_dim=2, min_length=1, max_length=5, gmsh_location="C:\\gmsh\\gmsh.exe"): """ This function mesh an occtopology using gmsh http://gmsh.info/. 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_dim : int, optional Perform 1D, 2D or 3D mesh generation. min_length : float, optional The minimum edge length of the mesh. max_length : float, optional The max edge length of the mesh. gmsh_location : str The location where the gmsh program is located. We assumed it is isntalled in C:\\gmsh\\gmsh.exe Returns ------- None : None The geometries are meshed and written to a stl file. """ # dump the geometry to a brep file from OCC.Core.BRepTools import breptools_Write import subprocess parent_path = os.path.abspath(os.path.join(stl_filepath, os.pardir)) filename = os.path.splitext(stl_filepath)[0] # create the brep file brep_file = os.path.join(parent_path, filename + ".brep") breptools_Write(occtopology, brep_file) # create the gmesh file geo_file = os.path.join(parent_path, "shape.geo") set_factory_str = 'SetFactory("OpenCASCADE");\n' mesh_min_length = 'Mesh.CharacteristicLengthMin = ' + str( min_length) + ';\n' mesh_max_length = 'Mesh.CharacteristicLengthMax = ' + str( max_length) + ';\n' brep_file_str = 'a() = ShapeFromFile("' + brep_file + '");' gmsh_geo_file_content = set_factory_str + mesh_min_length + mesh_max_length + brep_file_str gmsh_geo_file = open(geo_file, "w") gmsh_geo_file.write(gmsh_geo_file_content) gmsh_geo_file.close() # call gmsh cwd = os.getcwd() gmsh_dir = os.path.abspath(os.path.join(gmsh_location, os.pardir)) os.chdir(gmsh_dir) command = "gmsh " + geo_file + " -" + str( mesh_dim) + " -algo del3d -o " + stl_filepath + " -format stl" print(command) process = subprocess.call(command) # load the stl file if process == 0 and os.path.isfile(stl_filepath): print("WROTE TO:", stl_filepath) os.chdir(cwd) else: print("Be sure gmsh is in your PATH")
def export_brep_selected(self): comp = self.make_comp_selcted() brepname = create_tempnum(self.rootname, self.tmpdir, ".brep") breptools_Write(comp, brepname)
# gmsh binary location GMSH_BINARY = 'gmsh' # create a temporary directory to store gmsh files tmp = tempfile.TemporaryDirectory() TMP_DIR = tmp.name print("Files will be saved to ", TMP_DIR) ventilator_shp = read_step_file( os.path.join('..', 'assets', 'models', 'Ventilator.stp')) # dump the geometry to a brep file, check it worked BREP_BASENAME = "ventilator.brep" BREP_FILENAME = os.path.join(TMP_DIR, BREP_BASENAME) breptools_Write(ventilator_shp, BREP_FILENAME) assert os.path.isfile(BREP_FILENAME) # create the gmesh file gmsh_file_content = """SetFactory("OpenCASCADE"); Mesh.CharacteristicLengthMin = 1; Mesh.CharacteristicLengthMax = 5; a() = ShapeFromFile('%s'); """ % BREP_BASENAME GEO_FILENAME = os.path.join(TMP_DIR, "ventilator.geo") gmsh_file = open(GEO_FILENAME, "w") gmsh_file.write(gmsh_file_content) gmsh_file.close() assert os.path.isfile(GEO_FILENAME)