1 # Copyright (C) 2019 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 center, semi-axis and passed point
25 from salome.shaper import model
28 from SketchAPI import *
30 __updated__ = "2019-09-09"
32 class TestEllipse(unittest.TestCase):
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.
49 self.assertEqual(model.dof(self.mySketch), self.myDOF)
51 def checkPointsEqual(self, thePoint1, thePoint2):
52 self.assertAlmostEqual(thePoint1.x(), thePoint2.x(), 5)
53 self.assertAlmostEqual(thePoint1.y(), thePoint2.y(), 5)
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)
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)
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()
74 majorRad = theEllipse.majorRadius()
75 self.assertAlmostEqual(distPF1 + distPF2, 2.0 * majorRad, 7)
78 def test_ellipse_by_center_and_focus(self):
79 """ Test 1. Create ellipse by coordinates of center, focus and minor radius
81 self.myEllipse1 = self.mySketch.addEllipse(self.myCenter.x(), self.myCenter.y(), self.myFocus.x(), self.myFocus.y(), self.myMinorRadius)
84 self.myEllipse2 = self.mySketch.addEllipse(self.myCenter, self.myFocus, self.myMinorRadius)
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)
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
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)
107 anEllipseFeature1 = model.lastSubFeature(self.mySketch, "SketchEllipse")
109 self.myEllipse2 = self.mySketch.addEllipse(self.myCenter, self.myFocus, self.myPassedPoint, True)
112 anEllipseFeature2 = model.lastSubFeature(self.mySketch, "SketchEllipse")
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)
130 def test_ellipse_with_fixed_center(self):
131 """ Test 3. Create ellipse which center is coincident with another point
133 aLine = self.mySketch.addLine(10, 10, 30, 40)
137 self.myEllipse1 = self.mySketch.addEllipse(aLine.endPoint(), self.myFocus, self.myPassedPoint, True)
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)
151 def test_ellipse_with_center_on_line(self):
152 """ Test 4. Create ellipse which center is coincident with a line
154 aLine = self.mySketch.addLine(10, 10, 30, 40)
158 self.myEllipse1 = self.mySketch.addEllipse([self.myCenter, aLine.result()], self.myFocus, self.myPassedPoint, True)
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)
172 def test_ellipse_with_center_on_circle(self):
173 """ Test 5. Create ellipse which center is coincident with a circle
175 aCircle = self.mySketch.addCircle(10, 10, 20)
179 self.myEllipse1 = self.mySketch.addEllipse([self.myCenter, aCircle.defaultResult()], self.myFocus, self.myPassedPoint, True)
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)
194 def test_ellipse_with_center_on_ellipse(self):
195 """ Test 6. Create ellipse which center is coincident with another ellipse
197 anOtherEllipse = self.mySketch.addEllipse(10, 10, 30, 20, 10)
201 self.myEllipse1 = self.mySketch.addEllipse([self.myCenter, anOtherEllipse.defaultResult()], self.myFocus, self.myPassedPoint, True)
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)
215 def test_ellipse_with_fixed_axis(self):
216 """ Test 7. Create ellipse which point on major semi-axis is coincident with another point
218 aLine = self.mySketch.addLine(10, 10, 30, 40)
222 self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, aLine.endPoint(), self.myPassedPoint, True)
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)
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
242 aLine = self.mySketch.addLine(10, 10, 30, 40)
246 self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, [self.myFocus, aLine.result()], self.myPassedPoint, True)
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)
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
262 aCircle = self.mySketch.addCircle(10, 10, 20)
266 self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, [self.myFocus, aCircle.defaultResult()], self.myPassedPoint, True)
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)
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
283 anOtherEllipse = self.mySketch.addEllipse(10, 10, 90, 40, 30)
287 self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, [self.myFocus, anOtherEllipse.defaultResult()], self.myPassedPoint, True)
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)
300 def test_ellipse_with_fixed_passed_point(self):
301 """ Test 11. Create ellipse which passed point is coincident with another point
303 aLine = self.mySketch.addLine(10, 10, 30, 40)
307 self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, self.myFocus, aLine.endPoint(), True)
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)
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.
324 aLine = self.mySketch.addLine(10, 10, 30, 40)
328 self.myEllipse1 = self.mySketch.addEllipse(self.myCenter, self.myFocus, [self.myPassedPoint, aLine.result()], True)
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)
344 if __name__ == "__main__":
345 test_program = unittest.main(exit=False)
346 assert test_program.result.wasSuccessful(), "Test failed"
347 assert model.checkPythonDump()