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 elliptic arc by center, semi-axis, start and end points
25 from salome.shaper import model
28 from SketchAPI import *
30 __updated__ = "2019-10-01"
32 class TestEllipticArc(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., 60.)
39 self.myStartPoint = GeomAPI_Pnt2d(60., 65.)
40 self.myEndPoint = GeomAPI_Pnt2d(60., 42.535751)
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_elliptic_arc_by_coordinates(self):
79 """ Test 1. Create elliptic arc by coordinates of center, point on the major axis, start and end points
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)
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)
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())
114 def test_elliptic_arc_by_points(self):
115 """ Test 2. Create elliptic arc by points
117 self.mySketch.addEllipticArc(self.myCenter, self.myFocus, self.myStartPoint, self.myEndPoint, False)
120 anEllipseFeature1 = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
121 self.myEllipse1 = SketchAPI_EllipticArc(anEllipseFeature1)
123 self.mySketch.addEllipticArc(self.myCenter, self.myFocus, self.myStartPoint, self.myEndPoint, True)
126 anEllipseFeature2 = model.lastSubFeature(self.mySketch, "SketchEllipticArc")
127 self.myEllipse2 = SketchAPI_EllipticArc(anEllipseFeature2)
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())
149 def test_elliptic_arc_with_fixed_center(self):
150 """ Test 3. Create elliptic arc which center is coincident with another point
152 aLine = self.mySketch.addLine(10, 10, 30, 40)
156 self.mySketch.addEllipticArc(aLine.endPoint(), self.myFocus, self.myStartPoint, self.myEndPoint, True)
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)
171 def test_elliptic_arc_with_center_on_line(self):
172 """ Test 4. Create elliptic arc which center is coincident with a line
174 aLine = self.mySketch.addLine(10, 10, 30, 40)
178 self.mySketch.addEllipticArc([self.myCenter, aLine.result()], self.myFocus, self.myStartPoint, self.myEndPoint, False)
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)
195 def test_elliptic_arc_with_center_on_circle(self):
196 """ Test 5. Create elliptic arc which center is coincident with a circle
198 aCircle = self.mySketch.addCircle(10, 10, 20)
202 self.mySketch.addEllipticArc([self.myCenter, aCircle.defaultResult()], self.myFocus, self.myStartPoint, self.myEndPoint, False)
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)
220 def test_elliptic_arc_with_center_on_ellipse(self):
221 """ Test 6. Create elliptic arc which center is coincident with another ellipse
223 anOtherEllipse = self.mySketch.addEllipse(10, 10, 30, 20, 10)
227 self.mySketch.addEllipticArc([self.myCenter, anOtherEllipse.defaultResult()], self.myFocus, self.myStartPoint, self.myEndPoint, False)
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)
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
248 aLine = self.mySketch.addLine(10, 10, 30, 40)
252 self.mySketch.addEllipticArc(self.myCenter, aLine.endPoint(), self.myStartPoint, self.myEndPoint, False)
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)
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.
275 aLine = self.mySketch.addLine(10, 10, 30, 40)
279 self.mySketch.addEllipticArc(self.myCenter, [self.myFocus, aLine.result()], self.myStartPoint, self.myEndPoint, True)
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)
289 def test_elliptic_arc_with_fixed_start_point(self):
290 """ Test 9. Create elliptic arc which start point is coincident with another point
292 aLine = self.mySketch.addLine(10, 10, 30, 40)
296 self.mySketch.addEllipticArc(self.myCenter, self.myFocus, aLine.endPoint(), self.myEndPoint, True)
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)
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.
314 aLine = self.mySketch.addLine(10, 10, 30, 40)
318 self.mySketch.addEllipticArc(self.myCenter, self.myFocus, [self.myStartPoint, aLine.result()], self.myEndPoint, False)
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)
332 def test_elliptic_arc_with_fixed_end_point(self):
333 """ Test 11. Create elliptic arc which end point is coincident with another point
335 aLine = self.mySketch.addLine(10, 10, 30, 40)
339 self.mySketch.addEllipticArc(self.myCenter, self.myFocus, self.myStartPoint, aLine.endPoint(), True)
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)
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.
357 aLine = self.mySketch.addLine(10, 10, 30, 40)
361 self.mySketch.addEllipticArc(self.myCenter, self.myFocus, self.myStartPoint, [self.myEndPoint, aLine.result()], False)
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)
376 if __name__ == "__main__":
377 test_program = unittest.main(exit=False)
378 assert test_program.result.wasSuccessful(), "Test failed"
379 assert model.checkPythonDump()