Skip to content

Commit 86fe2f3

Browse files
First version of Face.makeSplineApprox
1 parent 5483114 commit 86fe2f3

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

cadquery/occ_impl/shapes.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import OCP.TopAbs as ta # Tolopolgy type enum
2121
import OCP.GeomAbs as ga # Geometry type enum
2222

23+
from OCP.Precision import Precision
24+
2325
from OCP.gp import (
2426
gp_Vec,
2527
gp_Pnt,
@@ -36,7 +38,7 @@
3638
)
3739

3840
# Array of points (used for B-spline construction):
39-
from OCP.TColgp import TColgp_HArray1OfPnt
41+
from OCP.TColgp import TColgp_HArray1OfPnt, TColgp_HArray2OfPnt
4042

4143
# Array of vectors (used for B-spline interpolation):
4244
from OCP.TColgp import TColgp_Array1OfVec
@@ -113,6 +115,7 @@
113115
GeomAPI_Interpolate,
114116
GeomAPI_ProjectPointOnSurf,
115117
GeomAPI_PointsToBSpline,
118+
GeomAPI_PointsToBSplineSurface,
116119
)
117120

118121
from OCP.BRepFill import BRepFill
@@ -1554,9 +1557,7 @@ def makeSplineApprox(
15541557
Approximate a spline through the provided points.
15551558
15561559
:param listOfVector: a list of Vectors that represent the points
1557-
:param tol: tolerance of the algorithm (consult OCC documentation). Used to check that the
1558-
specified points are not too close to each other, and that tangent vectors are not too
1559-
short. (In either case interpolation may fail.)
1560+
:param tol: tolerance of the algorithm (consult OCC documentation).
15601561
:param smoothing: optional tuple of 3 weigths use for variational smoothing (default: None)
15611562
:param minDeg: minimum spline degree. Enforced only when smothing is None (default: 1)
15621563
:param maxDeg: maximum spline degree (default: 6)
@@ -2067,6 +2068,47 @@ def makeFromWires(
20672068

20682069
return cls(face).fix()
20692070

2071+
@classmethod
2072+
def makeSplineApprox(
2073+
cls: Type["Face"],
2074+
points: List[List[Vector]],
2075+
tol: float = 1e-2,
2076+
smoothing: Optional[Tuple[float, float, float]] = None,
2077+
minDeg: int = 1,
2078+
maxDeg: int = 3,
2079+
) -> "Face":
2080+
"""
2081+
Approximate a spline surface through the provided points.
2082+
2083+
:param points: a 2D list of Vectors that represent the points
2084+
:param tol: tolerance of the algorithm (consult OCC documentation).
2085+
:param smoothing: optional tuple of 3 weigths use for variational smoothing (default: None)
2086+
:param minDeg: minimum spline degree. Enforced only when smothing is None (default: 1)
2087+
:param maxDeg: maximum spline degree (default: 6)
2088+
:return: an Face
2089+
"""
2090+
points_ = TColgp_HArray2OfPnt(1, len(points), 1, len(points[0]))
2091+
2092+
for i, vi in enumerate(points):
2093+
for j, v in enumerate(vi):
2094+
points_.SetValue(i + 1, j + 1, v.toPnt())
2095+
2096+
if smoothing:
2097+
spline_builder = GeomAPI_PointsToBSplineSurface(
2098+
points_, *smoothing, DegMax=maxDeg, Tol3D=tol
2099+
)
2100+
else:
2101+
spline_builder = GeomAPI_PointsToBSplineSurface(
2102+
points_, DegMin=minDeg, DegMax=maxDeg, Tol3D=tol
2103+
)
2104+
2105+
if not spline_builder.IsDone():
2106+
raise ValueError("B-spline approximation failed")
2107+
2108+
spline_geom = spline_builder.Surface()
2109+
2110+
return cls(BRepBuilderAPI_MakeFace(spline_geom, Precision.Confusion_s()).Face())
2111+
20702112
def fillet2D(self, radius: float, vertices: Iterable[Vertex]) -> "Face":
20712113
"""
20722114
Apply 2D fillet to a face

0 commit comments

Comments
 (0)