Esempio n. 1
0
    def __init__(self, solid, pnt=None, tol=1.0e-7):
        pnt = CheckGeom.to_point(pnt)

        if not CheckGeom.is_point(pnt):
            self._tool = BRepClass3d_SolidClassifier(solid.object)
        else:
            self._tool = BRepClass3d_SolidClassifier(solid.object, pnt, tol)
Esempio n. 2
0
    def to_shape(entity):
        """
        Convent an entity to a shape. If already a shape the entity is
        returned. If the entity is geometry it is converted to its
        corresponding shape.

        :param entity: The entity.
        :type entity: afem.topology.entities.Shape or
            afem.geometry.entities.Curve or afem.geometry.entities.Surface or
            point_like

        :return: The shape.
        :rtype: afem.topology.entities.Shape

        :raise TypeError: If entity cannot be converted to a shape.
        """
        if entity is None:
            return None

        if isinstance(entity, Shape):
            return entity

        if CheckGeom.is_point_like(entity):
            return Vertex.by_point(entity)
        elif CheckGeom.is_curve(entity):
            return Edge.by_curve(entity)
        elif CheckGeom.is_surface(entity):
            return Face.by_surface(entity)
        else:
            n = entity.__class__.__name__
            raise TypeError('Cannot convert a {} to a shape.'.format(n))
Esempio n. 3
0
    def __init__(self, srf1, srf2, tol=1.0e-10):
        adp_srf1 = AdaptorSurface.to_adaptor(srf1)
        adp_srf2 = AdaptorSurface.to_adaptor(srf2)
        tool = Extrema_ExtSS(adp_srf1.object, adp_srf2.object, tol, tol)

        if not tool.IsDone():
            msg = 'Extrema between curve and surface failed.'
            raise RuntimeError(msg)

        self._nsol = tool.NbExt()
        self._parallel = tool.IsParallel()
        results = []
        for i in range(1, self._nsol + 1):
            di = sqrt(tool.SquareDistance(i))
            gp_pnt1, gp_pnt2 = tool.Points(i)
            p1 = CheckGeom.to_point(gp_pnt1)
            p2 = CheckGeom.to_point(gp_pnt2)
            results.append((di, p1, p2))

        results.sort(key=lambda tup: tup[0])

        self._dmin = results[0][0]
        self._dmax = results[-1][0]
        self._dist = [row[0] for row in results]
        self._pnts1 = [row[1] for row in results]
        self._pnts2 = [row[2] for row in results]
Esempio n. 4
0
    def __init__(self, shape1, shape2, pnt=None, tol=-1.):
        shape = IntersectShapes(shape1, shape2).shape

        pnt = CheckGeom.to_point(pnt)

        self._found = False
        self._pln = None
        if pnt is None:
            tool = PlaneByEdges(shape, tol)
            self._found = tool.found
            self._pln = tool.plane
        elif CheckGeom.is_point(pnt):
            edges = shape.edges
            pnts = [pnt]
            for edge in edges:
                BRepMesh_IncrementalMesh(edge.object, 0.001)
                loc = TopLoc_Location()
                poly3d = BRep_Tool.Polygon3D_(edge.object, loc)
                if poly3d.NbNodes == 0:
                    continue
                tcol_pnts = poly3d.Nodes()
                for i in range(1, tcol_pnts.Length() + 1):
                    gp_pnt = tcol_pnts.Value(i)
                    pnt = CheckGeom.to_point(gp_pnt)
                    pnts.append(pnt)
            if len(pnts) < 3:
                raise ValueError('Less than three points to fit a plane.')
            if tol < 0.:
                tol = 1.0e-7
            tool = PlaneByApprox(pnts, tol)
            self._found = True
            self._pln = tool.plane
        else:
            raise TypeError('Invalid input.')
Esempio n. 5
0
    def __init__(self, p1, p2, p3):
        p1 = CheckGeom.to_point(p1)
        p2 = CheckGeom.to_point(p2)
        p3 = CheckGeom.to_point(p3)

        circle = CircleBy3Points(p1, p2, p3).circle

        super(SphereBy3Points, self).__init__(circle.center, circle.radius)
Esempio n. 6
0
 def __init__(self, p1, p2):
     # Build
     p1 = CheckGeom.to_point(p1)
     p2 = CheckGeom.to_point(p2)
     builder = BRepBuilderAPI_MakeEdge(p1, p2)
     self._e = Edge(builder.Edge())
     self._v1 = Vertex(builder.Vertex1())
     self._v2 = Vertex(builder.Vertex2())
Esempio n. 7
0
    def __init__(self, crv, pln, direction=None, keep_param=True):
        super(ProjectCurveToPlane, self).__init__()

        direction = CheckGeom.to_direction(direction)
        if not CheckGeom.is_direction(direction):
            direction = pln.object.Pln().Axis().Direction()

        # OCC projection
        hcrv = GeomProjLib.ProjectOnPlane_(crv.object, pln.object, direction,
                                           keep_param)

        self._crv = Curve(hcrv)
Esempio n. 8
0
    def set_sref(self, sref):
        """
        Set the reference surface. This method also automatically creates a
        "reference surface shape" by converting the given surface into a face,
        dividing it if it is closed, and then dividing it at C0 boundaries.
        This shape is made available to improve robustness for certain
        topological operations and can be accessed via the `sref_shape`
        property.

        :param afem.geometry.entities.Surface sref: The surface.

        :return: None.

        :raise TypeError: If *sref* is not a surface.
        """
        if not CheckGeom.is_surface(sref):
            msg = 'Invalid surface type.'
            raise TypeError(msg)

        # Set the surface
        self._sref = sref

        # Convert to a shape for robustness
        shape = FaceBySurface(sref).face
        shape = DivideClosedShape(shape).shape
        shape = DivideC0Shape(shape).shape
        self._sref_shape = shape
Esempio n. 9
0
    def __init__(self, pnt, other_shapes):
        pnt = CheckGeom.to_point(pnt)
        if not pnt:
            raise TypeError('Invalid point type provided.')

        v = Vertex.by_point(pnt)
        super(DistancePointToShapes, self).__init__(v, other_shapes)
Esempio n. 10
0
    def __init__(self, pnt, crv, tol=1.0e-10):
        adp_crv = AdaptorCurve.to_adaptor(crv)
        tool = Extrema_ExtPC(pnt, adp_crv.object, tol)

        if not tool.IsDone():
            msg = 'Extrema between point and curve failed.'
            raise RuntimeError(msg)

        self._nsol = tool.NbExt()
        results = []
        for i in range(1, self._nsol + 1):
            di = sqrt(tool.SquareDistance(i))
            ext_pnt = tool.Point(i)
            gp_pnt = ext_pnt.Value()
            ui = ext_pnt.Parameter()
            pi = CheckGeom.to_point(gp_pnt)
            results.append((di, ui, pi))

        results.sort(key=lambda tup: tup[0])

        self._dmin = results[0][0]
        self._dmax = results[-1][0]
        self._dist = [row[0] for row in results]
        self._prms = [row[1] for row in results]
        self._pnts = [row[2] for row in results]
Esempio n. 11
0
    def translate(self,
                  vector,
                  elements=(),
                  copy=False,
                  make_groups=False,
                  target_mesh=None):
        """
        Translate the elements.

        :param vector_like vector: The translation vector.
        :param collection.Sequence(afem.smesh.entities.Element) elements: The
            elements to transform. If none are provided then the whole mesh is
            used.
        :param bool copy: Option to copy elements.
        :param bool make_groups: Option to make groups.
        :param afem.smesh.entities.Mesh target_mesh: The target mesh to place
            elements.

        :return: List of element ID's.
        :rtype: list(int)
        """
        vector = CheckGeom.to_vector(vector)

        trsf = gp_Trsf()
        trsf.SetTranslation(vector)

        return self.transform(trsf, elements, copy, make_groups, target_mesh)
Esempio n. 12
0
    def __init__(self, shape, pnt):
        pnt = CheckGeom.to_point(pnt)

        if shape.shape_type not in [Shape.FACE, Shape.SHELL]:
            raise RuntimeError('Invalid shape for creating half-space.')
        self._solid = Solid(
            BRepPrimAPI_MakeHalfSpace(shape.object, pnt).Solid())
Esempio n. 13
0
 def __init__(self, pnts, close=False):
     builder = BRepBuilderAPI_MakePolygon()
     for p in pnts:
         p = CheckGeom.to_point(p)
         builder.Add(p)
     if close:
         builder.Close()
     self._w = Wire(builder.Wire())
Esempio n. 14
0
    def plane(self):
        """
        :return: The reference surface if it is a plane.
        :rtype: afem.geometry.entities.Plane

        :raise TypeError: If the reference surface is not a plane.
        """
        if CheckGeom.is_plane(self.sref):
            return self.sref
        raise TypeError('Reference surface is not a plane.')
Esempio n. 15
0
    def by_point(pnt):
        """
        Create a vertex by a point.

        :param point_like pnt: The point.

        :return: The vertex.
        :rtype: afem.topology.entities.Vertex
        """
        pnt = CheckGeom.to_point(pnt)
        return Vertex(BRepBuilderAPI_MakeVertex(pnt).Vertex())
Esempio n. 16
0
    def perform(self, pnt, tol=1.0e-7):
        """
        Perform the classification with the point and tolerance.

        :param point_like pnt: The point.
        :param float tol: The tolerance.

        :return: None.
        """
        pnt = CheckGeom.to_point(pnt)
        self._tool.Perform(pnt, tol)
Esempio n. 17
0
    def set_enforced_nodes(self, shapes, pnts):
        """
        Set enforced nodes on shapes.

        :param list(afem.topology.entities.Shape) shapes: List of shapes.
        :param list(point_like) pnts: List of points for enforced nodes.

        :return: None.
        """
        pnts = [CheckGeom.to_point(p) for p in pnts]
        shapes_ = [s.object for s in shapes]
        self._hyp.SetEnforcedNodes(shapes_, pnts)
Esempio n. 18
0
    def __init__(self, pnt, crv, direction=None, update=False):
        super(ProjectPointToCurve, self).__init__()

        pnt = CheckGeom.to_point(pnt)
        direction = CheckGeom.to_direction(direction)
        adp_crv = AdaptorCurve.to_adaptor(crv)
        self._results = []

        if not direction:
            ext = Extrema_ExtPC(pnt, adp_crv.object)
            npts = ext.NbExt()
            for i in range(1, npts + 1):
                poc = ext.Point(i)
                ui = poc.Parameter()
                pi = adp_crv.eval(ui)
                di = sqrt(ext.SquareDistance(i))
                self._results.append([pi, ui, di])
        else:
            # Use minimum distance between line and curve to project point
            # along a direction.
            line = Line.by_direction(pnt, direction)
            adp_crv2 = AdaptorCurve.to_adaptor(line)
            ext = Extrema_ExtCC(adp_crv.object, adp_crv2.object)
            npts = ext.NbExt()
            for i in range(1, npts + 1):
                poc1, poc2 = Extrema_POnCurv(), Extrema_POnCurv()
                ext.Points(i, poc1, poc2)
                ui = poc1.Parameter()
                pi = adp_crv.eval(ui)
                di = sqrt(ext.SquareDistance(i))
                self._results.append([pi, ui, di])

        # Sort by distance and return.
        if self._results:
            self._results.sort(key=lambda lst: lst[2])

        if update:
            pnt.set_xyz(self.nearest_point)
Esempio n. 19
0
    def approx_points(self, upr, lwr, close=True):
        """
        Approximate a 2-D airfoil curve using a collection of upper and lower
        points. All other curves will be cleared from the cross section before
        this method is called.

        :param collections.Sequence(point2d_like) upr: The upper points to
            approximate. The sequence should start at the leading edge and
            move towards the trailing edge.
        :param collections.Sequence(point2d_like) lwr: The lower points to
            approximate. The sequence should start at the leading edge and
            move towards the trailing edge.
        :param bool close: Option to close the airfoil by adding a segment.

        :return: The 2-D curve.
        :rtype: afem.geometry.entities.NurbsCurve2D

        .. note::

            Before approximating the 2-D curve the upper and lower points
            are combined to form a single curve. It is important that the
            points are organized as described above so they are sorted
            correctly for the approximation.
        """
        upr = [CheckGeom.to_point2d(p) for p in upr]
        lwr = [CheckGeom.to_point2d(p) for p in lwr]

        # Store 2-D LE and TE points
        self._le2d = upr[0]
        self._upr_te2d = upr[-1]
        self._lwr_te2d = lwr[-1]

        # Combine points for approximation
        upr.reverse()
        pnts = upr + lwr[1:]

        self.clear()
        return self.add_approx(pnts, close)
Esempio n. 20
0
    def __init__(self, pnt, srf, direction=None, update=False, tol=1.0e-7):
        super(ProjectPointToSurface, self).__init__()

        pnt = CheckGeom.to_point(pnt)
        direction = CheckGeom.to_direction(direction)
        adp_srf = AdaptorSurface.to_adaptor(srf)
        self._results = []

        if not direction:
            ext = Extrema_ExtPS(pnt, adp_srf.object, tol, tol)
            npts = ext.NbExt()
            for i in range(1, npts + 1):
                pos = ext.Point(i)
                ui, vi = pos.Parameter(0., 0.)
                pi = adp_srf.eval(ui, vi)
                di = sqrt(ext.SquareDistance(i))
                self._results.append([pi, (ui, vi), di])
        else:
            # Use minimum distance between line and surface to project point
            # along a direction.
            line = Line.by_direction(pnt, direction)
            adp_crv = AdaptorCurve.to_adaptor(line)
            ext = Extrema_ExtCS(adp_crv.object, adp_srf.object, tol, tol)
            npts = ext.NbExt()
            for i in range(1, npts + 1):
                poc, pos = Extrema_POnCurv(), Extrema_POnSurf()
                ext.Points(i, poc, pos)
                ui, vi = pos.Parameter(0., 0.)
                pi = adp_srf.eval(ui, vi)
                di = sqrt(ext.SquareDistance(i))
                self._results.append([pi, (ui, vi), di])

        # Sort by distance and return.
        if self._results:
            self._results.sort(key=lambda lst: lst[2])

        if update:
            pnt.set_xyz(self.nearest_point)
Esempio n. 21
0
    def set_sref(self, sref):
        """
        Set the part reference surface.

        :param afem.geometry.entities.Surface sref: The surface.

        :return: None.

        :raise TypeError: If *sref* is an invalid surface.
        """
        if not CheckGeom.is_surface(sref):
            msg = 'Invalid surface type.'
            raise TypeError(msg)
        self._sref = sref
Esempio n. 22
0
    def cut_hole(self, d, ds, u0=None, is_rel=False):
        """
        Cut a hole in the part and update its shape.

        :param float d: Diameter of hole.
        :param float ds: The distance.
        :param float u0: The parameter. If not provided the first parameter
            of the reference curve will be used.
        :param bool is_rel: Option specifying if the distance is absolute or
            a relative to the length of the reference curve. If relative, then
            *ds* is multiplied by the curve length to get the absolute value.

        :return: The status of the Boolean operation that will cut the hole.
            *True* if the operation was performed, *False* if not.
        :rtype: bool

        :raise AttributeError: If the part does not have a reference curve or
            surface.
        """
        if not self.has_cref:
            msg = 'Part does not have a reference curve.'
            raise AttributeError(msg)

        if not self.has_sref:
            msg = 'Part does not have a reference surface.'
            raise AttributeError(msg)

        # Intersect the shape with a plane to use for the hole height location
        pln = self.plane_from_parameter(ds, u0, is_rel)
        bop = IntersectShapes(self._shape, pln)
        wires = WiresByShape(bop.shape).wires
        los = LengthOfShapes(wires)
        max_length = los.max_length
        wire = los.longest_shape
        if not isinstance(wire, Wire):
            return False
        mid_length = max_length / 2.
        p = PointAlongShape(wire, mid_length).point
        u, v = self.invert_sref(p)
        v = self.sref.norm(u, v)
        v = CheckGeom.to_direction(v)
        ax1 = Axis1(p, v)

        # Cut the hole
        r = d / 2.
        bop = CutCylindricalHole(self._shape, r, ax1)
        self.set_shape(bop.shape)

        return bop.is_done
Esempio n. 23
0
    def add_pnt(self, pnt):
        """
        Add a point to the bounding box.

        :param point_like pnt: The point.

        :return: None.

        :raise TypeError: If *pnt* cannot be converted to a point.
        """
        pnt = CheckGeom.to_point(pnt)
        if not pnt:
            msg = 'Invalid point type provided.'
            raise TypeError(msg)

        self.Add(pnt)
Esempio n. 24
0
    def is_pnt_out(self, pnt):
        """
        Check to see if the point is outside the bounding box.

        :param point_like pnt: The point.

        :return: *True* if outside, *False* if not.
        :rtype: bool

        :raise TypeError: If *pnt* cannot be converted to a point.
        """
        pnt = CheckGeom.to_point(pnt)
        if not pnt:
            msg = 'Invalid point type provided.'
            raise TypeError(msg)

        return self.IsOut(pnt)
Esempio n. 25
0
    def query_point(self, p0, distance_upper_bound=inf):
        """
        Find the intersection result nearest to the provided point.

        :param point_like p0: Point to search from.
        :param float distance_upper_bound: Return only results within this
            distance.

        :return: Distance to nearest intersection result and its index (d, i).
            Returns (None, None) if no results are available.
        :rtype: tuple
        """
        if not self.success:
            return None, None
        p0 = CheckGeom.to_point(p0)
        d, i = self._kdt.query(p0.xyz, 1, 0., 2, distance_upper_bound)
        return d, i
Esempio n. 26
0
    def by_points(pnts, close=False):
        """
        Create polygonal wire by connecting points.

        :param collections.Sequence(point_like) pnts: The ordered points.
        :param bool close: Option to close the wire.

        :return: The new wire.
        :rtype: afem.topology.entities.Wire
        """
        builder = BRepBuilderAPI_MakePolygon()
        for p in pnts:
            p = CheckGeom.to_point(p)
            builder.Add(p)
        if close:
            builder.Close()
        return Wire(builder.Wire())
Esempio n. 27
0
    def is_line_out(self, line):
        """
        Check to see if the line intersects the box.

        :param afem.geometry.entities.Line line: The line.

        :return: *True* if outside, *False* if it intersects.
        :rtype: bool

        :raise TypeError: If *line* is not a line.
        """

        if not CheckGeom.is_line(line):
            msg = 'Methods requires a Line instance.'
            raise TypeError(msg)

        return self.IsOut(line.object)
Esempio n. 28
0
    def is_pln_out(self, pln):
        """
        Check to see if the plane intersects the box.

        :param afem.geometry.entities.Plane pln: The plane.

        :return: *True* if outside, *False* if it intersects.
        :rtype: bool

        :raise TypeError: If *pln* is not a plane.
        """

        if not CheckGeom.is_plane(pln):
            msg = 'Methods requires a Plane instance.'
            raise TypeError(msg)

        return self.IsOut(pln.object)
Esempio n. 29
0
    def set_cref(self, cref):
        """
        Set the reference curve.

        :param afem.geometry.entities.Curve cref: The curve. If it is not a
            :class:`.TrimmedCurve`, then it will be converted to one. Access
            the original curve using the *basis_curve* property (i.e.,
            part.cref.basis_curve).
        :param cref: The reference curve.

        :return: None.

        :raise TypeError: If *cref* is not a curve.
        """
        if not CheckGeom.is_curve(cref):
            raise TypeError('Invalid curve type.')

        if isinstance(cref, TrimmedCurve):
            self._cref = cref
        else:
            self._cref = TrimmedCurve.by_parameters(cref)
Esempio n. 30
0
    def trim_u2(self, entity):
        """
        Trim the last parameter of the reference curve by interesting it with
        the entity.

        :param entity: The entity.
        :type entity: afem.geometry.entities.Geometry or
            afem.topology.entities.Shape

        :return: None.

        :raise RuntimeError: If an intersection with the reference curve cannot
            be found.
        """
        shape1 = Shape.to_shape(self.cref)
        shape2 = Shape.to_shape(entity)
        bop = IntersectShapes(shape1, shape2)
        if not bop.is_done:
            raise RuntimeError('Failed to intersect reference curve.')

        pnts = [v.point for v in bop.vertices]
        p = CheckGeom.nearest_point(self.cref.p2, pnts)
        self.set_p2(p)