Salome HOME
71c84325ae7c50644fad38472b1dc0f26114e49c
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateEllipseByCenterSemiaxisAndPassed.py
1 # Copyright (C) 2019-2023  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 creation of ellipse by center, semi-axis and passed point
22 """
23
24 import unittest
25 from salome.shaper import model
26
27 from GeomAPI import *
28 from SketchAPI import *
29
30 __updated__ = "2019-09-09"
31
32 class TestEllipse(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(50., 50.)
38     self.myFocus = GeomAPI_Pnt2d(70., 50.)
39     self.myPassedPoint = GeomAPI_Pnt2d(60., 60.)
40     self.myMinorRadius = 20.
41     self.myDOF = 0
42
43   def tearDown(self):
44     self.checkDOF()
45     model.end()
46
47
48   def checkDOF(self):
49     self.assertEqual(model.dof(self.mySketch), self.myDOF)
50
51   def checkPointsEqual(self, thePoint1, thePoint2):
52     self.assertAlmostEqual(thePoint1.x(), thePoint2.x(), 5)
53     self.assertAlmostEqual(thePoint1.y(), thePoint2.y(), 5)
54
55   def checkPointOnLine(self, theCoordinates, theLine):
56     point = GeomAPI_Pnt2d(theCoordinates.x(), theCoordinates.y())
57     dist = model.distancePointLine(point, theLine)
58     self.assertAlmostEqual(dist, 0, 7)
59
60   def checkPointOnCircle(self, theCoordinates, theCircle):
61     point = GeomAPI_Pnt2d(theCoordinates.x(), theCoordinates.y())
62     dist = model.distancePointPoint(point, theCircle.center())
63     self.assertAlmostEqual(dist , theCircle.radius().value(), 7)
64
65   def checkPointOnEllipse(self, theCoordinates, theEllipse):
66     point = GeomAPI_Pnt2d(theCoordinates.x(), theCoordinates.y())
67     firstFocus2d = GeomAPI_Pnt2d(theEllipse.firstFocus().x(), theEllipse.firstFocus().y())
68     distPF1 = model.distancePointPoint(firstFocus2d,  point)
69     secondFocus2d = GeomAPI_Pnt2d(theEllipse.secondFocus().x(), theEllipse.secondFocus().y())
70     distPF2 = model.distancePointPoint(secondFocus2d,  point)
71     if issubclass(type(theEllipse), SketchAPI_Ellipse):
72       majorRad = theEllipse.majorRadius().value()
73     else:
74       majorRad = theEllipse.majorRadius()
75     self.assertAlmostEqual(distPF1 + distPF2, 2.0 * majorRad, 7)
76
77
78   def test_ellipse_by_center_and_focus(self):
79     """ Test 1. Create ellipse by coordinates of center, focus and minor radius
80     """
81     self.myEllipse1 = self.mySketch.addEllipse(self.myCenter.x(), self.myCenter.y(), self.myFocus.x(), self.myFocus.y(), self.myMinorRadius)
82     self.myDOF += 5
83
84     self.myEllipse2 = self.mySketch.addEllipse(self.myCenter, self.myFocus, self.myMinorRadius)
85     self.myDOF += 5
86     model.do()
87
88     # check both ellipses are equal
89     anEllipse1 = self.myEllipse1.defaultResult().shape().edge().ellipse()
90     anEllipse2 = self.myEllipse2.defaultResult().shape().edge().ellipse()
91     self.checkPointsEqual(anEllipse1.center(), anEllipse2.center())
92     self.checkPointsEqual(anEllipse1.firstFocus(), anEllipse2.firstFocus())
93     self.checkPointsEqual(anEllipse1.secondFocus(), anEllipse2.secondFocus())
94     self.assertAlmostEqual(anEllipse1.minorRadius(), anEllipse2.minorRadius())
95     self.assertAlmostEqual(anEllipse1.majorRadius(), anEllipse2.majorRadius())
96     # check number of features
97     model.testNbSubFeatures(self.mySketch, "SketchPoint", 0)
98     model.testNbSubFeatures(self.mySketch, "SketchLine", 0)
99     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 2)
100
101   def test_ellipse_by_semiaxis_and_passed(self):
102     """ Test 2. Create ellipse by coordinates of center, major semi-axis point and a passed point on ellipse
103     """
104     self.myEllipse1 = self.mySketch.addEllipse(self.myCenter.x(), self.myCenter.y(), self.myFocus.x(), self.myFocus.y(), self.myPassedPoint.x(), self.myPassedPoint.y(), True)
105     self.myDOF += 5
106     model.do()
107     anEllipseFeature1 = model.lastSubFeature(self.mySketch, "SketchEllipse")
108
109     self.myEllipse2 = self.mySketch.addEllipse(self.myCenter, self.myFocus, self.myPassedPoint, True)
110     self.myDOF += 5
111     model.do()
112     anEllipseFeature2 = model.lastSubFeature(self.mySketch, "SketchEllipse")
113
114     # check both ellipses are equal
115     anEllipse1 = anEllipseFeature1.lastResult().shape().edge().ellipse()
116     anEllipse2 = anEllipseFeature2.lastResult().shape().edge().ellipse()
117     self.checkPointsEqual(anEllipse1.center(), anEllipse2.center())
118     self.checkPointsEqual(anEllipse1.firstFocus(), anEllipse2.firstFocus())
119     self.checkPointsEqual(anEllipse1.secondFocus(), anEllipse2.secondFocus())
120     self.assertAlmostEqual(anEllipse1.minorRadius(), anEllipse2.minorRadius())
121     self.assertAlmostEqual(anEllipse1.majorRadius(), anEllipse2.majorRadius())
122     # check passed point on ellipse
123     self.checkPointOnEllipse(self.myPassedPoint, anEllipse1)
124     self.checkPointOnEllipse(self.myPassedPoint, anEllipse2)
125     # check number of features
126     model.testNbSubFeatures(self.mySketch, "SketchPoint", 14)
127     model.testNbSubFeatures(self.mySketch, "SketchLine", 4)
128     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 2)
129
130   def test_ellipse_with_fixed_center(self):
131     """ Test 3. Create ellipse which center is coincident with another point
132     """
133     aLine = self.mySketch.addLine(10, 10, 30, 40)
134     self.myDOF += 4
135     model.do()
136
137     self.myEllipse1 = self.mySketch.addEllipse(aLine.endPoint(), self.myFocus, self.myPassedPoint, True)
138     self.myDOF += 3
139     model.do()
140     # check ellipse
141     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipse")
142     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
143     self.checkPointsEqual(anEllipse.center(), aLine.endPoint())
144     self.checkPointOnEllipse(self.myPassedPoint, anEllipse)
145     # check number of features
146     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
147     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
148     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
149     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
150
151   def test_ellipse_with_center_on_line(self):
152     """ Test 4. Create ellipse which center is coincident with a line
153     """
154     aLine = self.mySketch.addLine(10, 10, 30, 40)
155     self.myDOF += 4
156     model.do()
157
158     self.myEllipse1 = self.mySketch.addEllipse([self.myCenter, aLine.result()], self.myFocus, self.myPassedPoint, True)
159     self.myDOF += 4
160     model.do()
161
162     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipse")
163     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
164     # check center on line
165     self.checkPointOnLine(anEllipse.center(), aLine)
166     # check number of features
167     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
168     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
169     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
170     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
171
172   def test_ellipse_with_center_on_circle(self):
173     """ Test 5. Create ellipse which center is coincident with a circle
174     """
175     aCircle = self.mySketch.addCircle(10, 10, 20)
176     self.myDOF += 3
177     model.do()
178
179     self.myEllipse1 = self.mySketch.addEllipse([self.myCenter, aCircle.defaultResult()], self.myFocus, self.myPassedPoint, True)
180     self.myDOF += 4
181     model.do()
182
183     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipse")
184     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
185     # check center on circle
186     self.checkPointOnCircle(anEllipse.center(), aCircle)
187     # check number of features
188     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
189     model.testNbSubFeatures(self.mySketch, "SketchLine", 2)
190     model.testNbSubFeatures(self.mySketch, "SketchCircle", 1)
191     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
192     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
193
194   def test_ellipse_with_center_on_ellipse(self):
195     """ Test 6. Create ellipse which center is coincident with another ellipse
196     """
197     anOtherEllipse = self.mySketch.addEllipse(10, 10, 30, 20, 10)
198     self.myDOF += 5
199     model.do()
200
201     self.myEllipse1 = self.mySketch.addEllipse([self.myCenter, anOtherEllipse.defaultResult()], self.myFocus, self.myPassedPoint, True)
202     self.myDOF += 4
203     model.do()
204
205     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipse")
206     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
207     # check center on ellipse
208     self.checkPointOnEllipse(anEllipse.center(), anOtherEllipse)
209     # check number of features
210     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
211     model.testNbSubFeatures(self.mySketch, "SketchLine", 2)
212     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 2)
213     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
214
215   def test_ellipse_with_fixed_axis(self):
216     """ Test 7. Create ellipse which point on major semi-axis is coincident with another point
217     """
218     aLine = self.mySketch.addLine(10, 10, 30, 40)
219     self.myDOF += 4
220     model.do()
221
222     self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, aLine.endPoint(), self.myPassedPoint, True)
223     self.myDOF += 3
224     model.do()
225     # check ellipse
226     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipse")
227     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
228     self.checkPointOnEllipse(self.myPassedPoint, anEllipse)
229     self.checkPointOnEllipse(aLine.endPoint(), anEllipse)
230     # check distance is equal to major semi-axis
231     dist = model.distancePointPoint(GeomAPI_Pnt2d(anEllipse.center().x(), anEllipse.center().y()), aLine.endPoint())
232     self.assertAlmostEqual(dist, anEllipse.majorRadius(), 7)
233     # check number of features
234     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
235     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
236     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
237     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
238
239   def test_ellipse_with_axis_on_line(self):
240     """ Test 8. Create ellipse which point on major semi-axis is coincident with a line
241     """
242     aLine = self.mySketch.addLine(10, 10, 30, 40)
243     self.myDOF += 4
244     model.do()
245
246     self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, [self.myFocus, aLine.result()], self.myPassedPoint, True)
247     self.myDOF += 4
248     model.do()
249
250     anEllipse = SketchAPI_Ellipse(model.lastSubFeature(self.mySketch, "SketchEllipse"))
251     # check axis point on line
252     self.checkPointOnLine(anEllipse.majorAxisPositive(), aLine)
253     # check number of features
254     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
255     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
256     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
257     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
258
259   def test_ellipse_with_axis_on_circle(self):
260     """ Test 9. Create ellipse which point on major semi-axis is coincident with a circle
261     """
262     aCircle = self.mySketch.addCircle(10, 10, 20)
263     self.myDOF += 3
264     model.do()
265
266     self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, [self.myFocus, aCircle.defaultResult()], self.myPassedPoint, True)
267     self.myDOF += 4
268     model.do()
269
270     anEllipse = SketchAPI_Ellipse(model.lastSubFeature(self.mySketch, "SketchEllipse"))
271     # check center on circle
272     self.checkPointOnCircle(anEllipse.majorAxisPositive(), aCircle)
273     # check number of features
274     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
275     model.testNbSubFeatures(self.mySketch, "SketchLine", 2)
276     model.testNbSubFeatures(self.mySketch, "SketchCircle", 1)
277     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
278     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
279
280   def test_ellipse_with_axis_on_ellipse(self):
281     """ Test 10. Create ellipse which point on major semi-axis is coincident with another ellipse
282     """
283     anOtherEllipse = self.mySketch.addEllipse(10, 10, 90, 40, 30)
284     self.myDOF += 5
285     model.do()
286
287     self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, [self.myFocus, anOtherEllipse.defaultResult()], self.myPassedPoint, True)
288     self.myDOF += 4
289     model.do()
290
291     anEllipse = SketchAPI_Ellipse(model.lastSubFeature(self.mySketch, "SketchEllipse"))
292     # check center on ellipse
293     self.checkPointOnEllipse(anEllipse.majorAxisPositive(), anOtherEllipse)
294     # check number of features
295     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
296     model.testNbSubFeatures(self.mySketch, "SketchLine", 2)
297     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 2)
298     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
299
300   def test_ellipse_with_fixed_passed_point(self):
301     """ Test 11. Create ellipse which passed point is coincident with another point
302     """
303     aLine = self.mySketch.addLine(10, 10, 30, 40)
304     self.myDOF += 4
305     model.do()
306
307     self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, self.myFocus, aLine.endPoint(), True)
308     self.myDOF += 4
309     model.do()
310     # check ellipse
311     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipse")
312     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
313     self.checkPointOnEllipse(aLine.endPoint(), anEllipse)
314     # check number of features
315     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
316     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
317     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
318     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
319
320   def test_ellipse_with_passed_point_on_line(self):
321     """ Test 12. Create ellipse which passed point is placed on a line.
322                  Check no constraints is applied.
323     """
324     aLine = self.mySketch.addLine(10, 10, 30, 40)
325     self.myDOF += 4
326     model.do()
327
328     self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, self.myFocus, [self.myPassedPoint, aLine.result()], True)
329     self.myDOF += 5
330     model.do()
331
332     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipse")
333     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
334     self.checkPointOnEllipse(self.myPassedPoint, anEllipse)
335     # check number of features
336     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
337     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
338     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
339     # check neither coincidence nor tangent feature exists
340     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 0)
341     model.testNbSubFeatures(self.mySketch, "SketchConstraintTangent", 0)
342
343
344 if __name__ == "__main__":
345     test_program = unittest.main(exit=False)
346     assert test_program.result.wasSuccessful(), "Test failed"
347     assert model.checkPythonDump()