Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / Test / TestMoveBSpline.py
1 # Copyright (C) 2019-2023  CEA, EDF
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 movement of the B-spline curve
22 """
23
24 import unittest
25 import math
26 from GeomAPI import *
27 from GeomDataAPI import geomDataAPI_Point2DArray
28 from SketchAPI import *
29 from salome.shaper import model
30
31 __updated__ = "2020-01-20"
32
33 class TestMoveBSpline(unittest.TestCase):
34   def setUp(self):
35     model.begin()
36     self.myDocument = model.moduleDocument()
37     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
38     self.myPoles = [GeomAPI_Pnt2d(20., 50.),
39                     GeomAPI_Pnt2d(70., 70.),
40                     GeomAPI_Pnt2d(80., 30.),
41                     GeomAPI_Pnt2d(50., 10.),
42                     GeomAPI_Pnt2d(90., -30.)]
43     self.mySpline = self.mySketch.addSpline(poles = self.myPoles)
44     self.myDOF = len(self.myPoles) * 2
45     model.do()
46     self.checkDOF()
47
48   def tearDown(self):
49     self.checkDOF()
50     model.end()
51
52   def checkDOF(self):
53     self.assertEqual(model.dof(self.mySketch), self.myDOF)
54
55   def checkPointCoordinates(self, thePoint, theCoordinates):
56     aCoord = []
57     if issubclass(type(theCoordinates), GeomAPI_Pnt2d):
58       aCoord = [theCoordinates.x(), theCoordinates.y()]
59     else:
60       aCoord = theCoordinates
61     DIGITS = 7 - math.floor(math.log10(math.hypot(aCoord[0], aCoord[1])))
62     self.assertAlmostEqual(thePoint.x(), aCoord[0], DIGITS)
63     self.assertAlmostEqual(thePoint.y(), aCoord[1], DIGITS)
64
65   def checkPoles(self, theBSpline, theCoordinates):
66     poles = theBSpline.poles()
67     for index, point in zip(range(0, len(theCoordinates)), theCoordinates):
68       self.checkPointCoordinates(poles.pnt(index), point)
69
70   def fixPoint(self, thePoint):
71     self.mySketch.setFixed(thePoint)
72     self.myDOF -= 2
73     model.do()
74     self.checkDOF()
75
76
77   def test_move_free_bspline(self):
78     """ Test 1. Movement of a free B-spline dragging the edge
79     """
80     oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
81     newPosition = GeomAPI_Pnt2d(120., 90.)
82     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
83     model.do()
84
85     # plane is XOY, no need to project oldPosition point
86     dx = newPosition.x() - oldPosition.x()
87     dy = newPosition.y() - oldPosition.y()
88
89     newPoles = []
90     for pole in self.myPoles:
91       newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
92     self.checkPoles(self.mySpline, newPoles)
93
94   def test_move_start_point(self):
95     """ Test 2. Movement of start point of a free B-spline curve
96     """
97     newPoles = self.myPoles
98
99     newPoles[0].setX(newPoles[0].x() - 20.)
100     newPoles[0].setY(newPoles[0].y() + 10.)
101     self.mySketch.move(self.mySpline.startPoint(), newPoles[0])
102     model.do()
103
104     self.checkPoles(self.mySpline, newPoles)
105
106   def test_move_end_point(self):
107     """ Test 3. Movement of end point of a free B-spline curve
108     """
109     newPoles = self.myPoles
110
111     newPoles[-1].setX(newPoles[-1].x() + 20.)
112     newPoles[-1].setY(newPoles[-1].y() + 10.)
113     self.mySketch.move(self.mySpline.endPoint(), newPoles[-1])
114     model.do()
115
116     self.checkPoles(self.mySpline, newPoles)
117
118
119   def test_move_bspline_with_start_point_fixed(self):
120     """ Test 4. Movement of a B-spline dragging the edge when start point is fixed
121     """
122     self.fixPoint(self.mySpline.startPoint())
123     model.do()
124
125     oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
126     newPosition = GeomAPI_Pnt2d(120., 90.)
127     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
128     model.do()
129
130     # plane is XOY, no need to project oldPosition point
131     dx = newPosition.x() - oldPosition.x()
132     dy = newPosition.y() - oldPosition.y()
133
134     newPoles = [self.myPoles[0]]
135     for pole in self.myPoles[1:]:
136       newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
137     self.checkPoles(self.mySpline, newPoles)
138
139   def test_move_start_point_with_start_point_fixed(self):
140     """ Test 5. Movement of start point of a free B-spline curve has no result if the first pole is fixed
141     """
142     self.fixPoint(self.mySpline.startPoint())
143     model.do()
144
145     self.mySketch.move(self.mySpline.startPoint(), self.myPoles[0].x() - 10., self.myPoles[0].y() + 10.)
146     model.do()
147
148     self.checkPoles(self.mySpline, self.myPoles)
149
150   def test_move_end_point_with_start_point_fixed(self):
151     """ Test 6. Movement of end point of a free B-spline curve when start point is fixed
152     """
153     self.fixPoint(self.mySpline.startPoint())
154     model.do()
155
156     newPoles = self.myPoles
157
158     newPoles[-1].setX(newPoles[-1].x() + 20.)
159     newPoles[-1].setY(newPoles[-1].y() + 10.)
160     self.mySketch.move(self.mySpline.endPoint(), newPoles[-1])
161     model.do()
162
163     self.checkPoles(self.mySpline, newPoles)
164
165
166   def test_move_bspline_with_end_point_fixed(self):
167     """ Test 7. Movement of a B-spline dragging the edge when end point is fixed
168     """
169     self.fixPoint(self.mySpline.endPoint())
170     model.do()
171
172     oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
173     newPosition = GeomAPI_Pnt2d(120., 90.)
174     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
175     model.do()
176
177     # plane is XOY, no need to project oldPosition point
178     dx = newPosition.x() - oldPosition.x()
179     dy = newPosition.y() - oldPosition.y()
180
181     newPoles = []
182     for pole in self.myPoles[:-1]:
183       newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
184     newPoles.append(self.myPoles[-1])
185     self.checkPoles(self.mySpline, newPoles)
186
187   def test_move_start_point_with_end_point_fixed(self):
188     """ Test 8. Movement of start point of a free B-spline curve when end point is fixed
189     """
190     self.fixPoint(self.mySpline.endPoint())
191     model.do()
192
193     newPoles = self.myPoles
194
195     newPoles[0].setX(newPoles[0].x() + 20.)
196     newPoles[0].setY(newPoles[0].y() + 10.)
197     self.mySketch.move(self.mySpline.startPoint(), self.myPoles[0])
198     model.do()
199
200     self.checkPoles(self.mySpline, self.myPoles)
201
202   def test_move_end_point_with_end_point_fixed(self):
203     """ Test 9. Movement of end point of a free B-spline curve has no result if the last pole is fixed
204     """
205     self.fixPoint(self.mySpline.endPoint())
206     model.do()
207
208     self.mySketch.move(self.mySpline.endPoint(), self.myPoles[-1].x() + 10., self.myPoles[-1].y() + 10.)
209     model.do()
210
211     self.checkPoles(self.mySpline, self.myPoles)
212
213
214   def test_move_fixed_bspline(self):
215     """ Test 10. Movement of a fully fixed B-spline
216     """
217     self.mySketch.setFixed(self.mySpline.defaultResult())
218     self.myDOF = 0
219     model.do()
220
221     newPosition = GeomAPI_Pnt2d(120., 90.)
222     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
223     model.do()
224
225     self.checkPoles(self.mySpline, self.myPoles)
226
227   def test_move_start_point_of_fixed_bspline(self):
228     """ Test 11. Movement of start point of a fully fixed B-spline curve
229     """
230     self.mySketch.setFixed(self.mySpline.defaultResult())
231     self.myDOF = 0
232     model.do()
233
234     self.mySketch.move(self.mySpline.startPoint(), self.myPoles[0].x() + 10., self.myPoles[0].y() + 10.)
235     model.do()
236
237     self.checkPoles(self.mySpline, self.myPoles)
238
239   def test_move_end_point_of_fixed_bspline(self):
240     """ Test 12. Movement of end point of a fully fixed B-spline curve
241     """
242     self.mySketch.setFixed(self.mySpline.defaultResult())
243     self.myDOF = 0
244     model.do()
245
246     self.mySketch.move(self.mySpline.endPoint(), self.myPoles[-1].x() + 10., self.myPoles[-1].y() + 10.)
247     model.do()
248
249     self.checkPoles(self.mySpline, self.myPoles)
250
251
252   def test_move_bspline_with_fixed_pole(self):
253     """ Test 13. Movement of a B-spline curve with fixed pole
254     """
255     [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
256     model.do()
257
258     self.fixPoint(Point_2.defaultResult())
259     model.do()
260
261     oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
262     newPosition = GeomAPI_Pnt2d(120., 90.)
263     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
264     model.do()
265
266     # plane is XOY, no need to project oldPosition point
267     dx = newPosition.x() - oldPosition.x()
268     dy = newPosition.y() - oldPosition.y()
269
270     newPoles = []
271     for pole in self.myPoles:
272       newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
273     newPoles[1].setX(newPoles[1].x() - dx)
274     newPoles[1].setY(newPoles[1].y() - dy)
275     self.checkPoles(self.mySpline, newPoles)
276
277   def test_move_start_point_with_fixed_pole(self):
278     """ Test 14. Movement of start point of a B-spline curve with fixed pole
279     """
280     [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
281     model.do()
282
283     self.fixPoint(Point_2.defaultResult())
284     model.do()
285
286     newPoles = self.myPoles
287
288     newPoles[0].setX(newPoles[0].x() + 20.)
289     newPoles[0].setY(newPoles[0].y() + 10.)
290     self.mySketch.move(self.mySpline.startPoint(), newPoles[0])
291     model.do()
292
293     self.checkPoles(self.mySpline, newPoles)
294
295   def test_move_end_point_with_fixed_pole(self):
296     """ Test 15. Movement of end point of a B-spline curve with fixed pole
297     """
298     [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
299     model.do()
300
301     self.fixPoint(Point_2.defaultResult())
302     model.do()
303
304     newPoles = self.myPoles
305
306     newPoles[-1].setX(newPoles[-1].x() + 20.)
307     newPoles[-1].setY(newPoles[-1].y() + 10.)
308     self.mySketch.move(self.mySpline.endPoint(), newPoles[-1])
309     model.do()
310
311     self.checkPoles(self.mySpline, newPoles)
312
313
314   def test_move_bspline_with_fixed_segment(self):
315     """ Test 16. Movement of a B-spline curve with fixed control segment
316     """
317     [Line_1, Line_2, Line_3, Line_4] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
318     model.do()
319
320     self.mySketch.setFixed(Line_1.defaultResult())
321     self.myDOF -= 4
322     model.do()
323
324     oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
325     newPosition = GeomAPI_Pnt2d(120., 90.)
326     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
327     model.do()
328
329     # plane is XOY, no need to project oldPosition point
330     dx = newPosition.x() - oldPosition.x()
331     dy = newPosition.y() - oldPosition.y()
332
333     newPoles = [self.myPoles[0], self.myPoles[1]]
334     for pole in self.myPoles[2:]:
335       newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
336     self.checkPoles(self.mySpline, newPoles)
337
338   def test_move_start_point_with_fixed_segment(self):
339     """ Test 17. Movement of start point of a B-spline curve with fixed control segment
340     """
341     [Line_1, Line_2, Line_3, Line_4] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
342     model.do()
343
344     self.mySketch.setFixed(Line_1.defaultResult())
345     self.myDOF -= 4
346     model.do()
347
348     self.mySketch.move(self.mySpline.startPoint(), self.myPoles[0].x() + 10., self.myPoles[0].y() - 20.)
349     model.do()
350
351     self.checkPoles(self.mySpline, self.myPoles)
352
353   def test_move_end_point_with_fixed_segment(self):
354     """ Test 18. Movement of end point of a B-spline curve with fixed control segment
355     """
356     [Line_1, Line_2, Line_3, Line_4] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
357     model.do()
358
359     self.mySketch.setFixed(Line_1.defaultResult())
360     self.myDOF -= 4
361     model.do()
362
363     newPoles = self.myPoles
364
365     newPoles[-1].setX(newPoles[-1].x() + 20.)
366     newPoles[-1].setY(newPoles[-1].y() + 10.)
367     self.mySketch.move(self.mySpline.endPoint(), newPoles[-1])
368     model.do()
369
370     self.checkPoles(self.mySpline, newPoles)
371
372
373   def test_move_pole_of_free_bspline(self):
374     """ Test 19. Movement of a pole of a B-spline curve
375     """
376     [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
377     [Line_1, Line_2, Line_3, Line_4] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
378     model.do()
379
380     newPoles = self.myPoles
381
382     newPoles[2].setX(newPoles[2].x() + 20.)
383     newPoles[2].setY(newPoles[2].y() + 20.)
384     self.mySketch.move(SketchAPI_Point(Point_3).coordinates(), newPoles[2])
385     model.do()
386
387     self.checkPoles(self.mySpline, newPoles)
388
389   def test_move_segment_of_free_bspline(self):
390     """ Test 20. Movement of a control segment of a B-spline curve
391     """
392     [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
393     [Line_1, Line_2, Line_3, Line_4] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
394     model.do()
395
396     oldPosition = GeomAPI_Pnt2d(0.5 * (self.myPoles[2].x() + self.myPoles[3].x()),
397                                 0.5 * (self.myPoles[2].y() + self.myPoles[3].y()))
398     newPosition = GeomAPI_Pnt2d(120., 90.)
399     self.mySketch.move(SketchAPI_Line(Line_3).defaultResult(), newPosition)
400     model.do()
401
402     dx = newPosition.x() - oldPosition.x()
403     dy = newPosition.y() - oldPosition.y()
404
405     newPoles = self.myPoles
406     newPoles[2].setX(newPoles[2].x() + dx)
407     newPoles[2].setY(newPoles[2].y() + dy)
408     newPoles[3].setX(newPoles[3].x() + dx)
409     newPoles[3].setY(newPoles[3].y() + dy)
410
411     self.checkPoles(self.mySpline, newPoles)
412
413
414
415 if __name__ == "__main__":
416     test_program = unittest.main(exit=False)
417     assert test_program.result.wasSuccessful(), "Test failed"
418     assert model.checkPythonDump()