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_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 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 load_stl(self, path): """ Load a stl model """ reader = StlAPI_Reader() shape = TopoDS_Shape() reader.Read(shape, path) return shape
#!/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()
import OCC 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()