Beispiel #1
0
 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)
Beispiel #2
0
    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
Beispiel #3
0
    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
Beispiel #4
0
    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
Beispiel #5
0
    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
Beispiel #6
0
 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)
Beispiel #7
0
    def to_edge(cls, entity):
        """
        Convert an entity to an edge.

        :param entity: The entity.

        :return: An edge.
        :rtype: OCCT.TopoDS.TopoDS_Edge

        :raise TypeError: If entity cannot be converted to an edge.
        """
        if isinstance(entity, TopoDS_Edge):
            return entity

        if cls.is_shape(entity) and entity.ShapeType() == TopAbs_EDGE:
            return TopoDS.Edge_(entity)

        raise TypeError('Failed to convert entity to an edge.')
Beispiel #8
0
    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)
Beispiel #9
0
    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.')