1 # Copyright (C) 2019-2020 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 movement of the B-spline curve
27 from GeomDataAPI import geomDataAPI_Point2DArray
28 from SketchAPI import *
29 from salome.shaper import model
31 __updated__ = "2020-01-20"
33 class TestMoveBSpline(unittest.TestCase):
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
53 self.assertEqual(model.dof(self.mySketch), self.myDOF)
55 def checkPointCoordinates(self, thePoint, theCoordinates):
57 if issubclass(type(theCoordinates), GeomAPI_Pnt2d):
58 aCoord = [theCoordinates.x(), theCoordinates.y()]
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)
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)
70 def fixPoint(self, thePoint):
71 self.mySketch.setFixed(thePoint)
77 def test_move_free_bspline(self):
78 """ Test 1. Movement of a free B-spline dragging the edge
80 oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
81 newPosition = GeomAPI_Pnt2d(120., 90.)
82 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
85 # plane is XOY, no need to project oldPosition point
86 dx = newPosition.x() - oldPosition.x()
87 dy = newPosition.y() - oldPosition.y()
90 for pole in self.myPoles:
91 newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
92 self.checkPoles(self.mySpline, newPoles)
94 def test_move_start_point(self):
95 """ Test 2. Movement of start point of a free B-spline curve
97 newPoles = self.myPoles
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])
104 self.checkPoles(self.mySpline, newPoles)
106 def test_move_end_point(self):
107 """ Test 3. Movement of end point of a free B-spline curve
109 newPoles = self.myPoles
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])
116 self.checkPoles(self.mySpline, newPoles)
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
122 self.fixPoint(self.mySpline.startPoint())
125 oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
126 newPosition = GeomAPI_Pnt2d(120., 90.)
127 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
130 # plane is XOY, no need to project oldPosition point
131 dx = newPosition.x() - oldPosition.x()
132 dy = newPosition.y() - oldPosition.y()
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)
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
142 self.fixPoint(self.mySpline.startPoint())
145 self.mySketch.move(self.mySpline.startPoint(), self.myPoles[0].x() - 10., self.myPoles[0].y() + 10.)
148 self.checkPoles(self.mySpline, self.myPoles)
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
153 self.fixPoint(self.mySpline.startPoint())
156 newPoles = self.myPoles
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])
163 self.checkPoles(self.mySpline, newPoles)
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
169 self.fixPoint(self.mySpline.endPoint())
172 oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
173 newPosition = GeomAPI_Pnt2d(120., 90.)
174 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
177 # plane is XOY, no need to project oldPosition point
178 dx = newPosition.x() - oldPosition.x()
179 dy = newPosition.y() - oldPosition.y()
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)
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
190 self.fixPoint(self.mySpline.endPoint())
193 newPoles = self.myPoles
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])
200 self.checkPoles(self.mySpline, self.myPoles)
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
205 self.fixPoint(self.mySpline.endPoint())
208 self.mySketch.move(self.mySpline.endPoint(), self.myPoles[-1].x() + 10., self.myPoles[-1].y() + 10.)
211 self.checkPoles(self.mySpline, self.myPoles)
214 def test_move_fixed_bspline(self):
215 """ Test 10. Movement of a fully fixed B-spline
217 self.mySketch.setFixed(self.mySpline.defaultResult())
221 newPosition = GeomAPI_Pnt2d(120., 90.)
222 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
225 self.checkPoles(self.mySpline, self.myPoles)
227 def test_move_start_point_of_fixed_bspline(self):
228 """ Test 11. Movement of start point of a fully fixed B-spline curve
230 self.mySketch.setFixed(self.mySpline.defaultResult())
234 self.mySketch.move(self.mySpline.startPoint(), self.myPoles[0].x() + 10., self.myPoles[0].y() + 10.)
237 self.checkPoles(self.mySpline, self.myPoles)
239 def test_move_end_point_of_fixed_bspline(self):
240 """ Test 12. Movement of end point of a fully fixed B-spline curve
242 self.mySketch.setFixed(self.mySpline.defaultResult())
246 self.mySketch.move(self.mySpline.endPoint(), self.myPoles[-1].x() + 10., self.myPoles[-1].y() + 10.)
249 self.checkPoles(self.mySpline, self.myPoles)
252 def test_move_bspline_with_fixed_pole(self):
253 """ Test 13. Movement of a B-spline curve with fixed pole
255 [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
258 self.fixPoint(Point_2.defaultResult())
261 oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
262 newPosition = GeomAPI_Pnt2d(120., 90.)
263 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
266 # plane is XOY, no need to project oldPosition point
267 dx = newPosition.x() - oldPosition.x()
268 dy = newPosition.y() - oldPosition.y()
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)
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
280 [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
283 self.fixPoint(Point_2.defaultResult())
286 newPoles = self.myPoles
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])
293 self.checkPoles(self.mySpline, newPoles)
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
298 [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
301 self.fixPoint(Point_2.defaultResult())
304 newPoles = self.myPoles
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])
311 self.checkPoles(self.mySpline, newPoles)
314 def test_move_bspline_with_fixed_segment(self):
315 """ Test 16. Movement of a B-spline curve with fixed control segment
317 [Line_1, Line_2, Line_3, Line_4] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
320 self.mySketch.setFixed(Line_1.defaultResult())
324 oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
325 newPosition = GeomAPI_Pnt2d(120., 90.)
326 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
329 # plane is XOY, no need to project oldPosition point
330 dx = newPosition.x() - oldPosition.x()
331 dy = newPosition.y() - oldPosition.y()
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)
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
341 [Line_1, Line_2, Line_3, Line_4] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
344 self.mySketch.setFixed(Line_1.defaultResult())
348 self.mySketch.move(self.mySpline.startPoint(), self.myPoles[0].x() + 10., self.myPoles[0].y() - 20.)
351 self.checkPoles(self.mySpline, self.myPoles)
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
356 [Line_1, Line_2, Line_3, Line_4] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
359 self.mySketch.setFixed(Line_1.defaultResult())
363 newPoles = self.myPoles
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])
370 self.checkPoles(self.mySpline, newPoles)
373 def test_move_pole_of_free_bspline(self):
374 """ Test 19. Movement of a pole of a B-spline curve
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])
380 newPoles = self.myPoles
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])
387 self.checkPoles(self.mySpline, newPoles)
389 def test_move_segment_of_free_bspline(self):
390 """ Test 20. Movement of a control segment of a B-spline curve
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])
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)
402 dx = newPosition.x() - oldPosition.x()
403 dy = newPosition.y() - oldPosition.y()
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)
411 self.checkPoles(self.mySpline, newPoles)
415 if __name__ == "__main__":
416 test_program = unittest.main(exit=False)
417 assert test_program.result.wasSuccessful(), "Test failed"
418 assert model.checkPythonDump()