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 toSTEP(shape, filename, verbose=False, tolerance=0.001): ''' Writes STL output of the solid :param filename: Path of the file to write JSON to :type filename: str :param verbose: Choose if you want to see the STEP stats :type verbose: bool :param tolerance: Provides control over quality of exported STEP. :type tolernace: float ''' class Verboseness(): def __enter__(self): if not verbose: import sys, os sys.stdout.flush() self.newstdout = os.dup(1) self.devnull = os.open('/dev/null', os.O_WRONLY) os.dup2(self.devnull, 1) os.close(self.devnull) sys.stdout = os.fdopen(self.newstdout, 'w') def __exit__(self, type, value, traceback): if not verbose: os.dup2(self.newstdout, 1) with Verboseness(): stepWriter = STEPControl.STEPControl_Writer() stepWriter.SetTolerance(tolerance) if shape: status = stepWriter.Transfer(shape, STEPControl.STEPControl_AsIs) if status: stepWriter.Write(filename)
def __init__(self, filename, verbose=False, schema="AP214CD", tolerance=1e-4): logger.info("StepExporter instantiated with filename : %s" % filename) logger.info("StepExporter schema : %s" % schema) logger.info("StepExporter tolerance : %s" % str(tolerance)) if schema not in ["AP203", "AP214CD"]: msg = "Unsupported STEP schema" logger.error(msg) raise AssertionError(msg) check_exporter_filename(filename, step_extensions) check_overwrite(filename) self._filename = filename self._shapes = list() self.verbose = verbose self._stepcontrol_writer = STEPControl.STEPControl_Writer() self._stepcontrol_writer.SetTolerance(tolerance) Interface.Interface_Static_SetCVal("write.step.schema", schema)
def readStepShape(fileName): logging.info("Reading STEP file:'" + fileName + "'...") stepReader = STEPControl.STEPControl_Reader() stepReader.ReadFile(fileName) numItems = stepReader.NbRootsForTransfer() numTranslated = stepReader.TransferRoots() logging.info("Read " + str(numTranslated) + " from File.") shape = stepReader.OneShape() logging.info("Done.") return shape
def readStepShape(fileName): log.info("Reading STEP file:'" + fileName + "'...") stepReader = STEPControl.STEPControl_Reader() if not os.path.exists(fileName): raise ValueError, "Error: '%s' Does not Exist!" % fileName stepReader.ReadFile(fileName) numItems = stepReader.NbRootsForTransfer() numTranslated = stepReader.TransferRoots() log.info("Read " + str(numTranslated) + " from File.") shape = stepReader.OneShape() print "Step file is of type %d" % shape.ShapeType() log.info("Done.") return shape
def read_file(self): """ Read the STEP file and stores the result in a _shapes list """ stepcontrol_reader = STEPControl.STEPControl_Reader() status = stepcontrol_reader.ReadFile(self._filename) if status == IFSelect.IFSelect_RetDone: stepcontrol_reader.PrintCheckLoad(False, IFSelect.IFSelect_ItemsByEntity) nb_roots = stepcontrol_reader.NbRootsForTransfer() logger.info("%i root(s)" % nb_roots) if nb_roots == 0: msg = "No root for transfer" logger.error(msg) raise ValueError(msg) stepcontrol_reader.PrintCheckTransfer( False, IFSelect.IFSelect_ItemsByEntity) self._number_of_shapes = stepcontrol_reader.NbShapes() for n in range(1, nb_roots + 1): logger.info("Root index %i" % n) ok = stepcontrol_reader.TransferRoot(n) logger.info("TransferRoots status : %i" % ok) if ok: # for i in range(1, self.nb_shapes + 1): a_shape = stepcontrol_reader.Shape(n) if a_shape.IsNull(): msg = "At least one shape in IGES cannot be transferred" logger.warning(msg) else: self._shapes.append(a_shape) logger.info("Appending a %s to list of shapes" % types_lut.topo_lut[a_shape.ShapeType()]) else: msg = "One shape could not be transferred" logger.warning(msg) warnings.warn(msg) return True else: msg = "Status is not IFSelect.IFSelect_RetDone" logger.error(msg) raise ValueError(msg)
def toSTEP(self, filename, verbose=False, tolerance=0.001): ''' Writes STEP output of the solid :param filename: Path of the file to write STEP to :type filename: str :param verbose: Choose if you want to see the STEP stats :type verbose: bool :param tolerance: Provides control over quality of exported STEP. :type tolernace: float ''' with self._Verboseness(verbose): stepWriter = STEPControl.STEPControl_Writer() stepWriter.SetTolerance(tolerance) if self.shape: status = stepWriter.Transfer(self.shape, STEPControl.STEPControl_AsIs) if status: stepWriter.Write(filename)
def fromSTEP(self, filename, verbose=False): ''' Imports a STEP file to a solid :param filename: Path of the file to write STEP to :type filename: str :param verbose: Choose if you want to see the STEP stats :type verbose: bool ''' with self._Verboseness(verbose): if filename: stepReader = STEPControl.STEPControl_Reader() status = stepReader.ReadFile(filename) if status: nbr = stepReader.NbRootsForTransfer() for n in range(1, nbr + 1): stepReader.TransferRoot(n) nbs = stepReader.NbShapes() if nbs == 1: if stepReader.Shape(1): self.shape = stepReader.Shape(1) return self
def pairs(itr): a,b = tee(itr) b.next() return izip(a,b) points = [(0,0,0), (1,1,1), (0,0,-2), (0,2,0)] vertices = [BRepBuilderAPI.BRepBuilderAPI_MakeVertex(gp.gp_Pnt(*p)) for p in points] #print "verts", vertices #edges = [BRepBuilderAPI.BRepBuilderAPI_MakeEdge(v1.Vertex(),v2.Vertex()) # for v1,v2 in pairs(vertices)] v=vertices edges = [BRepBuilderAPI.BRepBuilderAPI_MakeEdge(v[i].Vertex(),v[j].Vertex()) for i,j, in [(0,1),(0,2),(0,3)] ] #print "edges", edges wire = BRepBuilderAPI.BRepBuilderAPI_MakeWire() for e in edges: wire.Add(e.Edge()) print wire step_export = STEPControl.STEPControl_Writer() step_export.Transfer(wire.Shape(), STEPControl.STEPControl_AsIs) step_export.Write("/home/bryan/test_wire.stp")
#!/usr/bin/python # coding: utf-8 r""" """ from viewer import view from OCC import BRepPrimAPI, STEPControl def get_shape(): box = BRepPrimAPI.BRepPrimAPI_MakeBox(10, 20, 30) shape = box.Shape() return shape shape = get_shape() view(shape) writer = STEPControl.STEPControl_Writer() writer.Transfer(shape, STEPControl.STEPControl_AsIs) writer.Write("test.step")
def export_shapes(shapeList, filename): print(shapeList) step_export = STEPControl.STEPControl_Writer() for shape in shapeList: step_export.Transfer(shape,STEPControl.STEPControl_AsIs) step_export.Write(str(filename))
def _readSTEP(self, inputFileName): stepReader = STEPControl.STEPControl_Reader() stepReader.ReadFile(inputFileName) numItems = stepReader.NbRootsForTransfer() numTranslated = stepReader.TransferRoots() return stepReader.OneShape()