Salome HOME
Merge remote-tracking branch 'origin/CEA_2019'
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateEllipticArcByExternal.py
1 # Copyright (C) 2019  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 creation of elliptic arc by external feature
22 """
23
24 import unittest
25 from salome.shaper import model
26
27 from GeomAPI import *
28 from SketchAPI import *
29
30 __updated__ = "2019-10-02"
31
32 # reference data
33 CENTER_POINT = GeomAPI_Pnt2d(50., 50.)
34 MAJOR_AXIS_POINT = GeomAPI_Pnt2d(70., 60.)
35 START_POINT = GeomAPI_Pnt2d(60., 65.)
36 END_POINT = GeomAPI_Pnt2d(60., 42.535751)
37 ARC_LENGTH_1 = 0
38 ARC_LENGTH_2 = 0
39
40 class TestEllipticArcByExternal(unittest.TestCase):
41   def setUp(self):
42     model.begin()
43     self.myDocument = model.moduleDocument()
44     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
45     self.myDOF = 0
46
47   def tearDown(self):
48     self.checkDOF()
49     model.end()
50
51
52   def checkDOF(self):
53     self.assertEqual(model.dof(self.mySketch), self.myDOF)
54
55   def checkPointsEqual(self, thePoint1, thePoint2):
56     self.assertAlmostEqual(thePoint1.x(), thePoint2.x(), 5)
57     self.assertAlmostEqual(thePoint1.y(), thePoint2.y(), 5)
58
59
60   def test_elliptic_arc_by_external_name_1(self):
61     """ Test 1. Create elliptic arc by name of external edge (direct)
62     """
63     self.myEllipse = self.mySketch.addEllipticArc("Sketch_1/SketchEllipticArc_1")
64     model.do()
65
66     # check ellipse parameters
67     anArcEdge = self.myEllipse.defaultResult().shape().edge()
68     anEllipse = anArcEdge.ellipse()
69     self.checkPointsEqual(anEllipse.center(), CENTER_POINT)
70     self.checkPointsEqual(self.myEllipse.majorAxisPositive(), MAJOR_AXIS_POINT)
71     self.checkPointsEqual(self.myEllipse.startPoint(), START_POINT)
72     self.checkPointsEqual(self.myEllipse.endPoint(), END_POINT)
73     self.assertAlmostEqual(anArcEdge.length(), ARC_LENGTH_1)
74
75   def test_elliptic_arc_by_external_name_2(self):
76     """ Test 2. Create elliptic arc by name of external edge (reversed)
77     """
78     self.myEllipse = self.mySketch.addEllipticArc("Sketch_1/SketchEllipticArc_2")
79     model.do()
80
81     # check ellipse parameters
82     anArcEdge = self.myEllipse.defaultResult().shape().edge()
83     anEllipse = anArcEdge.ellipse()
84     self.checkPointsEqual(anEllipse.center(), CENTER_POINT)
85     self.checkPointsEqual(self.myEllipse.majorAxisPositive(), MAJOR_AXIS_POINT)
86     self.checkPointsEqual(anArcEdge.firstPoint(), END_POINT)
87     self.checkPointsEqual(anArcEdge.lastPoint(), START_POINT)
88     self.assertAlmostEqual(anArcEdge.length(), ARC_LENGTH_2)
89
90   def test_elliptic_arc_by_external_selection_1(self):
91     """ Test 3. Create elliptic arc by selected edge (direct)
92     """
93     self.myEllipse = self.mySketch.addEllipticArc(ELLIPTIC_ARC_1.results()[-1])
94     model.do()
95
96     # check ellipse parameters
97     anArcEdge = self.myEllipse.defaultResult().shape().edge()
98     anEllipse = anArcEdge.ellipse()
99     self.checkPointsEqual(anEllipse.center(), CENTER_POINT)
100     self.checkPointsEqual(self.myEllipse.majorAxisPositive(), MAJOR_AXIS_POINT)
101     self.checkPointsEqual(self.myEllipse.startPoint(), START_POINT)
102     self.checkPointsEqual(self.myEllipse.endPoint(), END_POINT)
103     self.assertAlmostEqual(anArcEdge.length(), ARC_LENGTH_1)
104
105   def test_elliptic_arc_by_external_selection_2(self):
106     """ Test 4. Create elliptic arc by selected edge (reversed)
107     """
108     self.myEllipse = self.mySketch.addEllipticArc(ELLIPTIC_ARC_2.results()[-1])
109     model.do()
110
111     # check ellipse parameters
112     anArcEdge = self.myEllipse.defaultResult().shape().edge()
113     anEllipse = anArcEdge.ellipse()
114     self.checkPointsEqual(anEllipse.center(), CENTER_POINT)
115     self.checkPointsEqual(self.myEllipse.majorAxisPositive(), MAJOR_AXIS_POINT)
116     self.checkPointsEqual(self.myEllipse.startPoint(), END_POINT)
117     self.checkPointsEqual(self.myEllipse.endPoint(), START_POINT)
118     self.assertAlmostEqual(anArcEdge.length(), ARC_LENGTH_2)
119
120
121 if __name__ == "__main__":
122     model.begin()
123     aDocument = model.moduleDocument()
124     aSketch = model.addSketch(aDocument, model.defaultPlane("XOY"))
125     aSketch.addEllipticArc(CENTER_POINT, MAJOR_AXIS_POINT, START_POINT, END_POINT, False)
126     model.do()
127
128     ELLIPTIC_ARC_1 = SketchAPI_EllipticArc(model.lastSubFeature(aSketch, "SketchEllipticArc"))
129     ARC_LENGTH_1 = ELLIPTIC_ARC_1.defaultResult().shape().edge().length()
130
131     aSketch.addEllipticArc(CENTER_POINT, MAJOR_AXIS_POINT, START_POINT, END_POINT, True)
132     model.end()
133
134     ELLIPTIC_ARC_2 = SketchAPI_EllipticArc(model.lastSubFeature(aSketch, "SketchEllipticArc"))
135     ARC_LENGTH_2 = ELLIPTIC_ARC_2.defaultResult().shape().edge().length()
136
137     # redefine end point
138     END_POINT = ELLIPTIC_ARC_2.endPoint()
139
140     test_program = unittest.main(exit=False)
141     assert test_program.result.wasSuccessful(), "Test failed"
142     assert model.checkPythonDump()