def __init__(self, wire, face=None): if face is None: explorer = BRepTools_WireExplorer(wire) else: explorer = BRepTools_WireExplorer(wire, face) edges = [] current_verts = [] while explorer.More(): ei = TopoDS.Edge_(explorer.Current()) vi = TopoDS.Vertex_(explorer.CurrentVertex()) edges.append(ei) current_verts.append(vi) explorer.Next() # CurrentVertex doesn't get the last vertex. Try to get it. ordered_verts = list(current_verts) if edges: data = ShapeExtend_WireData(wire) fix = ShapeFix_WireSegment(data) v1 = fix.LastVertex() ordered_verts.append(v1) self._edges = edges self._current_verts = current_verts self._ordered_verts = ordered_verts
def __init__(self, shape): if not shape.IsNull(): if not shape.ShapeType() == Shape.SHELL: raise TypeError('Shape is not a TopoDS_Shell.') if not isinstance(shape, TopoDS_Shell): shape = TopoDS.Shell_(shape) super(Shell, self).__init__(shape)
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 __init__(self, shape): if not shape.IsNull(): if not shape.ShapeType() == Shape.COMPOUND: raise TypeError('Shape is not a TopoDS_Compound.') if not isinstance(shape, TopoDS_Compound): shape = TopoDS.Compound_(shape) super(Compound, self).__init__(shape)
def __init__(self, shape): if not shape.IsNull(): if not shape.ShapeType() == Shape.EDGE: raise TypeError('Shape is not a TopoDS_Edge.') if not isinstance(shape, TopoDS_Edge): shape = TopoDS.Edge_(shape) super(Edge, self).__init__(shape)
def get_vertices(shape, unique=True): """ Get vertices from a shape. :param OCCT.TopoDS.TopoDS_Shape shape: The shape. :param bool unique: Option to return only unique vertices. :return: Vertices of shape. :rtype: list[OCCT.TopoDS.TopoDS_Vertex] """ if isinstance(shape, TopoDS_Vertex): return [shape] exp = TopExp_Explorer(shape, TopAbs_VERTEX) vertices = [] while exp.More(): vi = exp.Current() vertex = TopoDS.Vertex_(vi) if unique: is_unique = True for v in vertices: if v.IsSame(vertex): is_unique = False break if is_unique: vertices.append(vertex) else: vertices.append(vertex) exp.Next() return vertices
def to_shell(cls, entity): """ Convert an entity to a shell. :param entity: The entity. :return: A shell. :rtype: OCCT.TopoDS.TopoDS_Shell :raise TypeError: If entity cannot be converted to a shell. """ if isinstance(entity, TopoDS_Shell): return entity if cls.is_shape(entity) and entity.ShapeType() == TopAbs_SHELL: return TopoDS.Shell_(entity) if cls.is_shape(entity) and entity.ShapeType() == TopAbs_FACE: shell = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(shell) builder.Add(shell, entity) return shell raise TypeError('Failed to convert entity to a shell.')
def __init__(self, shape): if not shape.IsNull(): if not shape.ShapeType() == Shape.WIRE: raise TypeError('Shape is not a TopoDS_Wire.') if not isinstance(shape, TopoDS_Wire): shape = TopoDS.Wire_(shape) super(Wire, self).__init__(shape)
def get_edges(shape, unique=True): """ Get edges from a shape. :param OCCT.TopoDS.TopoDS_Shape shape: The shape. :param bool unique: Option to return only unique edges. :return: Edges of shape. :rtype: list[OCCT.TopoDS.TopoDS_Edge] """ if isinstance(shape, TopoDS_Edge): return [shape] exp = TopExp_Explorer(shape, TopAbs_EDGE) edges = [] while exp.More(): ei = exp.Current() edge = TopoDS.Edge_(ei) if unique: is_unique = True for e in edges: if e.IsSame(edge): is_unique = False break if is_unique: edges.append(edge) else: edges.append(edge) exp.Next() return edges
def __init__(self, shape): if not shape.IsNull(): if not shape.ShapeType() == Shape.SOLID: raise TypeError('Shape is not a TopoDS_Solid.') if not isinstance(shape, TopoDS_Solid): shape = TopoDS.Solid_(shape) super(Solid, self).__init__(shape)
def __init__(self, shape): if not shape.IsNull(): if not shape.ShapeType() == Shape.VERTEX: raise TypeError('Shape is not a TopoDS_Vertex.') if not isinstance(shape, TopoDS_Vertex): shape = TopoDS.Vertex_(shape) super(Vertex, self).__init__(shape)
def to_compound(cls, entity): """ Convert an entity to a compound. :param entity: The entity. :return: A compound :rtype: OCCT.TopoDS.TopoDS_Compound :raise TypeError: If entity cannot be converted to a compound. """ if isinstance(entity, TopoDS_Compound): return entity if cls.is_shape(entity) and entity.ShapeType() == TopAbs_COMPOUND: return TopoDS.Compound_(entity) if cls.is_shape(entity): cp = TopoDS_Compound builder = BRep_Builder() builder.MakeCompound(cp) builder.Add(cp, entity) return cp raise TypeError('Failed to convert entity to a compound.')
def curve_of_wire(wire): """ Get the curve of the wire. The edges are concatenated so the resulting curve may be C0 continuous. :param OCCT.TopoDS.TopoDS_Wire wire: The wire. :return: Concatenated curve of wire. :rtype: OCCT.Geom.Geom_BSplineCurve :raise RuntimeError: If an unsupported curve type is found. """ geom_convert = GeomConvert_CompCurveToBSplineCurve() exp = BRepTools_WireExplorer(wire) tol = ExploreShape.global_tolerance(wire, 1) while exp.More(): e = TopoDS.Edge_(exp.Current()) exp.Next() adp_crv = BRepAdaptor_Curve(e) geom_convert.Add(adp_crv.BSpline(), tol) crv = geom_convert.BSplineCurve() if not isinstance(crv, Geom_BSplineCurve): msg = 'Unsupported curve type.' raise RuntimeError(msg) return crv
def shape_to_face(self, shape): if isinstance(shape, OccShape): shape = shape.shape shape = Topology.cast_shape(shape) if isinstance(shape, (TopoDS_Face, TopoDS_Wire)): return shape if isinstance(shape, TopoDS_Edge): return BRepBuilderAPI_MakeWire(shape).Wire() return TopoDS.Wire_(shape)
def is_line(cls, shape): """ Check if an edge or wire is a line. This can be used to see if an edge can be used for length dimensions. Returns ------- bool: Bool Whether the shape is a part of a line """ edge = TopoDS.Edge_(shape) curve = BRepAdaptor_Curve(edge) return curve.GetType() == GeomAbs.GeomAbs_Line
def curve(self): """ :return: The curve formed by concatenating all the underlying curves of the edges. :rtype: afem.geometry.entities.NurbsCurve """ geom_convert = GeomConvert_CompCurveToBSplineCurve() exp = BRepTools_WireExplorer(self.object) tol = self.tol_max while exp.More(): e = TopoDS.Edge_(exp.Current()) exp.Next() adp_crv = BRepAdaptor_Curve(e) geom_convert.Add(adp_crv.BSpline(), tol) geom_curve = geom_convert.BSplineCurve() return Curve.wrap(geom_curve)
def to_compsolid(cls, entity): """ Convert an entity to a compsolid. :param entity: The entity. :return: A compsolid. :rtype: OCCT.TopoDS.TopoDS_CompSolid :raise TypeError: If entity cannot be converted to a compsolid. """ if isinstance(entity, TopoDS_CompSolid): return entity if cls.is_shape(entity) and entity.ShapeType() == TopAbs_COMPSOLID: return TopoDS.CompSolid_(entity) raise TypeError('Failed to convert entity to a compsolid.')
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 cast_curve(cls, shape, expected_type=None): """ Attempt to cast the shape (an edge or wire) to a curve Parameters ---------- shape: TopoDS_Edge The shape to cast expected_type: GeomAbs_CurveType The type to restrict Returns ------- curve: Curve or None The curve or None if it could not be created or if it was not of the expected type (if given). """ edge = TopoDS.Edge_(shape) curve = BRepAdaptor_Curve(edge) return cls.curve_factory[curve.GetType()](curve)
def get_shells(shape): """ Get shells from a shape. :param OCCT.TopoDS.TopoDS_Shape shape: The shape. :return: Shells of shape. :rtype: list[OCCT.TopoDS.TopoDS_Shell] """ if isinstance(shape, TopoDS_Shell): return [shape] exp = TopExp_Explorer(shape, TopAbs_SHELL) shells = [] while exp.More(): si = exp.Current() shell = TopoDS.Shell_(si) shells.append(shell) exp.Next() return shells
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 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 get_compounds(shape): """ Get compounds from a shape. :param OCCT.TopoDS.TopoDS_Shape shape: The shape. :return: Compounds of shape. :rtype: list[OCCT.TopoDS.TopoDS_Compound] """ if isinstance(shape, TopoDS_Compound): return [shape] exp = TopExp_Explorer(shape, TopAbs_COMPOUND) compounds = [] while exp.More(): ci = exp.Current() compound = TopoDS.Compound_(ci) compounds.append(compound) exp.Next() return compounds
def get_wires(shape): """ Get wires from a shape. :param OCCT.TopoDS.TopoDS_Shape shape: The shape. :return: Wires of shape. :rtype: list[OCCT.TopoDS.TopoDS_Wire] """ if isinstance(shape, TopoDS_Wire): return [shape] exp = TopExp_Explorer(shape, TopAbs_WIRE) wires = [] while exp.More(): wi = exp.Current() wire = TopoDS.Wire_(wi) wires.append(wire) exp.Next() return wires
def get_solids(shape): """ Get solids from a shape. :param OCCT.TopoDS.TopoDS_Shape shape: The shape. :return: Solids of shape. :rtype: list[OCCT.TopoDS.TopoDS_Solid] """ if isinstance(shape, TopoDS_Solid): return [shape] exp = TopExp_Explorer(shape, TopAbs_SOLID) solids = [] while exp.More(): si = exp.Current() solid = TopoDS.Solid_(si) solids.append(solid) exp.Next() return solids
def __init__(self, shapes, intersect=False, fuzzy_val=None, nondestructive=False): super(VolumeMaker, self).__init__(None, None, fuzzy_val, nondestructive, BOPAlgo_MakerVolume) self.set_args(shapes) if intersect: self._bop.SetIntersect(True) else: self._bop.SetIntersect(False) self.build() self._solids = [] for solid in ExploreShape.get_solids(self.shape): self._solids.append(TopoDS.Solid_(solid))
def to_wire(cls, entity): """ Convert an entity to a wire. :param entity: The entity. :return: A wire. :rtype: OCCT.TopoDS.TopoDS_Wire :raise TypeError: If entity cannot be converted to a wire. """ if isinstance(entity, TopoDS_Wire): return entity if cls.is_shape(entity) and entity.ShapeType() == TopAbs_EDGE: return BRepBuilderAPI_MakeWire(entity).Wire() if cls.is_shape(entity) and entity.ShapeType() == TopAbs_WIRE: return TopoDS.Wire_(entity) raise TypeError('Failed to convert entity to a wire.')
def to_vertex(cls, entity): """ Convert an entity to a vertex. :param entity: The entity. :return: A vertex. :rtype: OCCT.TopoDS.TopoDS_Vertex :raise TypeError: If entity cannot be converted to a vertex. """ if isinstance(entity, TopoDS_Vertex): return entity if isinstance(entity, gp_Pnt): return BRepBuilderAPI_MakeVertex(entity).Vertex() if cls.is_shape(entity) and entity.ShapeType() == TopAbs_VERTEX: return TopoDS.Vertex_(entity) raise TypeError('Failed to convert entity to a vertex.')
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
def create_shape(self): d = self.declaration t = self.get_transform() w, h = d.width, d.height if d.rx or d.ry: rx, ry = d.rx, d.ry if not ry: ry = rx elif not rx: rx = ry # Clamp to the valid range rx = min(w / 2, rx) ry = min(h / 2, ry) # Bottom p1 = gp_Pnt(0 + rx, 0, 0) p2 = gp_Pnt(0 + w - rx, 0, 0) # Right p3 = gp_Pnt(w, ry, 0) p4 = gp_Pnt(w, h - ry, 0) # Top p5 = gp_Pnt(w - rx, h, 0) p6 = gp_Pnt(rx, h, 0) # Left p7 = gp_Pnt(0, h - ry, 0) p8 = gp_Pnt(0, ry, 0) shape = BRepBuilderAPI_MakeWire() e = d.tolerance # Bottom if not p1.IsEqual(p2, e): shape.Add(BRepBuilderAPI_MakeEdge(p1, p2).Edge()) # Arc bottom right c = make_ellipse((w - rx, ry, 0), rx, ry) shape.Add( BRepBuilderAPI_MakeEdge( GC_MakeArcOfEllipse(c, p2, p3, False).Value()).Edge()) # Right if not p3.IsEqual(p4, e): shape.Add(BRepBuilderAPI_MakeEdge(p3, p4).Edge()) # Arc top right c.SetLocation(gp_Pnt(w - rx, h - ry, 0)) shape.Add( BRepBuilderAPI_MakeEdge( GC_MakeArcOfEllipse(c, p4, p5, False).Value()).Edge()) # Top if not p5.IsEqual(p6, e): shape.Add(BRepBuilderAPI_MakeEdge(p5, p6).Edge()) # Arc top left c.SetLocation(gp_Pnt(rx, h - ry, 0)) shape.Add( BRepBuilderAPI_MakeEdge( GC_MakeArcOfEllipse(c, p6, p7, False).Value()).Edge()) # Left if not p7.IsEqual(p8, e): shape.Add(BRepBuilderAPI_MakeEdge(p7, p8).Edge()) # Arc bottom left c.SetLocation(gp_Pnt(rx, ry, 0)) shape.Add( BRepBuilderAPI_MakeEdge( GC_MakeArcOfEllipse(c, p8, p1, False).Value()).Edge()) shape = shape.Wire() shape.Closed(True) else: shape = BRepBuilderAPI_MakePolygon(gp_Pnt(0, 0, 0), gp_Pnt(w, 0, 0), gp_Pnt(w, h, 0), gp_Pnt(0, h, 0), True).Wire() wire = TopoDS.Wire_(BRepBuilderAPI_Transform(shape, t, False).Shape()) self.curve = BRepAdaptor_CompCurve(wire) self.shape = wire