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