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, update=False) display.DisplayShape(path_wire, update=False) display.DisplayShape(pipe, update=True)
def test_surfaces_from_offsets(self): '''Test: surfaces from offsets''' array1 = [] array1.append(gp_Pnt(-4, 5, 5)) array1.append(gp_Pnt(-3, 6, 6)) array1.append(gp_Pnt(-1, 7, 7)) array1.append(gp_Pnt(0, 8, 8)) array1.append(gp_Pnt(2, 9, 9)) SPL1 = GeomAPI_PointsToBSpline( point_list_to_TColgp_Array1OfPnt(array1)).Curve() array2 = [] array2.append(gp_Pnt(-4, 5, 2)) array2.append(gp_Pnt(-3, 6, 3)) array2.append(gp_Pnt(-1, 7, 4)) array2.append(gp_Pnt(0, 8, 5)) array2.append(gp_Pnt(2, 9, 6)) SPL2 = GeomAPI_PointsToBSpline( point_list_to_TColgp_Array1OfPnt(array2)).Curve() aGeomFill1 = GeomFill_BSplineCurves(SPL1, SPL2, GeomFill_StretchStyle) aGeomSurface = aGeomFill1.Surface() offset = 1 GOS = Geom_OffsetSurface(aGeomSurface, offset) offset = 2 GOS1 = Geom_OffsetSurface(aGeomSurface, offset) offset = 3 GOS2 = Geom_OffsetSurface(aGeomSurface, offset) face1 = make_face(aGeomSurface) face2 = make_face(GOS) face3 = make_face(GOS1) face4 = make_face(GOS2)
def quad_to_face(points: Quad) -> TopoDS_Face: """Convert a quad to a BRep face with an underlying ruled surface. Parameters ---------- points : [point, point, point, point] Four points defining a quad. Returns ------- TopoDS_Face Raises ------ AssertionError If the number of points is not 4. """ assert len(points) == 4, "The number of input points should be four." points = [Point(*point) for point in points] curve1 = GeomAPI_PointsToBSpline( array1_from_points1([points[0], points[1]])).Curve() curve2 = GeomAPI_PointsToBSpline( array1_from_points1([points[3], points[2]])).Curve() srf = geomfill_Surface(curve1, curve2) return BRepBuilderAPI_MakeFace(srf, 1e-6).Face()
def spl_curv_pts(pts=[gp_Pnt()]): num = len(pts) p_array = TColgp_Array1OfPnt(1, num) for idx, pnt in enumerate(pts): p_array.SetValue(idx + 1, pnt) api = GeomAPI_PointsToBSpline(p_array) return p_array, api.Curve()
def points_to_bspline(pnts): ''' Points to bspline ''' pnts = point_list_to_TColgp_Array1OfPnt(pnts) crv = GeomAPI_PointsToBSpline(pnts) return crv.Curve()
def gen_data_spline(dat): num = dat.shape[0] pts = TColgp_Array1OfPnt(1, num) for i, xyz in enumerate(dat): pnt = gp_Pnt(*xyz) pts.SetValue(i+1, pnt) geo_spl = GeomAPI_PointsToBSpline(pts, 4) return geo_spl.Curve()
def spline_from_polyline(pol): from OCC.Extend.ShapeFactory import point_list_to_TColgp_Array1OfPnt, make_face from OCC.Core.GeomAPI import GeomAPI_PointsToBSpline array = [] for p in pol: array.append(gp_Pnt(p[0], p[1], p[2])) pt_list1 = point_list_to_TColgp_Array1OfPnt(array) SPL1 = GeomAPI_PointsToBSpline(pt_list1).Curve() SPL1 = GeomAPI_PointsToBSpline(pt_list1) edge = BRepBuilderAPI_MakeEdge(SPL1.Curve()) wire = BRepBuilderAPI_MakeWire(edge.Edge()) return edge.Shape(), wire
def spl_curv(px, py, pz): num = px.size pts = [] p_array = TColgp_Array1OfPnt(1, num) for idx, t in enumerate(px): x = px[idx] y = py[idx] z = pz[idx] pnt = gp_Pnt(x, y, z) pts.append(pnt) p_array.SetValue(idx + 1, pnt) api = GeomAPI_PointsToBSpline(p_array) return p_array, api.Curve()
def test_surface_from_curves(self): '''Test: surfaces from curves''' 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)) aaa = point_list_to_TColgp_Array1OfPnt(array) SPL1 = GeomAPI_PointsToBSpline(aaa).Curve() 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)) bbb = point_list_to_TColgp_Array1OfPnt(a2) SPL2 = GeomAPI_PointsToBSpline(bbb).Curve() aGeomFill1 = GeomFill_BSplineCurves(SPL1, SPL2, GeomFill_StretchStyle) SPL3 = Geom_BSplineCurve.DownCast(SPL1.Translated(gp_Vec(10, 0, 0))) SPL4 = Geom_BSplineCurve.DownCast(SPL2.Translated(gp_Vec(10, 0, 0))) aGeomFill2 = GeomFill_BSplineCurves(SPL3, SPL4, GeomFill_CoonsStyle) SPL5 = Geom_BSplineCurve.DownCast(SPL1.Translated(gp_Vec(20, 0, 0))) SPL6 = Geom_BSplineCurve.DownCast(SPL2.Translated(gp_Vec(20, 0, 0))) aGeomFill3 = GeomFill_BSplineCurves(SPL5, SPL6, GeomFill_CurvedStyle) aBSplineSurface1 = aGeomFill1.Surface() aBSplineSurface2 = aGeomFill2.Surface() aBSplineSurface3 = aGeomFill3.Surface()
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: ''' from OCC.GCPnts import GCPnts_UniformDeflection 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 xrange(1, defl.NbPoints())] resampled_curve = GeomAPI_PointsToBSpline(point_list_to_TColgp_Array1OfPnt(sampled_pnts), degreeMin, degreeMax, continuity, tolerance) return resampled_curve.Curve().GetObject()
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)
def InterpolatePoints(self): curgp_Array1CurvePoles2d = point_list_to_TColgp_Array1OfPnt2d(self.Poles2d) curgp_Array1CurvePoles = point_list_to_TColgp_Array1OfPnt(self.Poles) # self.FindPoints(self.Poles2d) # self.FindPoints(self.Poles) try: myGeom2d_BSplineCurve = Geom2dAPI_PointsToBSpline(curgp_Array1CurvePoles2d) if myGeom2d_BSplineCurve.IsDone(): self.myGeom2d_BSplineCurve = myGeom2d_BSplineCurve.Curve() except Exception as e: print(e) try: myGeom_BSplineCurve = GeomAPI_PointsToBSpline(curgp_Array1CurvePoles) if myGeom_BSplineCurve.IsDone(): self.myGeom_BSplineCurve = myGeom_BSplineCurve.Curve() except Exception as e: print(e)
def approx_points_by_piecewise_bezier(points_3d, degree, tol): if degree not in (2, 3): raise RuntimeError( "SVG files only support Bezier curves of degree 2 or 3") points_3d_occ = [gp_Pnt(*p) for p in points_3d] approx_spline = GeomAPI_PointsToBSpline( point_list_to_TColgp_Array1OfPnt(points_3d_occ), degree, degree, GeomAbs_C1, tol) # TO DO: # why the following string doesn't yield a spline of degree < 3 # approx_spline = GeomConvert_ApproxCurve(spline, tol, order, max_segments, max_degree) if not approx_spline.IsDone(): raise RuntimeError( "Could not approximate points within a given tolerance") return GeomConvert_BSplineCurveToBezierCurve(approx_spline.Curve())
def curv_spl(px, py, axs=gp_Ax3()): nx = px.shape[0] pts = TColgp_Array1OfPnt(1, nx) for idx in range(pts.Lower(), pts.Upper() + 1): pnt = gp_Pnt(px[idx - 1], py[idx - 1], 0) pts.SetValue(idx, pnt) curv = GeomAPI_PointsToBSpline(pts, 3, 8, GeomAbs_C2, 0.001).Curve() return curv
def __init__(self): super(Sketch_CommandPointToBSpline, self).__init__("PointsToBSpline") self.IndexCounter = 1 self.tempPnt2d = gp.Origin2d() self.myFirstgp_Pnt = gp.Origin() self.tempPnt = gp.Origin() self.curEdge = TopoDS_Edge() self.myBSplineCurveAction = BSplineCurveAction.Nothing self.Poles2d = [gp.Origin2d()] * 2 self.Poles = [gp.Origin()] * 2 curgp_Array1CurvePoles2d = point_list_to_TColgp_Array1OfPnt2d(self.Poles2d) curgp_Array1CurvePoles = point_list_to_TColgp_Array1OfPnt(self.Poles) self.myGeom2d_BSplineCurve = Geom2dAPI_PointsToBSpline(curgp_Array1CurvePoles2d).Curve() self.myGeom_BSplineCurve = GeomAPI_PointsToBSpline(curgp_Array1CurvePoles).Curve() self.myRubberAIS_Shape = AIS_Shape(self.curEdge) self.myRubberAIS_Shape.SetColor(Quantity_Color(Quantity_NOC_LIGHTPINK1))
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() # 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() # Fill with StretchStyle aGeomFill1 = GeomFill_BSplineCurves(SPL1, SPL2, GeomFill_StretchStyle) SPL3 = Geom_BSplineCurve.DownCast(SPL1.Translated(gp_Vec(10, 0, 0))) SPL4 = Geom_BSplineCurve.DownCast(SPL2.Translated(gp_Vec(10, 0, 0))) # Fill with CoonsStyle aGeomFill2 = GeomFill_BSplineCurves(SPL3, SPL4, GeomFill_CoonsStyle) SPL5 = Geom_BSplineCurve.DownCast(SPL1.Translated(gp_Vec(20, 0, 0))) SPL6 = Geom_BSplineCurve.DownCast(SPL2.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)
def test_surfaces_from_revolution(self): '''Test: surfaces from revolution''' array = [] array.append(gp_Pnt(0, 0, 1)) array.append(gp_Pnt(1, 2, 2)) array.append(gp_Pnt(2, 3, 3)) array.append(gp_Pnt(4, 3, 4)) array.append(gp_Pnt(5, 5, 5)) aCurve = GeomAPI_PointsToBSpline(point_list_to_TColgp_Array1OfPnt(array)).Curve() SOR = Geom_SurfaceOfRevolution(aCurve, gp_OX()) edge = make_edge(aCurve) self.assertFalse(edge is None) face = make_face(SOR) self.assertFalse(face is None)
def test_pipes(self): """Test: pipes""" a1 = [] a1.append(gp_Pnt(-4, 0, 2)) a1.append(gp_Pnt(-5, 1, 0)) a1.append(gp_Pnt(-6, 2, -2)) a1.append(gp_Pnt(-5, 4, -7)) a1.append(gp_Pnt(-3, 5, -12)) xxx = point_list_to_TColgp_Array1OfPnt(a1) SPL1 = GeomAPI_PointsToBSpline(xxx).Curve() aPipe = GeomFill_Pipe(SPL1, True) aPipe.Perform(False, False) aSurface = aPipe.Surface() self.assertIsNotNone(aSurface) E = GC_MakeEllipse(gp_XOY(), 2, 1).Value() aPipe2 = GeomFill_Pipe(SPL1, E, GeomFill_IsConstantNormal) aPipe2.Perform(False, False) aSurface2 = aPipe2.Surface() aSurface2.Translate(gp_Vec(5, 0, 0)) TC1 = GC_MakeSegment(gp_Pnt(1, 1, 1), gp_Pnt(2, 2, 2)).Value() TC2 = GC_MakeSegment(gp_Pnt(1, 1, 0), gp_Pnt(3, 2, 1)).Value() aPipe3 = GeomFill_Pipe(SPL1, TC1, TC2) aPipe3.Perform(False, False) aSurface3 = aPipe3.Surface() aSurface3.Translate(gp_Vec(10, 0, 0)) for _, mode in enumerate([ GeomFill_IsConstantNormal, GeomFill_IsCorrectedFrenet, GeomFill_IsDarboux, GeomFill_IsFrenet, GeomFill_IsGuideAC, GeomFill_IsGuideACWithContact, GeomFill_IsGuidePlan, GeomFill_IsGuidePlanWithContact, ]): E = GC_MakeEllipse(gp_XOY(), 2, 1).Value() aPipe2 = GeomFill_Pipe(SPL1, TC1, TC2, mode) aPipe2.Perform(False, False) aSurface2 = aPipe2.Surface() aSurface2.Translate(gp_Vec(5, 5, 0))
def makeBSpline(self, point_array, last_point=None): if last_point: array = TColgp_Array1OfPnt(1, len(point_array) + 1) for i, p in enumerate(point_array): array.SetValue(i + 1, p) array.SetValue(len(point_array) + 1, last_point) else: array = TColgp_Array1OfPnt(1, len(point_array)) for i, p in enumerate(point_array): array.SetValue(i + 1, p) try: bspline = GeomAPI_PointsToBSpline(array).Curve() if self._tmp_geometry: edge = BRepBuilderAPI_MakeEdge(bspline) shapes = edge.Shape() self._tmp_geometry.SetShape(shapes) self._tmp_geometry.Redisplay(False) else: ais_bspline = self._display.DisplayShape(bspline) self._tmp_geometry = ais_bspline except: pass
def test_distances(self): '''Test: distances''' array1 = [] array1.append(gp_Pnt(-5, 1, 2)) array1.append(gp_Pnt(-5, 2, 2)) array1.append(gp_Pnt(-5.3, 3, 1)) array1.append(gp_Pnt(-5, 4, 1)) array1.append(gp_Pnt(-5, 5, 2)) SPL1 = GeomAPI_PointsToBSpline( point_list_to_TColgp_Array1OfPnt(array1)).Curve() array2 = [] array2.append(gp_Pnt(4, 1, 2)) array2.append(gp_Pnt(4, 2, 2)) array2.append(gp_Pnt(3.7, 3, 1)) array2.append(gp_Pnt(4, 4, 1)) array2.append(gp_Pnt(4, 5, 2)) SPL2 = GeomAPI_PointsToBSpline( point_list_to_TColgp_Array1OfPnt(array2)).Curve() aGeomFill1 = GeomFill_BSplineCurves(SPL1, SPL2, GeomFill_StretchStyle) aSurf1 = aGeomFill1.Surface() array3 = TColgp_Array2OfPnt(1, 5, 1, 5) array3.SetValue(1, 1, gp_Pnt(-4, -4, 5)) array3.SetValue(1, 2, gp_Pnt(-4, -2, 5)) array3.SetValue(1, 3, gp_Pnt(-4, 0, 4)) array3.SetValue(1, 4, gp_Pnt(-4, 2, 5)) array3.SetValue(1, 5, gp_Pnt(-4, 4, 5)) array3.SetValue(2, 1, gp_Pnt(-2, -4, 4)) array3.SetValue(2, 2, gp_Pnt(-2, -2, 4)) array3.SetValue(2, 3, gp_Pnt(-2, 0, 4)) array3.SetValue(2, 4, gp_Pnt(-2, 2, 4)) array3.SetValue(2, 5, gp_Pnt(-2, 5, 4)) array3.SetValue(3, 1, gp_Pnt(0, -4, 3.5)) array3.SetValue(3, 2, gp_Pnt(0, -2, 3.5)) array3.SetValue(3, 3, gp_Pnt(0, 0, 3.5)) array3.SetValue(3, 4, gp_Pnt(0, 2, 3.5)) array3.SetValue(3, 5, gp_Pnt(0, 5, 3.5)) array3.SetValue(4, 1, gp_Pnt(2, -4, 4)) array3.SetValue(4, 2, gp_Pnt(2, -2, 4)) array3.SetValue(4, 3, gp_Pnt(2, 0, 3.5)) array3.SetValue(4, 4, gp_Pnt(2, 2, 5)) array3.SetValue(4, 5, gp_Pnt(2, 5, 4)) array3.SetValue(5, 1, gp_Pnt(4, -4, 5)) array3.SetValue(5, 2, gp_Pnt(4, -2, 5)) array3.SetValue(5, 3, gp_Pnt(4, 0, 5)) array3.SetValue(5, 4, gp_Pnt(4, 2, 6)) array3.SetValue(5, 5, gp_Pnt(4, 5, 5)) aSurf2 = GeomAPI_PointsToBSplineSurface(array3).Surface() ESS = GeomAPI_ExtremaSurfaceSurface(aSurf1, aSurf2) dist = ESS.LowerDistance() self.assertGreater(dist, 1.25) self.assertLess(dist, 1.26) a, b = gp_Pnt(), gp_Pnt() ESS.NearestPoints(a, b) NbExtrema = ESS.NbExtrema() for k in range(1, NbExtrema + 1): P3, P4 = gp_Pnt(), gp_Pnt() ESS.Points(k, P3, P4) aCurve = GC_MakeSegment(P3, P4).Value() self.assertFalse(aCurve is None)
from OCC.Core.STEPControl import STEPControl_Writer, STEPControl_AsIs from OCC.Core.Interface import Interface_Static_SetCVal from OCC.Core.TopoDS import topods_Face from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeFace a1 = [] a1.append(gp_Pnt(-4, 0, 2)) a1.append(gp_Pnt(-7, 2, 2)) a1.append(gp_Pnt(-6, 3, 1)) a1.append(gp_Pnt(-4, 3, -1)) a1.append(gp_Pnt(-3, 5, -2)) pt_list1 = point_list_to_TColgp_Array1OfPnt(a1) SPL1 = GeomAPI_PointsToBSpline(pt_list1).Curve() 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() fill = GeomFill_BSplineCurves(SPL1, SPL2, GeomFill_CoonsStyle) surface = fill.Surface() face = topods_Face(BRepBuilderAPI_MakeFace(surface, 1e-6).Shape())
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()
class Sketch_CommandPointToBSpline(Sketch_Command): def __init__(self): super(Sketch_CommandPointToBSpline, self).__init__("PointsToBSpline") self.IndexCounter = 1 self.tempPnt2d = gp.Origin2d() self.myFirstgp_Pnt = gp.Origin() self.tempPnt = gp.Origin() self.curEdge = TopoDS_Edge() self.myBSplineCurveAction = BSplineCurveAction.Nothing self.Poles2d = [gp.Origin2d()] * 2 self.Poles = [gp.Origin()] * 2 curgp_Array1CurvePoles2d = point_list_to_TColgp_Array1OfPnt2d( self.Poles2d) curgp_Array1CurvePoles = point_list_to_TColgp_Array1OfPnt(self.Poles) self.myGeom2d_BSplineCurve = Geom2dAPI_PointsToBSpline( curgp_Array1CurvePoles2d).Curve() self.myGeom_BSplineCurve = GeomAPI_PointsToBSpline( curgp_Array1CurvePoles).Curve() self.myRubberAIS_Shape = AIS_Shape(self.curEdge) def Action(self): self.myBSplineCurveAction = BSplineCurveAction.Input_1Point def InterpolatePoints(self): curgp_Array1CurvePoles2d = point_list_to_TColgp_Array1OfPnt2d( self.Poles2d) curgp_Array1CurvePoles = point_list_to_TColgp_Array1OfPnt(self.Poles) try: myGeom2d_BSplineCurve = Geom2dAPI_PointsToBSpline( curgp_Array1CurvePoles2d) if myGeom2d_BSplineCurve.IsDone(): self.myGeom2d_BSplineCurve = myGeom2d_BSplineCurve.Curve() except Exception as e: print(e) try: myGeom_BSplineCurve = GeomAPI_PointsToBSpline( curgp_Array1CurvePoles) if myGeom_BSplineCurve.IsDone(): self.myGeom_BSplineCurve = myGeom_BSplineCurve.Curve() except Exception as e: print(e) def FindPoints(self, li): for i in li: if type(i) == gp_Pnt: print("3d", i.X(), i.Y(), i.Z()) elif type(i) == gp_Pnt2d: print("2d", i.X(), i.Y()) def MouseInputEvent(self, thePnt2d: gp_Pnt2d, buttons, modifier): self.curPnt2d = self.myAnalyserSnap.MouseInput(thePnt2d) if self.myBSplineCurveAction == BSplineCurveAction.Nothing: pass elif self.myBSplineCurveAction == BSplineCurveAction.Input_1Point: # calculate first point self.myFirstgp_Pnt2d = gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y()) self.myFirstgp_Pnt = elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d) self.myFirstPoint.SetPnt(self.myFirstgp_Pnt) # self.myRubberLine.SetPoints(self.myFirstPoint, self.myFirstPoint) # store first points self.Poles2d[0] = gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y()) self.Poles[0] = gp_Pnt(self.myFirstgp_Pnt.X(), self.myFirstgp_Pnt.Y(), self.myFirstgp_Pnt.Z()) # calculate interpolated bspline from stored points self.InterpolatePoints() self.interpolateBspline = Sketch_Bspline(self.myContext, self.curCoordinateSystem) self.interpolateBspline.AddPoles(self.curPnt2d) # self.myContext.Display(self.myRubberLine, True) self.myBSplineCurveAction = BSplineCurveAction.Input_2Point self.IndexCounter = 2 elif self.myBSplineCurveAction == BSplineCurveAction.Input_2Point: self.Poles2d[1] = gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y()) self.tempPnt = elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d) self.Poles[1] = gp_Pnt(self.tempPnt.X(), self.tempPnt.Y(), self.tempPnt.Z()) # self.myGeom_BSplineCurve.SetPole(self.IndexCounter,gp_Pnt(self.tempPnt.X(), self.tempPnt.Y(), self.tempPnt.Z())) self.InterpolatePoints() self.mySecondPoint.SetPnt(self.tempPnt) ME = BRepBuilderAPI_MakeEdge(self.myGeom_BSplineCurve) if ME.IsDone(): self.interpolateBspline.AddPoles(self.curPnt2d) self.curEdge = ME.Edge() self.myRubberAIS_Shape.Set(self.curEdge) # self.myContext.Remove(self.myRubberLine, True) self.myContext.Display(self.myRubberAIS_Shape, True) self.Poles2d.append(self.curPnt2d) self.Poles.append(self.tempPnt) self.InterpolatePoints() self.IndexCounter += 1 self.myBSplineCurveAction = BSplineCurveAction.Input_OtherPoints elif self.myBSplineCurveAction == BSplineCurveAction.Input_OtherPoints: # self.myGeom2d_BSplineCurve.SetPole(self.IndexCounter, gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y())) self.Poles2d[self.IndexCounter - 1] = gp_Pnt2d( self.curPnt2d.X(), self.curPnt2d.Y()) self.tempPnt = elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d) self.Poles[self.IndexCounter - 1] = gp_Pnt(self.tempPnt.X(), self.tempPnt.Y(), self.tempPnt.Z()) # self.myGeom_BSplineCurve.SetPole(self.IndexCounter,gp_Pnt(self.tempPnt.X(), self.tempPnt.Y(), self.tempPnt.Z())) self.InterpolatePoints() self.mySecondPoint.SetPnt(self.tempPnt) ME = BRepBuilderAPI_MakeEdge(self.myGeom_BSplineCurve) if ME.IsDone(): self.interpolateBspline.AddPoles(self.curPnt2d) self.curEdge = ME.Edge() if self.IndexCounter > MAXIMUMPOLES: self.closeBSpline() else: self.myRubberAIS_Shape.Set(self.curEdge) self.myContext.Redisplay(self.myRubberAIS_Shape, True) self.Poles2d.append(self.curPnt2d) self.Poles.append(self.tempPnt) self.InterpolatePoints() self.tempPnt2d = gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y()) self.IndexCounter += 1 return False def MouseMoveEvent(self, thePnt2d: gp_Pnt2d, buttons, modifiers): self.curPnt2d = self.myAnalyserSnap.MouseMove(thePnt2d) if self.myBSplineCurveAction == BSplineCurveAction.Nothing: pass elif self.myBSplineCurveAction == BSplineCurveAction.Input_1Point: pass elif self.myBSplineCurveAction == BSplineCurveAction.Input_2Point: self.mySecondPoint.SetPnt( elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d)) # self.myRubberLine.SetPoints(self.myFirstPoint, self.mySecondPoint) # self.myContext.Redisplay(self.myRubberLine, True) elif self.myBSplineCurveAction == BSplineCurveAction.Input_OtherPoints: self.myGeom2d_BSplineCurve.SetPole(self.IndexCounter - 1, self.curPnt2d) self.mySecondPoint.SetPnt( elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d)) self.myGeom_BSplineCurve.SetPole(self.IndexCounter - 1, self.mySecondPoint.Pnt()) ME = BRepBuilderAPI_MakeEdge(self.myGeom_BSplineCurve) if ME.IsDone(): self.curEdge = ME.Edge() self.myRubberAIS_Shape.Set(self.curEdge) self.myContext.Redisplay(self.myRubberAIS_Shape, True) else: self.IndexCounter -= 1 def CancelEvent(self): if self.myBSplineCurveAction == BSplineCurveAction.Nothing: pass elif self.myBSplineCurveAction == BSplineCurveAction.Input_1Point: self.rootNode.removeChild(self.rootNode.childCount() - 1) # self.myContext.Remove(self.myRubberLine, True) self.interpolateBspline.RemoveLabel() del self.interpolateBspline elif self.myBSplineCurveAction == BSplineCurveAction.Input_2Point: self.rootNode.removeChild(self.rootNode.childCount() - 1) # self.myContext.Remove(self.myRubberLine, True) self.interpolateBspline.RemoveLabel() del self.interpolateBspline elif self.myBSplineCurveAction == BSplineCurveAction.Input_OtherPoints: # remove the last pole del self.Poles2d[-1] del self.Poles[-1] self.InterpolatePoints() ME = BRepBuilderAPI_MakeEdge(self.myGeom_BSplineCurve) if ME.IsDone(): self.curEdge = ME.Edge() self.IndexCounter -= 1 self.closeBSpline() self.myBSplineCurveAction = BSplineCurveAction.Nothing def GetTypeOfMethod(self): return Sketch_ObjectTypeOfMethod.PointsToBSpline_Method def closeBSpline(self): self.myContext.Remove(self.myRubberAIS_Shape, True) self.interpolateBspline_node = BsplineNode( self.interpolateBspline.GetName(), self.rootNode) self.interpolateBspline.ComputeInterpolation() self.interpolateBspline_node.setSketchObject(self.interpolateBspline) self.AddObject(self.interpolateBspline.GetGeometry2d(), self.interpolateBspline.GetAIS_Object(), Sketch_GeometryType.CurveSketchObject) self.Poles2d = [gp.Origin2d()] * 2 self.Poles = [gp.Origin()] * 2 self.InterpolatePoints() self.myBSplineCurveAction = BSplineCurveAction.Input_1Point
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
# In[3]: my_renderer = JupyterRenderer() # Generation of the bspline profile using an array of 5 points. The spline is interpolated through these points. # In[4]: # 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() # Generation of the linear path. # In[5]: # 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() # Build the prism model resulting from the bspline extrusion allong the linear path # In[6]:
def get_simple_bound(pts): spl1 = GeomAPI_PointsToBSpline(pts).Curve() spl1_adap = GeomAdaptor_HCurve(spl1) bound1 = GeomFill_SimpleBound(spl1_adap, 0.001, 0.001) return spl1, bound1
class Sketch_CommandPointToBSpline(Sketch_Command): def __init__(self): super(Sketch_CommandPointToBSpline, self).__init__("PointsToBSpline") self.IndexCounter = 1 self.tempPnt2d = gp.Origin2d() self.myFirstgp_Pnt = gp.Origin() self.tempPnt = gp.Origin() self.curEdge = TopoDS_Edge() self.myBSplineCurveAction = BSplineCurveAction.Nothing self.Poles2d = [gp.Origin2d()] * 2 self.Poles = [gp.Origin()] * 2 curgp_Array1CurvePoles2d = point_list_to_TColgp_Array1OfPnt2d(self.Poles2d) curgp_Array1CurvePoles = point_list_to_TColgp_Array1OfPnt(self.Poles) self.myGeom2d_BSplineCurve = Geom2dAPI_PointsToBSpline(curgp_Array1CurvePoles2d).Curve() self.myGeom_BSplineCurve = GeomAPI_PointsToBSpline(curgp_Array1CurvePoles).Curve() self.myRubberAIS_Shape = AIS_Shape(self.curEdge) self.myRubberAIS_Shape.SetColor(Quantity_Color(Quantity_NOC_LIGHTPINK1)) def Action(self): self.myBSplineCurveAction = BSplineCurveAction.Input_1Point def InterpolatePoints(self): curgp_Array1CurvePoles2d = point_list_to_TColgp_Array1OfPnt2d(self.Poles2d) curgp_Array1CurvePoles = point_list_to_TColgp_Array1OfPnt(self.Poles) # self.FindPoints(self.Poles2d) # self.FindPoints(self.Poles) try: myGeom2d_BSplineCurve = Geom2dAPI_PointsToBSpline(curgp_Array1CurvePoles2d) if myGeom2d_BSplineCurve.IsDone(): self.myGeom2d_BSplineCurve = myGeom2d_BSplineCurve.Curve() except Exception as e: print(e) try: myGeom_BSplineCurve = GeomAPI_PointsToBSpline(curgp_Array1CurvePoles) if myGeom_BSplineCurve.IsDone(): self.myGeom_BSplineCurve = myGeom_BSplineCurve.Curve() except Exception as e: print(e) def FindPoints(self,li): for i in li: if type(i)==gp_Pnt: print("3d",i.X(),i.Y(),i.Z()) elif type(i)==gp_Pnt2d: print("2d",i.X(), i.Y()) def MouseInputEvent(self, thePnt2d: gp_Pnt2d, buttons, modifier): self.curPnt2d = self.myAnalyserSnap.MouseInput(thePnt2d) if self.myBSplineCurveAction == BSplineCurveAction.Nothing: pass elif self.myBSplineCurveAction == BSplineCurveAction.Input_1Point: self.myFirstgp_Pnt2d = gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y()) self.myFirstgp_Pnt = elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d) self.myFirstPoint.SetPnt(self.myFirstgp_Pnt) self.myRubberLine.SetPoints(self.myFirstPoint, self.myFirstPoint) self.Poles2d[0] = gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y()) self.Poles[0] = gp_Pnt(self.myFirstgp_Pnt.X(), self.myFirstgp_Pnt.Y(), self.myFirstgp_Pnt.Z()) self.InterpolatePoints() myGeom2d_Point = Geom2d_CartesianPoint(self.curPnt2d) myAIS_Point = AIS_Point(self.myFirstPoint) self.myContext.Display(myAIS_Point, True) self.AddObject(myGeom2d_Point, myAIS_Point, Sketch_GeometryType.PointSketchObject) self.myContext.Display(self.myRubberLine, True) self.myBSplineCurveAction = BSplineCurveAction.Input_2Point self.IndexCounter = 2 elif self.myBSplineCurveAction == BSplineCurveAction.Input_2Point: # self.myGeom2d_BSplineCurve.SetPole(self.IndexCounter, gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y())) self.Poles2d[1] = gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y()) self.tempPnt = elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d) self.Poles[1] = gp_Pnt(self.tempPnt.X(), self.tempPnt.Y(), self.tempPnt.Z()) # self.myGeom_BSplineCurve.SetPole(self.IndexCounter,gp_Pnt(self.tempPnt.X(), self.tempPnt.Y(), self.tempPnt.Z())) self.InterpolatePoints() self.mySecondPoint.SetPnt(self.tempPnt) ME = BRepBuilderAPI_MakeEdge(self.myGeom_BSplineCurve) if ME.IsDone(): self.storePoles() self.curEdge = ME.Edge() self.myRubberAIS_Shape.Set(self.curEdge) self.myContext.Remove(self.myRubberLine, True) self.myContext.Display(self.myRubberAIS_Shape, True) self.Poles2d.append(self.curPnt2d) self.Poles.append(self.tempPnt) self.InterpolatePoints() self.IndexCounter += 1 self.myBSplineCurveAction = BSplineCurveAction.Input_OtherPoints elif self.myBSplineCurveAction == BSplineCurveAction.Input_OtherPoints: # self.myGeom2d_BSplineCurve.SetPole(self.IndexCounter, gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y())) self.Poles2d[self.IndexCounter-1] = gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y()) self.tempPnt = elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d) self.Poles[self.IndexCounter-1] = gp_Pnt(self.tempPnt.X(), self.tempPnt.Y(), self.tempPnt.Z()) # self.myGeom_BSplineCurve.SetPole(self.IndexCounter,gp_Pnt(self.tempPnt.X(), self.tempPnt.Y(), self.tempPnt.Z())) self.InterpolatePoints() self.mySecondPoint.SetPnt(self.tempPnt) ME = BRepBuilderAPI_MakeEdge(self.myGeom_BSplineCurve) if ME.IsDone(): self.storePoles() self.curEdge = ME.Edge() if self.IndexCounter > MAXIMUMPOLES: self.closeBSpline() else: self.myRubberAIS_Shape.Set(self.curEdge) self.myContext.Redisplay(self.myRubberAIS_Shape, True) self.Poles2d.append(self.curPnt2d) self.Poles.append(self.tempPnt) self.InterpolatePoints() self.tempPnt2d = gp_Pnt2d(self.curPnt2d.X(), self.curPnt2d.Y()) self.IndexCounter += 1 return False def MouseMoveEvent(self, thePnt2d: gp_Pnt2d, buttons, modifiers): self.curPnt2d = self.myAnalyserSnap.MouseMove(thePnt2d) if self.myBSplineCurveAction == BSplineCurveAction.Nothing: pass elif self.myBSplineCurveAction == BSplineCurveAction.Input_1Point: pass elif self.myBSplineCurveAction == BSplineCurveAction.Input_2Point: self.mySecondPoint.SetPnt(elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d)) self.myRubberLine.SetPoints(self.myFirstPoint, self.mySecondPoint) self.myContext.Redisplay(self.myRubberLine, True) elif self.myBSplineCurveAction == BSplineCurveAction.Input_OtherPoints: self.myGeom2d_BSplineCurve.SetPole(self.IndexCounter-1, self.curPnt2d) self.mySecondPoint.SetPnt(elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d)) self.myGeom_BSplineCurve.SetPole(self.IndexCounter-1, self.mySecondPoint.Pnt()) ME = BRepBuilderAPI_MakeEdge(self.myGeom_BSplineCurve) if ME.IsDone(): self.curEdge = ME.Edge() self.myRubberAIS_Shape.Set(self.curEdge) self.myContext.Redisplay(self.myRubberAIS_Shape, True) else: self.IndexCounter -= 1 def CancelEvent(self): if self.myBSplineCurveAction == BSplineCurveAction.Nothing: pass elif self.myBSplineCurveAction == BSplineCurveAction.Input_1Point: pass elif self.myBSplineCurveAction == BSplineCurveAction.Input_2Point: self.myContext.Remove(self.myRubberLine, True) elif self.myBSplineCurveAction == BSplineCurveAction.Input_OtherPoints: ME = BRepBuilderAPI_MakeEdge(self.myGeom_BSplineCurve) if ME.IsDone(): self.curEdge = ME.Edge() self.IndexCounter -= 1 self.closeBSpline() self.myBSplineCurveAction = BSplineCurveAction.Nothing def GetTypeOfMethod(self): return Sketch_ObjectTypeOfMethod.PointsToBSpline_Method def storePoles(self): myGeom2d_Point = Geom2d_CartesianPoint(self.curPnt2d) self.mySecondPoint.SetPnt(elclib.To3d(self.curCoordinateSystem.Ax2(), self.curPnt2d)) myGeom_Point = Geom_CartesianPoint(self.mySecondPoint.X(), self.mySecondPoint.Y(), self.mySecondPoint.Z()) myAIS_Point = AIS_Point(myGeom_Point) self.myContext.Display(myAIS_Point, True) self.AddObject(myGeom2d_Point, myAIS_Point, Sketch_GeometryType.PointSketchObject) def closeBSpline(self): self.myContext.Remove(self.myRubberAIS_Shape, True) self.storePoles() myAIS_Shape = AIS_Shape(self.curEdge) self.AddObject(self.myGeom2d_BSplineCurve, myAIS_Shape, Sketch_GeometryType.CurveSketchObject) self.myContext.Display(myAIS_Shape, True) self.Poles2d = [gp.Origin2d()] * 2 self.Poles = [gp.Origin()] * 2 self.InterpolatePoints() self.myBSplineCurveAction = BSplineCurveAction.Input_1Point
py = np.linspace(-1, 1, 15) * 120 / 2 mesh = np.meshgrid(px, py) surf = mesh[0]**2 / 100 + mesh[1]**2 / 1000 surf[7, 5] = 50 obj.display.DisplayShape(spl_face(*mesh, surf)) p0 = gp_Pnt(mesh[0][0, 0], mesh[1][0, 0], surf[0, 0]) p1 = gp_Pnt(mesh[0][0, -1], mesh[1][0, -1], surf[0, -1]) p2 = gp_Pnt(mesh[0][-1, 0], mesh[1][-1, 0], surf[-1, 0]) p3 = gp_Pnt(mesh[0][-1, 1], mesh[1][-1, -1], surf[-1, -1]) pt = np.linspace(0, 2 * np.pi, 100) pts = [] p_array = TColgp_Array1OfPnt(1, 100 - 1) for idx, t in enumerate(pt[:-1]): x = 75.0 * np.cos(t) y = 75.0 * np.sin(t) z = 50 * np.cos(2 * t) pnt = gp_Pnt(x, y, z) pts.append(pnt) p_array.SetValue(idx + 1, pnt) api = GeomAPI_PointsToBSpline(p_array) #curve = geomapi.To3d(api.Curve()) for idx, t in enumerate(pts): obj.display.DisplayShape(t) obj.display.DisplayShape(api.Curve()) obj.show_axs_pln(axs, scale=20) obj.show()
def __init__(self): plotocc.__init__(self) self.axs = gp_Ax3() p_array = TColgp_Array1OfPnt(1, 100) for idx, pt in enumerate(np.linspace(-2.0, 2.0, 100)): pnt = gp_Pnt(300 * np.sin(pt), 100 * np.cos(3 * pt), 0) p_array.SetValue(idx + 1, pnt) api = GeomAPI_PointsToBSpline(p_array) self.curv = api.Curve() print(self.curv) api = BRepOffsetAPI_ThruSections() api.SetSmoothing(True) num_list = [ 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 4, 4, 4 ] p1, v1, v2 = gp_Pnt(), gp_Vec(), gp_Vec() for idx, pt in enumerate(np.linspace(0.0, 0.5, len(num_list))): GeomLProp_CurveTool.D2(self.curv, pt, p1, v1, v2) v3 = v1.Crossed(v2) axis = gp_Ax3(p1, vec_to_dir(v1), vec_to_dir(v2)) poly = self.make_PolyWire(num=num_list[idx], radi=20, axs=axis) api.AddWire(poly) self.show_axs_pln(axis, scale=10) self.display.DisplayShape(poly) api.Build() self.display.DisplayShape(api.Shape()) write_step_file(api.Shape(), "./tmp/ThruPipe_Hex.stp") api = BRepOffsetAPI_ThruSections() # api.SetSmoothing(True) p1, v1, v2 = gp_Pnt(), gp_Vec(), gp_Vec() for idx, pt in enumerate(np.linspace(0.55, 1.0, 20)): GeomLProp_CurveTool.D2(self.curv, pt, p1, v1, v2) v3 = v1.Crossed(v2) axis = gp_Ax3(p1, vec_to_dir(v1), vec_to_dir(v2)) shft = 90 * pt poly = self.make_Ellip(rxy=[15, 10], shft=shft, axs=axis) api.AddWire(poly) self.display.DisplayShape(poly) api.Build() self.display.DisplayShape(api.Shape()) write_step_file(api.Shape(), "./tmp/ThruPipe_Ellip.stp")