1 # Copyright (C) 2019-2021 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 constraint "Tangent" applied to B-spline and another entity
27 from salome.shaper import model
30 from GeomAlgoAPI import *
31 from SketchAPI import *
33 __updated__ = "2020-01-22"
35 class TestTangentBSpline(unittest.TestCase):
38 self.myDocument = model.moduleDocument()
39 self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
40 self.myPoles = [GeomAPI_Pnt2d(-10, -30), GeomAPI_Pnt2d(20, -15), GeomAPI_Pnt2d(-10, 0), GeomAPI_Pnt2d(20, 15), GeomAPI_Pnt2d(-10, 30)]
41 self.myWeights = [1, 3, 5, 3, 1]
42 self.mySpline = self.mySketch.addSpline(poles = self.myPoles, weights = self.myWeights)
43 self.myControlPoles = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
44 self.myControlLines = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3])
47 self.myExpectedFailure = False
48 self.myDOF = len(self.myPoles) * 2
49 self.myNbPoints = len(self.myPoles)
50 self.myNbLines = len(self.myPoles) - 1
54 self.myNbEllipticArcs = 0
56 self.myNbInternals = len(self.myPoles) * 3 - 2
57 self.myNbCoincidence = 0
62 if self.myExpectedFailure:
63 assert(self.mySketch.solverError() != ""), "PlaneGCS limitation: if you see this message, then PlaneGCS has solved the set of constraints correctly"
67 model.testNbSubFeatures(self.mySketch, "SketchPoint", self.myNbPoints)
68 model.testNbSubFeatures(self.mySketch, "SketchLine", self.myNbLines)
69 model.testNbSubFeatures(self.mySketch, "SketchArc", self.myNbArcs)
70 model.testNbSubFeatures(self.mySketch, "SketchCircle", self.myNbCircles)
71 model.testNbSubFeatures(self.mySketch, "SketchEllipse", self.myNbEllipses)
72 model.testNbSubFeatures(self.mySketch, "SketchEllipticArc", self.myNbEllipticArcs)
73 model.testNbSubFeatures(self.mySketch, "SketchBSpline", self.myNbBSplines)
74 model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidenceInternal", self.myNbInternals)
75 model.testNbSubFeatures(self.mySketch, "SketchConstraintCoincidence", self.myNbCoincidence)
76 model.testNbSubFeatures(self.mySketch, "SketchConstraintTangent", self.myNbTangency)
80 self.assertEqual(model.dof(self.mySketch), self.myDOF)
82 def assertTangentFeatures(self, theFeature1, theFeature2):
83 shapes = [theFeature1.results()[-1].resultSubShapePair()[0].shape(),
84 theFeature2.results()[-1].resultSubShapePair()[0].shape()]
87 if e.isLine() or e.isArc() or e.isEllipse():
89 e.setRange(params[0] - 100, params[1] + 100)
90 # TODO (azv): complete checking the tangent curves
92 def assertPointLineDistance(self, thePoint, theLine, theExpectedDistance = 0):
93 dist = model.distancePointLine(thePoint, theLine)
94 self.assertAlmostEqual(dist, theExpectedDistance)
96 def assertTangentLineCircle(self, theLine, theCircle):
97 self.assertPointLineDistance(theCircle.center(), theLine, theCircle.radius().value())
99 def assertTangentLineEllipse(self, theLine, theEllipse):
100 aLine = GeomAPI_Lin2d(theLine.startPoint().pnt(), theLine.endPoint().pnt())
101 projF1 = aLine.project(theEllipse.firstFocus().pnt())
102 projF2 = aLine.project(theEllipse.secondFocus().pnt())
104 distF1P1 = model.distancePointPoint(theEllipse.firstFocus(), projF1)
105 distF2P2 = model.distancePointPoint(theEllipse.secondFocus(), projF2)
107 tgPoint = GeomAPI_Pnt2d((projF1.x() * distF2P2 + projF2.x() * distF1P1) / (distF1P1 + distF2P2), (projF1.y() * distF2P2 + projF2.y() * distF1P1) / (distF1P1 + distF2P2))
108 distF1T = model.distancePointPoint(theEllipse.firstFocus(), tgPoint)
109 distF2T = model.distancePointPoint(theEllipse.secondFocus(), tgPoint)
110 self.assertAlmostEqual(distF1T + distF2T, 2 * theEllipse.majorRadius().value())
113 def test_line_tangent(self):
114 """ Test 1. Set tangency between B-spline and a line
116 aLine = self.mySketch.addLine(10, -10, 90, 40)
121 aTangency = self.mySketch.setTangent(self.mySpline.result(), aLine.result())
124 self.assertNotEqual(aTangency.feature().error(), "")
128 def test_circle_tangent(self):
129 """ Test 2. Set tangency between B-spline and a circle
131 aCircle = self.mySketch.addCircle(10, 10, 20)
132 self.myNbCircles += 1
136 aTangency = self.mySketch.setTangent(self.mySpline.result(), aCircle.defaultResult())
139 self.assertNotEqual(aTangency.feature().error(), "")
143 def test_arc_tangent(self):
144 """ Test 3. Set tangency between B-spline and an arc
146 anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
151 aTangency = self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
154 self.assertNotEqual(aTangency.feature().error(), "")
158 def test_ellipse_tangent(self):
159 """ Test 4. Set tangency between B-spline and an ellipse
161 anEllipse = self.mySketch.addEllipse(10, 10, 20, 10, 7)
162 self.myNbEllipses += 1
166 aTangency = self.mySketch.setTangent(self.mySpline.result(), anEllipse.defaultResult())
169 self.assertNotEqual(aTangency.feature().error(), "")
173 def test_elliptic_arc_tangent(self):
174 """ Test 5. Set tangency between B-spline and an elliptic arc
176 anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
177 self.myNbEllipticArcs += 1
181 aTangency = self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
184 self.assertNotEqual(aTangency.feature().error(), "")
188 def test_spline_tangent(self):
189 """ Test 6. Set tangency between two B-spline curves
191 aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
192 self.myNbBSplines += 1
193 self.myDOF += aSpline.poles().size() * 2
196 aTangency = self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
199 self.assertNotEqual(aTangency.feature().error(), "")
204 def test_line_tangent_coincident_by_pole(self):
205 """ Test 7. Set tangency between B-spline and a line coincident with B-spline start point
207 aLine = self.mySketch.addLine(-15, -25, 50, 40)
212 self.mySketch.setCoincident(self.mySpline.startPoint(), aLine.result())
213 self.myNbCoincidence += 1
217 aTangency = self.mySketch.setTangent(self.mySpline.result(), aLine.result())
220 self.assertNotEqual(aTangency.feature().error(), "")
224 def test_circle_tangent_coincident_by_pole(self):
225 """ Test 8. Set tangency between B-spline and a circle coincident with B-spline end point
227 aCircle = self.mySketch.addCircle(10, 10, 20)
228 self.myNbCircles += 1
232 self.mySketch.setCoincident(self.mySpline.startPoint(), aCircle.defaultResult())
233 self.myNbCoincidence += 1
237 aTangency = self.mySketch.setTangent(self.mySpline.result(), aCircle.defaultResult())
240 self.assertNotEqual(aTangency.feature().error(), "")
244 def test_arc_tangent_coincident_by_pole(self):
245 """ Test 9. Set tangency between B-spline and an arc coincident with B-spline end point
247 anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
252 self.mySketch.setCoincident(self.mySpline.endPoint(), anArc.defaultResult())
253 self.myNbCoincidence += 1
257 aTangency = self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
260 self.assertNotEqual(aTangency.feature().error(), "")
264 def test_ellipse_tangent_coincident_by_pole(self):
265 """ Test 10. Set tangency between B-spline and an ellipse coincident with B-spline start point
267 anEllipse = self.mySketch.addEllipse(10, 10, 20, 10, 7)
268 self.myNbEllipses += 1
272 self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipse.defaultResult())
273 self.myNbCoincidence += 1
277 aTangency = self.mySketch.setTangent(self.mySpline.result(), anEllipse.defaultResult())
280 self.assertNotEqual(aTangency.feature().error(), "")
284 def test_elliptic_arc_tangent_coincident_by_pole(self):
285 """ Test 11. Set tangency between B-spline and an elliptic arc coincident with B-spline start point
287 anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
288 self.myNbEllipticArcs += 1
292 self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipticArc.defaultResult())
293 self.myNbCoincidence += 1
297 aTangency = self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
300 self.assertNotEqual(aTangency.feature().error(), "")
305 def test_line_tangent_coincident_by_boundaries(self):
306 """ Test 12. Set tangency between B-spline and a line, coincident by their start points
308 aLine = self.mySketch.addLine(10, -10, 90, 40)
313 self.mySketch.setCoincident(self.mySpline.startPoint(), aLine.startPoint())
314 self.myNbCoincidence += 1
318 self.mySketch.setTangent(self.mySpline.result(), aLine.result())
319 self.myNbTangency += 1
323 self.assertPointLineDistance(aLine.endPoint(), self.myControlLines[0])
325 def test_arc_tangent_coincident_by_boundaries(self):
326 """ Test 13. Set tangency between B-spline and an arc, coincident by their start points
328 anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
333 self.mySketch.setCoincident(self.mySpline.startPoint(), anArc.startPoint())
334 self.myNbCoincidence += 1
338 self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
339 self.myNbTangency += 1
343 self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[0]), anArc)
345 def test_elliptic_arc_tangent_coincident_by_boundaries(self):
346 """ Test 14. Set tangency between B-spline and an elliptic arc, coincident by their start points
348 anEllipticArc = self.mySketch.addEllipticArc(10, -10, 20, -10, 22.2065556157337, -10, 10, 3, True)
349 self.myNbEllipticArcs += 1
353 self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipticArc.startPoint())
354 self.myNbCoincidence += 1
358 self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
359 self.myNbTangency += 1
363 self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
365 def test_spline_tangent_coincident_by_boundaries(self):
366 """ Test 15. Set tangency between two B-spline curves coincident with B-spline start point
368 aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
369 self.myNbBSplines += 1
370 self.myDOF += aSpline.poles().size() * 2
373 self.mySketch.setCoincident(self.mySpline.startPoint(), aSpline.startPoint())
374 self.myNbCoincidence += 1
378 self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
379 self.myNbTangency += 1
383 #self.assertPointLineDistance(aSpline.poles()[1], self.myControlLines[0])
384 self.myExpectedFailure = True
387 def test_line_tangent_coincident_by_aux(self):
388 """ Test 16. Set tangency between B-spline and a line, coincident by their start points
390 aLine = self.mySketch.addLine(10, -10, 90, 40)
395 self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), aLine.startPoint())
396 self.myNbCoincidence += 1
400 self.mySketch.setTangent(self.mySpline.result(), aLine.result())
401 self.myNbTangency += 1
405 self.assertPointLineDistance(aLine.endPoint(), self.myControlLines[0])
407 def test_arc_tangent_coincident_by_aux(self):
408 """ Test 17. Set tangency between B-spline and an arc, coincident by their start points
410 anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
415 self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), anArc.startPoint())
416 self.myNbCoincidence += 1
420 self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
421 self.myNbTangency += 1
425 self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[0]), anArc)
427 def test_elliptic_arc_tangent_coincident_by_aux(self):
428 """ Test 18. Set tangency between B-spline and an elliptic arc, coincident by their start points
430 anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
431 self.myNbEllipticArcs += 1
435 self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), anEllipticArc.startPoint())
436 self.myNbCoincidence += 1
440 self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
441 self.myNbTangency += 1
445 self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
447 def test_spline_tangent_coincident_by_aux(self):
448 """ Test 19. Set tangency between two B-spline curves coincident with B-spline start point
450 aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
451 self.myNbBSplines += 1
452 self.myDOF += aSpline.poles().size() * 2
455 self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), aSpline.startPoint())
456 self.myNbCoincidence += 1
460 self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
461 self.myNbTangency += 1
465 #self.assertPointLineDistance(aSpline.poles().pnt(1), self.myControlLines[0])
466 self.myExpectedFailure = True
470 if __name__ == "__main__":
471 test_program = unittest.main(exit=False)
472 assert test_program.result.wasSuccessful(), "Test failed"
473 #assert model.checkPythonDump()