1 # Copyright (C) 2020-2022 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-27"
39 class TestInterpolation(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):
69 for p in self.myPoints:
70 shape = p.defaultResult().shape()
71 self.assertTrue(shapetools.minimalDistance(shape, theCurve) < TOLERANCE)
74 def test_interpolation(self):
75 """ Test 1. Create B-spline curve by set of points
77 self.mySpline = self.mySketch.addInterpolation(self.myPointRefs)
78 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
79 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
80 self.myDOF += len(curve.poles()) * 2 - len(self.myPointRefs) - 2
84 self.assertTrue(self.mySpline.feature())
85 self.assertTrue(self.mySpline.feature().error() == "")
86 self.assertTrue(self.mySpline.degree().value() == 3)
87 self.assertTrue(curve.isPeriodic() == False)
88 EXPECTED_LEN = 172.237458
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_interpolation_periodic(self):
94 """ Test 2. Create periodic B-spline curve by set of points
96 self.mySpline = self.mySketch.addInterpolation(self.myPointRefs, periodic = True)
97 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
98 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
99 self.myDOF += len(curve.poles()) * 2 - len(self.myPointRefs)
100 self.myNbPeriodicSplines += 1
103 self.assertTrue(self.mySpline.feature())
104 self.assertTrue(self.mySpline.feature().error() == "")
105 self.assertTrue(self.mySpline.degree().value() == 3)
106 self.assertTrue(curve.isPeriodic() == True)
107 EXPECTED_LEN = 262.60929483
108 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
109 # check points lying of the created curve
110 self.checkPointsOnCurve(edge)
112 def test_interpolation_closed(self):
113 """ Test 3. Create closed but not periodic B-spline curve by set of points
115 self.mySpline = self.mySketch.addInterpolation(self.myPointRefs, closed = True)
116 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
117 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
118 self.myDOF += len(curve.poles()) * 2 - len(self.myPointRefs) - 3
119 self.myNbSplines += 1
122 self.assertTrue(self.mySpline.feature())
123 self.assertTrue(self.mySpline.feature().error() == "")
124 self.assertTrue(self.mySpline.degree().value() == 3)
125 self.assertTrue(curve.isPeriodic() == False)
126 self.assertTrue(curve.poles()[0].distance(curve.poles()[-1]) < TOLERANCE)
127 EXPECTED_LEN = 274.25674928
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_interpolation_reorder(self):
133 """ Test 4. Check reordering of points works properly
136 # use low-level API to create feature
137 curveFitting = featureToCompositeFeature(self.mySketch.feature()).addFeature("SketchCurveFitting")
138 curveFitting.string("type").setValue("interpolation_type")
139 curveFitting.boolean("periodic").setValue(False)
140 curveFitting.boolean("closed").setValue(False)
141 pointsAttr = curveFitting.refattrlist("points")
142 for ind in [0, 3, 4, 2, 1]:
143 pointsAttr.append(self.myPointRefs[ind])
145 curveFitting.customAction("reorder_points")
148 self.mySpline = SketchAPI_BSpline(model.lastSubFeature(self.mySketch, "SketchBSpline"))
149 edge = GeomAPI_Edge(self.mySpline.defaultResult().shape())
150 curve = GeomAPI_BSpline(GeomAPI_Curve(edge))
151 self.myDOF += len(curve.poles()) * 2 - len(self.myPointRefs) - 2
152 self.myNbSplines += 1
155 self.assertTrue(self.mySpline.feature())
156 self.assertTrue(self.mySpline.feature().error() == "")
157 self.assertTrue(self.mySpline.degree().value() == 3)
158 self.assertTrue(curve.isPeriodic() == False)
159 EXPECTED_LEN = 172.237458
160 self.assertTrue(math.fabs(edge.length() - EXPECTED_LEN) < TOLERANCE, "Curve length: {}; expected: {}".format(edge.length(), EXPECTED_LEN))
161 # check points lying of the created curve
162 self.checkPointsOnCurve(edge)
165 if __name__ == "__main__":
166 test_program = unittest.main(exit=False)
167 assert test_program.result.wasSuccessful(), "Test failed"
168 assert model.checkPythonDump()