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