def iges2png(iges_file, png_file): '''usage: %prog <iges_file> <png_file> converts from IGES to PNG stuff it leaves behind: input.iges.stl input.iges.pov input.iges.pov.inc output.png''' print "iges2png: loading iges file." my_iges_importer = IGESImporter(iges_file) my_iges_importer.ReadFile() the_shapes = my_iges_importer.GetShapes() main_shape = the_shapes[0] print "iges2png: saving iges as stl." stl_exp = StlAPI() stl_exp.Write(main_shape, iges_file + ".stl") print "iges2png: converting stl to pov" stl2pov(iges_file + ".stl", iges_file + ".pov") print "iges2png: converting pov to png" pov2png(iges_file + ".pov", png_file) print "iges2png: done"
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 LoadFile(self, filename): extension = os.path.basename(filename).split(".").pop().lower() start_time = time.time() if extension =="step" or extension == "stp": stepReader = STEPControl.STEPControl_Reader() stepReader.ReadFile(str(filename)) numTranslated = stepReader.TransferRoots() shape = stepReader.OneShape() elif extension == "stl": shape = TopoDS.TopoDS_Shape() stl_reader = StlAPI.StlAPI_Reader() stl_reader.Read(shape,str(filename)) elif extension =="iges" or extension =="igs": i = IGESControl.IGESControl_Controller() i.Init() iges_reader = IGESControl.IGESControl_Reader() iges_reader.ReadFile(str(filename)) iges_reader.TransferRoots() shape = iges_reader.OneShape() elif extension == "brep": shape = TopoDS.TopoDS_Shape() builder = BRep.BRep_Builder() BRepTools.BRepTools().Read(shape,str(filename),builder) else: return True self.canva._3dDisplay.EraseAll() self.canva._3dDisplay.DisplayShape(shape) wx.SafeYield() self.canva._3dDisplay.View_Iso() self.canva._3dDisplay.FitAll() end_time = time.time() self.SetTitle("pythonOCC Interactive Console %s:%s"%(VERSION,filename)) duration = end_time-start_time print "%s STEP file loaded and displayed in %f seconds."%(filename,duration)
def file_to_shape(pth): '''get a Shape from an .iges or .step file''' assert os.path.isfile(pth), '%s is not a valid directory' % (pth) ext = os.path.splitext(pth)[1] print 'ext', ext assert ext in ['.iges', '.igs', '.stp', '.step', '.brep', '.stl'], '%s is not an readable format' % (ext) if ext in ['.iges', '.igs']: __i = IGESControl_Controller() __i.Init() reader = IGESControl_Reader() elif ext in ['.step', '.stp']: reader = STEPControl_Reader() elif ext == '.brep': from OCC import TopoDS, BRep, BRepTools shape = TopoDS.TopoDS_Shape() builder = BRep.BRep_Builder() BRepTools.BRepTools().Read(shape, pth, builder) return shape elif ext == '.stl': from OCC import TopoDS, StlAPI shape = TopoDS.TopoDS_Shape() stl_reader = StlAPI.StlAPI_Reader() stl_reader.Read(shape, pth) return shape reader.ReadFile(pth) n_translated = reader.TransferRoots() shape = reader.OneShape() del reader return shape
def LoadFile(self,filename): extension = os.path.basename(filename).split(".").pop().lower() start_time = time.time() if extension =="step" or extension == "stp": from OCC.Utils.DataExchange.STEP import STEPImporter stepReader = STEPImporter(str(filename)) stepReader.read_file() shape = stepReader.get_shapes() elif extension == "stl": from OCC import TopoDS, StlAPI shape = TopoDS.TopoDS_Shape() stl_reader = StlAPI.StlAPI_Reader() stl_reader.Read(shape,str(filename)) elif extension =="iges" or extension =="igs": from OCC import IGESControl i = IGESControl.IGESControl_Controller() i.Init() iges_reader = IGESControl.IGESControl_Reader() iges_reader.ReadFile(str(filename)) iges_reader.TransferRoots() shape = iges_reader.OneShape() elif extension == "brep": from OCC import TopoDS, BRep, BRepTools shape = TopoDS.TopoDS_Shape() builder = BRep.BRep_Builder() BRepTools.BRepTools().Read(shape,str(filename),builder) else: return True self.canva._display.DisplayShape(shape) end_time = time.time() self.SetTitle("CAD Viewer - pythonOCC %s:%s"%(VERSION,filename)) duration = end_time-start_time print "%s STEP file loaded and displayed in %f seconds."%(filename,duration)
def write_file(self): r"""Write file""" mesh = BRepMesh_IncrementalMesh( self._shape, self._line_deflection, self._is_relative, self._angular_deflection, self._in_parallel) mesh.Perform() stl_writer = StlAPI.StlAPI_Writer() stl_writer.SetASCIIMode(self._ascii_mode) stl_writer.Write(self._shape, self._filename) logger.info("Wrote STL file")
def readSTLShape(fileName): ts = TopoDS.TopoDS() log.info("Reading STL:'" + fileName + "'...") #read stl file shape = TopoDS.TopoDS_Shape() stl_reader = StlAPI.StlAPI_Reader() stl_reader.Read(shape, fileName) return fixShape(shape)
def toSTL(self, filename, ascii=False, deflection=0.01): ''' Writes STL output of the solid :param filename: Path of the file to write JSON to :type filename: str :param ascii: choose between ASCII or Binary STL format :type ascii: bool :param deflection: Provides control over quality of exported STL. Higher the deflection value, lower the accuracy of exported STL, smaller the size of resulting file. Lower the deflection value, more accurate the exported STL, bigger the size of resulting file. :type deflection: float ''' stl_writer = StlAPI.StlAPI_Writer() stl_writer.SetASCIIMode(ascii) stl_writer.SetDeflection(deflection) stl_writer.Write(self.shape, filename)
def readSTLShape(fileName): ts = TopoDS.TopoDS() logging.info("Reading STL:'" + fileName + "'...") #read stl file shape = TopoDS.TopoDS_Shape() stl_reader = StlAPI.StlAPI_Reader() stl_reader.Read(shape, fileName) logging.info("Fixing holes and degenerated Meshes...") sf = ShapeFix.ShapeFix_Shape(shape) sf.Perform() fixedShape = sf.Shape() logging.info("Making Solid from the Shell...") #bb = BRepBuilderAPI.BRepBuilderAPI_MakeSolid(ts.Shell(fixedShape)); #bb.Build(); bb = ShapeFix.ShapeFix_Solid() return bb.SolidFromShell(ts.Shell(fixedShape)) logging.info("Done.") return bb.Solid()
def shape_to_file(shape, pth, filename, format='iges'): '''write a Shape to a .iges .brep .stl or .step file''' _pth = os.path.join(pth, filename) assert not os.path.isdir(_pth), 'wrong path, filename' _file = "%s.%s" % (_pth, format) _formats = ['iges', 'igs', 'step', 'stp', 'brep', 'stl'] assert format in _formats, '%s is not a readable format, should be one of %s ' % ( format, _formats) if format in ['iges', 'igs']: i = IGESControl_Controller() i.Init() writer = IGESControl_Writer() writer.AddShape(shape) writer.Write(_file) return _file elif format in ['step', 'stp']: i = STEPControl_Controller() i.Init() writer = STEPControl_Writer() writer.Transfer(shape, STEPControl_AsIs) writer.Write(_file) return _file elif format == 'brep': from OCC import TopoDS, BRep, BRepTools #shape = TopoDS.TopoDS_Shape() builder = BRep.BRep_Builder() BRepTools.BRepTools().Write(shape, _file) elif format == 'stl': from OCC import TopoDS, StlAPI #shape = TopoDS.TopoDS_Shape() stl_reader = StlAPI.StlAPI_Writer() stl_reader.Read(shape, _file) else: raise TypeError( 'format should be one of [iges,igs], [step,stp], brep, stl\ngot %s' % (format))
''' xform = gp_Trsf() xform.SetValues( 1.5, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, Precision_Angular(), Precision_Confusion() ); brep = BRepBuilderAPI_GTransform(shape, gp_GTrsf(xform), False) ''' # This options works as desired xform = gp_GTrsf() xform.SetVectorialPart(gp_Mat( 1.5, 0, 0, 0, 1, 0, 0, 0, 1, )) brep = BRepBuilderAPI_GTransform(shape, xform, False) brep.Build() shape = brep.Shape() stl_writer = StlAPI.StlAPI_Writer() stl_writer.Write(shape, 'scaled-cylinder.stl')
def read_file(self): r"""Read the STL file and stores the result in a TopoDS_Shape""" stl_reader = StlAPI.StlAPI_Reader() shape = TopoDS.TopoDS_Shape() stl_reader.Read(shape, self._filename) self._shape = shape
def toSTL(shape, filename, ascii=False, deflection=0.001): stl_writer = StlAPI.StlAPI_Writer() stl_writer.SetASCIIMode(ascii) stl_writer.SetDeflection(deflection) stl_writer.Write(shape, filename)
def write_file(self): stl_writer = StlAPI() stl_writer.Write(self._shape, self._filename, self._ASCIIMode)
def _readSTL(self, inputFileName): ts = TopoDS.TopoDS() shape = TopoDS.TopoDS_Shape() stl_reader = StlAPI.StlAPI_Reader() stl_reader.Read(shape, inputFileName) return shape