Ejemplo n.º 1
0
def points_to_bspline(pnts):
    '''
    Points to bspline
    '''
    pnts = point_list_to_TColgp_Array1OfPnt(pnts)
    crv = GeomAPI_PointsToBSpline(pnts)
    return crv.Curve()
def pipe():
    # the bspline path, must be a wire
    array2 = TColgp_Array1OfPnt(1, 3)
    array2.SetValue(1, gp_Pnt(0, 0, 0))
    array2.SetValue(2, gp_Pnt(0, 1, 2))
    array2.SetValue(3, gp_Pnt(0, 2, 3))
    bspline2 = GeomAPI_PointsToBSpline(array2).Curve()
    path_edge = BRepBuilderAPI_MakeEdge(bspline2).Edge()
    path_wire = BRepBuilderAPI_MakeWire(path_edge).Wire()

    # the bspline profile. Profile mist be a wire
    array = TColgp_Array1OfPnt(1, 5)
    array.SetValue(1, gp_Pnt(0, 0, 0))
    array.SetValue(2, gp_Pnt(1, 2, 0))
    array.SetValue(3, gp_Pnt(2, 3, 0))
    array.SetValue(4, gp_Pnt(4, 3, 0))
    array.SetValue(5, gp_Pnt(5, 5, 0))
    bspline = GeomAPI_PointsToBSpline(array).Curve()
    profile_edge = BRepBuilderAPI_MakeEdge(bspline).Edge()

    # pipe
    pipe = BRepOffsetAPI_MakePipe(path_wire, profile_edge).Shape()

    display.DisplayShape(profile_edge, color='WHITE', update=False)
    display.DisplayShape(path_wire, color='BLUE', update=False)
    display.DisplayShape(pipe, update=True)
Ejemplo n.º 3
0
def get_simple_bound(rndPts0):    
    _spl1 = GeomAPI_PointsToBSpline(rndPts0) #not a GeomAPI_PointsToBSplineSurface ??
    _spl1.thisown = False
    spl1  = _spl1.Curve()
    
    spl1_adap = GeomAdaptor_HCurve(spl1)
    spl1_adap.thisown = False
    spl1_adap_h = spl1_adap.GetHandle()
    bound1 = GeomFill_SimpleBound(spl1_adap_h, 0.001, 0.001)
    bound1.thisown = False
    bound1_h = bound1.GetHandle()
    return spl1, bound1_h
Ejemplo n.º 4
0
def get_simple_bound(rndPts0):
    _spl1 = GeomAPI_PointsToBSpline(
        rndPts0)  #not a GeomAPI_PointsToBSplineSurface ??
    _spl1.thisown = False
    spl1 = _spl1.Curve()

    spl1_adap = GeomAdaptor_HCurve(spl1)
    spl1_adap.thisown = False
    spl1_adap_h = spl1_adap.GetHandle()
    bound1 = GeomFill_SimpleBound(spl1_adap_h, 0.001, 0.001)
    bound1.thisown = False
    bound1_h = bound1.GetHandle()
    return spl1, bound1_h
Ejemplo n.º 5
0
 def DisplayG01Line(coor1, coor2):
     array = TColgp_Array1OfPnt(1, 2)
     array.SetValue(1, gp_Pnt(coor1[0], coor1[1], coor1[2]))
     array.SetValue(2, gp_Pnt(coor2[0], coor2[1], coor2[2]))
     bspline2 = GeomAPI_PointsToBSpline(array).Curve()
     path_edge = BRepBuilderAPI_MakeEdge(bspline2).Edge()
     display.DisplayColoredShape(path_edge, 'GREEN')
def prism():
    # the bspline profile
    array = TColgp_Array1OfPnt(1, 5)
    array.SetValue(1, gp_Pnt(0, 0, 0))
    array.SetValue(2, gp_Pnt(1, 2, 0))
    array.SetValue(3, gp_Pnt(2, 3, 0))
    array.SetValue(4, gp_Pnt(4, 3, 0))
    array.SetValue(5, gp_Pnt(5, 5, 0))
    bspline = GeomAPI_PointsToBSpline(array).Curve()
    profile = BRepBuilderAPI_MakeEdge(bspline).Edge()

    # the linear path
    starting_point = gp_Pnt(0., 0., 0.)
    end_point = gp_Pnt(0., 0., 6.)
    vec = gp_Vec(starting_point, end_point)
    path = BRepBuilderAPI_MakeEdge(starting_point, end_point).Edge()

    # extrusion
    prism = BRepPrimAPI_MakePrism(profile, vec).Shape()

    display.DisplayShape(profile, update=False)
    display.DisplayShape(starting_point, update=False)
    display.DisplayShape(end_point, update=False)
    display.DisplayShape(path, update=False)
    display.DisplayShape(prism, update=True)
Ejemplo n.º 7
0
def make_bspline_edge(pyptlist, mindegree=3, maxdegree=8):
    array = TColgp_Array1OfPnt(1, len(pyptlist))
    pcnt = 1
    for pypt in pyptlist:
        gppt = make_gppnt(pypt)
        array.SetValue(pcnt, gppt)
        pcnt += 1
    bcurve = GeomAPI_PointsToBSpline(array, mindegree, maxdegree).Curve()
    curve_edge = BRepBuilderAPI_MakeEdge(bcurve)
    return curve_edge.Edge()
Ejemplo n.º 8
0
    def DisplayG02Arc(coor1, coor2, ARCCMD):
        f = G02EXE(coor1, coor2, ARCCMD)
        print(f)
        pty = f[0]
        center = f[1]
        radius = f[2]
        startA = f[3]
        endA = f[4]
        if endA - startA >= 0:
            endA = -2 * math.pi + endA

        dis = endA - startA
        dis = dis / 30

        array = TColgp_Array1OfPnt(1, 30)
        if pty == 'xy':
            for i in range(29):
                array.SetValue(
                    i + 1,
                    gp_Pnt(radius * math.cos(startA + dis * i) + center[0],
                           radius * math.sin(startA + dis * i) + center[1],
                           coor1[2]))
            array.SetValue(
                30,
                gp_Pnt(radius * math.cos(endA) + center[0],
                       radius * math.sin(endA) + center[1], coor1[2]))

        elif pty == 'zx':
            for i in range(29):
                array.SetValue(
                    i + 1,
                    gp_Pnt(radius * math.sin(startA + dis * i) + center[1],
                           coor1[1],
                           radiud * math.cos(startA + dis * i) + center[0]))
            array.SetValue(
                30,
                gp_Pnt(radius * math.sin(endA) + center[1], coor1[1],
                       radiud * math.cos(endA) + center[0]))

        elif pty == 'yz':
            for i in range(29):
                array.SetValue(
                    i + 1,
                    gp_Pnt(coor1[0],
                           radiud * math.cos(startA + dis * i) + center[0],
                           radius * math.sin(startA + dis * i) + center[1]))
            array.SetValue(
                30,
                gp_Pnt(coor1[0],
                       radiud * math.cos(endA) + center[0],
                       radius * math.sin(endA) + center[1]))

        bspline2 = GeomAPI_PointsToBSpline(array).Curve()
        path_edge = BRepBuilderAPI_MakeEdge(bspline2).Edge()
        display.DisplayColoredShape(path_edge, 'RED')
Ejemplo n.º 9
0
def resample_curve_with_uniform_deflection(curve,
                                           deflection=0.5,
                                           degreeMin=3,
                                           degreeMax=8,
                                           continuity=GeomAbs_C2,
                                           tolerance=1e-4):
    """
    fits a bspline through the samples on `curve`
    @param curve: TopoDS_Wire, TopoDS_Edge, curve
    @param n_samples:
    """
    crv = to_adaptor_3d(curve)
    defl = GCPnts_UniformDeflection(crv, deflection)
    with assert_isdone(defl, 'failed to compute UniformDeflection'):
        print("Number of points:", defl.NbPoints())
    sampled_pnts = [defl.Value(i) for i in range(1, defl.NbPoints())]
    resampled_curve = GeomAPI_PointsToBSpline(
        point_list_to_TColgp_Array1OfPnt(sampled_pnts), degreeMin, degreeMax,
        continuity, tolerance)
    return resampled_curve.Curve().GetObject()
Ejemplo n.º 10
0
    def create_shape(self):
        d = self.declaration
        if not d.points:
            raise ValueError("Must have at least two points")
        # Poles and weights
        pts = TColgp_Array1OfPnt(1, len(d.points))
        set_value = pts.SetValue

        # TODO: Support weights
        for i, p in enumerate(d.points):
            set_value(i + 1, gp_Pnt(*p))

        self.shape = GeomAPI_PointsToBSpline(pts).Curve().GetObject()
Ejemplo n.º 11
0
 def DisplayG00Line(coor1, coor2):
     coors = [
         coor1, [coor2[0], coor1[1], coor1[2]],
         [coor2[0], coor2[1], coor1[2]], coor2
     ]
     for i in range(3):
         array = TColgp_Array1OfPnt(1, 2)
         array.SetValue(1, gp_Pnt(coors[i][0], coors[i][1], coors[i][2]))
         array.SetValue(
             2, gp_Pnt(coors[i + 1][0], coors[i + 1][1], coors[i + 1][2]))
         bspline2 = GeomAPI_PointsToBSpline(array).Curve()
         path_edge = BRepBuilderAPI_MakeEdge(bspline2).Edge()
         display.DisplayColoredShape(path_edge, 'YELLOW')
Ejemplo n.º 12
0
    def makeSpline(cls, listOfVector):
        """
        Interpolate a spline through the provided points.
        :param cls:
        :param listOfVector: a list of Vectors that represent the points
        :return: an Edge
        """
        pnts = TColgp_Array1OfPnt(0, len(listOfVector) - 1)
        for ix, v in enumerate(listOfVector):
            pnts.SetValue(ix, v.toPnt())

        spline_geom = GeomAPI_PointsToBSpline(pnts).Curve()

        return cls(BRepBuilderAPI_MakeEdge(spline_geom).Edge())
Ejemplo n.º 13
0
    def test_stl(self, event=None):
        #you probably dont have these
        #occ_shape = load_stl("~/local/pythonocc-0.3/pythonOCC/src/samples/Level2/DataExchange/sample.stl")
        #occ_shape = load_stl("~/local/legos/diver.stl") #http://adl.serveftp.org/lab/legos/diver.stl
        #occ_shape = load_stl(os.path.join(os.path.curdir, "models/cube.stl"))

        #make a box
        occ_shape = BRepPrimAPI_MakeBox(Point(0, 0, 0), Point(1, 1, 1)).Shape()
        display.DisplayShape(occ_shape)

        shape = Face(occ_shape)
        temp_points = list(set(shape.points))
        #self.assertTrue(len(temp_points)>0)

        #TODO: cluster points and make surfaces. but how do you compute the first parameter to build_plate?
        surf = Surface()
        surf.shape = occ_shape
        surf.points = temp_points
        #surf.approximate() #should be more like test_extract_shape_vertices
        curve1 = GeomAPI_PointsToBSpline(point_list_1(surf.points)).Curve()
        curve1.GetObject()
        e1 = Edge(curve1.GetHandle())
        display.DisplayShape(e1)
Ejemplo n.º 14
0
def fit_spline(points, degree_min=3, degree_max=8, continuity='C2', tol=1e-3):
    """Approximates a set of points on the plane with a B-spline.

    Parameters
    ----------
    points : ndarray
        2D ndarray with N>=2 rows and 2 columns, representing the points on the plane, ordered
        from one end point to the other. The first column gives the x, the second column gives the
        the y coordinates of the points.
    degree_min : int, optional
        Minimum degree of the spline. The default is 3.
    degree_max : int, optional
        Maximum degree of the spline. The default is 8.
    continuity : {'C0', 'G1', 'C1', 'G2', 'C2', 'C3', 'CN'}, optional
        The continuity of the spline will be at least `continuity`. The default is 'C2'.
        For their meanings, consult with
        https://www.opencascade.com/doc/occt-7.4.0/refman/html/_geom_abs___shape_8hxx.html
    tol : float, optional
        The distance from the points to the spline will be lower than `tol`. The default is 1e-3.

    Returns
    -------
    spline : Geom_BSplineCurve
        For details on the resulting spline, see the OpenCASCADE documentation:
        https://www.opencascade.com/doc/occt-7.4.0/refman/html/class_geom___b_spline_curve.html

    Raises
    ------
    ValueError
        If the minimum degree of the B-spline to be constructed is greater than its maximum degree.

    """
    if degree_min > degree_max:
        raise ValueError(
            'Minimum degree must not be lower than the maximum degree.')
    # Create points from the input array that OCCT understands
    n_point = np.size(points, 0)
    pts = TColgp_Array1OfPnt(0, n_point - 1)
    for i in range(n_point):
        pts.SetValue(i, gp_Pnt(float(points[i][0]), float(points[i][1]), 0))
    # Construct the spline
    continuity = _spline_continuity_enum(continuity)
    spline = GeomAPI_PointsToBSpline(pts, degree_min, degree_max, continuity,
                                     tol).Curve()
    return spline
def surface_from_curves():
    '''
    @param display:
    '''
    # First spline
    array = []
    array.append(gp_Pnt(-4, 0, 2))
    array.append(gp_Pnt(-7, 2, 2))
    array.append(gp_Pnt(-6, 3, 1))
    array.append(gp_Pnt(-4, 3, -1))
    array.append(gp_Pnt(-3, 5, -2))

    pt_list1 = point_list_to_TColgp_Array1OfPnt(array)
    SPL1 = GeomAPI_PointsToBSpline(pt_list1).Curve()
    SPL1_c = SPL1.GetObject()

    # Second spline
    a2 = []
    a2.append(gp_Pnt(-4, 0, 2))
    a2.append(gp_Pnt(-2, 2, 0))
    a2.append(gp_Pnt(2, 3, -1))
    a2.append(gp_Pnt(3, 7, -2))
    a2.append(gp_Pnt(4, 9, -1))
    pt_list2 = point_list_to_TColgp_Array1OfPnt(a2)
    SPL2 = GeomAPI_PointsToBSpline(pt_list2).Curve()
    SPL2_c = SPL2.GetObject()

    # Fill with StretchStyle
    aGeomFill1 = GeomFill_BSplineCurves(SPL1, SPL2, GeomFill_StretchStyle)

    SPL3 = Handle_Geom_BSplineCurve_DownCast(
        SPL1_c.Translated(gp_Vec(10, 0, 0)))
    SPL4 = Handle_Geom_BSplineCurve_DownCast(
        SPL2_c.Translated(gp_Vec(10, 0, 0)))
    # Fill with CoonsStyle
    aGeomFill2 = GeomFill_BSplineCurves(SPL3, SPL4, GeomFill_CoonsStyle)
    SPL5 = Handle_Geom_BSplineCurve_DownCast(
        SPL1_c.Translated(gp_Vec(20, 0, 0)))
    SPL6 = Handle_Geom_BSplineCurve_DownCast(
        SPL2_c.Translated(gp_Vec(20, 0, 0)))
    # Fill with CurvedStyle
    aGeomFill3 = GeomFill_BSplineCurves(SPL5, SPL6, GeomFill_CurvedStyle)

    aBSplineSurface1 = aGeomFill1.Surface()
    aBSplineSurface2 = aGeomFill2.Surface()
    aBSplineSurface3 = aGeomFill3.Surface()

    display.DisplayShape(make_face(aBSplineSurface1, 1e-6))
    display.DisplayShape(make_face(aBSplineSurface2, 1e-6))
    display.DisplayShape(make_face(aBSplineSurface3, 1e-6), update=True)
Ejemplo n.º 16
0
def points_to_bspline(pnts):
    pts = TColgp_Array1OfPnt(0, len(pnts) - 1)
    for n, i in enumerate(pnts):
        pts.SetValue(n, i)
    crv = GeomAPI_PointsToBSpline(pts)
    return crv.Curve()
def points_to_bspline(pnts, deg=3, periodic=False, tangents=None,
                      scale=False, continuity=GeomAbs_C2):
    """
    Points to bspline: originally from pythonocc-utils, changed to allow numpy
    arrays as input
    Paramters
    ---------
    pnts : list or numpy array
        array of x, y, z points
    deg : integer
        degree of the fitted bspline
    periodic : Bool (default=False)
        If true, OCC.GeomAPI_Interpolate will be used instead of the
        GeomAPI_PointsToBspline. Curve tangent vectors can then be
        enforced at the interpolation pnts
    tangents : array (default=None)
        list of [x,y,z] tangent vectors to be specificied at points:
        if only 2 tangents are specified, these will be enforced at the
        start and end points, otherwise tangents should have the same length
        as pnts and will be enforced at each point.
    Scale : Bool (default=False)
        Will scale the tangents (gives a smoother Periodic curve if False)
    continuity : OCC.GeomAbs.GeomAbs_XX type (default C2)
        The order of continuity (C^0, C^1, C^2, G^0, ....)

    Returns
    -------
    crv : OCC.Geom.BSplineCurve

    Notes
    -----
    """
    if not periodic and (tangents is None):
        _type = TColgp_Array1OfPnt
        pnts = point_array_to_TColgp_PntArrayType(pnts, _type)
        # Fit the curve to the point array
        deg_min = deg
        deg_max = deg
        crv = GeomAPI_PointsToBSpline(pnts, deg_min, deg_max, continuity).Curve()
    else:
        _type = TColgp_HArray1OfPnt
        pnts = point_array_to_TColgp_PntArrayType(pnts, _type)
        tol = 0.001
        interp = GeomAPI_Interpolate(pnts.GetHandle(), periodic, tol)
        if tangents is not None:
            N = tangents.shape[0]
            if N == 2:
                try:
                    interp.Load(gp_Vec(*tangents[0, :]), gp_Vec(*tangents[1, :]),
                                scale)
                except:
                    # Python 3 issue: using * on numpy array breaks gp_Vec
                    interp.Load(gp_Vec(*tangents[0, :].tolist()),
                                gp_Vec(*tangents[1, :].tolist()),
                                scale)
            else:
                tan_array = TColgp_Array1OfVec(1, N)
                for i in range(1, N + 1):
                    try:
                        tan_array.SetValue(i, gp_Vec(*tangents[i - 1, :]))
                    except TypeError:
                        # Python 3 issue: using * on numpy array breaks gp_Vec
                        tan_array.SetValue(i, gp_Vec(*tangents[i - 1, :].tolist()))

                tan_flags = TColStd_HArray1OfBoolean(1, N)
                tan_flags.Init(True)  # Set all true i.e. enforce all tangents
                interp.Load(tan_array, tan_flags.GetHandle(), scale)
        interp.Perform()
        crv = interp.Curve()
    return crv
def get_simple_bound(pts):
    spl1 = GeomAPI_PointsToBSpline(pts).Curve()
    spl1_adap_h = GeomAdaptor_HCurve(spl1).GetHandle()
    bound1_h = GeomFill_SimpleBound(spl1_adap_h, 0.001, 0.001).GetHandle()
    return spl1, bound1_h