1 # Copyright (C) 2019-2021 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 Test creation of ellipse by majoraxis and passed point
25 from salome.shaper import model
28 from SketchAPI import *
30 __updated__ = "2019-09-12"
32 class TestEllipse(unittest.TestCase):
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.)
48 self.assertEqual(model.dof(self.mySketch), self.myDOF)
50 def checkPointsEqual(self, thePoint1, thePoint2):
51 self.assertAlmostEqual(thePoint1.x(), thePoint2.x(), 5)
52 self.assertAlmostEqual(thePoint1.y(), thePoint2.y(), 5)
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)
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)
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)
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
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)
79 anEllipseFeature1 = model.lastSubFeature(self.mySketch, "SketchEllipse")
81 self.myEllipse2 = self.mySketch.addEllipse(self.myAxisStart, self.myAxisEnd, self.myPassedPoint, False)
84 anEllipseFeature2 = model.lastSubFeature(self.mySketch, "SketchEllipse")
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)
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
105 aLine = self.mySketch.addLine(10, 10, 30, 40)
109 self.myEllipse1 = self.mySketch.addEllipse(aLine.startPoint(), self.myAxisEnd, self.myPassedPoint, False)
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)
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
129 aLine = self.mySketch.addLine(10, 10, 30, 40)
133 self.myEllipse1 = self.mySketch.addEllipse([self.myAxisStart, aLine.result()], self.myAxisEnd, self.myPassedPoint, False)
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)
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
149 aCircle = self.mySketch.addCircle(10, 10, 20)
153 self.myEllipse1 = self.mySketch.addEllipse([self.myAxisStart, aCircle.defaultResult()], self.myAxisEnd, self.myPassedPoint, False)
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)
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
170 anOtherEllipse = self.mySketch.addEllipse(10, 10, 30, 20, 10)
174 self.myEllipse1 = self.mySketch.addEllipse([self.myAxisStart, anOtherEllipse.defaultResult()], self.myAxisEnd, self.myPassedPoint, False)
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)
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
190 aLine = self.mySketch.addLine(10, 10, 90, 40)
194 self.myEllipse1 = self.mySketch.addEllipse(self.myAxisStart, aLine.endPoint(), self.myPassedPoint, False)
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)
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
214 aLine = self.mySketch.addLine(10, 10, 30, 40)
218 self.myEllipse1 = self.mySketch.addEllipse(self.myAxisStart, [self.myAxisEnd, aLine.result()], self.myPassedPoint, False)
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)
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
234 aCircle = self.mySketch.addCircle(10, 10, 20)
238 self.myEllipse1 = self.mySketch.addEllipse(self.myAxisStart, [self.myAxisEnd, aCircle.defaultResult()], self.myPassedPoint, False)
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)
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
255 anOtherEllipse = self.mySketch.addEllipse(10, 10, 90, 40, 30)
259 self.myEllipse1 = self.mySketch.addEllipse(self.myAxisStart, [self.myAxisEnd, anOtherEllipse.defaultResult()], self.myPassedPoint, False)
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)
272 def test_ellipse_with_fixed_passed_point(self):
273 """ Test 10. Create ellipse which passed point is coincident with another point
275 aLine = self.mySketch.addLine(10, 10, 30, 40)
279 self.myEllipse1 = self.mySketch.addEllipse(self.myAxisStart, self.myAxisEnd, aLine.endPoint(), False)
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)
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.
296 aLine = self.mySketch.addLine(10, 10, 30, 40)
300 self.myEllipse1 = self.mySketch.addEllipse(self.myAxisStart, self.myAxisEnd, [self.myPassedPoint, aLine.result()], False)
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)
316 if __name__ == "__main__":
317 test_program = unittest.main(exit=False)
318 assert test_program.result.wasSuccessful(), "Test failed"
319 assert model.checkPythonDump()