def main(argv): step_reader = STEPControl_Reader() status = step_reader.ReadFile(argv[0]) if status == IFSelect_RetDone: # check status failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) number_of_roots = step_reader.NbRootsForTransfer() ok = False i = 1 while not ok and i <= number_of_roots: ok = step_reader.TransferRoot(i) i += 1 _nbs = step_reader.NbShapes() aResShape = step_reader.Shape(1) else: print("Error: can't read file.") sys.exit(0) display, start_display, add_menu, add_function_to_menu = init_display() display.DisplayShape(aResShape, update=True) start_display()
def read_step_file(filename, return_as_shapes=False, verbosity=False): """ read the STEP file and returns a compound filename: the file path return_as_shapes: optional, False by default. If True returns a list of shapes, else returns a single compound verbosity: optionl, False by default. """ assert os.path.isfile(filename) step_reader = STEPControl_Reader() status = step_reader.ReadFile(filename) if status == IFSelect_RetDone: # check status if verbosity: failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) ok = step_reader.TransferRoot(1) _nbs = step_reader.NbShapes() shape_to_return = step_reader.Shape(1) # a compound assert not shape_to_return.IsNull() else: raise AssertionError("Error: can't read file.") if return_as_shapes: shape_to_return = TopologyExplorer(shape_to_return).solids() return shape_to_return
def load_stp(self, path): """ Load a stp model """ reader = STEPControl_Reader() status = reader.ReadFile(path) if status != IFSelect_RetDone: raise ValueError("Failed to load: {}".format(path)) reader.PrintCheckLoad(False, IFSelect_ItemsByEntity) reader.PrintCheckTransfer(False, IFSelect_ItemsByEntity) ok = reader.TransferRoot() return reader.Shape(1)
def read_step_file(filename, as_compound=True, verbosity=True): """ read the STEP file and returns a compound filename: the file path verbosity: optional, False by default. as_compound: True by default. If there are more than one shape at root, gather all shapes into one compound. Otherwise returns a list of shapes. """ if not os.path.isfile(filename): raise FileNotFoundError("%s not found." % filename) step_reader = STEPControl_Reader() status = step_reader.ReadFile(filename) if status == IFSelect_RetDone: # check status if verbosity: failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) transfer_result = step_reader.TransferRoots() if not transfer_result: raise AssertionError("Transfer failed.") _nbs = step_reader.NbShapes() if _nbs == 0: raise AssertionError("No shape to transfer.") elif _nbs == 1: # most cases return step_reader.Shape(1) elif _nbs > 1: print("Number of shapes:", _nbs) shps = [] # loop over root shapes for k in range(1, _nbs + 1): new_shp = step_reader.Shape(k) if not new_shp.IsNull(): shps.append(new_shp) if as_compound: builder = BRep_Builder() compound = TopoDS_Compound() builder.MakeCompound(compound) for s in shps: builder.Add(compound, s) # shps = compound # compound, result = list_of_shapes_to_compound(shps) # if not result: # print("Warning: all shapes were not added to the compound") return compound else: print("Warning, returns a list of shapes.") return shps else: raise AssertionError("Error: can't read file.") return None
def read_step(filename): from OCC.STEPControl import STEPControl_Reader from OCC.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity step_reader = STEPControl_Reader() status = step_reader.ReadFile(filename) if status == IFSelect_RetDone: failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) ok = step_reader.TransferRoot(1) _nbs = step_reader.NbShapes() return step_reader.Shape(1) else: raise ValueError('Cannot read the file')
def read_step_file(filename): """ read the STEP file and returns a compound """ step_reader = STEPControl_Reader() status = step_reader.ReadFile(filename) if status == IFSelect_RetDone: # check status failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) step_reader.TransferRoot(1) a_shape = step_reader.Shape(1) else: print("Error: can't read file.") sys.exit(0) return a_shape
def read_step_file(filename): step_reader = STEPControl_Reader() status = step_reader.ReadFile(filename) if status == IFSelect_RetDone: # check status failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) ok = step_reader.TransferRoot(1) _nbs = step_reader.NbShapes() aResShape = step_reader.Shape(1) else: print("Error: can't read file.") sys.exit(0) return aResShape
def _load_model_file(filePath): step_reader = STEPControl_Reader() status = step_reader.ReadFile(filePath) if status == IFSelect_RetDone: # check status failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) ok = step_reader.TransferRoot(1) _nbs = step_reader.NbShapes() _shape = step_reader.Shape(1) return _shape else: raise Exception("Error: can't read file - Method: _load_STEP_file")
def step_reader(step_string): from OCC.StlAPI import StlAPI_Writer from OCC.STEPControl import STEPControl_Reader from OCC.BRep import BRep_Builder from OCC.TopoDS import TopoDS_Compound from OCC.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity builder = BRep_Builder() comp = TopoDS_Compound() builder.MakeCompound(comp) stl_writer = StlAPI_Writer() stl_writer.SetASCIIMode(True) with io.tmpfile(contents=io.shapes()[shape_name][:][0]) as tmpfile: step_reader = STEPControl_Reader() status = step_reader.ReadFile(tmpfile[1]) if status == IFSelect_RetDone: # check status failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) ok = step_reader.TransferRoot(1) nbs = step_reader.NbShapes() l = [] for i in range(1, nbs + 1): shape = step_reader.Shape(i) builder.Add(comp, shape) with io.tmpfile(suffix='.stl') as tmpf: stl_writer.Write(comp, tmpf[1]) tmpf[0].flush() reader = vtk.vtkSTLReader() reader.SetFileName(tmpf[1]) reader.Update() return reader
def make_shape(shape_name): global current_color # cf CADMBTB_API, but cannot get the same color order colors = list( reversed([ Quantity_NOC_DARKVIOLET, Quantity_NOC_BLUE1, Quantity_NOC_GREEN, Quantity_NOC_RED, Quantity_NOC_ORANGE, Quantity_NOC_SALMON, Quantity_NOC_YELLOW ])) with IO.tmpfile(contents=io.shapes()[shape_name][:][0]) as tmpfile: step_reader = STEPControl_Reader() status = step_reader.ReadFile(tmpfile[1]) if status == IFSelect_RetDone: # check status failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) ok = step_reader.TransferRoot(1) nbs = step_reader.NbShapes() l = [] for i in range(1, nbs + 1): ais_shape = display.DisplayShape(step_reader.Shape(i), update=True, transparency=.55) ais_shape.GetObject().SetColor(colors[current_color % 6]) current_color += 1 ais_shape.GetObject().SetMaterial( Graphic3d.Graphic3d_NOM_PLASTIC) l.append(ais_shape) return l
import sys step_reader = STEPControl_Reader() #status = step_reader.ReadFile('../stp/TABBY_EVO_step_asm.stp') #status = step_reader.ReadFile('../stp/single.stp') #status = step_reader.ReadFile('../stp/both.stp') #status = step_reader.ReadFile('../stp/part123.stp') status = step_reader.ReadFile('../stp/wheel.stp') #status = step_reader.ReadFile('../stp/tab2clean.stp') #status = step_reader.ReadFile('../stp/example.stp') #status = step_reader.ReadFile('../stp/TabbyEvo_4.stp') #status = step_reader.ReadFile('../stp/cylinder_block.stp') if status == IFSelect_RetDone: # check status failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) ok = step_reader.TransferRoot(1) _nbs = step_reader.NbShapes() aResShape = step_reader.Shape(1) else: print("Error: can't read file.") sys.exit(0) #display, start_display, add_menu, add_function_to_menu = init_display() #display.DisplayShape(aResShape, update=True) #f = display.View.View().GetObject()