Salome HOME
0802da210321396e3cbdf56efd0a8a5d6d45e4fa
[modules/shaper.git] / src / SketchPlugin / Test / TestBSplineAddPole.py
1 # Copyright (C) 2020-2022  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 for adding a pole to already created B-spline curve
22 """
23
24 from salome.shaper import model
25 from GeomAPI import *
26 import random
27
28 TOLERANCE = 1.e-7
29
30 def assertSubFeatures(theSketch, theNbPoints, theNbLines, theNbSplines, theNbSplinesP, theNbCoincidences, theNbInternal):
31     model.testNbSubFeatures(theSketch, "SketchPoint", theNbPoints)
32     model.testNbSubFeatures(theSketch, "SketchLine", theNbLines)
33     model.testNbSubFeatures(theSketch, "SketchBSpline", theNbSplines)
34     model.testNbSubFeatures(theSketch, "SketchBSplinePeriodic", theNbSplinesP)
35     model.testNbSubFeatures(theSketch, "SketchConstraintCoincidence", theNbCoincidences)
36     model.testNbSubFeatures(theSketch, "SketchConstraintCoincidenceInternal", theNbInternal)
37
38 def assertPoles(thePoles, theReference):
39     assert(thePoles.size() == len(theReference))
40     for ind in range(0, len(theReference)):
41         pole = thePoles.pnt(ind)
42         ref = GeomAPI_Pnt2d(theReference[ind][0], theReference[ind][1])
43         assert(model.distancePointPoint(pole, ref) < TOLERANCE), "Index = {}, pole = ({}, {}), refdata = ({}, {})".format(ind, pole.x(), pole.y(), ref.x(), ref.y())
44
45
46 model.begin()
47 partSet = model.moduleDocument()
48 Part_1 = model.addPart(partSet)
49 Part_1_doc = Part_1.document()
50 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
51
52 SketchBSpline_1_poles = [(-25, 5), (-15, 35), (15, 35), (28, 5)]
53 SketchBSpline_1 = Sketch_1.addSpline(poles = SketchBSpline_1_poles)
54 [SketchPoint_1, SketchPoint_2, SketchPoint_3, SketchPoint_4] = SketchBSpline_1.controlPoles(auxiliary = [0, 1, 2, 3])
55 [SketchLine_1, SketchLine_2, SketchLine_3] = SketchBSpline_1.controlPolygon(auxiliary = [0, 1, 2])
56
57 SketchBSplinePeriodic_1_poles = [(-20, -10), (20, -40), (20, -10), (-20, -40)]
58 SketchBSplinePeriodic_1 = Sketch_1.addSpline(poles = SketchBSplinePeriodic_1_poles, periodic = True)
59 [SketchPoint_5, SketchPoint_6, SketchPoint_7, SketchPoint_8] = SketchBSplinePeriodic_1.controlPoles(auxiliary = [0, 1, 2, 3])
60 [SketchLine_4, SketchLine_5, SketchLine_6, SketchLine_7] = SketchBSplinePeriodic_1.controlPolygon(auxiliary = [0, 1, 2, 3])
61 model.do()
62
63 # check original values
64 NBPOINTS = 8
65 NBLINES = 7
66 NBSPLINES = 1
67 NBSPLINESPERIODIC = 1
68 NBCOINCIDENCES = 0
69 NBINTERNAL = 22
70 assertSubFeatures(Sketch_1, NBPOINTS, NBLINES, NBSPLINES, NBSPLINESPERIODIC, NBCOINCIDENCES, NBINTERNAL)
71 assertPoles(SketchBSpline_1.poles(), SketchBSpline_1_poles)
72 assertPoles(SketchBSplinePeriodic_1.poles(), SketchBSplinePeriodic_1_poles)
73
74 # add poles to non-periodic B-spline
75 ind = 0
76 while ind < len(SketchBSpline_1_poles):
77     x = random.uniform(-25, 25)
78     y = random.uniform(5, 40)
79     SketchBSpline_1.insertPole(ind, GeomAPI_Pnt2d(x, y))
80     if ind + 1 < len(SketchBSpline_1_poles):
81         SketchBSpline_1_poles.insert(ind + 1, (x, y))
82     else:
83         SketchBSpline_1_poles.insert(len(SketchBSpline_1_poles) - 1, (x, y))
84     ind += 2
85     model.do()
86
87     NBPOINTS += 1
88     NBLINES += 1
89     NBINTERNAL += 3
90     assertSubFeatures(Sketch_1, NBPOINTS, NBLINES, NBSPLINES, NBSPLINESPERIODIC, NBCOINCIDENCES, NBINTERNAL)
91     assertPoles(SketchBSpline_1.poles(), SketchBSpline_1_poles)
92
93 # add poles to periodic B-spline
94 ind = 0
95 while ind < len(SketchBSplinePeriodic_1_poles):
96     x = random.uniform(-25, 25)
97     y = random.uniform(-45, -5)
98     SketchBSplinePeriodic_1.insertPole(ind, GeomAPI_Pnt2d(x, y))
99     SketchBSplinePeriodic_1_poles.insert(ind + 1, (x, y))
100     ind += 2
101     model.do()
102
103     NBPOINTS += 1
104     NBLINES += 1
105     NBINTERNAL += 3
106     assertSubFeatures(Sketch_1, NBPOINTS, NBLINES, NBSPLINES, NBSPLINESPERIODIC, NBCOINCIDENCES, NBINTERNAL)
107     assertPoles(SketchBSplinePeriodic_1.poles(), SketchBSplinePeriodic_1_poles)
108
109 model.end()
110
111 # check error on incorrect action
112 model.begin()
113 assert(not SketchBSpline_1.feature().customAction("wrong_action"))
114 model.end()
115
116 #assert(model.checkPythonDump())