Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketchPlugin / Test / TestMoveEllipse.py
1 # Copyright (C) 2017-2019  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 GeomDataAPI import geomDataAPI_Point2D
28 from salome.shaper import model
29
30 __updated__ = "2019-09-12"
31
32 class TestMoveEllipse(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 = [70., 50.]
38     self.myFocus = [100., 70.]
39     self.myMinorRadius = 20.
40     self.myEllipse = self.mySketch.addEllipse(self.myCenter[0], self.myCenter[1], self.myFocus[0], self.myFocus[1], self.myMinorRadius)
41     self.myDOF = 5
42     model.do()
43     self.checkDOF()
44     self.myMajorRadius = self.myEllipse.majorRadius().value()
45
46   def tearDown(self):
47     self.checkDOF()
48     model.end()
49
50   def checkDOF(self):
51     self.assertEqual(model.dof(self.mySketch), self.myDOF)
52
53   def checkPointCoordinates(self, thePoint, theCoordinates):
54     aCoord = []
55     if issubclass(type(theCoordinates), GeomAPI_Pnt2d):
56       aCoord = [theCoordinates.x(), theCoordinates.y()]
57     else:
58       aCoord = theCoordinates
59     DIGITS = 7 - math.floor(math.log10(math.hypot(aCoord[0], aCoord[1])))
60     self.assertAlmostEqual(thePoint.x(), aCoord[0], DIGITS)
61     self.assertAlmostEqual(thePoint.y(), aCoord[1], DIGITS)
62
63   def checkPointOnEllipse(self, theCoordinates, theEllipse):
64     point = GeomAPI_Pnt2d(theCoordinates.x(), theCoordinates.y())
65     firstFocus2d = GeomAPI_Pnt2d(theEllipse.firstFocus().x(), theEllipse.firstFocus().y())
66     distPF1 = model.distancePointPoint(firstFocus2d,  point)
67     secondFocus2d = GeomAPI_Pnt2d(theEllipse.secondFocus().x(), theEllipse.secondFocus().y())
68     distPF2 = model.distancePointPoint(secondFocus2d,  point)
69     self.assertAlmostEqual(distPF1 + distPF2, 2.0 * theEllipse.majorRadius().value(), 7 - math.floor(math.log10(theEllipse.majorRadius().value())))
70
71   def fixMajorRadius(self):
72     self.mySketch.setDistance(self.myEllipse.center(), self.myEllipse.majorAxisPositive(), self.myMajorRadius)
73     self.myDOF -= 1
74     model.do()
75     self.checkDOF()
76
77   def fixMinorRadius(self):
78     self.mySketch.setDistance(self.myEllipse.center(), self.myEllipse.minorAxisPositive(), self.myMinorRadius)
79     self.myDOF -= 1
80     model.do()
81     self.checkDOF()
82
83   def fixPoint(self, thePoint):
84     self.mySketch.setFixed(thePoint)
85     self.myDOF -= 2
86     model.do()
87     self.checkDOF()
88
89
90   def test_move_center_free_ellipse(self):
91     """ Test 1. Movement of central point of a free ellipse
92     """
93     newPosition = [self.myCenter[0] + 20., self.myCenter[1] + 10.]
94     self.mySketch.move(self.myEllipse.center(), newPosition[0], newPosition[1])
95     model.do()
96     self.checkPointCoordinates(self.myEllipse.center(), newPosition)
97     self.assertAlmostEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
98     self.assertAlmostEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
99
100   def test_move_free_ellipse(self):
101     """ Test 2. Movement of a free ellipse dragging the edge
102     """
103     newPosition = GeomAPI_Pnt2d(120., 90.)
104     self.mySketch.move(self.myEllipse.defaultResult(), newPosition.x(), newPosition.y())
105     model.do()
106     self.checkPointCoordinates(self.myEllipse.center(), self.myCenter)
107     self.checkPointOnEllipse(newPosition, self.myEllipse)
108     self.assertNotEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
109     self.assertNotEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
110
111   def test_move_center_ellipse_fixed_major_radius(self):
112     """ Test 3. Movement of central point of ellipse with fixed major radius
113     """
114     self.fixMajorRadius()
115
116     newPosition = [self.myCenter[0] + 20., self.myCenter[1] + 10.]
117     self.mySketch.move(self.myEllipse.center(), newPosition[0], newPosition[1])
118     model.do()
119     self.checkPointCoordinates(self.myEllipse.center(), newPosition)
120     self.assertAlmostEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
121     self.assertAlmostEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
122
123   def test_move_ellipse_fixed_major_radius(self):
124     """ Test 4. Movement of ellipse with fixed major radius
125     """
126     self.fixMajorRadius()
127
128     newPosition = GeomAPI_Pnt2d(80., 80.)
129     self.mySketch.move(self.myEllipse.defaultResult(), newPosition.x(), newPosition.y())
130     model.do()
131     self.checkPointOnEllipse(newPosition, self.myEllipse)
132     self.assertNotEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
133     self.assertAlmostEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
134
135   def test_move_center_ellipse_fixed_minor_radius(self):
136     """ Test 5. Movement of central point of ellipse with fixed minor radius
137     """
138     self.fixMinorRadius()
139
140     newPosition = [self.myCenter[0] + 20., self.myCenter[1] + 10.]
141     self.mySketch.move(self.myEllipse.center(), newPosition[0], newPosition[1])
142     model.do()
143     self.checkPointCoordinates(self.myEllipse.center(), newPosition)
144     self.assertAlmostEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
145     self.assertAlmostEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
146
147   def test_move_ellipse_fixed_minor_radius(self):
148     """ Test 6. Movement of ellipse with fixed minor radius
149     """
150     self.fixMinorRadius()
151
152     newPosition = GeomAPI_Pnt2d(120., 90.)
153     self.mySketch.move(self.myEllipse.defaultResult(), newPosition.x(), newPosition.y())
154     model.do()
155     self.checkPointOnEllipse(newPosition, self.myEllipse)
156     self.assertAlmostEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
157     self.assertNotEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
158
159   def test_move_center_ellipse_fixed_center(self):
160     """ Test 7. Movement of central point of ellipse with fixed center (nothing should be changed)
161     """
162     self.fixPoint(self.myEllipse.center())
163
164     newPosition = [self.myCenter[0] + 20., self.myCenter[1] + 10.]
165     self.mySketch.move(self.myEllipse.center(), newPosition[0], newPosition[1])
166     model.do()
167     self.checkPointCoordinates(self.myEllipse.center(), self.myCenter)
168     self.assertAlmostEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
169     self.assertAlmostEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
170
171   def test_move_ellipse_fixed_center(self):
172     """ Test 8. Movement of ellipse with fixed center
173     """
174     self.fixPoint(self.myEllipse.center())
175
176     newPosition = GeomAPI_Pnt2d(120., 90.)
177     self.mySketch.move(self.myEllipse.defaultResult(), newPosition.x(), newPosition.y())
178     model.do()
179     self.checkPointOnEllipse(newPosition, self.myEllipse)
180     self.assertNotEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
181     self.assertNotEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
182
183   def test_move_center_ellipse_fixed_focus(self):
184     """ Test 9. Movement of central point of ellipse with fixed focus
185     """
186     self.fixPoint(self.myEllipse.firstFocus())
187
188     newPosition = GeomAPI_Pnt2d(self.myCenter[0] + 20., self.myCenter[1] + 10.)
189     self.mySketch.move(self.myEllipse.center(), newPosition.x(), newPosition.y())
190     model.do()
191     self.checkPointCoordinates(self.myEllipse.center(), newPosition)
192     self.checkPointCoordinates(self.myEllipse.firstFocus(), self.myFocus)
193     self.assertNotEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
194     self.assertNotEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
195
196   def test_move_focus_ellipse_fixed_focus(self):
197     """ Test 10. Movement of a focus point of ellipse with fixed focus (nothing should be changed)
198     """
199     self.fixPoint(self.myEllipse.firstFocus())
200
201     newPosition = GeomAPI_Pnt2d(self.myFocus[0] + 10., self.myFocus[1] + 10.)
202     self.mySketch.move(self.myEllipse.firstFocus(), newPosition.x(), newPosition.y())
203     model.do()
204     self.checkPointCoordinates(self.myEllipse.firstFocus(), self.myFocus)
205     self.assertAlmostEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
206     self.assertAlmostEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
207
208   def test_move_ellipse_fixed_focus(self):
209     """ Test 11. Movement of ellipse with fixed focus
210     """
211     self.fixPoint(self.myEllipse.firstFocus())
212
213     newPosition = GeomAPI_Pnt2d(80., 90.)
214     self.mySketch.move(self.myEllipse.defaultResult(), newPosition.x(), newPosition.y())
215     model.do()
216     self.checkPointOnEllipse(newPosition, self.myEllipse)
217     self.checkPointCoordinates(self.myEllipse.firstFocus(), self.myFocus)
218     self.assertNotEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
219     self.assertNotEqual(self.myEllipse.majorRadius().value(), self.myMajorRadius)
220
221   def test_move_fixed_ellipse(self):
222     """ Test 12. Trying to move fully fixed ellipse
223     """
224     self.mySketch.setFixed(self.myEllipse.results()[-1])
225     self.myDOF -= 5
226     model.do()
227     self.checkDOF()
228
229     newPosition = [120., 90.]
230     self.mySketch.move(self.myEllipse.defaultResult(), newPosition[0], newPosition[1])
231     model.do()
232     self.checkPointCoordinates(self.myEllipse.center(), self.myCenter)
233     self.checkPointCoordinates(self.myEllipse.firstFocus(), self.myFocus)
234     self.assertAlmostEqual(self.myEllipse.minorRadius().value(), self.myMinorRadius)
235
236
237 if __name__ == "__main__":
238     test_program = unittest.main(exit=False)
239     assert test_program.result.wasSuccessful(), "Test failed"
240     assert(model.checkPythonDump())