def import_step(self, fn): """ Import a STEP file generated by the OpenVSP. :param str fn: The full path to the file. :return: None. :raise RuntimeError: If the file cannot be read. """ print('Importing OpenVSP STEP file...') # Build a compound for geometric sets compound = TopoDS_Compound() BRep_Builder().MakeCompound(compound) # Read file step_reader = STEPControl_Reader() status = step_reader.ReadFile(fn) if status not in [IFSelect_RetVoid, IFSelect_RetDone]: raise RuntimeError("Unable to read OpenVSP STEP file.") # TODO Convert to desired units # Interface_Static.SetCVal_("xstep.cascade.unit", "SOME UNIT HERE") # Transfer # OpenVSP STEP files result in one root and one shape (a compound) step_reader.TransferRoot(1) master_shape = step_reader.Shape(1) # Iterate over master shape to find compounds for geometric sets. These # sets contain the surfaces that make up the component. iterator = TopoDS_Iterator(master_shape, True, True) more = True while iterator.More() and more: print('--Processing a component...') # The compound compound = iterator.Value() # Hack to handle single component for now... if compound.ShapeType() != TopAbs_COMPOUND: compound = master_shape more = False solid, is_valid, invalid_shapes = _build_solid( compound, self._divide) if is_valid: print(' Successfully built a solid.') self._solids.append(solid) else: print(' Failed to build a valid solid.') self._invalid.append(solid) self._invalid_shapes += invalid_shapes # Next shape iterator.Next() print('Finished.\n')
def shape_iter(self): """ :return: Yield the underlying sub-shape(s). :rtype: collections.Iterable(afem.topology.entities.Shape) """ it = TopoDS_Iterator(self.object) while it.More(): yield Shape.wrap(it.Value()) it.Next()
def _process_solid(self, shape, color_tool): shapes = [] color = self._get_color(shape, color_tool) it = TopoDS_Iterator() it.Initialize(shape, False, False) while it.More(): #shapes.append(TopoShape(shape=shape, color=color)) shapes.extend(self._process_shell(it.Value(), color_tool, color)) it.Next() return shapes
def dumpTopology(shape, level=0): """ Print the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() if s == TopAbs_VERTEX: pnt = brt.Pnt(topods_Vertex(shape)) print(".." * level + "<Vertex %i: %s %s %s>" % (hash(shape), pnt.X(), pnt.Y(), pnt.Z())) else: print(".." * level, end="") print(shapeTypeString(shape)) it = TopoDS_Iterator(shape) while it.More(): shp = it.Value() it.Next() dumpTopology(shp, level + 1)
def _topods_iterator_check(shape, check): """ Iterate on the shape and print errors and store invalid shapes. """ invalid = [] it = TopoDS_Iterator(shape) while it.More(): sub_shape = it.Value() result = check.Result(sub_shape) list_of_status = result.Status() for status in list_of_status: if status != BRepCheck_NoError: msg = ' {0}-->{1}'.format(status, sub_shape.ShapeType()) print(msg) invalid.append(sub_shape) it.Next() invalid += _topods_iterator_check(sub_shape, check) return invalid
def _process_shape(self, shape, color_tool): shapes = [] shape_type = shape.ShapeType() print("Process {}".format(shape_type)) if shape_type in (TopAbs_COMPOUND, TopAbs_COMPSOLID): it = TopoDS_Iterator() it.Initialize(shape, False, False) while it.More(): shapes.extend(self._process_shape(it.Value(), color_tool)) it.Next() elif shape_type == TopAbs_SOLID: shapes.extend(self._process_solid(shape, color_tool)) elif shape_type == TopAbs_SHELL: shapes.extend(self._process_shell(shape, color_tool)) elif shape_type == TopAbs_FACE: shapes.append(self._process_face(shape, color_tool)) else: color = self._get_color(shape, color_tool) shapes.append(TopoShape(shape=shape, color=color)) return shapes
def intersection(self, shape): """ Returns the resulting intersection of this and the given shape or None. """ op = BOPAlgo_Section() op.AddArgument(self.shape) op.AddArgument(shape) op.Perform() if op.HasErrors(): return r = op.Shape() if r.IsNull(): return n = r.NbChildren() if n == 0: return it = TopoDS_Iterator(r) results = [] while it.More(): results.append(Topology.cast_shape(it.Value())) it.Next() return results