1 # Copyright (C) 2020-2023 CEA, EDF
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 Test creation of B-spline curve passing through the list of points
27 from salome.shaper import model
30 from ModelAPI import *
31 from ModelHighAPI import *
32 from SketchAPI import *
33 from GeomAlgoAPI import GeomAlgoAPI_ShapeTools as shapetools
35 __updated__ = "2020-06-28"
40 class TestApproximationExternal(unittest.TestCase):
43 self.myDocument = model.moduleDocument()
44 self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
47 for name in POINT_NAMES:
48 proj = self.mySketch.addProjection(model.selection("VERTEX", name), False)
49 self.myPoints.append(SketchAPI_Point(proj.createdFeature()))
50 self.myPointRefs.append(self.myPoints[-1].coordinates())
52 self.myNbPoints = len(self.myPoints)
55 self.myNbPeriodicSplines = 0
60 model.testNbSubFeatures(self.mySketch, "SketchPoint", self.myNbPoints)
61 model.testNbSubFeatures(self.mySketch, "SketchLine", self.myNbLines)
62 model.testNbSubFeatures(self.mySketch, "SketchBSpline", self.myNbSplines)
63 model.testNbSubFeatures(self.mySketch, "SketchBSplinePeriodic", self.myNbPeriodicSplines)
67 self.assertEqual(model.dof(self.mySketch), self.myDOF)
69 def checkPointsOnCurve(self, theCurve, theTolerance = 1.e-3):
70 for p in self.myPoints:
71 shape = p.defaultResult().shape()
72 self.assertTrue(shapetools.minimalDistance(shape, theCurve) < theTolerance)
75 def test_approximation_default(self):
76 """ Test 1. Create B-spline curve approximating a set of points with the default precision
78 self.mySpline = self.mySketch.addApproximation(self.myPointRefs)
79 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
80 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
81 self.myDOF += len(curve.poles()) * 2 - 4
85 self.assertTrue(self.mySpline.feature())
86 self.assertTrue(self.mySpline.feature().error() == "")
87 self.assertTrue(self.mySpline.degree().value() == 4)
88 self.assertTrue(curve.isPeriodic() == False)
89 EXPECTED_LEN = 189.17832318
90 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
91 # check points lying of the created curve
92 self.checkPointsOnCurve(edge)
94 def test_approximation_custom(self):
95 """ Test 2. Create B-spline curve approximating a set of points with the custom precision
98 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, precision = PRECISION)
99 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
100 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
101 self.myDOF += len(curve.poles()) * 2 - 4
102 self.myNbSplines += 1
105 self.assertTrue(self.mySpline.feature())
106 self.assertTrue(self.mySpline.feature().error() == "")
107 self.assertTrue(self.mySpline.degree().value() == 3)
108 self.assertTrue(curve.isPeriodic() == False)
109 EXPECTED_LEN = 166.97400096
110 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
111 # check points lying of the created curve
112 self.checkPointsOnCurve(edge, PRECISION)
114 def test_approximation_periodic_default(self):
115 """ Test 3. Create periodic B-spline curve approximating set of points with the default precision
117 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, periodic = True)
118 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
119 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
120 self.myDOF += len(curve.poles()) * 2
121 self.myNbPeriodicSplines += 1
124 self.assertTrue(self.mySpline.feature())
125 self.assertTrue(self.mySpline.feature().error() == "")
126 self.assertTrue(self.mySpline.degree().value() == 3)
127 self.assertTrue(curve.isPeriodic() == True)
128 EXPECTED_LEN = 390.98013747
129 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
130 # check points lying of the created curve
131 self.checkPointsOnCurve(edge)
133 def test_approximation_periodic_custom(self):
134 """ Test 4. Create periodic B-spline curve approximating set of points with the custom precision
137 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, precision = PRECISION, periodic = True)
138 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
139 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
140 self.myDOF += len(curve.poles()) * 2
141 self.myNbPeriodicSplines += 1
144 self.assertTrue(self.mySpline.feature())
145 self.assertTrue(self.mySpline.feature().error() == "")
146 self.assertTrue(self.mySpline.degree().value() == 3)
147 self.assertTrue(curve.isPeriodic() == True)
148 EXPECTED_LEN = 390.98013747
149 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
150 # check points lying of the created curve
151 self.checkPointsOnCurve(edge, PRECISION)
153 def test_approximation_closed_default(self):
154 """ Test 5. Create closed but not periodic B-spline curve approximating set of points with the default precision
156 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, closed = True)
157 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
158 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
159 self.myDOF += len(curve.poles()) * 2 - 4
160 self.myNbSplines += 1
163 self.assertTrue(self.mySpline.feature())
164 self.assertTrue(self.mySpline.feature().error() == "")
165 self.assertTrue(self.mySpline.degree().value() == 5)
166 self.assertTrue(curve.isPeriodic() == False)
167 self.assertTrue(curve.poles()[0].distance(curve.poles()[-1]) < TOLERANCE)
168 EXPECTED_LEN = 413.18489109
169 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
170 # check points lying of the created curve
171 self.checkPointsOnCurve(edge)
173 def test_approximation_closed_custom(self):
174 """ Test 6. Create closed but not periodic B-spline curve approximating set of points with the custom precision
177 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, precision = PRECISION, closed = True)
178 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
179 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
180 self.myDOF += len(curve.poles()) * 2 - 4
181 self.myNbSplines += 1
184 self.assertTrue(self.mySpline.feature())
185 self.assertTrue(self.mySpline.feature().error() == "")
186 self.assertTrue(self.mySpline.degree().value() == 3)
187 self.assertTrue(curve.isPeriodic() == False)
188 self.assertTrue(curve.poles()[0].distance(curve.poles()[-1]) < TOLERANCE)
189 EXPECTED_LEN = 276.27724118
190 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
191 # check points lying of the created curve
192 self.checkPointsOnCurve(edge, PRECISION)
194 def test_approximation_reorder(self):
195 """ Test 7. Check reordering of points works properly
199 # use low-level API to create feature
200 curveFitting = featureToCompositeFeature(self.mySketch.feature()).addFeature("SketchCurveFitting")
201 curveFitting.string("type").setValue("approximation_type")
202 curveFitting.real("precision").setValue(PRECISION)
203 curveFitting.boolean("periodic").setValue(False)
204 curveFitting.boolean("closed").setValue(False)
205 pointsAttr = curveFitting.refattrlist("points")
206 for ind in [0, 3, 4, 2, 1]:
207 pointsAttr.append(self.myPointRefs[ind])
209 curveFitting.customAction("reorder_points")
212 self.mySpline = SketchAPI_BSpline(model.lastSubFeature(self.mySketch, "SketchBSpline"))
213 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
214 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
215 self.myDOF += len(curve.poles()) * 2 - 4
216 self.myNbSplines += 1
219 self.assertTrue(self.mySpline.feature())
220 self.assertTrue(self.mySpline.feature().error() == "")
221 self.assertTrue(self.mySpline.degree().value() == 3)
222 self.assertTrue(curve.isPeriodic() == False)
223 EXPECTED_LEN = 166.97400096
224 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
225 # check points lying of the created curve
226 self.checkPointsOnCurve(edge, PRECISION)
229 if __name__ == "__main__":
230 # Create a sketch with the external points
232 document = model.moduleDocument()
233 coordinates = [(50., 50.), (70., 70.), (80., 30.), (50., 10.), (10., -30.)]
234 for p in coordinates:
235 point3D = model.addPoint(document, p[0], p[1], 0)
236 POINT_NAMES.append(point3D.name())
240 test_program = unittest.main(exit=False)
241 assert test_program.result.wasSuccessful(), "Test failed"
242 assert model.checkPythonDump()