Salome HOME
abe100a1ffc9cc97f18598166c6c8bdf844885f1
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintAngleEllipse.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 "Angle" 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-12"
33
34 class TestAngleEllipse(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     self.myAngle = 60
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.myOX = self.mySketch.addLine("OX")
47     model.do()
48     self.myEllipse = SketchAPI_Ellipse(model.lastSubFeature(self.mySketch, "SketchEllipse"))
49     self.myCenter = macroEllipse.center()
50     self.myFocus1 = macroEllipse.focus1()
51     self.myFocus2 = macroEllipse.focus2()
52     self.myMajorAxis = macroEllipse.majorAxis()
53     self.myMajorStart = macroEllipse.majorAxisStart()
54     self.myMajorEnd = macroEllipse.majorAxisEnd()
55     self.myMinorAxis = macroEllipse.minorAxis()
56     self.myMinorStart = macroEllipse.minorAxisStart()
57     self.myMinorEnd = macroEllipse.minorAxisEnd()
58
59
60   def tearDown(self):
61     model.end()
62     self.checkDOF()
63     self.assertPoints(self.myCenter.coordinates(), self.myEllipse.center())
64     self.assertPoints(self.myFocus1.coordinates(), self.myEllipse.firstFocus())
65     self.assertPoints(self.myFocus2.coordinates(), self.myEllipse.secondFocus())
66     self.assertPoints(self.myMajorStart.coordinates(), self.myEllipse.majorAxisNegative())
67     self.assertPoints(self.myMajorEnd.coordinates(), self.myEllipse.majorAxisPositive())
68     self.assertPoints(self.myMajorAxis.startPoint(), self.myEllipse.majorAxisNegative())
69     self.assertPoints(self.myMajorAxis.endPoint(), self.myEllipse.majorAxisPositive())
70     self.assertPoints(self.myMinorStart.coordinates(), self.myEllipse.minorAxisNegative())
71     self.assertPoints(self.myMinorEnd.coordinates(), self.myEllipse.minorAxisPositive())
72     self.assertPoints(self.myMinorAxis.startPoint(), self.myEllipse.minorAxisNegative())
73     self.assertPoints(self.myMinorAxis.endPoint(), self.myEllipse.minorAxisPositive())
74     model.testNbSubFeatures(self.mySketch, "SketchPoint", 7)
75     model.testNbSubFeatures(self.mySketch, "SketchLine", 3)
76     model.testNbSubFeatures(self.mySketch, "SketchEllipse", 1)
77     model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidenceInternal", 11)
78
79
80   def checkDOF(self):
81     self.assertEqual(model.dof(self.mySketch), self.myDOF)
82
83   def assertPoints(self, thePoint1, thePoint2):
84     self.assertAlmostEqual(thePoint1.x(), thePoint2.x())
85     self.assertAlmostEqual(thePoint1.y(), thePoint2.y())
86
87   def assertAngle(self, theStart, theEnd, theAngle):
88     dirX = (theEnd.x() - theStart.x()) / math.hypot(theEnd.x() - theStart.x(), theEnd.y() - theStart.y())
89     self.assertAlmostEqual(math.fabs(dirX), math.cos(theAngle * math.pi / 180))
90
91
92   def test_angle_major_axis(self):
93     """ Test 1. Make angle between major axis of ellipse and OX
94     """
95     self.mySketch.setAngle(self.myOX.result(), self.myMajorAxis.result(), self.myAngle)
96     self.myDOF -= 1
97     model.do()
98     self.assertAngle(self.myMajorAxis.startPoint(), self.myMajorAxis.endPoint(), self.myAngle)
99     self.assertAngle(self.myMajorStart.coordinates(), self.myMajorEnd.coordinates(), self.myAngle)
100     model.testNbSubFeatures(self.mySketch, "SketchConstraintAngle", 1)
101
102   def test_angle_minor_axis(self):
103     """ Test 2. Make angle between minor axis of ellipse and OX
104     """
105     self.mySketch.setAngle(self.myOX.result(), self.myMinorAxis.result(), self.myAngle)
106     self.myDOF -= 1
107     model.do()
108     self.assertAngle(self.myMinorAxis.startPoint(), self.myMinorAxis.endPoint(), self.myAngle)
109     self.assertAngle(self.myMinorStart.coordinates(), self.myMinorEnd.coordinates(), self.myAngle)
110     model.testNbSubFeatures(self.mySketch, "SketchConstraintAngle", 1)
111
112   def test_angle_ellipse_axes(self):
113     """ Test 3. Set angle between axes of an ellipse. Check conflicting constraints.
114     """
115     self.mySketch.setAngle(self.myMajorAxis.result(), self.myMinorAxis.result(), self.myAngle)
116     model.end()
117     self.assertNotEqual(self.mySketch.solverError(), "")
118     model.undo()
119     model.begin()
120
121
122 if __name__ == "__main__":
123     test_program = unittest.main(exit=False)
124     assert test_program.result.wasSuccessful(), "Test failed"
125     assert model.checkPythonDump()