Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistanceEllipse.py
1 # Copyright (C) 2019-2022  CEA/DEN, EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21     Test constraint "Distance" applied sub-elements of an ellipse
22 """
23
24 import unittest
25 import math
26
27 from salome.shaper import model
28
29 from GeomAPI import *
30 from SketchAPI import *
31
32 __updated__ = "2019-09-12"
33 TOLERANCE = 1.e-7
34
35 class TestDistanceEllipse(unittest.TestCase):
36   def setUp(self):
37     axisStart = GeomAPI_Pnt2d(20., 60.)
38     axisEnd = GeomAPI_Pnt2d(80., 50.)
39     passedPoint = GeomAPI_Pnt2d(60., 70.)
40
41     model.begin()
42     self.myDocument = model.moduleDocument()
43     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
44     macroEllipse = self.mySketch.addEllipse(axisStart, axisEnd, passedPoint, False)
45     self.myDOF = 5
46     self.myOrigin = self.mySketch.addPoint("Origin")
47     self.myOX = self.mySketch.addLine("OX")
48     model.do()
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
60     self.myDistance = 50
61
62   def tearDown(self):
63     model.end()
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"
66       model.undo()
67     else:
68       self.checkDOF()
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)
85
86
87   def checkDOF(self):
88     self.assertEqual(model.dof(self.mySketch), self.myDOF)
89
90   def checkPointPointDistance(self, thePoint1, thePoint2):
91     self.mySketch.setDistance(thePoint1, thePoint2, self.myDistance)
92     self.myDOF -= 1
93     model.do()
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)
99
100   def checkPointLineDistance(self, thePoint, theLine):
101     self.mySketch.setDistance(thePoint, theLine.result(), self.myDistance)
102     self.myDOF -= 1
103     model.do()
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)
108
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)
113
114
115   def test_distance_center(self):
116     """ Test 1. Set distance to the Origin from the center of ellipse
117     """
118     self.checkPointPointDistance(self.myCenter.coordinates(), self.myOrigin.coordinates())
119
120   def test_distance_first_focus(self):
121     """ Test 2. Set distance to the Origin from the first focus of ellipse
122     """
123     self.checkPointPointDistance(self.myFocus1.coordinates(), self.myOrigin.coordinates())
124
125   def test_distance_second_focus(self):
126     """ Test 3. Set distance to the Origin from the second focus of ellipse
127     """
128     self.checkPointPointDistance(self.myFocus2.coordinates(), self.myOrigin.coordinates())
129
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
132     """
133     self.checkPointPointDistance(self.myMajorStart.coordinates(), self.myOrigin.coordinates())
134
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
137     """
138     self.checkPointPointDistance(self.myMajorEnd.coordinates(), self.myOrigin.coordinates())
139
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
142     """
143     self.checkPointPointDistance(self.myMinorStart.coordinates(), self.myOrigin.coordinates())
144
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
147     """
148     self.myExpectFailure = True
149     self.checkPointPointDistance(self.myMinorEnd.coordinates(), self.myOrigin.coordinates())
150
151
152   def test_distance_center_to_line(self):
153     """ Test 8. Set distance from theOX to the center of ellipse
154     """
155     self.checkPointLineDistance(self.myCenter.coordinates(), self.myOX)
156
157   def test_distance_first_focus_to_line(self):
158     """ Test 9. Set distance from theOX to the first focus of ellipse
159     """
160     self.checkPointLineDistance(self.myFocus1.coordinates(), self.myOX)
161
162   def test_distance_second_focus_to_line(self):
163     """ Test 10. Set distance from theOX to the second focus of ellipse
164     """
165     self.checkPointLineDistance(self.myFocus2.coordinates(), self.myOX)
166
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
169     """
170     self.checkPointLineDistance(self.myMajorStart.coordinates(), self.myOX)
171
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
174     """
175     self.checkPointLineDistance(self.myMajorEnd.coordinates(), self.myOX)
176
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
179     """
180     self.checkPointLineDistance(self.myMinorStart.coordinates(), self.myOX)
181
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
184     """
185     self.myDistance = 150
186     self.checkPointLineDistance(self.myMinorEnd.coordinates(), self.myOX)
187
188
189   def test_distance_origin_to_major_axis(self):
190     """ Test 15. Set distance from the Origin to the major axis of the ellipse
191     """
192     self.checkPointLineDistance(self.myOrigin.coordinates(), self.myMajorAxis)
193
194   def test_distance_origin_to_minor_axis(self):
195     """ Test 16. Set distance from the Origin to the minor axis of the ellipse
196     """
197     self.checkPointLineDistance(self.myOrigin.coordinates(), self.myMinorAxis)
198
199
200 if __name__ == "__main__":
201     test_program = unittest.main(exit=False)
202     assert test_program.result.wasSuccessful(), "Test failed"
203     assert model.checkPythonDump()