Salome HOME
Copyright update 2020
[modules/shaper.git] / src / SketchPlugin / Test / TestMoveEllipticArc.py
1 # Copyright (C) 2017-2020  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 movement of the sketch ellipse
22 """
23
24 import unittest
25 import math
26 from GeomAPI import GeomAPI_Pnt2d
27 from SketchAPI import *
28 from salome.shaper import model
29
30 __updated__ = "2019-10-15"
31
32 class TestMoveEllipticArc(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.myCenter = GeomAPI_Pnt2d(70., 50.)
38     self.myAxisPoint = GeomAPI_Pnt2d(100., 70.)
39     self.myStartPoint = GeomAPI_Pnt2d(75., 75.)
40     self.myEndPoint = GeomAPI_Pnt2d(45., 55.)
41     macroEllipticArc = self.mySketch.addEllipticArc(self.myCenter, self.myAxisPoint, self.myStartPoint, self.myEndPoint, False)
42     self.myDOF = 7
43     model.do()
44     self.checkDOF()
45     self.myEllipticArc = SketchAPI_EllipticArc(model.lastSubFeature(self.mySketch, "SketchEllipticArc"))
46     self.myMajorRadius = self.myEllipticArc.majorRadius().value()
47     self.myMinorRadius = self.myEllipticArc.minorRadius().value()
48
49   def tearDown(self):
50     self.checkDOF()
51     self.checkPointOnEllipse(self.myEllipticArc.startPoint(), self.myEllipticArc)
52     self.checkPointOnEllipse(self.myEllipticArc.endPoint(), self.myEllipticArc)
53     model.end()
54
55   def checkDOF(self):
56     self.assertEqual(model.dof(self.mySketch), self.myDOF)
57
58   def checkPointCoordinates(self, thePoint, theCoordinates):
59     aCoord = []
60     if issubclass(type(theCoordinates), GeomAPI_Pnt2d):
61       aCoord = [theCoordinates.x(), theCoordinates.y()]
62     else:
63       aCoord = theCoordinates
64     DIGITS = 7 - math.floor(math.log10(math.hypot(aCoord[0], aCoord[1])))
65     self.assertAlmostEqual(thePoint.x(), aCoord[0], DIGITS)
66     self.assertAlmostEqual(thePoint.y(), aCoord[1], DIGITS)
67
68   def checkPointOnEllipse(self, theCoordinates, theEllipticArc):
69     point = GeomAPI_Pnt2d(theCoordinates.x(), theCoordinates.y())
70     firstFocus2d = GeomAPI_Pnt2d(theEllipticArc.firstFocus().x(), theEllipticArc.firstFocus().y())
71     distPF1 = model.distancePointPoint(firstFocus2d,  point)
72     secondFocus2d = GeomAPI_Pnt2d(theEllipticArc.secondFocus().x(), theEllipticArc.secondFocus().y())
73     distPF2 = model.distancePointPoint(secondFocus2d,  point)
74     self.assertAlmostEqual(distPF1 + distPF2, 2.0 * theEllipticArc.majorRadius().value(), 7 - math.floor(math.log10(theEllipticArc.majorRadius().value())))
75
76   def fixMajorRadius(self):
77     self.mySketch.setDistance(self.myEllipticArc.center(), self.myEllipticArc.majorAxisPositive(), self.myMajorRadius)
78     self.myDOF -= 1
79     model.do()
80     self.checkDOF()
81
82   def fixMinorRadius(self):
83     self.mySketch.setDistance(self.myEllipticArc.center(), self.myEllipticArc.minorAxisPositive(), self.myMinorRadius)
84     self.myDOF -= 1
85     model.do()
86     self.checkDOF()
87
88   def fixPoint(self, thePoint):
89     self.mySketch.setFixed(thePoint)
90     self.myDOF -= 2
91     model.do()
92     self.checkDOF()
93
94
95   def test_move_center_free_ellipse(self):
96     """ Test 1. Movement of central point of a free elliptic arc
97     """
98     newPosition = [self.myCenter.x() + 20., self.myCenter.y() + 10.]
99     self.mySketch.move(self.myEllipticArc.center(), newPosition[0], newPosition[1])
100     model.do()
101     self.checkPointCoordinates(self.myEllipticArc.center(), newPosition)
102
103   def test_move_free_ellipse(self):
104     """ Test 2. Movement of a free ellipse dragging the edge
105     """
106     newPosition = GeomAPI_Pnt2d(110., 80.)
107     self.mySketch.move(self.myEllipticArc.defaultResult(), newPosition.x(), newPosition.y())
108     model.do()
109     self.checkPointCoordinates(self.myEllipticArc.center(), self.myCenter)
110     self.checkPointOnEllipse(newPosition, self.myEllipticArc)
111     self.assertNotEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
112     self.assertNotEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
113
114   def test_move_center_ellipse_fixed_major_radius(self):
115     """ Test 3. Movement of central point of ellipse with fixed major radius
116     """
117     self.fixMajorRadius()
118
119     newPosition = [self.myCenter.x() + 20., self.myCenter.y() + 10.]
120     self.mySketch.move(self.myEllipticArc.center(), newPosition[0], newPosition[1])
121     model.do()
122     self.checkPointCoordinates(self.myEllipticArc.center(), newPosition)
123     self.assertAlmostEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
124
125   def test_move_ellipse_fixed_major_radius(self):
126     """ Test 4. Movement of ellipse with fixed major radius
127     """
128     self.fixMajorRadius()
129
130     newPosition = GeomAPI_Pnt2d(80., 80.)
131     self.mySketch.move(self.myEllipticArc.defaultResult(), newPosition.x(), newPosition.y())
132     model.do()
133     self.checkPointOnEllipse(newPosition, self.myEllipticArc)
134     self.assertNotEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
135     self.assertAlmostEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
136
137   def test_move_center_ellipse_fixed_minor_radius(self):
138     """ Test 5. Movement of central point of ellipse with fixed minor radius
139     """
140     self.fixMinorRadius()
141
142     newPosition = [self.myCenter.x() + 20., self.myCenter.y() + 10.]
143     self.mySketch.move(self.myEllipticArc.center(), newPosition[0], newPosition[1])
144     model.do()
145     self.checkPointCoordinates(self.myEllipticArc.center(), newPosition)
146     self.assertAlmostEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
147
148   def test_move_ellipse_fixed_minor_radius(self):
149     """ Test 6. Movement of ellipse with fixed minor radius
150     """
151     self.fixMinorRadius()
152
153     newPosition = GeomAPI_Pnt2d(110., 80.)
154     self.mySketch.move(self.myEllipticArc.defaultResult(), newPosition.x(), newPosition.y())
155     model.do()
156     self.checkPointOnEllipse(newPosition, self.myEllipticArc)
157     self.assertAlmostEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
158     self.assertNotEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
159
160   def test_move_center_ellipse_fixed_center(self):
161     """ Test 7. Movement of central point of ellipse with fixed center (nothing should be changed)
162     """
163     self.fixPoint(self.myEllipticArc.center())
164
165     newPosition = [self.myCenter.x() + 20., self.myCenter.y() + 10.]
166     self.mySketch.move(self.myEllipticArc.center(), newPosition[0], newPosition[1])
167     model.do()
168     self.checkPointCoordinates(self.myEllipticArc.center(), self.myCenter)
169     self.assertAlmostEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
170     self.assertAlmostEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
171
172   def test_move_ellipse_fixed_center(self):
173     """ Test 8. Movement of ellipse with fixed center
174     """
175     self.fixPoint(self.myEllipticArc.center())
176
177     newPosition = GeomAPI_Pnt2d(110., 80.)
178     self.mySketch.move(self.myEllipticArc.defaultResult(), newPosition.x(), newPosition.y())
179     model.do()
180     self.checkPointOnEllipse(newPosition, self.myEllipticArc)
181     self.assertNotEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
182     self.assertNotEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
183
184   def test_move_center_ellipse_fixed_focus(self):
185     """ Test 9. Movement of central point of ellipse with fixed focus
186     """
187     focus = [self.myEllipticArc.firstFocus().x(), self.myEllipticArc.firstFocus().y()]
188     self.fixPoint(self.myEllipticArc.firstFocus())
189
190     newPosition = GeomAPI_Pnt2d(self.myCenter.x() + 20., self.myCenter.y() + 10.)
191     self.mySketch.move(self.myEllipticArc.center(), newPosition.x(), newPosition.y())
192     model.do()
193     self.checkPointCoordinates(self.myEllipticArc.center(), newPosition)
194     self.checkPointCoordinates(self.myEllipticArc.firstFocus(), focus)
195     self.assertNotEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
196     self.assertNotEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
197
198   def test_move_focus_ellipse_fixed_focus(self):
199     """ Test 10. Movement of a focus point of ellipse with fixed focus (nothing should be changed)
200     """
201     focus = [self.myEllipticArc.firstFocus().x(), self.myEllipticArc.firstFocus().y()]
202     self.fixPoint(self.myEllipticArc.firstFocus())
203
204     newPosition = GeomAPI_Pnt2d(focus[0] + 10., focus[1] + 10.)
205     self.mySketch.move(self.myEllipticArc.firstFocus(), newPosition.x(), newPosition.y())
206     model.do()
207     self.checkPointCoordinates(self.myEllipticArc.firstFocus(), focus)
208     self.assertAlmostEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
209     self.assertAlmostEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
210
211   def test_move_ellipse_fixed_focus(self):
212     """ Test 11. Movement of ellipse with fixed focus
213     """
214     focus = [self.myEllipticArc.firstFocus().x(), self.myEllipticArc.firstFocus().y()]
215     self.fixPoint(self.myEllipticArc.firstFocus())
216
217     newPosition = GeomAPI_Pnt2d(80., 90.)
218     self.mySketch.move(self.myEllipticArc.defaultResult(), newPosition.x(), newPosition.y())
219     model.do()
220     self.checkPointOnEllipse(newPosition, self.myEllipticArc)
221     self.checkPointCoordinates(self.myEllipticArc.firstFocus(), focus)
222     self.assertNotEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
223     self.assertNotEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
224
225   def test_move_fixed_ellipse(self):
226     """ Test 12. Trying to move fully fixed ellipse
227     """
228     self.mySketch.setFixed(self.myEllipticArc.results()[-1])
229     self.myDOF -= 7
230     model.do()
231     self.checkDOF()
232
233     newPosition = [110., 80.]
234     self.mySketch.move(self.myEllipticArc.defaultResult(), newPosition[0], newPosition[1])
235     model.do()
236     self.checkPointCoordinates(self.myEllipticArc.center(), self.myCenter)
237     self.checkPointCoordinates(self.myEllipticArc.majorAxisPositive(), self.myAxisPoint)
238     self.checkPointCoordinates(self.myEllipticArc.startPoint(), self.myStartPoint)
239     self.checkPointCoordinates(self.myEllipticArc.endPoint(), self.myEndPoint)
240     self.assertAlmostEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
241     self.assertAlmostEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
242
243   def test_move_start_point_free_ellipse(self):
244     """ Test 13. Trying to move start point of elliptic arc
245     """
246     newPosition = [self.myStartPoint.x() + 10., self.myStartPoint.y() + 10.]
247     self.mySketch.move(self.myEllipticArc.startPoint(), newPosition[0], newPosition[1])
248     model.do()
249     self.checkPointCoordinates(self.myEllipticArc.startPoint(), newPosition)
250
251   def test_move_end_point_free_ellipse(self):
252     """ Test 14. Trying to move end point of elliptic arc
253     """
254     newPosition = [self.myEndPoint.x() - 10., self.myEndPoint.y() + 10.]
255     self.mySketch.move(self.myEllipticArc.endPoint(), newPosition[0], newPosition[1])
256     model.do()
257     self.checkPointCoordinates(self.myEllipticArc.endPoint(), newPosition)
258
259   def test_move_start_point_fixed_ellipse(self):
260     """ Test 15. Trying to move start point of fully fixed ellipse
261     """
262     self.mySketch.setFixed(self.myEllipticArc.results()[-1])
263     self.myDOF -= 7
264     model.do()
265     self.checkDOF()
266
267     newPosition = [self.myStartPoint.x() + 10., self.myStartPoint.y() + 10.]
268     self.mySketch.move(self.myEllipticArc.startPoint(), newPosition[0], newPosition[1])
269     model.do()
270     self.checkPointCoordinates(self.myEllipticArc.center(), self.myCenter)
271     self.checkPointCoordinates(self.myEllipticArc.majorAxisPositive(), self.myAxisPoint)
272     self.checkPointCoordinates(self.myEllipticArc.startPoint(), self.myStartPoint)
273     self.checkPointCoordinates(self.myEllipticArc.endPoint(), self.myEndPoint)
274     self.assertAlmostEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
275     self.assertAlmostEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
276
277   def test_move_end_point_fixed_ellipse(self):
278     """ Test 16. Trying to move end point of fully fixed ellipse
279     """
280     self.mySketch.setFixed(self.myEllipticArc.results()[-1])
281     self.myDOF -= 7
282     model.do()
283     self.checkDOF()
284
285     newPosition = [self.myEndPoint.x() - 10., self.myEndPoint.y() + 10.]
286     self.mySketch.move(self.myEllipticArc.endPoint(), newPosition[0], newPosition[1])
287     model.do()
288     self.checkPointCoordinates(self.myEllipticArc.center(), self.myCenter)
289     self.checkPointCoordinates(self.myEllipticArc.majorAxisPositive(), self.myAxisPoint)
290     self.checkPointCoordinates(self.myEllipticArc.startPoint(), self.myStartPoint)
291     self.checkPointCoordinates(self.myEllipticArc.endPoint(), self.myEndPoint)
292     self.assertAlmostEqual(self.myEllipticArc.majorRadius().value(), self.myMajorRadius)
293     self.assertAlmostEqual(self.myEllipticArc.minorRadius().value(), self.myMinorRadius)
294
295
296 if __name__ == "__main__":
297     test_program = unittest.main(exit=False)
298     assert test_program.result.wasSuccessful(), "Test failed"