Salome HOME
fa16be628f81d723dd3bd373bfd5708c5d403457
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateBSpline.py
1 # Copyright (C) 2020-2021  CEA/DEN, EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21     Test creation of B-spline curve
22 """
23
24 import unittest
25 from salome.shaper import model
26
27 from GeomAPI import *
28 from SketchAPI import *
29
30 __updated__ = "2020-01-17"
31
32 class TestBSpline(unittest.TestCase):
33   def setUp(self):
34     model.begin()
35     self.myDocument = model.moduleDocument()
36     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
37     self.myPoles = [GeomAPI_Pnt2d(50., 50.), GeomAPI_Pnt2d(70., 70.), GeomAPI_Pnt2d(80., 30.), GeomAPI_Pnt2d(50., 10.), GeomAPI_Pnt2d(10., -30.)]
38     self.myPolesCoordinates = [(50., 50.), (70., 70.), (80., 30.), (50., 10.), (10., -30.)]
39     self.myDegree = 3;
40     self.myDOF = 0
41     self.myNbPoints = 0
42     self.myNbLines = 0
43     self.myNbSplines = 0
44
45   def tearDown(self):
46     self.checkDOF()
47     model.end()
48     model.testNbSubFeatures(self.mySketch, "SketchPoint", self.myNbPoints)
49     model.testNbSubFeatures(self.mySketch, "SketchLine", self.myNbLines)
50     model.testNbSubFeatures(self.mySketch, "SketchBSpline", self.myNbSplines)
51
52
53   def checkDOF(self):
54     self.assertEqual(model.dof(self.mySketch), self.myDOF)
55
56
57   def test_bspline_by_coordinates(self):
58     """ Test 1. Create B-spline curve by coordinates of its poles
59     """
60     self.mySpline = self.mySketch.addSpline(poles = self.myPolesCoordinates)
61     self.myDOF += len(self.myPolesCoordinates) * 2
62     self.myNbSplines += 1
63     model.do()
64
65     assert(self.mySpline.feature())
66     assert(self.mySpline.feature().error() == "")
67     assert(self.mySpline.degree().value() == self.myDegree)
68
69   def test_bspline_by_poles(self):
70     """ Test 2. Create B-spline curve by poles
71     """
72     self.mySpline = self.mySketch.addSpline(poles = self.myPoles)
73     self.myDOF += len(self.myPoles) * 2
74     self.myNbSplines += 1
75     model.do()
76
77     assert(self.mySpline.feature())
78     assert(self.mySpline.feature().error() == "")
79     assert(self.mySpline.degree().value() == self.myDegree)
80
81   def test_bspline_by_degree_and_poles(self):
82     """ Test 3. Create B-spline curve by poles and degree
83     """
84     self.myDegree = 4
85     self.mySpline = self.mySketch.addSpline(degree = self.myDegree, poles = self.myPoles)
86     self.myDOF += len(self.myPoles) * 2
87     self.myNbSplines += 1
88     model.do()
89
90     assert(self.mySpline.feature())
91     assert(self.mySpline.feature().error() == "")
92     assert(self.mySpline.degree().value() == self.myDegree)
93
94   def test_bspline_by_poles_and_weights(self):
95     """ Test 4. Create B-spline curve by poles and weights
96     """
97     self.mySpline = self.mySketch.addSpline(poles = self.myPoles, weights = [1, 2, 3, 2, 1])
98     self.myDOF += len(self.myPoles) * 2
99     self.myNbSplines += 1
100     model.do()
101
102     assert(self.mySpline.feature())
103     assert(self.mySpline.feature().error() == "")
104     assert(self.mySpline.degree().value() == self.myDegree)
105
106   def test_bspline_by_parametric(self):
107     """ Test 5. Create B-spline curve by whole set of parameters
108     """
109     self.myDegree = 5
110     self.myPolesCoordinates = [(-79.8578274581199, 75.5284518447357),
111                                (-64.6205376770376, 62.7428476092882),
112                                (-49.3832478959552, 49.9572433738407),
113                                (-34.1459581148729, 37.1716391383932),
114                                (-18.9086683337906, 24.3860349029457),
115                                (-3.55842111132817, 11.5056481200973),
116                                (-3.37993197286247, 11.42995541724),
117                                (-3.1778022626919, 11.4565662984205),
118                                (-3.02498570721059, 11.575876223351),
119                                (8.46852511720001, 27.9903107977019),
120                                (19.8774589601206, 44.2839569245217),
121                                (31.2863928030413, 60.5776030513415),
122                                (42.6953266459619, 76.8712491781612),
123                                (54.1042604888826, 93.164895304981)
124                               ]
125     self.mySpline = self.mySketch.addSpline(degree = self.myDegree,
126                                             poles = self.myPolesCoordinates,
127                                             weights = [1, 1, 1, 1, 1, 1, 0.957903314642061, 0.95790331464206, 1, 1, 1, 1, 1, 1],
128                                             knots = [-494.543457494654, 500, 507.372773368102, 1501.91623086297],
129                                             multiplicities = [6, 4, 4, 6])
130     self.myDOF += len(self.myPolesCoordinates) * 2
131     self.myNbSplines += 1
132     model.do()
133
134     assert(self.mySpline.feature())
135     assert(self.mySpline.feature().error() == "")
136     assert(self.mySpline.degree().value() == self.myDegree)
137
138   def test_bspline_linear(self):
139     """ Test 6. Create B-spline curve by 2 poles
140     """
141     self.myDegree = 1
142     self.mySpline = self.mySketch.addSpline(poles = self.myPoles[:2])
143     self.myDOF += 4
144     self.myNbSplines += 1
145     model.do()
146
147     assert(self.mySpline.feature())
148     assert(self.mySpline.feature().error() == "")
149     assert(self.mySpline.degree().value() == self.myDegree)
150
151   def test_bspline_parabola(self):
152     """ Test 7. Create B-spline curve by 3 poles
153     """
154     self.myDegree = 2
155     self.mySpline = self.mySketch.addSpline(poles = self.myPoles[:3])
156     self.myDOF += 6
157     self.myNbSplines += 1
158     model.do()
159
160     assert(self.mySpline.feature())
161     assert(self.mySpline.feature().error() == "")
162     assert(self.mySpline.degree().value() == self.myDegree)
163
164   def test_bspline_with_poles(self):
165     """ Test 8. Create B-spline curve and points coincident with its poles
166     """
167     self.mySpline = self.mySketch.addSpline(poles = self.myPoles)
168     self.mySpline.controlPoles(regular = [0, 2], auxiliary = [1, 3])
169     self.myDOF += len(self.myPoles) * 2
170     self.myNbSplines += 1
171     self.myNbPoints += 4
172     model.do()
173
174     assert(self.mySpline.feature())
175     assert(self.mySpline.feature().error() == "")
176     assert(self.mySpline.degree().value() == self.myDegree)
177
178   def test_bspline_with_polygon(self):
179     """ Test 9. Create B-spline curve and its control polygon
180     """
181     self.mySpline = self.mySketch.addSpline(poles = self.myPoles)
182     self.mySpline.controlPolygon(regular = [0, 2], auxiliary = [1, 3])
183     self.myDOF += len(self.myPoles) * 2
184     self.myNbSplines += 1
185     self.myNbLines += 4
186     model.do()
187
188     assert(self.mySpline.feature())
189     assert(self.mySpline.feature().error() == "")
190     assert(self.mySpline.degree().value() == self.myDegree)
191
192 if __name__ == "__main__":
193     test_program = unittest.main(exit=False)
194     assert test_program.result.wasSuccessful(), "Test failed"
195     assert model.checkPythonDump()