Salome HOME
Updated copyright comment
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateEllipticArc.py
1 # Copyright (C) 2019-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 creation of elliptic arc by center, semi-axis, start and end points
22 """
23
24 import unittest
25 from salome.shaper import model
26
27 from GeomAPI import *
28 from SketchAPI import *
29
30 __updated__ = "2019-10-01"
31
32 class TestEllipticArc(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., 60.)
39     self.myStartPoint = GeomAPI_Pnt2d(60., 65.)
40     self.myEndPoint = GeomAPI_Pnt2d(60., 42.535751)
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_elliptic_arc_by_coordinates(self):
79     """ Test 1. Create elliptic arc by coordinates of center, point on the major axis, start and end points
80     """
81     self.myEllipse1 = self.mySketch.addEllipticArc(self.myCenter.x(), self.myCenter.y(),
82                                                    self.myFocus.x(), self.myFocus.y(),
83                                                    self.myStartPoint.x(), self.myStartPoint.y(),
84                                                    self.myEndPoint.x(), self.myEndPoint.y(), False)
85     self.myDOF += 7
86
87     self.myEllipse2 = self.mySketch.addEllipticArc(self.myCenter.x(), self.myCenter.y(),
88                                                    self.myFocus.x(), self.myFocus.y(),
89                                                    self.myStartPoint.x(), self.myStartPoint.y(),
90                                                    self.myEndPoint.x(), self.myEndPoint.y(), True)
91     self.myDOF += 7
92     model.do()
93
94     # check both ellipses are equal
95     anArcEdge1 = self.myEllipse1.defaultResult().shape().edge()
96     anArcEdge2 = self.myEllipse2.defaultResult().shape().edge()
97     anEllipse1 = anArcEdge1.ellipse()
98     anEllipse2 = anArcEdge2.ellipse()
99     self.checkPointsEqual(anEllipse1.center(), anEllipse2.center())
100     self.checkPointsEqual(anEllipse1.firstFocus(), anEllipse2.firstFocus())
101     self.checkPointsEqual(anEllipse1.secondFocus(), anEllipse2.secondFocus())
102     self.assertAlmostEqual(anEllipse1.minorRadius(), anEllipse2.minorRadius())
103     self.assertAlmostEqual(anEllipse1.majorRadius(), anEllipse2.majorRadius())
104     self.checkPointsEqual(self.myEllipse1.startPoint(), self.myEllipse2.startPoint())
105     self.checkPointsEqual(self.myEllipse1.endPoint(), self.myEllipse2.endPoint())
106     # check number of features
107     model.testNbSubFeatures(self.mySketch, "SketchPoint", 0)
108     model.testNbSubFeatures(self.mySketch, "SketchLine", 0)
109     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 2)
110     # check middle points are different
111     assert(anArcEdge1.middlePoint().x() < self.myStartPoint.x())
112     assert(anArcEdge2.middlePoint().x() > self.myStartPoint.x())
113
114   def test_elliptic_arc_by_points(self):
115     """ Test 2. Create elliptic arc by points
116     """
117     self.mySketch.addEllipticArc(self.myCenter, self.myFocus, self.myStartPoint, self.myEndPoint, False)
118     self.myDOF += 7
119     model.do()
120     anEllipseFeature1 = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
121     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature1)
122
123     self.mySketch.addEllipticArc(self.myCenter, self.myFocus, self.myStartPoint, self.myEndPoint, True)
124     self.myDOF += 7
125     model.do()
126     anEllipseFeature2 = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
127     self.myEllipse2 = SketchAPI_EllipticArc(anEllipseFeature2)
128
129     # check both ellipses are equal
130     anArcEdge1 = anEllipseFeature1.lastResult().shape().edge()
131     anArcEdge2 = anEllipseFeature2.lastResult().shape().edge()
132     anEllipse1 = anArcEdge1.ellipse()
133     anEllipse2 = anArcEdge2.ellipse()
134     self.checkPointsEqual(anEllipse1.center(), anEllipse2.center())
135     self.checkPointsEqual(anEllipse1.firstFocus(), anEllipse2.firstFocus())
136     self.checkPointsEqual(anEllipse1.secondFocus(), anEllipse2.secondFocus())
137     self.assertAlmostEqual(anEllipse1.minorRadius(), anEllipse2.minorRadius())
138     self.assertAlmostEqual(anEllipse1.majorRadius(), anEllipse2.majorRadius())
139     self.checkPointsEqual(self.myEllipse1.startPoint(), self.myEllipse2.startPoint())
140     self.checkPointsEqual(self.myEllipse1.endPoint(), self.myEllipse2.endPoint())
141     # check number of features
142     model.testNbSubFeatures(self.mySketch, "SketchPoint", 14)
143     model.testNbSubFeatures(self.mySketch, "SketchLine", 4)
144     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 2)
145     # check middle points are different
146     assert(anArcEdge1.middlePoint().x() < self.myStartPoint.x())
147     assert(anArcEdge2.middlePoint().x() > self.myStartPoint.x())
148
149   def test_elliptic_arc_with_fixed_center(self):
150     """ Test 3. Create elliptic arc which center is coincident with another point
151     """
152     aLine = self.mySketch.addLine(10, 10, 30, 40)
153     self.myDOF += 4
154     model.do()
155
156     self.mySketch.addEllipticArc(aLine.endPoint(), self.myFocus, self.myStartPoint, self.myEndPoint, True)
157     self.myDOF += 5
158     model.do()
159     # check ellipse
160     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
161     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature)
162     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
163     self.checkPointsEqual(anEllipse.center(), aLine.endPoint())
164     self.checkPointOnEllipse(self.myStartPoint, anEllipse)
165     # check number of features
166     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
167     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
168     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
169     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
170
171   def test_elliptic_arc_with_center_on_line(self):
172     """ Test 4. Create elliptic arc which center is coincident with a line
173     """
174     aLine = self.mySketch.addLine(10, 10, 30, 40)
175     self.myDOF += 4
176     model.do()
177
178     self.mySketch.addEllipticArc([self.myCenter, aLine.result()], self.myFocus, self.myStartPoint, self.myEndPoint, False)
179     self.myDOF += 6
180     model.do()
181
182     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
183     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature)
184     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
185     # check center on line
186     self.checkPointOnLine(anEllipse.center(), aLine)
187     self.checkPointOnEllipse(self.myEllipse1.startPoint(), anEllipse)
188     self.checkPointOnEllipse(self.myEllipse1.endPoint(), anEllipse)
189     # check number of features
190     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
191     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
192     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
193     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
194
195   def test_elliptic_arc_with_center_on_circle(self):
196     """ Test 5. Create elliptic arc which center is coincident with a circle
197     """
198     aCircle = self.mySketch.addCircle(10, 10, 20)
199     self.myDOF += 3
200     model.do()
201
202     self.mySketch.addEllipticArc([self.myCenter, aCircle.defaultResult()], self.myFocus, self.myStartPoint, self.myEndPoint, False)
203     self.myDOF += 6
204     model.do()
205
206     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
207     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature)
208     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
209     # check center on circle
210     self.checkPointOnCircle(anEllipse.center(), aCircle)
211     self.checkPointOnEllipse(self.myEllipse1.startPoint(), anEllipse)
212     self.checkPointOnEllipse(self.myEllipse1.endPoint(), anEllipse)
213     # check number of features
214     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
215     model.testNbSubFeatures(self.mySketch, "SketchLine", 2)
216     model.testNbSubFeatures(self.mySketch, "SketchCircle", 1)
217     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
218     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
219
220   def test_elliptic_arc_with_center_on_ellipse(self):
221     """ Test 6. Create elliptic arc which center is coincident with another ellipse
222     """
223     anOtherEllipse = self.mySketch.addEllipse(10, 10, 30, 20, 10)
224     self.myDOF += 5
225     model.do()
226
227     self.mySketch.addEllipticArc([self.myCenter, anOtherEllipse.defaultResult()], self.myFocus, self.myStartPoint, self.myEndPoint, False)
228     self.myDOF += 6
229     model.do()
230
231     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
232     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature)
233     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
234     # check center on ellipse
235     self.checkPointOnEllipse(anEllipse.center(), anOtherEllipse)
236     self.checkPointOnEllipse(self.myEllipse1.startPoint(), anEllipse)
237     self.checkPointOnEllipse(self.myEllipse1.endPoint(), anEllipse)
238     # check number of features
239     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
240     model.testNbSubFeatures(self.mySketch, "SketchLine", 2)
241     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
242     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
243     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
244
245   def test_elliptic_arc_with_fixed_axis(self):
246     """ Test 7. Create elliptic arc which point on major semi-axis is coincident with another point
247     """
248     aLine = self.mySketch.addLine(10, 10, 30, 40)
249     self.myDOF += 4
250     model.do()
251
252     self.mySketch.addEllipticArc(self.myCenter, aLine.endPoint(), self.myStartPoint, self.myEndPoint, False)
253     self.myDOF += 6
254     model.do()
255     # check ellipse
256     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
257     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature)
258     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
259     self.checkPointOnEllipse(self.myStartPoint, anEllipse)
260     self.checkPointOnEllipse(aLine.endPoint(), anEllipse)
261     # check distance is equal to major semi-axis
262     dist = model.distancePointPoint(GeomAPI_Pnt2d(anEllipse.center().x(), anEllipse.center().y()), aLine.endPoint())
263     self.assertAlmostEqual(dist, anEllipse.majorRadius(), 7)
264     self.checkPointsEqual(self.myEllipse1.majorAxisPositive(), aLine.endPoint())
265     # check number of features
266     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
267     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
268     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
269     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
270
271   def test_elliptic_arc_with_axis_on_line(self):
272     """ Test 8. Create elliptic arc which point on major semi-axis is coincident with a line.
273                 Check no coincidence constraint is created.
274     """
275     aLine = self.mySketch.addLine(10, 10, 30, 40)
276     self.myDOF += 4
277     model.do()
278
279     self.mySketch.addEllipticArc(self.myCenter, [self.myFocus, aLine.result()], self.myStartPoint, self.myEndPoint, True)
280     self.myDOF += 7
281     model.do()
282
283     # check number of features
284     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
285     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
286     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
287     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 0)
288
289   def test_elliptic_arc_with_fixed_start_point(self):
290     """ Test 9. Create elliptic arc which start point is coincident with another point
291     """
292     aLine = self.mySketch.addLine(10, 10, 30, 40)
293     self.myDOF += 4
294     model.do()
295
296     self.mySketch.addEllipticArc(self.myCenter, self.myFocus, aLine.endPoint(), self.myEndPoint, True)
297     self.myDOF += 5
298     model.do()
299     # check ellipse
300     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
301     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature)
302     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
303     self.checkPointOnEllipse(aLine.endPoint(), anEllipse)
304     self.checkPointsEqual(aLine.endPoint(), self.myEllipse1.startPoint())
305     # check number of features
306     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
307     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
308     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
309     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
310
311   def test_elliptic_arc_with_start_point_on_line(self):
312     """ Test 10. Create elliptic arc which start point is placed on a line.
313     """
314     aLine = self.mySketch.addLine(10, 10, 30, 40)
315     self.myDOF += 4
316     model.do()
317
318     self.mySketch.addEllipticArc(self.myCenter, self.myFocus, [self.myStartPoint, aLine.result()], self.myEndPoint, False)
319     self.myDOF += 6
320     model.do()
321
322     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
323     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature)
324     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
325     self.checkPointOnLine(self.myEllipse1.startPoint(), aLine)
326     # check number of features
327     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
328     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
329     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
330     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
331
332   def test_elliptic_arc_with_fixed_end_point(self):
333     """ Test 11. Create elliptic arc which end point is coincident with another point
334     """
335     aLine = self.mySketch.addLine(10, 10, 30, 40)
336     self.myDOF += 4
337     model.do()
338
339     self.mySketch.addEllipticArc(self.myCenter, self.myFocus, self.myStartPoint, aLine.endPoint(), True)
340     self.myDOF += 5
341     model.do()
342     # check ellipse
343     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
344     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature)
345     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
346     self.checkPointOnEllipse(aLine.endPoint(), anEllipse)
347     self.checkPointsEqual(aLine.endPoint(), self.myEllipse1.endPoint())
348     # check number of features
349     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
350     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
351     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
352     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
353
354   def test_elliptic_arc_with_end_point_on_line(self):
355     """ Test 12. Create elliptic arc which end point is placed on a line.
356     """
357     aLine = self.mySketch.addLine(10, 10, 30, 40)
358     self.myDOF += 4
359     model.do()
360
361     self.mySketch.addEllipticArc(self.myCenter, self.myFocus, self.myStartPoint, [self.myEndPoint, aLine.result()], False)
362     self.myDOF += 6
363     model.do()
364
365     anEllipseFeature = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
366     self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature)
367     anEllipse = anEllipseFeature.lastResult().shape().edge().ellipse()
368     self.checkPointOnLine(self.myEllipse1.endPoint(), aLine)
369     # check number of features
370     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
371     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
372     model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
373     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
374
375
376 if __name__ == "__main__":
377     test_program = unittest.main(exit=False)
378     assert test_program.result.wasSuccessful(), "Test failed"
379     assert model.checkPythonDump()