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 constraint "Distance" applied sub-elements of an ellipse
27 from salome.shaper import model
30 from SketchAPI import *
32 __updated__ = "2019-09-12"
35 class TestDistanceEllipse(unittest.TestCase):
37 axisStart = GeomAPI_Pnt2d(20., 60.)
38 axisEnd = GeomAPI_Pnt2d(80., 50.)
39 passedPoint = GeomAPI_Pnt2d(60., 70.)
42 self.myDocument = model.moduleDocument()
43 self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
44 macroEllipse = self.mySketch.addEllipse(axisStart, axisEnd, passedPoint, False)
46 self.myOrigin = self.mySketch.addPoint("Origin")
47 self.myOX = self.mySketch.addLine("OX")
49 self.myEllipse = SketchAPI_Ellipse(model.lastSubFeature(self.mySketch, "SketchEllipse"))
50 self.myCenter = macroEllipse.center()
51 self.myFocus1 = macroEllipse.focus1()
52 self.myFocus2 = macroEllipse.focus2()
53 self.myMajorAxis = macroEllipse.majorAxis()
54 self.myMajorStart = macroEllipse.majorAxisStart()
55 self.myMajorEnd = macroEllipse.majorAxisEnd()
56 self.myMinorAxis = macroEllipse.minorAxis()
57 self.myMinorStart = macroEllipse.minorAxisStart()
58 self.myMinorEnd = macroEllipse.minorAxisEnd()
59 self.myExpectFailure = False
64 if self.myExpectFailure:
65 assert(self.mySketch.solverError() != ""), "PlaneGCS limitation: if you see this message, then PlaneGCS has solved the set of constraints correctly"
69 self.assertPoints(self.myCenter.coordinates(), self.myEllipse.center())
70 self.assertPoints(self.myFocus1.coordinates(), self.myEllipse.firstFocus())
71 self.assertPoints(self.myFocus2.coordinates(), self.myEllipse.secondFocus())
72 self.assertPoints(self.myMajorStart.coordinates(), self.myEllipse.majorAxisNegative())
73 self.assertPoints(self.myMajorEnd.coordinates(), self.myEllipse.majorAxisPositive())
74 self.assertPoints(self.myMajorAxis.startPoint(), self.myEllipse.majorAxisNegative())
75 self.assertPoints(self.myMajorAxis.endPoint(), self.myEllipse.majorAxisPositive())
76 self.assertPoints(self.myMinorStart.coordinates(), self.myEllipse.minorAxisNegative())
77 self.assertPoints(self.myMinorEnd.coordinates(), self.myEllipse.minorAxisPositive())
78 self.assertPoints(self.myMinorAxis.startPoint(), self.myEllipse.minorAxisNegative())
79 self.assertPoints(self.myMinorAxis.endPoint(), self.myEllipse.minorAxisPositive())
80 model.testNbSubFeatures(self.mySketch, "SketchPoint", 8)
81 model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
82 model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
83 model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidenceInternal", 11)
84 model.testNbSubFeatures(self.mySketch, "SketchConstraintDistance", 1)
88 self.assertEqual(model.dof(self.mySketch), self.myDOF)
90 def checkPointPointDistance(self, thePoint1, thePoint2):
91 self.mySketch.setDistance(thePoint1, thePoint2, self.myDistance)
94 if not self.myExpectFailure:
95 NB_DIGITS = math.floor(-math.log10(TOLERANCE) - math.log10(self.myDistance))
96 self.assertAlmostEqual(model.distancePointPoint(thePoint1, thePoint2), self.myDistance, NB_DIGITS)
97 self.assertGreater(self.myEllipse.majorRadius().value(), 0.0)
98 self.assertGreater(self.myEllipse.minorRadius().value(), 0.0)
100 def checkPointLineDistance(self, thePoint, theLine):
101 self.mySketch.setDistance(thePoint, theLine.result(), self.myDistance)
104 NB_DIGITS = math.floor(-math.log10(TOLERANCE) - math.log10(self.myDistance))
105 self.assertAlmostEqual(model.distancePointLine(thePoint, theLine), self.myDistance, NB_DIGITS)
106 self.assertGreater(self.myEllipse.majorRadius().value(), 0.0)
107 self.assertGreater(self.myEllipse.minorRadius().value(), 0.0)
109 def assertPoints(self, thePoint1, thePoint2):
110 NB_DIGITS = math.floor(-math.log10(TOLERANCE) - math.log10(self.myDistance))
111 self.assertAlmostEqual(thePoint1.x(), thePoint2.x(), NB_DIGITS)
112 self.assertAlmostEqual(thePoint1.y(), thePoint2.y(), NB_DIGITS)
115 def test_distance_center(self):
116 """ Test 1. Set distance to the Origin from the center of ellipse
118 self.checkPointPointDistance(self.myCenter.coordinates(), self.myOrigin.coordinates())
120 def test_distance_first_focus(self):
121 """ Test 2. Set distance to the Origin from the first focus of ellipse
123 self.checkPointPointDistance(self.myFocus1.coordinates(), self.myOrigin.coordinates())
125 def test_distance_second_focus(self):
126 """ Test 3. Set distance to the Origin from the second focus of ellipse
128 self.checkPointPointDistance(self.myFocus2.coordinates(), self.myOrigin.coordinates())
130 def test_distance_major_axis_start(self):
131 """ Test 4. Set distance to the Origin from the start point on the major axis of ellipse
133 self.checkPointPointDistance(self.myMajorStart.coordinates(), self.myOrigin.coordinates())
135 def test_distance_major_axis_end(self):
136 """ Test 5. Set distance to the Origin from the end point on the major axis of ellipse
138 self.checkPointPointDistance(self.myMajorEnd.coordinates(), self.myOrigin.coordinates())
140 def test_distance_minor_axis_start(self):
141 """ Test 6. Set distance to the Origin from the start point on the minor axis of ellipse
143 self.checkPointPointDistance(self.myMinorStart.coordinates(), self.myOrigin.coordinates())
145 def test_distance_minor_axis_end(self):
146 """ Test 7. Set distance to the Origin from the end point on the minor axis of ellipse
148 self.myExpectFailure = True
149 self.checkPointPointDistance(self.myMinorEnd.coordinates(), self.myOrigin.coordinates())
152 def test_distance_center_to_line(self):
153 """ Test 8. Set distance from theOX to the center of ellipse
155 self.checkPointLineDistance(self.myCenter.coordinates(), self.myOX)
157 def test_distance_first_focus_to_line(self):
158 """ Test 9. Set distance from theOX to the first focus of ellipse
160 self.checkPointLineDistance(self.myFocus1.coordinates(), self.myOX)
162 def test_distance_second_focus_to_line(self):
163 """ Test 10. Set distance from theOX to the second focus of ellipse
165 self.checkPointLineDistance(self.myFocus2.coordinates(), self.myOX)
167 def test_distance_major_axis_start_to_line(self):
168 """ Test 11. Set distance from theOX to the start point on the major axis of ellipse
170 self.checkPointLineDistance(self.myMajorStart.coordinates(), self.myOX)
172 def test_distance_major_axis_end_to_line(self):
173 """ Test 12. Set distance from theOX to the end point on the major axis of ellipse
175 self.checkPointLineDistance(self.myMajorEnd.coordinates(), self.myOX)
177 def test_distance_minor_axis_start_to_line(self):
178 """ Test 13. Set distance from theOX to the start point on the minor axis of ellipse
180 self.checkPointLineDistance(self.myMinorStart.coordinates(), self.myOX)
182 def test_distance_minor_axis_end_to_line(self):
183 """ Test 14. Set distance from theOX to the end point on the minor axis of ellipse
185 self.myDistance = 150
186 self.checkPointLineDistance(self.myMinorEnd.coordinates(), self.myOX)
189 def test_distance_origin_to_major_axis(self):
190 """ Test 15. Set distance from the Origin to the major axis of the ellipse
192 self.checkPointLineDistance(self.myOrigin.coordinates(), self.myMajorAxis)
194 def test_distance_origin_to_minor_axis(self):
195 """ Test 16. Set distance from the Origin to the minor axis of the ellipse
197 self.checkPointLineDistance(self.myOrigin.coordinates(), self.myMinorAxis)
200 if __name__ == "__main__":
201 test_program = unittest.main(exit=False)
202 assert test_program.result.wasSuccessful(), "Test failed"
203 assert model.checkPythonDump()