Salome HOME
Sketcher: implement possibility to set zero distance
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistanceZero.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 the zero value of the constraint "Distance"
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 TestZeroDistance(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 assertDistance(self, theObject1, theObject2, theDistance, isSigned = False):
49     dist = -1.
50     if issubclass(type(theObject1), SketchAPI_SketchEntity):
51       if isSigned:
52         dist = model.signedDistancePointLine(theObject2, theObject1)
53       else:
54         dist = model.distancePointLine(theObject2, theObject1)
55     elif issubclass(type(theObject2), SketchAPI_SketchEntity):
56       if isSigned:
57         dist = model.signedDistancePointLine(theObject1, theObject2)
58       else:
59         dist = model.distancePointLine(theObject1, theObject2)
60     else:
61       dist = model.distancePointPoint(theObject1, theObject2)
62     self.assertTrue(math.fabs(dist - theDistance) < TOLERANCE, "Current distance = {}, reference = {}".format(dist, theDistance))
63     model.checkSketch(self.mySketch, self.myDOF)
64
65
66   def test_distance_points_nzznz(self):
67     """ Test 1. Change point-point distance from non-zero to zero and back to non-zero
68     """
69     dist = self.mySketch.setDistance(self.myLine1.endPoint(), self.myLine2.endPoint(), 20)
70     self.myDOF -= 1
71     model.do()
72     self.assertDistance(self.myLine1.endPoint(), self.myLine2.endPoint(), 20)
73
74     SketchAPI_Constraint(dist).setValue(0)
75     self.myDOF -= 1
76     model.do()
77     self.assertDistance(self.myLine1.endPoint(), self.myLine2.endPoint(), 0)
78
79     SketchAPI_Constraint(dist).setValue(20)
80     self.myDOF += 1
81     model.do()
82     self.assertDistance(self.myLine1.endPoint(), self.myLine2.endPoint(), 20)
83
84   def test_distance_points_znzz(self):
85     """ Test 2. Change point-point distance from zero to non-zero and back to zero
86     """
87     dist = self.mySketch.setDistance(self.myLine1.startPoint(), self.myLine3.endPoint(), 0)
88     self.myDOF -= 2
89     model.do()
90     self.assertDistance(self.myLine1.startPoint(), self.myLine3.endPoint(), 0)
91
92     SketchAPI_Constraint(dist).setValue(10)
93     self.myDOF += 1
94     model.do()
95     self.assertDistance(self.myLine1.startPoint(), self.myLine3.endPoint(), 10)
96
97     SketchAPI_Constraint(dist).setValue(0)
98     self.myDOF -= 1
99     model.do()
100     self.assertDistance(self.myLine1.startPoint(), self.myLine3.endPoint(), 0)
101
102
103   def test_unsigned_distance_nzznz(self):
104     """ Test 3. Change unsigned point-line distance from non-zero to zero and back to non-zero
105     """
106     dist = self.mySketch.setDistance(self.myLine1.result(), self.myLine2.endPoint(), 20)
107     self.myDOF -= 1
108     model.do()
109     self.assertDistance(self.myLine1, self.myLine2.endPoint(), 20)
110
111     SketchAPI_Constraint(dist).setValue(0)
112     model.do()
113     self.assertDistance(self.myLine1, self.myLine2.endPoint(), 0)
114
115     SketchAPI_Constraint(dist).setValue(20)
116     model.do()
117     self.assertDistance(self.myLine1, self.myLine2.endPoint(), 20)
118
119   def test_unsigned_distance_znzz(self):
120     """ Test 4. Change unsigned point-line distance from zero to non-zero and back to zero
121     """
122     dist = self.mySketch.setDistance(self.myLine2.startPoint(), self.myLine1.result(), 0)
123     self.myDOF -= 1
124     model.do()
125     self.assertDistance(self.myLine2.startPoint(), self.myLine1, 0)
126
127     SketchAPI_Constraint(dist).setValue(10)
128     model.do()
129     self.assertDistance(self.myLine2.startPoint(), self.myLine1, 10)
130
131     SketchAPI_Constraint(dist).setValue(0)
132     model.do()
133     self.assertDistance(self.myLine2.startPoint(), self.myLine1, 0)
134
135
136   def test_signed_distance_nzznz(self):
137     """ Test 5. Change signed point-line distance from non-zero to zero and back to non-zero
138     """
139     dist = self.mySketch.setDistance(self.myLine1.result(), self.myLine2.endPoint(), 20, True)
140     self.myDOF -= 1
141     model.do()
142     self.assertDistance(self.myLine1, self.myLine2.endPoint(), -20, True)
143
144     SketchAPI_Constraint(dist).setValue(0)
145     model.do()
146     self.assertDistance(self.myLine1, self.myLine2.endPoint(), 0, True)
147
148     SketchAPI_Constraint(dist).setValue(20)
149     model.do()
150     self.assertDistance(self.myLine1, self.myLine2.endPoint(), -20, True)
151
152   def test_signed_distance_nzznz_2(self):
153     """ Test 6. Change signed point-line distance from non-zero to zero and back to non-zero
154     """
155     dist = self.mySketch.setDistance(self.myLine3.startPoint(), self.myLine1.result(), 10, True)
156     self.myDOF -= 1
157     model.do()
158     self.assertDistance(self.myLine1, self.myLine3.startPoint(), 10, True)
159
160     SketchAPI_Constraint(dist).setValue(0)
161     model.do()
162     self.assertDistance(self.myLine1, self.myLine3.startPoint(), 0, True)
163
164     SketchAPI_Constraint(dist).setValue(20)
165     model.do()
166     self.assertDistance(self.myLine1, self.myLine3.startPoint(), 20, True)
167
168   def test_signed_distance_znzz(self):
169     """ Test 7. Change signed point-line distance from zero to non-zero and back to zero
170     """
171     dist = self.mySketch.setDistance(self.myLine2.startPoint(), self.myLine1.result(), 0, True)
172     self.myDOF -= 1
173     model.do()
174     self.assertDistance(self.myLine2.startPoint(), self.myLine1, 0)
175
176     SketchAPI_Constraint(dist).setValue(10)
177     model.do()
178     self.assertDistance(self.myLine2.startPoint(), self.myLine1, 10)
179
180     SketchAPI_Constraint(dist).setValue(0)
181     model.do()
182     self.assertDistance(self.myLine2.startPoint(), self.myLine1, 0)
183
184
185 if __name__ == "__main__":
186     test_program = unittest.main(exit=False)
187     assert test_program.result.wasSuccessful(), "Test failed"
188     assert model.checkPythonDump()