Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistanceHorizontalZero.py
1 # Copyright (C) 2019-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 the zero value of the constraint "DistanceHorizontal"
22 """
23
24 import unittest
25 import math
26
27 from salome.shaper import model
28 from SketchAPI import *
29
30 __updated__ = "2019-10-22"
31 TOLERANCE = 1.e-6
32
33 class TestZeroDistanceHorizontal(unittest.TestCase):
34   def setUp(self):
35     model.begin()
36     self.myDocument = model.moduleDocument()
37     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
38     self.myLine1 = self.mySketch.addLine(10, 10, 45, 27.5)
39     self.myLine2 = self.mySketch.addLine(20, 15, 30, 40)
40     self.myLine3 = self.mySketch.addLine(10, 0, 10, 10)
41     model.do()
42     self.myDOF = 12
43
44   def tearDown(self):
45     model.end()
46     model.checkSketch(self.mySketch, self.myDOF)
47
48   def assertDistanceHorizontal(self, theObject1, theObject2, theDistance):
49     dist = theObject2.x() - theObject1.x()
50     self.assertTrue(math.fabs(dist - theDistance) < TOLERANCE, "Current distance = {}, reference = {}".format(dist, theDistance))
51     model.checkSketch(self.mySketch, self.myDOF)
52
53
54   def test_distance_positive_nzznz(self):
55     """ Test 1. Change distance from non-zero to zero and back to non-zero
56     """
57     dist = self.mySketch.setHorizontalDistance(self.myLine1.startPoint(), self.myLine2.startPoint(), 10)
58     self.myDOF -= 1
59     model.do()
60     self.assertDistanceHorizontal(self.myLine1.startPoint(), self.myLine2.startPoint(), 10)
61
62     SketchAPI_Constraint(dist).setValue(0)
63     model.do()
64     self.assertDistanceHorizontal(self.myLine1.startPoint(), self.myLine2.startPoint(), 0)
65
66     SketchAPI_Constraint(dist).setValue(10)
67     model.do()
68     self.assertDistanceHorizontal(self.myLine1.startPoint(), self.myLine2.startPoint(), 10)
69
70   def test_distance_negative_nzznz(self):
71     """ Test 2. Change distance from non-zero to zero and back to non-zero
72     """
73     dist = self.mySketch.setHorizontalDistance(self.myLine1.endPoint(), self.myLine2.endPoint(), 15)
74     self.myDOF -= 1
75     model.do()
76     self.assertDistanceHorizontal(self.myLine1.endPoint(), self.myLine2.endPoint(), -15)
77
78     SketchAPI_Constraint(dist).setValue(0)
79     model.do()
80     self.assertDistanceHorizontal(self.myLine1.endPoint(), self.myLine2.endPoint(), 0)
81
82     SketchAPI_Constraint(dist).setValue(10)
83     model.do()
84     self.assertDistanceHorizontal(self.myLine1.endPoint(), self.myLine2.endPoint(), -10)
85
86   def test_distance_equal_znzz(self):
87     """ Test 3. Change distance from zero to non-zero and back to zero
88     """
89     dist = self.mySketch.setHorizontalDistance(self.myLine1.startPoint(), self.myLine3.endPoint(), 0)
90     self.myDOF -= 1
91     model.do()
92     self.assertDistanceHorizontal(self.myLine1.startPoint(), self.myLine3.endPoint(), 0)
93
94     SketchAPI_Constraint(dist).setValue(10)
95     model.do()
96     self.assertDistanceHorizontal(self.myLine1.startPoint(), self.myLine3.endPoint(), 10)
97
98     SketchAPI_Constraint(dist).setValue(0)
99     model.do()
100     self.assertDistanceHorizontal(self.myLine1.startPoint(), self.myLine3.endPoint(), 0)
101
102   def test_distance_notequal_znzz(self):
103     """ Test 4. Change distance from zero to non-zero and back to zero
104     """
105     dist = self.mySketch.setHorizontalDistance(self.myLine1.startPoint(), self.myLine3.startPoint(), 0)
106     self.myDOF -= 1
107     model.do()
108     self.assertDistanceHorizontal(self.myLine1.startPoint(), self.myLine3.startPoint(), 0)
109
110     SketchAPI_Constraint(dist).setValue(10)
111     model.do()
112     self.assertDistanceHorizontal(self.myLine1.startPoint(), self.myLine3.startPoint(), 10)
113
114     SketchAPI_Constraint(dist).setValue(0)
115     model.do()
116     self.assertDistanceHorizontal(self.myLine1.startPoint(), self.myLine3.startPoint(), 0)
117
118
119 if __name__ == "__main__":
120     test_program = unittest.main(exit=False)
121     assert test_program.result.wasSuccessful(), "Test failed"
122     assert model.checkPythonDump()