Salome HOME
524dcd1a0835c6a528281a40db27d0dd6478a703
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateBSplinePeriodic.py
1 # Copyright (C) 2020-2022  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 periodic 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-24"
31
32 class TestBSplinePeriodic(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, "SketchBSplinePeriodic", 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, periodic = True)
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, periodic = True)
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, periodic = True)
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], periodic = True)
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 = 3
110     self.myPolesCoordinates = [(-10, 0), (-20, 20), (0, 10), (20, 20),
111                                (10, 0), (20, -20), (0, -10), (-20, -20)
112                               ]
113     self.mySpline = self.mySketch.addSpline(degree = self.myDegree,
114                                             poles = self.myPolesCoordinates,
115                                             weights = [1, 1, 1, 1, 1, 1, 1, 1],
116                                             knots = [0, 1, 2, 3, 4, 5, 6, 7, 8],
117                                             multiplicities = [1, 1, 1, 1, 1, 1, 1, 1, 1],
118                                             periodic = True)
119     self.myDOF += len(self.myPolesCoordinates) * 2
120     self.myNbSplines += 1
121     model.do()
122
123     assert(self.mySpline.feature())
124     assert(self.mySpline.feature().error() == "")
125     assert(self.mySpline.degree().value() == self.myDegree)
126
127   def test_bspline_linear(self):
128     """ Test 6. Create B-spline curve by 2 poles
129     """
130     self.myDegree = 1
131     self.mySpline = self.mySketch.addSpline(poles = self.myPoles[:2], periodic = True)
132     self.myDOF += 4
133     self.myNbSplines += 1
134     model.do()
135
136     assert(self.mySpline.feature())
137     assert(self.mySpline.feature().error() == "")
138     assert(self.mySpline.degree().value() == self.myDegree)
139
140   def test_bspline_parabola(self):
141     """ Test 7. Create B-spline curve by 3 poles
142     """
143     self.myDegree = 2
144     self.mySpline = self.mySketch.addSpline(poles = self.myPoles[:3], periodic = True)
145     self.myDOF += 6
146     self.myNbSplines += 1
147     model.do()
148
149     assert(self.mySpline.feature())
150     assert(self.mySpline.feature().error() == "")
151     assert(self.mySpline.degree().value() == self.myDegree)
152
153   def test_bspline_with_poles(self):
154     """ Test 8. Create B-spline curve and points coincident with its poles
155     """
156     self.mySpline = self.mySketch.addSpline(poles = self.myPoles, periodic = True)
157     self.mySpline.controlPoles(regular = [0, 2], auxiliary = [1, 3])
158     self.myDOF += len(self.myPoles) * 2
159     self.myNbSplines += 1
160     self.myNbPoints += 4
161     model.do()
162
163     assert(self.mySpline.feature())
164     assert(self.mySpline.feature().error() == "")
165     assert(self.mySpline.degree().value() == self.myDegree)
166
167   def test_bspline_with_polygon(self):
168     """ Test 9. Create B-spline curve and its control polygon
169     """
170     self.mySpline = self.mySketch.addSpline(poles = self.myPoles, periodic = True)
171     self.mySpline.controlPolygon(regular = [0, 2], auxiliary = [1, 3])
172     self.myDOF += len(self.myPoles) * 2
173     self.myNbSplines += 1
174     self.myNbLines += 4
175     model.do()
176
177     assert(self.mySpline.feature())
178     assert(self.mySpline.feature().error() == "")
179     assert(self.mySpline.degree().value() == self.myDegree)
180
181 if __name__ == "__main__":
182     test_program = unittest.main(exit=False)
183     assert test_program.result.wasSuccessful(), "Test failed"
184     assert model.checkPythonDump()