Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchPlugin / Test / TestMoveLine.py
1 # Copyright (C) 2017-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 movement of the sketch line
22 """
23
24 import unittest
25 from GeomDataAPI import geomDataAPI_Point2D
26 from salome.shaper import model
27
28 __updated__ = "2017-05-11"
29
30 class TestMoveLine(unittest.TestCase):
31   def setUp(self):
32     model.begin()
33     self.myDocument = model.moduleDocument()
34     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
35     self.myStart = [70., 50.]
36     self.myEnd = [100., 20.]
37     self.myLine = self.mySketch.addLine(self.myStart[0], self.myStart[1], self.myEnd[0], self.myEnd[1])
38     self.myDOF = 4
39     model.do()
40     self.checkDOF()
41
42   def tearDown(self):
43     self.checkDOF()
44     model.end()
45     assert(model.checkPythonDump())
46
47   def checkDOF(self):
48     self.assertEqual(model.dof(self.mySketch), self.myDOF)
49
50   def checkPointCoordinates(self, thePoint, theCoordinates):
51     self.assertAlmostEqual(thePoint.x(), theCoordinates[0])
52     self.assertAlmostEqual(thePoint.y(), theCoordinates[1])
53
54   def checkPointOnLine(self, theCoordinates):
55     self.assertAlmostEqual(model.distancePointLine(theCoordinates, self.myLine), 0.)
56
57   def test_move_start_of_free_line(self):
58     """ Test 1. Movement of start point of a free line
59     """
60     newPosition = [self.myStart[0] + 20., self.myStart[1] + 10.]
61     self.mySketch.move(self.myLine.startPoint(), newPosition[0], newPosition[1])
62     model.do()
63     self.checkPointCoordinates(self.myLine.startPoint(), newPosition)
64     self.checkPointCoordinates(self.myLine.endPoint(), self.myEnd)
65
66   def test_move_end_of_free_line(self):
67     """ Test 2. Movement of end point of a free line
68     """
69     newPosition = [self.myEnd[0] + 20., self.myEnd[1] + 10.]
70     self.mySketch.move(self.myLine.endPoint(), newPosition[0], newPosition[1])
71     model.do()
72     self.checkPointCoordinates(self.myLine.startPoint(), self.myStart)
73     self.checkPointCoordinates(self.myLine.endPoint(), newPosition)
74
75   def test_move_free_line(self):
76     """ Test 3. Movement of free line
77     """
78     diff = [self.myEnd[0] - self.myStart[0], self.myEnd[1] - self.myStart[1]]
79
80     newPosition = [100., 100.]
81     self.mySketch.move(self.myLine.defaultResult(), newPosition[0], newPosition[1])
82     model.do()
83     self.checkPointOnLine(newPosition)
84
85     # additionally check the line keeps geometry (relative positions of extremities)
86     startPoint = self.myLine.startPoint()
87     endPoint = [startPoint.x() + diff[0], startPoint.y() + diff[1]]
88     self.checkPointCoordinates(self.myLine.endPoint(), endPoint)
89
90   def test_move_line_start_fixed(self):
91     """ Test 4. Movement of a line, which start point is fixed
92     """
93     fixed = self.mySketch.setFixed(self.myLine.startPoint())
94     self.myDOF -= 2
95     model.do()
96     self.checkDOF()
97
98     newPosition = [100., 100.]
99     self.mySketch.move(self.myLine.defaultResult(), newPosition[0], newPosition[1])
100     model.do()
101     self.checkPointCoordinates(self.myLine.startPoint(), self.myStart)
102     self.assertNotEqual(self.myLine.endPoint().x(), self.myEnd[0])
103     self.assertNotEqual(self.myLine.endPoint().y(), self.myEnd[1])
104
105   def test_move_line_end_fixed(self):
106     """ Test 5. Movement of a line, which end point is fixed
107     """
108     fixed = self.mySketch.setFixed(self.myLine.endPoint())
109     self.myDOF -= 2
110     model.do()
111     self.checkDOF()
112
113     newPosition = [100., 100.]
114     self.mySketch.move(self.myLine.defaultResult(), newPosition[0], newPosition[1])
115     model.do()
116     self.assertNotEqual(self.myLine.startPoint().x(), self.myStart[0])
117     self.assertNotEqual(self.myLine.startPoint().y(), self.myStart[1])
118     self.checkPointCoordinates(self.myLine.endPoint(), self.myEnd)
119
120   def test_move_line_fully_fixed(self):
121     """ Test 6. Movement of fully fixed line (should not change its coordinates)
122     """
123     fixed = self.mySketch.setFixed(self.myLine.defaultResult())
124     self.myDOF -= 4
125     model.do()
126
127     newPosition = [100., 100.]
128     self.mySketch.move(self.myLine.defaultResult(), newPosition[0], newPosition[1])
129     model.do()
130     self.checkPointCoordinates(self.myLine.startPoint(), self.myStart)
131     self.checkPointCoordinates(self.myLine.endPoint(), self.myEnd)
132
133
134 if __name__ == "__main__":
135     test_program = unittest.main(exit=False)
136     assert test_program.result.wasSuccessful(), "Test failed"