1 # Copyright (C) 2019-2020 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 Test the zero value of the constraint "Distance"
27 from salome.shaper import model
28 from SketchAPI import *
30 __updated__ = "2019-10-22"
33 class TestZeroDistance(unittest.TestCase):
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)
46 model.checkSketch(self.mySketch, self.myDOF)
48 def assertDistance(self, theObject1, theObject2, theDistance, isSigned = False):
50 if issubclass(type(theObject1), SketchAPI_SketchEntity):
52 dist = model.signedDistancePointLine(theObject2, theObject1)
54 dist = model.distancePointLine(theObject2, theObject1)
55 elif issubclass(type(theObject2), SketchAPI_SketchEntity):
57 dist = model.signedDistancePointLine(theObject1, theObject2)
59 dist = model.distancePointLine(theObject1, theObject2)
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)
66 def test_distance_points_nzznz(self):
67 """ Test 1. Change point-point distance from non-zero to zero and back to non-zero
69 dist = self.mySketch.setDistance(self.myLine1.endPoint(), self.myLine2.endPoint(), 20)
72 self.assertDistance(self.myLine1.endPoint(), self.myLine2.endPoint(), 20)
74 SketchAPI_Constraint(dist).setValue(0)
77 self.assertDistance(self.myLine1.endPoint(), self.myLine2.endPoint(), 0)
79 SketchAPI_Constraint(dist).setValue(20)
82 self.assertDistance(self.myLine1.endPoint(), self.myLine2.endPoint(), 20)
84 def test_distance_points_znzz(self):
85 """ Test 2. Change point-point distance from zero to non-zero and back to zero
87 dist = self.mySketch.setDistance(self.myLine1.startPoint(), self.myLine3.endPoint(), 0)
90 self.assertDistance(self.myLine1.startPoint(), self.myLine3.endPoint(), 0)
92 SketchAPI_Constraint(dist).setValue(10)
95 self.assertDistance(self.myLine1.startPoint(), self.myLine3.endPoint(), 10)
97 SketchAPI_Constraint(dist).setValue(0)
100 self.assertDistance(self.myLine1.startPoint(), self.myLine3.endPoint(), 0)
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
106 dist = self.mySketch.setDistance(self.myLine1.result(), self.myLine2.endPoint(), 20)
109 self.assertDistance(self.myLine1, self.myLine2.endPoint(), 20)
111 SketchAPI_Constraint(dist).setValue(0)
113 self.assertDistance(self.myLine1, self.myLine2.endPoint(), 0)
115 SketchAPI_Constraint(dist).setValue(20)
117 self.assertDistance(self.myLine1, self.myLine2.endPoint(), 20)
119 def test_unsigned_distance_znzz(self):
120 """ Test 4. Change unsigned point-line distance from zero to non-zero and back to zero
122 dist = self.mySketch.setDistance(self.myLine2.startPoint(), self.myLine1.result(), 0)
125 self.assertDistance(self.myLine2.startPoint(), self.myLine1, 0)
127 SketchAPI_Constraint(dist).setValue(10)
129 self.assertDistance(self.myLine2.startPoint(), self.myLine1, 10)
131 SketchAPI_Constraint(dist).setValue(0)
133 self.assertDistance(self.myLine2.startPoint(), self.myLine1, 0)
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
139 dist = self.mySketch.setDistance(self.myLine1.result(), self.myLine2.endPoint(), 20, True)
142 self.assertDistance(self.myLine1, self.myLine2.endPoint(), -20, True)
144 SketchAPI_Constraint(dist).setValue(0)
146 self.assertDistance(self.myLine1, self.myLine2.endPoint(), 0, True)
148 SketchAPI_Constraint(dist).setValue(20)
150 self.assertDistance(self.myLine1, self.myLine2.endPoint(), -20, True)
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
155 dist = self.mySketch.setDistance(self.myLine3.startPoint(), self.myLine1.result(), 10, True)
158 self.assertDistance(self.myLine1, self.myLine3.startPoint(), 10, True)
160 SketchAPI_Constraint(dist).setValue(0)
162 self.assertDistance(self.myLine1, self.myLine3.startPoint(), 0, True)
164 SketchAPI_Constraint(dist).setValue(20)
166 self.assertDistance(self.myLine1, self.myLine3.startPoint(), 20, True)
168 def test_signed_distance_znzz(self):
169 """ Test 7. Change signed point-line distance from zero to non-zero and back to zero
171 dist = self.mySketch.setDistance(self.myLine2.startPoint(), self.myLine1.result(), 0, True)
174 self.assertDistance(self.myLine2.startPoint(), self.myLine1, 0)
176 SketchAPI_Constraint(dist).setValue(10)
178 self.assertDistance(self.myLine2.startPoint(), self.myLine1, 10)
180 SketchAPI_Constraint(dist).setValue(0)
182 self.assertDistance(self.myLine2.startPoint(), self.myLine1, 0)
185 if __name__ == "__main__":
186 test_program = unittest.main(exit=False)
187 assert test_program.result.wasSuccessful(), "Test failed"
188 assert model.checkPythonDump()