Salome HOME
74eec4a8a65a38b98e8b0a194c08fa92fed8d175
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintCollinearEllipse.py
1 # Copyright (C) 2019-2023  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 "Collinear" applied for ellipse's axes
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-18"
33
34 class TestCollinearEllipse(unittest.TestCase):
35   def setUp(self):
36     axisStart = GeomAPI_Pnt2d(30., 60.)
37     axisEnd = GeomAPI_Pnt2d(80., 50.)
38     passedPoint = GeomAPI_Pnt2d(60., 60.)
39
40     model.begin()
41     self.myDocument = model.moduleDocument()
42     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
43     macroEllipse = self.mySketch.addEllipse(axisStart, axisEnd, passedPoint, False)
44     self.myDOF = 5
45     self.myOX = self.mySketch.addLine("OX")
46     model.do()
47     self.myEllipse = SketchAPI_Ellipse(model.lastSubFeature(self.mySketch, "SketchEllipse"))
48     self.myCenter = macroEllipse.center()
49     self.myFocus1 = macroEllipse.focus1()
50     self.myFocus2 = macroEllipse.focus2()
51     self.myMajorAxis = macroEllipse.majorAxis()
52     self.myMajorStart = macroEllipse.majorAxisStart()
53     self.myMajorEnd = macroEllipse.majorAxisEnd()
54     self.myMinorAxis = macroEllipse.minorAxis()
55     self.myMinorStart = macroEllipse.minorAxisStart()
56     self.myMinorEnd = macroEllipse.minorAxisEnd()
57
58
59   def tearDown(self):
60     model.end()
61     self.checkDOF()
62     self.assertPoints(self.myCenter.coordinates(), self.myEllipse.center())
63     self.assertPoints(self.myFocus1.coordinates(), self.myEllipse.firstFocus())
64     self.assertPoints(self.myFocus2.coordinates(), self.myEllipse.secondFocus())
65     self.assertPoints(self.myMajorStart.coordinates(), self.myEllipse.majorAxisNegative())
66     self.assertPoints(self.myMajorEnd.coordinates(), self.myEllipse.majorAxisPositive())
67     self.assertPoints(self.myMajorAxis.startPoint(), self.myEllipse.majorAxisNegative())
68     self.assertPoints(self.myMajorAxis.endPoint(), self.myEllipse.majorAxisPositive())
69     self.assertPoints(self.myMinorStart.coordinates(), self.myEllipse.minorAxisNegative())
70     self.assertPoints(self.myMinorEnd.coordinates(), self.myEllipse.minorAxisPositive())
71     self.assertPoints(self.myMinorAxis.startPoint(), self.myEllipse.minorAxisNegative())
72     self.assertPoints(self.myMinorAxis.endPoint(), self.myEllipse.minorAxisPositive())
73     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
74     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
75     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
76     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidenceInternal", 11)
77
78
79   def checkDOF(self):
80     self.assertEqual(model.dof(self.mySketch), self.myDOF)
81
82   def assertPoints(self, thePoint1, thePoint2):
83     self.assertAlmostEqual(thePoint1.x(), thePoint2.x())
84     self.assertAlmostEqual(thePoint1.y(), thePoint2.y())
85
86
87   def test_collinear_major_axis(self):
88     """ Test 1. Make major axis of ellipse collinear with OX
89     """
90     self.mySketch.setCollinear(self.myOX.result(), self.myMajorAxis.result())
91     self.myDOF -= 2
92     model.do()
93     self.assertAlmostEqual(self.myMajorAxis.startPoint().y(), 0)
94     self.assertAlmostEqual(self.myMajorAxis.endPoint().y(), 0)
95     self.assertAlmostEqual(self.myMajorStart.coordinates().y(), 0)
96     self.assertAlmostEqual(self.myMajorEnd.coordinates().y(), 0)
97     model.testNbSubFeatures(self.mySketch, "SketchConstraintCollinear", 1)
98
99   def test_collinear_minor_axis(self):
100     """ Test 2. Make minor axis of ellipse collinear with OX
101     """
102     self.mySketch.setCollinear(self.myOX.result(), self.myMinorAxis.result())
103     self.myDOF -= 2
104     model.end()
105     # solver shows wrong result
106     assert(self.mySketch.solverError() != ""), "PlaneGCS limitation: if you see this message, then PlaneGCS has solved the set of constraints correctly"
107     model.undo()
108
109     model.begin()
110     self.mySketch.move(self.myMinorStart.coordinates(), 0, 70)
111     self.mySketch.move(self.myMinorEnd.coordinates(), 50, 70)
112     model.do()
113     self.mySketch.setCollinear(self.myOX.result(), self.myMinorAxis.result())
114     model.do()
115     self.assertAlmostEqual(self.myMinorAxis.startPoint().y(), 0)
116     self.assertAlmostEqual(self.myMinorAxis.endPoint().y(), 0)
117     self.assertAlmostEqual(self.myMinorStart.coordinates().y(), 0)
118     self.assertAlmostEqual(self.myMinorEnd.coordinates().y(), 0)
119     model.testNbSubFeatures(self.mySketch, "SketchConstraintCollinear", 1)
120
121   def test_collinear_ellipse_axes(self):
122     """ Test 3. Set collinear axes of an ellipse. Check conflicting constraints.
123     """
124     self.mySketch.setCollinear(self.myMajorAxis, self.myMinorAxis)
125     model.end()
126     self.assertNotEqual(self.mySketch.solverError(), "")
127     model.undo()
128     model.begin()
129
130
131 if __name__ == "__main__":
132     test_program = unittest.main(exit=False)
133     assert test_program.result.wasSuccessful(), "Test failed"
134     assert model.checkPythonDump()