Salome HOME
556df334d81e6297b57c74666b568764f3ab9516
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintMiddlePointOnArc.py
1 # Copyright (C) 2017-2021  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 middle point on an arc
22 """
23
24 import unittest
25 import math
26
27 from salome.shaper import model
28 from GeomAPI import GeomAPI_Dir2d
29
30 __updated__ = "2019-09-03"
31
32 class TestMiddlePointOnArc(unittest.TestCase):
33   def setUp(self):
34     model.begin()
35     self.myTestPassed = True
36     self.myDocument = model.moduleDocument()
37     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
38     self.myArc = self.mySketch.addArc(50, 50, 70, 50, 50, 70, False)
39     self.myLine = self.mySketch.addLine(55, 60, 50, 0)
40     self.myDOF = 9
41     model.do()
42     self.checkDOF()
43
44   def tearDown(self):
45     if self.myTestPassed:
46       model.assertArcValidity(self.myArc)
47       self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
48       self.checkDOF()
49     model.end()
50     assert(model.checkPythonDump())
51
52   def checkDOF(self):
53     self.assertEqual(model.dof(self.mySketch), self.myDOF)
54
55   def checkMiddlePoint(self, thePoint, theArc):
56     self.myTestPassed = False
57     # check point on arc
58     dist = thePoint.pnt().distance(theArc.center().pnt())
59     NB_DIGITS = 7 - math.floor(math.log10(theArc.radius().value()))
60     self.assertAlmostEqual(dist, theArc.radius().value(), NB_DIGITS)
61     # check middle point
62     dirPC = GeomAPI_Dir2d(thePoint.x() - theArc.center().x(),
63                           thePoint.y() - theArc.center().y())
64     dirSC = GeomAPI_Dir2d(theArc.startPoint().x() - theArc.center().x(),
65                           theArc.startPoint().y() - theArc.center().y())
66     dirEC = GeomAPI_Dir2d(theArc.endPoint().x() - theArc.center().x(),
67                           theArc.endPoint().y() - theArc.center().y())
68     angleSP = dirSC.angle(dirPC)
69     anglePE = dirPC.angle(dirEC)
70     self.assertAlmostEqual(angleSP, anglePE)
71     self.assertEqual(angleSP < 0, theArc.reversed().value())
72     self.myTestPassed = True
73
74   def rotatePoint(self, thePoint, theCenter, theAngle):
75     dirX = thePoint.x() - theCenter.x()
76     dirY = thePoint.y() - theCenter.y()
77     newX = theCenter.x() + dirX * math.cos(theAngle) - dirY * math.sin(theAngle)
78     newY = theCenter.y() + dirX * math.sin(theAngle) + dirY * math.cos(theAngle)
79     self.mySketch.move(thePoint, newX, newY)
80
81   def moveArc(self):
82     ANGLE_STEP = math.pi * 5.0 / 180.0
83     ANGLE_THRESHOLD = math.pi
84     # move start point of the arc clockwise
85     fullAngle = 0.0
86     while fullAngle < ANGLE_THRESHOLD:
87       self.rotatePoint(self.myArc.startPoint(), self.myArc.center(), -ANGLE_STEP)
88       model.do()
89       self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
90       fullAngle += ANGLE_STEP
91     # move start point of the arc conterclockwise
92     fullAngle = 0.0
93     while fullAngle < ANGLE_THRESHOLD:
94       self.rotatePoint(self.myArc.startPoint(), self.myArc.center(), ANGLE_STEP)
95       model.do()
96       self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
97       fullAngle += ANGLE_STEP
98
99     # move end point of the arc clockwise
100     fullAngle = 0.0
101     while fullAngle < ANGLE_THRESHOLD:
102       self.rotatePoint(self.myArc.endPoint(), self.myArc.center(), -ANGLE_STEP)
103       model.do()
104       self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
105       fullAngle += ANGLE_STEP
106     # move end point of the arc conterclockwise
107     fullAngle = 0.0
108     while fullAngle < ANGLE_THRESHOLD:
109       self.rotatePoint(self.myArc.endPoint(), self.myArc.center(), ANGLE_STEP)
110       model.do()
111       self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
112       fullAngle += ANGLE_STEP
113
114     # move center of the arc
115     DELTA = [1.0, 1.0]
116     for i in range(0, 40):
117       if i == 10 or i == 30:
118         DELTA = [-DELTA[0], -DELTA[1]]
119       self.mySketch.move(self.myArc.center(), self.myArc.center().x() + DELTA[0], self.myArc.center().y() + DELTA[1])
120       model.do()
121       self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
122     DELTA = [-1.0, 1.0]
123     for i in range(0, 40):
124       if i == 10 or i == 30:
125         DELTA = [-DELTA[0], -DELTA[1]]
126       self.mySketch.move(self.myArc.center(), self.myArc.center().x() + DELTA[0], self.myArc.center().y() + DELTA[1])
127       model.do()
128       self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
129
130   def moveLine(self):
131     DELTA = [1.0, 0.0]
132     for i in range(0, 40):
133       if i == 10 or i == 30:
134         DELTA = [-DELTA[0], -DELTA[1]]
135       self.mySketch.move(self.myLine.startPoint(), self.myLine.startPoint().x() + DELTA[0], self.myLine.startPoint().y() + DELTA[1])
136       model.do()
137       self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
138     DELTA = [0.0, 1.0]
139     for i in range(0, 40):
140       if i == 10 or i == 30:
141         DELTA = [-DELTA[0], -DELTA[1]]
142       self.mySketch.move(self.myLine.startPoint(), self.myLine.startPoint().x() + DELTA[0], self.myLine.startPoint().y() + DELTA[1])
143       model.do()
144       self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
145
146
147   def test_middle_point_PA(self):
148     """ Test 1. Set middle point constraint (point is the first argument)
149     """
150     self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
151     self.myDOF -= 2
152     model.do()
153
154   def test_middle_point_AP(self):
155     """ Test 2. Set middle point constraint (point is the second argument)
156     """
157     self.mySketch.setMiddlePoint(self.myArc.results()[1], self.myLine.startPoint())
158     self.myDOF -= 2
159     model.do()
160
161   def test_coincident_middle_point(self):
162     """ Test 3. Set middle point constraint for the point already coincident with the arc
163     """
164     self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
165     model.do()
166     self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
167     self.myDOF -= 2
168     model.do()
169
170   def test_middle_point_coincident(self):
171     """ Test 4. Set concidence of the point and the arc which are already constrained with middle point
172     """
173     self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
174     model.do()
175     self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
176     self.myDOF -= 2
177     model.do()
178
179   @unittest.expectedFailure
180   def test_middle_point_limitation(self):
181     """ Test 5. Check middle point fails if the point's coordinates are equal to the arc boundary point
182     """
183     self.myLine.startPoint().setValue(self.myArc.endPoint().pnt())
184     model.do()
185     coincidence = self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
186     self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
187     self.myDOF -= 2
188     model.do()
189     # this check will fail due to the limitation of PlanGCS
190     self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
191
192   def test_middle_point_move_arc(self):
193     """ Test 6. Set middle point constraint and move arc
194     """
195     self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
196     self.myDOF -= 2
197     model.do()
198     self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
199     self.moveArc()
200
201   def test_middle_point_coincidence_move_arc(self):
202     """ Test 7. Set coincidence and middle point constraint and move arc
203     """
204     self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
205     model.do()
206     self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
207     self.myDOF -= 2
208     model.do()
209     self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
210     self.moveArc()
211
212   def test_middle_point_move_line(self):
213     """ Test 8. Set middle point constraint and move line
214     """
215     self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
216     self.myDOF -= 2
217     model.do()
218     self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
219     self.moveLine()
220
221   def test_middle_point_coincidence_move_line(self):
222     """ Test 9. Set coincidence and middle point constraint and move line
223     """
224     self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
225     model.do()
226     self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
227     self.myDOF -= 2
228     model.do()
229     self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
230     self.moveLine()
231
232   def test_remove_middle_point(self):
233     """ Test 10. Set and then remove middle point constraint
234     """
235     mp = self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
236     self.myDOF -= 2
237     model.do()
238     model.assertArcValidity(self.myArc)
239     self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
240     self.checkDOF()
241     # remove middle point
242     self.myDocument.removeFeature(mp.feature())
243     self.myDOF += 2
244     model.do()
245     self.checkDOF()
246     # set flag False to avoid checking middle point constraint in tearDown() method
247     self.myTestPassed = False
248
249
250 if __name__ == "__main__":
251     test_program = unittest.main(exit=False)
252     assert test_program.result.wasSuccessful(), "Test failed"