def __init__(self, shape): if not shape.IsNull(): if not shape.ShapeType() == Shape.FACE: raise TypeError('Shape is not a TopoDS_Face.') if not isinstance(shape, TopoDS_Face): shape = TopoDS.Face_(shape) super(Face, self).__init__(shape)
def to_face(cls, entity): """ Convert an entity to a face. :param entity: The entity. :return: A face. :rtype: OCCT.TopoDS.TopoDS_Face :raise TypeError: If entity cannot be converted to a face. """ if isinstance(entity, TopoDS_Face): return entity if cls.is_shape(entity) and entity.ShapeType() == TopAbs_FACE: return TopoDS.Face_(entity) raise TypeError('Failed to convert entity to a face.')
def get_faces(shape): """ Get faces from a shape. :param OCCT.TopoDS.TopoDS_Shape shape: The shape. :return: Faces of shape. :rtype: list[OCCT.TopoDS.TopoDS_Face] """ if isinstance(shape, TopoDS_Face): return [shape] exp = TopExp_Explorer(shape, TopAbs_FACE) faces = [] while exp.More(): fi = exp.Current() face = TopoDS.Face_(fi) faces.append(face) exp.Next() return faces
def to_shape(cls, entity): """ Convert the entity to a shape. This method tries to convert the entity to its most specific shape type. :param entity: The entity. :return: A shape. :rtype: OCCT.TopoDS.TopoDS_Shape :raise TypeError: If entity cannot be converted to a shape. """ if entity is None: return None # Shapes if isinstance(entity, TopoDS_Shape): if entity.IsNull(): raise TypeError('Cannot convert null shape.') elif entity.ShapeType() == TopAbs_VERTEX: return TopoDS.Vertex_(entity) elif entity.ShapeType() == TopAbs_EDGE: return TopoDS.Edge_(entity) elif entity.ShapeType() == TopAbs_WIRE: return TopoDS.Wire_(entity) elif entity.ShapeType() == TopAbs_FACE: return TopoDS.Face_(entity) elif entity.ShapeType() == TopAbs_SHELL: return TopoDS.Shell_(entity) elif entity.ShapeType() == TopAbs_SOLID: return TopoDS.Solid_(entity) elif entity.ShapeType() == TopAbs_COMPSOLID: return TopoDS.CompSolid_(entity) elif entity.ShapeType() == TopAbs_COMPOUND: return TopoDS.Compound_(entity) else: raise TypeError('Failed to convert entity to a shape.') raise TypeError('Failed to convert entity to a shape.')
def cast_surface(cls, shape, expected_type=None): """ Attempt to cast the shape (a face) to a surface Parameters ---------- shape: TopoDS_Face The shape to cast expected_type: GeomAbs_SurfaceType The type to restrict Returns ------- surface: BRepAdaptor_Surface or None The surface or None if it could not be created or did not match the expected type (if given). """ if isinstance(shape, TopoDS_Face): face = shape else: face = TopoDS.Face_(shape) surface = BRepAdaptor_Surface(face, True) if expected_type is not None and surface.GetType() != expected_type: return None return surface