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 brep_reader(brep_string, indx): from OCC.StlAPI import StlAPI_Writer from OCC.BRepTools import BRepTools_ShapeSet shape_set = BRepTools_ShapeSet() shape_set.ReadFromString(brep_string) shape = shape_set.Shape(shape_set.NbShapes()) location = shape_set.Locations().Location(indx) shape.Location(location) stl_writer = StlAPI_Writer() with IO.tmpfile(suffix='.stl') as tmpf: stl_writer.Write(shape, tmpf[1]) tmpf[0].flush() reader = vtk.vtkSTLReader() reader.SetFileName(tmpf[1]) reader.Update() return reader
renderer_window = vtk.vtkRenderWindow() interactor_renderer = vtk.vtkRenderWindowInteractor() readers = dict() mappers = dict() actors = [] vtk_reader = { 'vtp' : vtk.vtkXMLPolyDataReader, 'stl' : vtk.vtkSTLReader } for shape_name in io.shapes(): shape_type = io.shapes()[shape_name].attrs['type'] if shape_type in ['vtp', 'stl']: with IO.tmpfile() as tmpf: tmpf[0].write(str(io.shapes()[shape_name][:][0])) tmpf[0].flush() reader = vtk_reader[shape_type]() reader.SetFileName(tmpf[1]) reader.Update() readers[shape_name] = reader # a try for smooth rendering but it does not work here normals = vtk.vtkPolyDataNormals() normals.SetInputConnection(reader.GetOutputPort()) normals.SetFeatureAngle(60.0) mapper = vtk.vtkDataSetMapper() add_compatiblity_methods(mapper) mapper.SetInputConnection(normals.GetOutputPort())