def read_cadfile(self, fileName, disp=True): print(fileName) base_dir = os.path.dirname(fileName) basename = os.path.basename(fileName) rootname, extname = os.path.splitext(fileName) if extname in [".stp", ".step"]: shpe = read_step_file(fileName) elif extname in [".igs", ".iges"]: shpe = read_iges_file(fileName) elif extname in [".stl"]: shpe = read_stl_file(fileName) elif extname in [".brep"]: shpe = TopoDS_Shape() builder = BRep_Builder() breptools_Read(shpe, fileName, builder) elif extname in [".geo"]: stlfile = self.import_geofile(fileName, 0.1) shpe = read_stl_file(stlfile) else: print("Incorrect file index") # sys.exit(0) if disp == True: self.display.DisplayShape(shpe, update=True) return shpe
def Simulate_Path(): suction_Dia=12 cover_percentage=0.4 step_over=suction_Dia*cover_percentage step_down=suction_Dia*0.5 point_list=Generating_Path_function(step_over,step_down) display.EraseAll() stl_shp1 = read_stl_file('mymodel1.stl') props3 = GProp_GProps() brepgprop_VolumeProperties(stl_shp1, props3) model_mass = props3.Mass() view_box=BRepPrimAPI_MakeBox(gp_Pnt(-30, -30, 0),100, 100, 60) display.DisplayShape(view_box.Shape(),color = "BLACK",transparency = 0.999) display.DisplayShape(stl_shp1) #powder box=material material_box=BRepPrimAPI_MakeBox(gp_Pnt(-18, -18, 0),58, 58, 22) props2 = GProp_GProps() brepgprop_VolumeProperties(material_box.Shape(), props2) mass2 = props2.Mass() first_simulate= simulate(suction_Dia,point_list[0],90,material_box) second_simulate=simulate(suction_Dia,point_list[1],45,first_simulate) props = GProp_GProps() brepgprop_VolumeProperties(second_simulate.Shape(), props) mass = props.Mass() print("remained volume = %",(mass*100/(mass2-model_mass))) display.DisplayShape(second_simulate.Shape(),color = "BLACK",transparency = 0.3)
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, "shape.brep") # create the gmesh file gmsh_geo_file_content = """SetFactory("OpenCASCADE"); Mesh.CharacteristicLengthMin = 1; Mesh.CharacteristicLengthMax = 5; a() = ShapeFromFile("shape.brep"); """ gmsh_geo_file = open("shape.geo", "w") gmsh_geo_file.write(gmsh_geo_file_content) gmsh_geo_file.close() # call gmsh gmsh_success = os.system("gmsh shape.geo -2 -o shape.stl -format stl") # load the stl file if gmsh_success != 0 and os.path.isfile("shape.stl") : return read_stl_file("shape.stl") else: print("Be sure gmsh is in your PATH") sys.exit()
def generate_solid(self): """ Generate an assembled solid shaft using the BRepBuilderAPI_MakeSolid algorithm. This method requires PythonOCC to be installed. :raises RuntimeError: if the assembling of the solid shaft is not completed successfully :return: solid shaft :rtype: OCC.Core.TopoDS.TopoDS_Solid """ ext = os.path.splitext(self.filename)[1][1:] if ext == 'stl': shaft_compound = read_stl_file(self.filename) elif ext == 'iges': iges_reader = IGESControl_Reader() iges_reader.ReadFile(self.filename) iges_reader.TransferRoots() shaft_compound = iges_reader.Shape() else: raise Exception('The shaft file is not in iges/stl formats') sewer = BRepBuilderAPI_Sewing(1e-2) sewer.Add(shaft_compound) sewer.Perform() result_sewed_shaft = sewer.SewedShape() shaft_solid_maker = BRepBuilderAPI_MakeSolid() shaft_solid_maker.Add(OCC.Core.TopoDS.topods_Shell(result_sewed_shaft)) if not shaft_solid_maker.IsDone(): raise RuntimeError('Unsuccessful assembling of solid shaft') shaft_solid = shaft_solid_maker.Solid() return shaft_solid
def convert_stl_handler(file_path): # 1. read stl file, if not binary, convert to binary convert_stl_path = file_path + '.stl' if not check_stl_binary(file_path): shapes = read_stl_file(file_path) write_stl_by_shapes(shapes, convert_stl_path) else: convert_stl_path = file_path return convert_stl_to_draco_gltf(file_path, convert_stl_path)
def import_stl(self, filename): stl_shp = read_stl_file(filename) scls = SCLShape(stl_shp) sclp = SCLPart3(self) sclp.set_shape(scls) name = get_inc_name("stl") sclp.set_name(name) debug("Creating stl %s" % (name, )) self.add_child_context(sclp)
def Simulate_offset(): path=Generating_Path() display.EraseAll() stl_shp = read_stl_file('mymodel3.stl') stl_shp2 = read_stl_file('mymodel2.stl') # for reading IGS files #stl_shp=read_iges_file() hieght=z_max_finder(stl_shp) #making chamber box #box_shape = BRepPrimAPI_MakeBox(gp_Pnt(-25, -25, 0),60, 60, hieght).Shape() display.DisplayShape(stl_shp2) #display.DisplayShape(box_shape,color = "BLACK", transparency = 0.7) path_offset=path[0] path_z_level=path[1] all_points=[] for j in range(len(path_offset)-1): edge_list=make_edges_list(path_offset[-j-1]) for i in range(len(edge_list)): display.DisplayShape(edge_list[i]) outer_points=find_boundary_points_form_offset(edge_list) #all_points=my_connector(outer_points,all_points) simulate(outer_points,45)
def handler(self, file_path, is_bin=False): super(StlModel, self).handler(file_path, is_bin) # read stl file, if not binary, convert to binary convert_stl_path = file_path + '.stl' clear_convert_stl = False if not self.check_binary(file_path): shapes = read_stl_file(file_path) self.write_by_shapes(shapes, convert_stl_path) clear_convert_stl = True else: convert_stl_path = file_path return self.convert_to_draco_gltf(file_path, convert_stl_path, is_bin, False, clear_convert_stl)
def Load_CAD_Model_STL(): stl_shp = read_stl_file('mymodel1.stl') # for reading IGS files #stl_shp=read_iges_file('mymodel4.igs') #stl_shp=TopoDS_Shape(stl_shp) #height of chamber box hieght=z_max_finder(stl_shp) #making chamber box box_shape = BRepPrimAPI_MakeBox(gp_Pnt(-18, -18, 0),65, 65, hieght).Shape() display.DisplayShape(stl_shp) display.DisplayShape(box_shape,color = "BLACK", transparency = 0.7) display.FitAll()
def Simulate_raster_from_offset(): path=Generating_Path() path_offset=path[0] path_z_level=path[1] display.EraseAll() stl_shp = read_stl_file('mymodel3.stl') #stl_shp2 = read_stl_file('mymodel2.stl') # for reading IGS files stl_shp2=read_iges_file('mymodel4.igs') all_points=[] hieght=z_max_finder(stl_shp) #making chamber box material_box = BRepPrimAPI_MakeBox(gp_Pnt(-70, -70, 0),130, 130, hieght) display.DisplayShape(stl_shp) #display.DisplayShape(box_shape,color = "BLACK", transparency = 0.7) for i in range(len(path_z_level)-1): my_path=raster_from_offset(-70,-70,65,65,path_z_level[-i-1],10,path_offset[-i-1]) show_path_from_points(my_path) all_points=my_connector(my_path,all_points) simulate(all_points,90,material_box)
def Generating_Path_function(step_over,step_down): raster_point_list=[] list1=[] path_points=[] stl_shp = read_stl_file('mymodel22.stl') #stl_shp2=read_iges_file('mymodel4.igs') z_max=z_max_finder(stl_shp) #number of possible levels nb_z_level= z_max//step_down nb_z_level=int(nb_z_level) #proper z level distance for the current loaded model with respect to standard my_z_level=z_max/nb_z_level for i in range(nb_z_level): # to avoid having section on z=0 then +1 is added to first level. myWire=Section_finder(i*my_z_level+1,stl_shp) list1.append(myWire) offset_oriented_list=[] z_level_list=[] for j in range(len(list1)): z_level=Get_my_Z(list1[j].Vertex()) offset=BRepOffsetAPI_MakeOffset(list1[j].Wire(),GeomAbs_Intersection,False) offset.Perform(0.5,0.0) display.DisplayShape(offset.Shape()) offset2=BRepOffsetAPI_MakeOffset(list1[j].Wire(),GeomAbs_Intersection,False) offset2.Perform(4,0.0) z_level_list.append(z_level) #to generate raster from offset path raster1=raster_from_offset(-20,-19,45,45,z_level,step_over,offset2) raster_point_list.append(raster1) #to generate offset oriented path offset_edge_list=make_edges_list(offset) offset_points=find_boundary_points_form_offset(offset_edge_list) offset_oriented_list.append(offset_points) safe_point=[1,1,z_max+2] top_level_point_list=top_level_raster(-20,-20,45,45,z_max,step_over) for k in range(len(top_level_point_list)): path_points.append(top_level_point_list[k]) path_points.append(safe_point) j=0 i=0 for i in range(len(raster_point_list)): entering_point=[raster_point_list[-i-1][0][0],raster_point_list[-i-1][0][1],z_max+5] path_points.append(entering_point) for j in range(len(raster_point_list[-i-1])): path_points.append(raster_point_list[-i-1][j]) rising_point=[raster_point_list[-i-1][j][0],raster_point_list[-i-1][j][1],z_max+5] path_points.append(rising_point) path_points.append(safe_point) #a different path_point list for offset oriented(with pitch angle) i=0 j=0 path_points2=[] for i in range(len(offset_oriented_list)): entering_point=[offset_oriented_list[-i-1][0][0],offset_oriented_list[-i-1][0][1],z_max+5,0] path_points2.append(entering_point) for j in range(len(offset_oriented_list[-i-1])): path_points2.append(offset_oriented_list[-i-1][j]) rising_point=[offset_oriented_list[-i-1][j][0],offset_oriented_list[-i-1][j][1],z_max+5,0] path_points2.append(rising_point) #point_path is a list of points that each point is a list with x,y,z in order to show # the path we should translate each point to a gp_Pnt calss i=0 path_points_gp=[] for i in range(len(path_points)): xp=path_points[i][0] yp=path_points[i][1] zp=path_points[i][2] apoint=gp_Pnt(xp,yp,zp) path_points_gp.append(apoint) i=0 path_length=0 for i in range(len(path_points_gp)-1): path_edge=BRepBuilderAPI_MakeEdge(path_points_gp[i],path_points_gp[i+1]) display.DisplayShape(path_edge.Shape()) g1 = GProp_GProps() brepgprop_LinearProperties(path_edge.Shape(), g1) length = g1.Mass() path_length+= length i=0 path_points_gp=[] for i in range(len(path_points2)): xp=path_points2[i][0] yp=path_points2[i][1] zp=path_points2[i][2] apoint=gp_Pnt(xp,yp,zp) path_points_gp.append(apoint) i=0 for i in range(len(path_points_gp)-1): path_edge=BRepBuilderAPI_MakeEdge(path_points_gp[i],path_points_gp[i+1]) display.DisplayShape(path_edge.Shape()) g1 = GProp_GProps() brepgprop_LinearProperties(path_edge.Shape(), g1) length = g1.Mass() path_length+= length print('Path Length=', path_length) return path_points,path_points2
def test_read_stl_file(self): read_stl_file(STL_ASCII_SAMPLE_FILE) read_stl_file(STL_BINARY_SAMPLE_FILE)
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) # call gmsh, generate an STL file STL_FILENAME = os.path.join(TMP_DIR, "ventilator.stl") os.system("%s %s -2 -o %s -format stl" % (GMSH_BINARY, GEO_FILENAME, STL_FILENAME)) assert os.path.isfile(STL_FILENAME) # load the stl file meshed_ventilator_shp = read_stl_file(STL_FILENAME) display.DisplayShape(translate_shp(ventilator_shp, gp_Vec(-100, 0, 0))) display.DisplayShape(meshed_ventilator_shp) start_display()
def import_to_STL(self, fileName): stl_shp = read_stl_file(fileName) return stl_shp
##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/>. import os from OCC.Display.SimpleGui import init_display from OCC.Extend.DataExchange import read_stl_file stl_filename = os.path.join('models', 'fan.stl') stl_shp = read_stl_file(stl_filename) display, start_display, add_menu, add_function_to_menu = init_display() display.DisplayShape(stl_shp, update=True) start_display()