1 # Copyright (C) 2017-2019 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 middle point on an arc
27 from salome.shaper import model
28 from GeomAPI import GeomAPI_Dir2d
30 __updated__ = "2019-09-03"
32 class TestMiddlePointOnArc(unittest.TestCase):
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)
46 model.assertArcValidity(self.myArc)
47 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
50 assert(model.checkPythonDump())
53 self.assertEqual(model.dof(self.mySketch), self.myDOF)
55 def checkMiddlePoint(self, thePoint, theArc):
56 self.myTestPassed = False
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)
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
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)
82 ANGLE_STEP = math.pi * 5.0 / 180.0
83 ANGLE_THRESHOLD = math.pi
84 # move start point of the arc clockwise
86 while fullAngle < ANGLE_THRESHOLD:
87 self.rotatePoint(self.myArc.startPoint(), self.myArc.center(), -ANGLE_STEP)
89 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
90 fullAngle += ANGLE_STEP
91 # move start point of the arc conterclockwise
93 while fullAngle < ANGLE_THRESHOLD:
94 self.rotatePoint(self.myArc.startPoint(), self.myArc.center(), ANGLE_STEP)
96 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
97 fullAngle += ANGLE_STEP
99 # move end point of the arc clockwise
101 while fullAngle < ANGLE_THRESHOLD:
102 self.rotatePoint(self.myArc.endPoint(), self.myArc.center(), -ANGLE_STEP)
104 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
105 fullAngle += ANGLE_STEP
106 # move end point of the arc conterclockwise
108 while fullAngle < ANGLE_THRESHOLD:
109 self.rotatePoint(self.myArc.endPoint(), self.myArc.center(), ANGLE_STEP)
111 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
112 fullAngle += ANGLE_STEP
114 # move center of the arc
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])
121 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
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])
128 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
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])
137 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
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])
144 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
147 def test_middle_point_PA(self):
148 """ Test 1. Set middle point constraint (point is the first argument)
150 self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
154 def test_middle_point_AP(self):
155 """ Test 2. Set middle point constraint (point is the second argument)
157 self.mySketch.setMiddlePoint(self.myArc.results()[1], self.myLine.startPoint())
161 def test_coincident_middle_point(self):
162 """ Test 3. Set middle point constraint for the point already coincident with the arc
164 self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
166 self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
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
173 self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
175 self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
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
183 self.myLine.startPoint().setValue(self.myArc.endPoint().pnt())
185 coincidence = self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
186 self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
189 # this check will fail due to the limitation of PlanGCS
190 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
192 def test_middle_point_move_arc(self):
193 """ Test 6. Set middle point constraint and move arc
195 self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
198 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
201 def test_middle_point_coincidence_move_arc(self):
202 """ Test 7. Set coincidence and middle point constraint and move arc
204 self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
206 self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
209 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
212 def test_middle_point_move_line(self):
213 """ Test 8. Set middle point constraint and move line
215 self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
218 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
221 def test_middle_point_coincidence_move_line(self):
222 """ Test 9. Set coincidence and middle point constraint and move line
224 self.mySketch.setCoincident(self.myLine.startPoint(), self.myArc.results()[1])
226 self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
229 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
232 def test_remove_middle_point(self):
233 """ Test 10. Set and then remove middle point constraint
235 mp = self.mySketch.setMiddlePoint(self.myLine.startPoint(), self.myArc.results()[1])
238 model.assertArcValidity(self.myArc)
239 self.checkMiddlePoint(self.myLine.startPoint(), self.myArc)
241 # remove middle point
242 self.myDocument.removeFeature(mp.feature())
246 # set flag False to avoid checking middle point constraint in tearDown() method
247 self.myTestPassed = False
250 if __name__ == "__main__":
251 test_program = unittest.main(exit=False)
252 assert test_program.result.wasSuccessful(), "Test failed"