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 constraint coincidence applied for elliptic arc and its sub-results
27 from salome.shaper import model
30 from SketchAPI import *
32 __updated__ = "2019-10-02"
34 class TestCoincidenceEllipticArc(unittest.TestCase):
36 center = GeomAPI_Pnt2d(-10., 5.)
37 axisEnd = GeomAPI_Pnt2d(40., -5.)
38 startPoint = GeomAPI_Pnt2d(20., 5.)
39 endPoint = GeomAPI_Pnt2d(-40., 5.)
42 self.myDocument = model.moduleDocument()
43 self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
44 macroEllipticArc = self.mySketch.addEllipticArc(center, axisEnd, startPoint, endPoint, False)
46 self.myOrigin = self.mySketch.addPoint("Origin")
47 self.myOX = self.mySketch.addLine("OX")
49 self.myEllipticArc = SketchAPI_EllipticArc(model.lastSubFeature(self.mySketch, "SketchEllipticArc"))
50 self.myCenter = macroEllipticArc.center()
51 self.myFocus1 = macroEllipticArc.focus1()
52 self.myFocus2 = macroEllipticArc.focus2()
53 self.myMajorAxis = macroEllipticArc.majorAxis()
54 self.myMajorStart = macroEllipticArc.majorAxisStart()
55 self.myMajorEnd = macroEllipticArc.majorAxisEnd()
56 self.myMinorAxis = macroEllipticArc.minorAxis()
57 self.myMinorStart = macroEllipticArc.minorAxisStart()
58 self.myMinorEnd = macroEllipticArc.minorAxisEnd()
59 self.myExpectFailure = False
63 if self.myExpectFailure:
64 assert(self.mySketch.solverError() != ""), "PlaneGCS limitation: if you see this message, then PlaneGCS has solved the set of constraints correctly"
68 self.assertPoints(self.myCenter.coordinates(), self.myEllipticArc.center())
69 self.assertPoints(self.myFocus1.coordinates(), self.myEllipticArc.firstFocus())
70 self.assertPoints(self.myFocus2.coordinates(), self.myEllipticArc.secondFocus())
71 self.assertPoints(self.myMajorStart.coordinates(), self.myEllipticArc.majorAxisNegative())
72 self.assertPoints(self.myMajorEnd.coordinates(), self.myEllipticArc.majorAxisPositive())
73 self.assertPoints(self.myMajorAxis.startPoint(), self.myEllipticArc.majorAxisNegative())
74 self.assertPoints(self.myMajorAxis.endPoint(), self.myEllipticArc.majorAxisPositive())
75 self.assertPoints(self.myMinorStart.coordinates(), self.myEllipticArc.minorAxisNegative())
76 self.assertPoints(self.myMinorEnd.coordinates(), self.myEllipticArc.minorAxisPositive())
77 self.assertPoints(self.myMinorAxis.startPoint(), self.myEllipticArc.minorAxisNegative())
78 self.assertPoints(self.myMinorAxis.endPoint(), self.myEllipticArc.minorAxisPositive())
79 model.testNbSubFeatures(self.mySketch, "SketchPoint", 8)
80 model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
81 model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", 1)
82 model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidenceInternal", 11)
83 model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", 1)
87 self.assertEqual(model.dof(self.mySketch), self.myDOF)
89 def checkPointFixing(self, thePoint):
90 self.mySketch.setCoincident(thePoint, self.myOrigin.coordinates())
93 if not self.myExpectFailure:
94 self.assertPoints(thePoint, self.myOrigin.coordinates())
95 self.assertGreater(self.myEllipticArc.majorRadius().value(), 0.0)
96 self.assertGreater(self.myEllipticArc.minorRadius().value(), 0.0)
98 def assertPoints(self, thePoint1, thePoint2):
99 self.assertAlmostEqual(thePoint1.x(), thePoint2.x())
100 self.assertAlmostEqual(thePoint1.y(), thePoint2.y())
102 def checkPointOnAxis(self, thePoint):
103 self.mySketch.setCoincident(thePoint, self.myOX.result())
106 if not self.myExpectFailure:
107 self.assertAlmostEqual(thePoint.y(), 0.0)
108 self.assertGreater(self.myEllipticArc.majorRadius().value(), 0.0)
109 self.assertGreater(self.myEllipticArc.minorRadius().value(), 0.0)
111 def checkPointOnLine(self, thePoint, theLineStart, theLineEnd):
112 vecP = [thePoint.x() - theLineStart.x(), thePoint.y() - theLineStart.y()]
113 vecL = [theLineEnd.x() - theLineStart.x(), theLineEnd.y() - theLineStart.y()]
114 dist = math.fabs(vecP[0] * vecL[1] - vecP[1] * vecL[0]) / math.hypot(vecL[0], vecL[1])
116 self.assertAlmostEqual(dist, 0.0)
117 self.assertGreater(self.myEllipticArc.majorRadius().value(), 0.0)
118 self.assertGreater(self.myEllipticArc.minorRadius().value(), 0.0)
120 def checkPointOnEllipse(self, thePoint, theEllipse):
121 firstFocus2d = GeomAPI_Pnt2d(theEllipse.firstFocus().x(), theEllipse.firstFocus().y())
122 distPF1 = model.distancePointPoint(firstFocus2d, thePoint)
123 secondFocus2d = GeomAPI_Pnt2d(theEllipse.secondFocus().x(), theEllipse.secondFocus().y())
124 distPF2 = model.distancePointPoint(secondFocus2d, thePoint)
125 self.assertAlmostEqual(distPF1 + distPF2, 2.0 * theEllipse.majorRadius().value(), 7)
128 def test_concident_center(self):
129 """ Test 1. Make center of elliptic arc coincident with the Origin
131 self.checkPointFixing(self.myCenter.coordinates())
133 def test_coincident_first_focus(self):
134 """ Test 2. Make first focus of elliptic arc coincident with the Origin
136 self.checkPointFixing(self.myFocus1.coordinates())
138 def test_coincident_second_focus(self):
139 """ Test 3. Make second focus of elliptic arc coincident with the Origin
141 self.checkPointFixing(self.myFocus2.coordinates())
143 def test_coincident_major_axis_start(self):
144 """ Test 4. Make start point on the major axis of elliptic arc coincident with the Origin
146 self.checkPointFixing(self.myMajorStart.coordinates())
148 def test_coincident_major_axis_end(self):
149 """ Test 5. Make end point on the major axis of elliptic arc coincident with the Origin
151 self.checkPointFixing(self.myMajorEnd.coordinates())
153 def test_coincident_minor_axis_start(self):
154 """ Test 6. Make start point on the minor axis of elliptic arc coincident with the Origin
156 self.checkPointFixing(self.myMinorStart.coordinates())
158 def test_coincident_minor_axis_end(self):
159 """ Test 7. Make end point on the minor axis of elliptic arc coincident with the Origin.
160 Check solver is failed to compute the coincidence.
162 self.myExpectFailure = True
163 self.checkPointFixing(self.myMinorEnd.coordinates())
165 def test_coincident_start(self):
166 """ Test 8. Make start point of elliptic arc coincident with the Origin.
167 Check solver is failed to compute the coincidence.
169 self.myExpectFailure = True
170 self.checkPointFixing(self.myEllipticArc.startPoint())
172 def test_coincident_end(self):
173 """ Test 9. Make end point of elliptic arc coincident with the Origin
175 self.checkPointFixing(self.myEllipticArc.endPoint())
178 def test_center_on_line(self):
179 """ Test 10. Make center of elliptic arc coincident with the OX
181 self.checkPointOnAxis(self.myCenter.coordinates())
183 def test_first_focus_on_line(self):
184 """ Test 11. Make first focus of elliptic arc coincident with the OX
186 self.checkPointOnAxis(self.myFocus1.coordinates())
188 def test_second_focus_on_line(self):
189 """ Test 12. Make second focus of elliptic arc coincident with the OX
191 self.checkPointOnAxis(self.myFocus2.coordinates())
193 def test_major_axis_start_on_line(self):
194 """ Test 13. Make start point on the major axis of elliptic arc coincident with the OX
196 self.checkPointOnAxis(self.myMajorStart.coordinates())
198 def test_major_axis_end_on_line(self):
199 """ Test 14. Make end point on the major axis of elliptic arc coincident with the OX
201 self.checkPointOnAxis(self.myMajorEnd.coordinates())
203 def test_minor_axis_start_on_line(self):
204 """ Test 15. Make start point on the minor axis of elliptic arc coincident with the OX
206 self.checkPointOnAxis(self.myMinorStart.coordinates())
208 def test_minor_axis_end_on_line(self):
209 """ Test 16. Make end point on the minor axis of elliptic arc coincident with the OX
211 self.myExpectFailure = True
212 self.checkPointOnAxis(self.myMinorEnd.coordinates())
214 def test_coincident_start_on_line(self):
215 """ Test 17. Make start point of elliptic arc coincident with the OX
217 self.checkPointOnAxis(self.myEllipticArc.startPoint())
219 def test_coincident_end_on_line(self):
220 """ Test 18. Make end point of elliptic arc coincident with the OX
222 self.checkPointOnAxis(self.myEllipticArc.endPoint())
225 def test_origin_on_major_axis(self):
226 """ Test 19. Make origin coincident with the major axis of the elliptic arc
228 self.mySketch.setCoincident(self.myMajorAxis.result(), self.myOrigin.coordinates())
231 self.checkPointOnLine(self.myOrigin.coordinates(), self.myMajorStart.coordinates(), self.myMajorEnd.coordinates())
233 def test_origin_on_minor_axis(self):
234 """ Test 20. Make origin coincident with the minor axis of the elliptic arc
236 self.mySketch.setCoincident(self.myMinorAxis.result(), self.myOrigin.coordinates())
239 # solver shows wrong result
240 assert(self.mySketch.solverError() != ""), "PlaneGCS limitation: if you see this message, then PlaneGCS has solved the set of constraints correctly"
243 # move elliptic arc and set coincidence once again
245 self.mySketch.move(self.myMinorStart, 20, 10)
247 self.mySketch.setCoincident(self.myMinorAxis.results()[-1], self.myOrigin.coordinates())
249 self.checkPointOnLine(self.myOrigin.coordinates(), self.myMinorStart.coordinates(), self.myMinorEnd.coordinates())
252 def test_origin_on_ellipse(self):
253 """ Test 21. Make origin coincident with the elliptic arc
255 self.mySketch.setCoincident(self.myEllipticArc.results()[-1], self.myOrigin.coordinates())
258 self.checkPointOnEllipse(self.myOrigin.coordinates(), self.myEllipticArc)
261 def test_start_on_major_axis(self):
262 """ Test 22. Make coincident the start point of the arc and the end of its major axis
264 self.myEllipticArc.startPoint().setValue(38, -2)
266 self.mySketch.setCoincident(self.myEllipticArc.startPoint(), self.myMajorAxis.endPoint())
269 self.assertPoints(self.myMajorAxis.endPoint(), self.myEllipticArc.startPoint())
271 def test_end_on_major_axis(self):
272 """ Test 23. Make coincident the end point of the arc and the start of its major axis
274 self.myEllipticArc.endPoint().setValue(-58, 12)
276 self.mySketch.setCoincident(self.myMajorAxis.startPoint(), self.myEllipticArc.endPoint())
279 self.assertPoints(self.myMajorAxis.startPoint(), self.myEllipticArc.endPoint())
281 def test_start_on_minor_axis(self):
282 """ Test 22. Make coincident the start point of the arc and the end of its minor axis
284 self.mySketch.setCoincident(self.myMinorAxis.endPoint(), self.myEllipticArc.startPoint())
287 self.assertPoints(self.myMinorAxis.endPoint(), self.myEllipticArc.startPoint())
289 def test_end_on_minor_axis(self):
290 """ Test 23. Make coincident the end point of the arc and the start of its minor axis
292 self.mySketch.setCoincident(self.myEllipticArc.endPoint(), self.myMinorAxis.startPoint())
295 self.assertPoints(self.myMinorAxis.startPoint(), self.myEllipticArc.endPoint())
298 if __name__ == "__main__":
299 test_program = unittest.main(exit=False)
300 assert test_program.result.wasSuccessful(), "Test failed"
301 assert model.checkPythonDump()