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