Salome HOME
4cdda7cbdf1a639507cd5b6fe79c89140a18878d
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintTangentBSpline.py
1 # Copyright (C) 2019-2023  CEA, EDF
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     aTangency = self.mySketch.setTangent(self.mySpline.result(), aLine.result())
122     model.end()
123
124     self.assertNotEqual(aTangency.feature().error(), "")
125     model.undo()
126     model.begin()
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     aTangency = self.mySketch.setTangent(self.mySpline.result(), aCircle.defaultResult())
137     model.end()
138
139     self.assertNotEqual(aTangency.feature().error(), "")
140     model.undo()
141     model.begin()
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     aTangency = self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
152     model.end()
153
154     self.assertNotEqual(aTangency.feature().error(), "")
155     model.undo()
156     model.begin()
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     aTangency = self.mySketch.setTangent(self.mySpline.result(), anEllipse.defaultResult())
167     model.end()
168
169     self.assertNotEqual(aTangency.feature().error(), "")
170     model.undo()
171     model.begin()
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     aTangency = self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
182     model.end()
183
184     self.assertNotEqual(aTangency.feature().error(), "")
185     model.undo()
186     model.begin()
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     aTangency = self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
197     model.end()
198
199     self.assertNotEqual(aTangency.feature().error(), "")
200     model.undo()
201     model.begin()
202
203
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
206     """
207     aLine = self.mySketch.addLine(-15, -25, 50, 40)
208     self.myNbLines += 1
209     self.myDOF += 4
210     model.do()
211
212     self.mySketch.setCoincident(self.mySpline.startPoint(), aLine.result())
213     self.myNbCoincidence += 1
214     self.myDOF -= 1
215     model.do()
216
217     aTangency = self.mySketch.setTangent(self.mySpline.result(), aLine.result())
218     model.end()
219
220     self.assertNotEqual(aTangency.feature().error(), "")
221     model.undo()
222     model.begin()
223
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
226     """
227     aCircle = self.mySketch.addCircle(10, 10, 20)
228     self.myNbCircles += 1
229     self.myDOF += 3
230     model.do()
231
232     self.mySketch.setCoincident(self.mySpline.startPoint(), aCircle.defaultResult())
233     self.myNbCoincidence += 1
234     self.myDOF -= 1
235     model.do()
236
237     aTangency = self.mySketch.setTangent(self.mySpline.result(), aCircle.defaultResult())
238     model.end()
239
240     self.assertNotEqual(aTangency.feature().error(), "")
241     model.undo()
242     model.begin()
243
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
246     """
247     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
248     self.myNbArcs += 1
249     self.myDOF += 5
250     model.do()
251
252     self.mySketch.setCoincident(self.mySpline.endPoint(), anArc.defaultResult())
253     self.myNbCoincidence += 1
254     self.myDOF -= 1
255     model.do()
256
257     aTangency = self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
258     model.end()
259
260     self.assertNotEqual(aTangency.feature().error(), "")
261     model.undo()
262     model.begin()
263
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
266     """
267     anEllipse = self.mySketch.addEllipse(10, 10, 20, 10, 7)
268     self.myNbEllipses += 1
269     self.myDOF += 5
270     model.do()
271
272     self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipse.defaultResult())
273     self.myNbCoincidence += 1
274     self.myDOF -= 1
275     model.do()
276
277     aTangency = self.mySketch.setTangent(self.mySpline.result(), anEllipse.defaultResult())
278     model.end()
279
280     self.assertNotEqual(aTangency.feature().error(), "")
281     model.undo()
282     model.begin()
283
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
286     """
287     anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
288     self.myNbEllipticArcs += 1
289     self.myDOF += 7
290     model.do()
291
292     self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipticArc.defaultResult())
293     self.myNbCoincidence += 1
294     self.myDOF -= 1
295     model.do()
296
297     aTangency = self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
298     model.end()
299
300     self.assertNotEqual(aTangency.feature().error(), "")
301     model.undo()
302     model.begin()
303
304
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
307     """
308     aLine = self.mySketch.addLine(10, -10, 90, 40)
309     self.myNbLines += 1
310     self.myDOF += 4
311     model.do()
312
313     self.mySketch.setCoincident(self.mySpline.startPoint(), aLine.startPoint())
314     self.myNbCoincidence += 1
315     self.myDOF -= 2
316     model.do()
317
318     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
319     self.myNbTangency += 1
320     self.myDOF -= 1
321     model.do()
322
323     self.assertPointLineDistance(aLine.endPoint(), self.myControlLines[0])
324
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
327     """
328     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
329     self.myNbArcs += 1
330     self.myDOF += 5
331     model.do()
332
333     self.mySketch.setCoincident(self.mySpline.startPoint(), anArc.startPoint())
334     self.myNbCoincidence += 1
335     self.myDOF -= 2
336     model.do()
337
338     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
339     self.myNbTangency += 1
340     self.myDOF -= 1
341     model.do()
342
343     self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[0]), anArc)
344
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
347     """
348     anEllipticArc = self.mySketch.addEllipticArc(10, -10, 20, -10, 22.2065556157337, -10, 10, 3, True)
349     self.myNbEllipticArcs += 1
350     self.myDOF += 7
351     model.do()
352
353     self.mySketch.setCoincident(self.mySpline.startPoint(), anEllipticArc.startPoint())
354     self.myNbCoincidence += 1
355     self.myDOF -= 2
356     model.do()
357
358     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
359     self.myNbTangency += 1
360     self.myDOF -= 1
361     model.do()
362
363     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
364
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
367     """
368     aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
369     self.myNbBSplines += 1
370     self.myDOF += aSpline.poles().size() * 2
371     model.do()
372
373     self.mySketch.setCoincident(self.mySpline.startPoint(), aSpline.startPoint())
374     self.myNbCoincidence += 1
375     self.myDOF -= 2
376     model.do()
377
378     self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
379     self.myNbTangency += 1
380     self.myDOF -= 1
381     model.do()
382
383     #self.assertPointLineDistance(aSpline.poles()[1], self.myControlLines[0])
384     self.myExpectedFailure = True
385
386
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
389     """
390     aLine = self.mySketch.addLine(10, -10, 90, 40)
391     self.myNbLines += 1
392     self.myDOF += 4
393     model.do()
394
395     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), aLine.startPoint())
396     self.myNbCoincidence += 1
397     self.myDOF -= 2
398     model.do()
399
400     self.mySketch.setTangent(self.mySpline.result(), aLine.result())
401     self.myNbTangency += 1
402     self.myDOF -= 1
403     model.do()
404
405     self.assertPointLineDistance(aLine.endPoint(), self.myControlLines[0])
406
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
409     """
410     anArc = self.mySketch.addArc(10, 10, 20, 10, 10, 20, False)
411     self.myNbArcs += 1
412     self.myDOF += 5
413     model.do()
414
415     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), anArc.startPoint())
416     self.myNbCoincidence += 1
417     self.myDOF -= 2
418     model.do()
419
420     self.mySketch.setTangent(self.mySpline.result(), anArc.defaultResult())
421     self.myNbTangency += 1
422     self.myDOF -= 1
423     model.do()
424
425     self.assertTangentLineCircle(SketchAPI_Line(self.myControlLines[0]), anArc)
426
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
429     """
430     anEllipticArc = self.mySketch.addEllipticArc(10, 10, 20, 10, 22.2065556157337, 10, 10, 17, True)
431     self.myNbEllipticArcs += 1
432     self.myDOF += 7
433     model.do()
434
435     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), anEllipticArc.startPoint())
436     self.myNbCoincidence += 1
437     self.myDOF -= 2
438     model.do()
439
440     self.mySketch.setTangent(self.mySpline.result(), anEllipticArc.defaultResult())
441     self.myNbTangency += 1
442     self.myDOF -= 1
443     model.do()
444
445     self.assertTangentLineEllipse(SketchAPI_Line(self.myControlLines[0]), anEllipticArc)
446
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
449     """
450     aSpline = self.mySketch.addSpline(poles = [(50, -20), (40, 0), (50, 20)])
451     self.myNbBSplines += 1
452     self.myDOF += aSpline.poles().size() * 2
453     model.do()
454
455     self.mySketch.setCoincident(SketchAPI_Point(self.myControlPoles[0]).coordinates(), aSpline.startPoint())
456     self.myNbCoincidence += 1
457     self.myDOF -= 2
458     model.do()
459
460     self.mySketch.setTangent(self.mySpline.result(), aSpline.result())
461     self.myNbTangency += 1
462     self.myDOF -= 1
463     model.do()
464
465     #self.assertPointLineDistance(aSpline.poles().pnt(1), self.myControlLines[0])
466     self.myExpectedFailure = True
467
468
469
470 if __name__ == "__main__":
471     test_program = unittest.main(exit=False)
472     assert test_program.result.wasSuccessful(), "Test failed"
473     #assert model.checkPythonDump()