]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestConstraintTangentBSpline.py
Salome HOME
Merge branch 'occ/shaper2smesh'
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintTangentBSpline.py
1 # Copyright (C) 2019-2020  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 constraint "Tangent" applied to B-spline and another entity
22 """
23
24 import unittest
25 import math
26
27 from salome.shaper import model
28
29 from GeomAPI import *
30 from GeomAlgoAPI import *
31 from SketchAPI import *
32
33 __updated__ = "2020-01-22"
34
35 class TestTangentBSpline(unittest.TestCase):
36   def setUp(self):
37     model.begin()
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])
45     model.do()
46
47     self.myExpectedFailure = False
48     self.myDOF = len(self.myPoles) * 2
49     self.myNbPoints = len(self.myPoles)
50     self.myNbLines = len(self.myPoles) - 1
51     self.myNbArcs = 0
52     self.myNbCircles = 0
53     self.myNbEllipses = 0
54     self.myNbEllipticArcs = 0
55     self.myNbBSplines = 1
56     self.myNbInternals = len(self.myPoles) * 3 - 2
57     self.myNbCoincidence = 0
58     self.myNbTangency = 0
59
60   def tearDown(self):
61     model.end()
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"
64       model.undo()
65     else:
66       self.checkDOF()
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)
77
78
79   def checkDOF(self):
80     self.assertEqual(model.dof(self.mySketch), self.myDOF)
81
82   def assertTangentFeatures(self, theFeature1, theFeature2):
83     shapes = [theFeature1.results()[-1].resultSubShapePair()[0].shape(),
84               theFeature2.results()[-1].resultSubShapePair()[0].shape()]
85     for s in shapes:
86       e = shapeToEdge(s)
87       if e.isLine() or e.isArc() or e.isEllipse():
88         params = e.getRange()
89         e.setRange(params[0] - 100, params[1] + 100)
90     # TODO (azv): complete checking the tangent curves
91
92   def assertPointLineDistance(self, thePoint, theLine, theExpectedDistance = 0):
93     dist = model.distancePointLine(thePoint, theLine)
94     self.assertAlmostEqual(dist, theExpectedDistance)
95
96   def assertTangentLineCircle(self, theLine, theCircle):
97     self.assertPointLineDistance(theCircle.center(), theLine, theCircle.radius().value())
98
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())
103
104     distF1P1 = model.distancePointPoint(theEllipse.firstFocus(), projF1)
105     distF2P2 = model.distancePointPoint(theEllipse.secondFocus(), projF2)
106
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())
111
112
113   def test_line_tangent(self):
114     """ Test 1. Set tangency between B-spline and a line
115     """
116     aLine = self.mySketch.addLine(10, -10, 90, 40)
117     self.myNbLines += 1
118     self.myDOF += 4
119     model.do()
120
121     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
122     self.myNbTangency += 1
123     self.myDOF -= 1
124     model.do()
125
126     self.assertTangentFeatures(aLine, self.mySpline)
127
128   def test_circle_tangent(self):
129     """ Test 2. Set tangency between B-spline and a circle
130     """
131     aCircle = self.mySketch.addCircle(10, 10, 20)
132     self.myNbCircles += 1
133     self.myDOF += 3
134     model.do()
135
136     self.mySketch.setTangent(self.mySpline.result(), aCircle.defaultResult())
137     self.myNbTangency += 1
138     self.myDOF -= 1
139     model.do()
140
141     self.assertTangentFeatures(aCircle, self.mySpline)
142
143   def test_arc_tangent(self):
144     """ Test 3. Set tangency between B-spline and an arc
145     """
146     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
147     self.myNbArcs += 1
148     self.myDOF += 5
149     model.do()
150
151     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
152     self.myNbTangency += 1
153     self.myDOF -= 1
154     model.do()
155
156     self.assertTangentFeatures(anArc, self.mySpline)
157
158   def test_ellipse_tangent(self):
159     """ Test 4. Set tangency between B-spline and an ellipse
160     """
161     anEllipse = self.mySketch.addEllipse(10, 10, 20, 10, 7)
162     self.myNbEllipses += 1
163     self.myDOF += 5
164     model.do()
165
166     self.mySketch.setTangent(self.mySpline.result(), anEllipse.defaultResult())
167     self.myNbTangency += 1
168     self.myDOF -= 1
169     model.do()
170
171     self.assertTangentFeatures(anEllipse, self.mySpline)
172
173   def test_elliptic_arc_tangent(self):
174     """ Test 5. Set tangency between B-spline and an elliptic arc
175     """
176     anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
177     self.myNbEllipticArcs += 1
178     self.myDOF += 7
179     model.do()
180
181     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
182     self.myNbTangency += 1
183     self.myDOF -= 1
184     model.do()
185
186     self.assertTangentFeatures(anEllipticArc, self.mySpline)
187
188   def test_spline_tangent(self):
189     """ Test 6. Set tangency between two B-spline curves
190     """
191     aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
192     self.myNbBSplines += 1
193     self.myDOF += aSpline.poles().size() * 2
194     model.do()
195
196     self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
197     self.myNbTangency += 1
198     self.myDOF -= 1
199     model.do()
200
201     #self.assertTangentFeatures(aSpline, self.mySpline)
202     self.myExpectedFailure = True
203
204
205   def test_line_tangent_coincident_by_pole(self):
206     """ Test 7. Set tangency between B-spline and a line coincident with B-spline start point
207     """
208     aLine = self.mySketch.addLine(-15, -25, 50, 40)
209     self.myNbLines += 1
210     self.myDOF += 4
211     model.do()
212
213     self.mySketch.setCoincident(self.mySpline.startPoint(), aLine.result())
214     self.myNbCoincidence += 1
215     self.myDOF -= 1
216     model.do()
217
218     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
219     self.myNbTangency += 1
220     self.myDOF -= 1
221     model.do()
222
223     self.assertPointLineDistance(self.mySpline.startPoint(), aLine)
224     self.assertTangentFeatures(aLine, self.mySpline)
225
226   def test_circle_tangent_coincident_by_pole(self):
227     """ Test 8. Set tangency between B-spline and a circle coincident with B-spline end point
228     """
229     aCircle = self.mySketch.addCircle(10, 10, 20)
230     self.myNbCircles += 1
231     self.myDOF += 3
232     model.do()
233
234     self.mySketch.setCoincident(self.mySpline.startPoint(), aCircle.defaultResult())
235     self.myNbCoincidence += 1
236     self.myDOF -= 1
237     model.do()
238
239     self.mySketch.setTangent(self.mySpline.result(), aCircle.defaultResult())
240     self.myNbTangency += 1
241     self.myDOF -= 1
242     model.do()
243
244     self.assertTangentFeatures(aCircle, self.mySpline)
245     dist = model.distancePointPoint(self.mySpline.startPoint(), aCircle.center())
246     self.assertAlmostEqual(dist, aCircle.radius().value())
247
248   def test_arc_tangent_coincident_by_pole(self):
249     """ Test 9. Set tangency between B-spline and an arc coincident with B-spline end point
250     """
251     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
252     self.myNbArcs += 1
253     self.myDOF += 5
254     model.do()
255
256     self.mySketch.setCoincident(self.mySpline.endPoint(), anArc.defaultResult())
257     self.myNbCoincidence += 1
258     self.myDOF -= 1
259     model.do()
260
261     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
262     self.myNbTangency += 1
263     self.myDOF -= 1
264     model.do()
265
266     self.assertTangentFeatures(anArc, self.mySpline)
267     dist = model.distancePointPoint(self.mySpline.endPoint(), anArc.center())
268     self.assertAlmostEqual(dist, anArc.radius().value())
269
270   def test_ellipse_tangent_coincident_by_pole(self):
271     """ Test 10. Set tangency between B-spline and an ellipse coincident with B-spline start point
272     """
273     anEllipse = self.mySketch.addEllipse(10, 10, 20, 10, 7)
274     self.myNbEllipses += 1
275     self.myDOF += 5
276     model.do()
277
278     self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipse.defaultResult())
279     self.myNbCoincidence += 1
280     self.myDOF -= 1
281     model.do()
282
283     self.mySketch.setTangent(self.mySpline.result(), anEllipse.defaultResult())
284     self.myNbTangency += 1
285     self.myDOF -= 1
286     model.do()
287
288     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipse)
289
290   def test_elliptic_arc_tangent_coincident_by_pole(self):
291     """ Test 11. Set tangency between B-spline and an elliptic arc coincident with B-spline start point
292     """
293     anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
294     self.myNbEllipticArcs += 1
295     self.myDOF += 7
296     model.do()
297
298     self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipticArc.defaultResult())
299     self.myNbCoincidence += 1
300     self.myDOF -= 1
301     model.do()
302
303     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
304     self.myNbTangency += 1
305     self.myDOF -= 1
306     model.do()
307
308     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
309
310
311   def test_line_tangent_coincident_by_boundaries(self):
312     """ Test 12. Set tangency between B-spline and a line, coincident by their start points
313     """
314     aLine = self.mySketch.addLine(10, -10, 90, 40)
315     self.myNbLines += 1
316     self.myDOF += 4
317     model.do()
318
319     self.mySketch.setCoincident(self.mySpline.startPoint(), aLine.startPoint())
320     self.myNbCoincidence += 1
321     self.myDOF -= 2
322     model.do()
323
324     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
325     self.myNbTangency += 1
326     self.myDOF -= 1
327     model.do()
328
329     self.assertPointLineDistance(aLine.endPoint(), self.myControlLines[0])
330
331   def test_arc_tangent_coincident_by_boundaries(self):
332     """ Test 13. Set tangency between B-spline and an arc, coincident by their start points
333     """
334     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
335     self.myNbArcs += 1
336     self.myDOF += 5
337     model.do()
338
339     self.mySketch.setCoincident(self.mySpline.startPoint(), anArc.startPoint())
340     self.myNbCoincidence += 1
341     self.myDOF -= 2
342     model.do()
343
344     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
345     self.myNbTangency += 1
346     self.myDOF -= 1
347     model.do()
348
349     self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[0]), anArc)
350
351   def test_elliptic_arc_tangent_coincident_by_boundaries(self):
352     """ Test 14. Set tangency between B-spline and an elliptic arc, coincident by their start points
353     """
354     anEllipticArc = self.mySketch.addEllipticArc(10, -10, 20, -10, 22.2065556157337, -10, 10, 3, True)
355     self.myNbEllipticArcs += 1
356     self.myDOF += 7
357     model.do()
358
359     self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipticArc.startPoint())
360     self.myNbCoincidence += 1
361     self.myDOF -= 2
362     model.do()
363
364     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
365     self.myNbTangency += 1
366     self.myDOF -= 1
367     model.do()
368
369     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
370
371   def test_spline_tangent_coincident_by_boundaries(self):
372     """ Test 15. Set tangency between two B-spline curves coincident with B-spline start point
373     """
374     aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
375     self.myNbBSplines += 1
376     self.myDOF += aSpline.poles().size() * 2
377     model.do()
378
379     self.mySketch.setCoincident(self.mySpline.startPoint(), aSpline.startPoint())
380     self.myNbCoincidence += 1
381     self.myDOF -= 2
382     model.do()
383
384     self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
385     self.myNbTangency += 1
386     self.myDOF -= 1
387     model.do()
388
389     #self.assertPointLineDistance(aSpline.poles()[1], self.myControlLines[0])
390     self.myExpectedFailure = True
391
392
393   def test_line_tangent_coincident_by_aux(self):
394     """ Test 16. Set tangency between B-spline and a line, coincident by their start points
395     """
396     aLine = self.mySketch.addLine(10, -10, 90, 40)
397     self.myNbLines += 1
398     self.myDOF += 4
399     model.do()
400
401     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), aLine.startPoint())
402     self.myNbCoincidence += 1
403     self.myDOF -= 2
404     model.do()
405
406     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
407     self.myNbTangency += 1
408     self.myDOF -= 1
409     model.do()
410
411     self.assertPointLineDistance(aLine.endPoint(), self.myControlLines[0])
412
413   def test_arc_tangent_coincident_by_aux(self):
414     """ Test 17. Set tangency between B-spline and an arc, coincident by their start points
415     """
416     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
417     self.myNbArcs += 1
418     self.myDOF += 5
419     model.do()
420
421     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), anArc.startPoint())
422     self.myNbCoincidence += 1
423     self.myDOF -= 2
424     model.do()
425
426     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
427     self.myNbTangency += 1
428     self.myDOF -= 1
429     model.do()
430
431     self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[0]), anArc)
432
433   def test_elliptic_arc_tangent_coincident_by_aux(self):
434     """ Test 18. Set tangency between B-spline and an elliptic arc, coincident by their start points
435     """
436     anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
437     self.myNbEllipticArcs += 1
438     self.myDOF += 7
439     model.do()
440
441     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), anEllipticArc.startPoint())
442     self.myNbCoincidence += 1
443     self.myDOF -= 2
444     model.do()
445
446     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
447     self.myNbTangency += 1
448     self.myDOF -= 1
449     model.do()
450
451     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
452
453   def test_spline_tangent_coincident_by_aux(self):
454     """ Test 19. Set tangency between two B-spline curves coincident with B-spline start point
455     """
456     aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
457     self.myNbBSplines += 1
458     self.myDOF += aSpline.poles().size() * 2
459     model.do()
460
461     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), aSpline.startPoint())
462     self.myNbCoincidence += 1
463     self.myDOF -= 2
464     model.do()
465
466     self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
467     self.myNbTangency += 1
468     self.myDOF -= 1
469     model.do()
470
471     #self.assertPointLineDistance(aSpline.poles().pnt(1), self.myControlLines[0])
472     self.myExpectedFailure = True
473
474
475
476 if __name__ == "__main__":
477     test_program = unittest.main(exit=False)
478     assert test_program.result.wasSuccessful(), "Test failed"
479     #assert model.checkPythonDump()