1 # Copyright (C) 2020-2021 CEA/DEN, EDF R&D
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"
39 class TestApproximation(unittest.TestCase):
42 self.myDocument = model.moduleDocument()
43 self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
44 coordinates = [(50., 50.), (70., 70.), (80., 30.), (50., 10.), (10., -30.)]
48 self.myPoints.append(self.mySketch.addPoint(p[0], p[1]))
49 self.myPointRefs.append(self.myPoints[-1].coordinates())
51 self.myNbPoints = len(self.myPoints)
54 self.myNbPeriodicSplines = 0
59 model.testNbSubFeatures(self.mySketch, "SketchPoint", self.myNbPoints)
60 model.testNbSubFeatures(self.mySketch, "SketchLine", self.myNbLines)
61 model.testNbSubFeatures(self.mySketch, "SketchBSpline", self.myNbSplines)
62 model.testNbSubFeatures(self.mySketch, "SketchBSplinePeriodic", self.myNbPeriodicSplines)
66 self.assertEqual(model.dof(self.mySketch), self.myDOF)
68 def checkPointsOnCurve(self, theCurve, theTolerance = 1.e-3):
69 for p in self.myPoints:
70 shape = p.defaultResult().shape()
71 self.assertTrue(shapetools.minimalDistance(shape, theCurve) < theTolerance)
74 def test_approximation_default(self):
75 """ Test 1. Create B-spline curve approximating a set of points with the default precision
77 self.mySpline = self.mySketch.addApproximation(self.myPointRefs)
78 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
79 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
80 self.myDOF += len(curve.poles()) * 2 - 4
84 self.assertTrue(self.mySpline.feature())
85 self.assertTrue(self.mySpline.feature().error() == "")
86 self.assertTrue(self.mySpline.degree().value() == 4)
87 self.assertTrue(curve.isPeriodic() == False)
88 EXPECTED_LEN = 189.17832318
89 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
90 # check points lying of the created curve
91 self.checkPointsOnCurve(edge)
93 def test_approximation_custom(self):
94 """ Test 2. Create B-spline curve approximating a set of points with the custom precision
97 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, precision = PRECISION)
98 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
99 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
100 self.myDOF += len(curve.poles()) * 2 - 4
101 self.myNbSplines += 1
104 self.assertTrue(self.mySpline.feature())
105 self.assertTrue(self.mySpline.feature().error() == "")
106 self.assertTrue(self.mySpline.degree().value() == 3)
107 self.assertTrue(curve.isPeriodic() == False)
108 EXPECTED_LEN = 166.97400096
109 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
110 # check points lying of the created curve
111 self.checkPointsOnCurve(edge, PRECISION)
113 def test_approximation_periodic_default(self):
114 """ Test 3. Create periodic B-spline curve approximating set of points with the default precision
116 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, periodic = True)
117 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
118 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
119 self.myDOF += len(curve.poles()) * 2
120 self.myNbPeriodicSplines += 1
123 self.assertTrue(self.mySpline.feature())
124 self.assertTrue(self.mySpline.feature().error() == "")
125 self.assertTrue(self.mySpline.degree().value() == 3)
126 self.assertTrue(curve.isPeriodic() == True)
127 EXPECTED_LEN = 390.98013747
128 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
129 # check points lying of the created curve
130 self.checkPointsOnCurve(edge)
132 def test_approximation_periodic_custom(self):
133 """ Test 4. Create periodic B-spline curve approximating set of points with the custom precision
136 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, precision = PRECISION, periodic = True)
137 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
138 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
139 self.myDOF += len(curve.poles()) * 2
140 self.myNbPeriodicSplines += 1
143 self.assertTrue(self.mySpline.feature())
144 self.assertTrue(self.mySpline.feature().error() == "")
145 self.assertTrue(self.mySpline.degree().value() == 3)
146 self.assertTrue(curve.isPeriodic() == True)
147 EXPECTED_LEN = 390.98013747
148 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
149 # check points lying of the created curve
150 self.checkPointsOnCurve(edge, PRECISION)
152 def test_approximation_closed_default(self):
153 """ Test 5. Create closed but not periodic B-spline curve approximating set of points with the default precision
155 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, closed = True)
156 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
157 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
158 self.myDOF += len(curve.poles()) * 2 - 4
159 self.myNbSplines += 1
162 self.assertTrue(self.mySpline.feature())
163 self.assertTrue(self.mySpline.feature().error() == "")
164 self.assertTrue(self.mySpline.degree().value() == 5)
165 self.assertTrue(curve.isPeriodic() == False)
166 self.assertTrue(curve.poles()[0].distance(curve.poles()[-1]) < TOLERANCE)
167 EXPECTED_LEN = 413.18489109
168 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
169 # check points lying of the created curve
170 self.checkPointsOnCurve(edge)
172 def test_approximation_closed_custom(self):
173 """ Test 6. Create closed but not periodic B-spline curve approximating set of points with the custom precision
176 self.mySpline = self.mySketch.addApproximation(self.myPointRefs, precision = PRECISION, closed = True)
177 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
178 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
179 self.myDOF += len(curve.poles()) * 2 - 4
180 self.myNbSplines += 1
183 self.assertTrue(self.mySpline.feature())
184 self.assertTrue(self.mySpline.feature().error() == "")
185 self.assertTrue(self.mySpline.degree().value() == 3)
186 self.assertTrue(curve.isPeriodic() == False)
187 self.assertTrue(curve.poles()[0].distance(curve.poles()[-1]) < TOLERANCE)
188 EXPECTED_LEN = 276.27724118
189 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
190 # check points lying of the created curve
191 self.checkPointsOnCurve(edge, PRECISION)
193 def test_approximation_reorder(self):
194 """ Test 7. Check reordering of points works properly
198 # use low-level API to create feature
199 curveFitting = featureToCompositeFeature(self.mySketch.feature()).addFeature("SketchCurveFitting")
200 curveFitting.string("type").setValue("approximation_type")
201 curveFitting.real("precision").setValue(PRECISION)
202 curveFitting.boolean("periodic").setValue(False)
203 curveFitting.boolean("closed").setValue(False)
204 pointsAttr = curveFitting.refattrlist("points")
205 for ind in [0, 3, 4, 2, 1]:
206 pointsAttr.append(self.myPointRefs[ind])
208 curveFitting.customAction("reorder_points")
211 self.mySpline = SketchAPI_BSpline(model.lastSubFeature(self.mySketch, "SketchBSpline"))
212 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
213 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
214 self.myDOF += len(curve.poles()) * 2 - 4
215 self.myNbSplines += 1
218 self.assertTrue(self.mySpline.feature())
219 self.assertTrue(self.mySpline.feature().error() == "")
220 self.assertTrue(self.mySpline.degree().value() == 3)
221 self.assertTrue(curve.isPeriodic() == False)
222 EXPECTED_LEN = 166.97400096
223 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
224 # check points lying of the created curve
225 self.checkPointsOnCurve(edge, PRECISION)
228 if __name__ == "__main__":
229 test_program = unittest.main(exit=False)
230 assert test_program.result.wasSuccessful(), "Test failed"
231 assert model.checkPythonDump()