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