Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / Test / TestMoveBSplinePeriodic.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 movement of the periodic B-spline curve
22 """
23
24 import unittest
25 import math
26 from GeomAPI import *
27 from GeomDataAPI import geomDataAPI_Point2DArray
28 from SketchAPI import *
29 from salome.shaper import model
30
31 __updated__ = "2020-01-20"
32
33 class TestMoveBSpline(unittest.TestCase):
34   def setUp(self):
35     model.begin()
36     self.myDocument = model.moduleDocument()
37     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
38     self.myPoles = [GeomAPI_Pnt2d(20., 50.),
39                     GeomAPI_Pnt2d(70., 70.),
40                     GeomAPI_Pnt2d(80., 30.),
41                     GeomAPI_Pnt2d(50., -10.),
42                     GeomAPI_Pnt2d(90., -30.)]
43     self.mySpline = self.mySketch.addSpline(poles = self.myPoles, periodic = True)
44     self.myDOF = len(self.myPoles) * 2
45     model.do()
46     self.checkDOF()
47
48   def tearDown(self):
49     self.checkDOF()
50     model.end()
51
52   def checkDOF(self):
53     self.assertEqual(model.dof(self.mySketch), self.myDOF)
54
55   def checkPointCoordinates(self, thePoint, theCoordinates):
56     aCoord = []
57     if issubclass(type(theCoordinates), GeomAPI_Pnt2d):
58       aCoord = [theCoordinates.x(), theCoordinates.y()]
59     else:
60       aCoord = theCoordinates
61     DIGITS = 7 - math.floor(math.log10(math.hypot(aCoord[0], aCoord[1])))
62     self.assertAlmostEqual(thePoint.x(), aCoord[0], DIGITS)
63     self.assertAlmostEqual(thePoint.y(), aCoord[1], DIGITS)
64
65   def checkPoles(self, theBSpline, theCoordinates):
66     poles = theBSpline.poles()
67     for index, point in zip(range(0, len(theCoordinates)), theCoordinates):
68       self.checkPointCoordinates(poles.pnt(index), point)
69
70   def fixPoint(self, thePoint):
71     self.mySketch.setFixed(thePoint)
72     self.myDOF -= 2
73     model.do()
74     self.checkDOF()
75
76
77   def test_move_free_bspline(self):
78     """ Test 1. Movement of a free B-spline dragging the edge
79     """
80     oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
81     newPosition = GeomAPI_Pnt2d(120., 90.)
82     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
83     model.do()
84
85     # plane is XOY, no need to project oldPosition point
86     dx = newPosition.x() - oldPosition.x()
87     dy = newPosition.y() - oldPosition.y()
88
89     newPoles = []
90     for pole in self.myPoles:
91       newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
92     self.checkPoles(self.mySpline, newPoles)
93
94
95   def test_move_fixed_bspline(self):
96     """ Test 2. Movement of a fully fixed B-spline
97     """
98     self.mySketch.setFixed(self.mySpline.defaultResult())
99     self.myDOF = 0
100     model.do()
101
102     newPosition = GeomAPI_Pnt2d(120., 90.)
103     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
104     model.do()
105
106     self.checkPoles(self.mySpline, self.myPoles)
107
108
109   def test_move_bspline_with_fixed_pole(self):
110     """ Test 3. Movement of a B-spline curve with fixed pole
111     """
112     [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
113     model.do()
114
115     self.fixPoint(Point_2.defaultResult())
116     model.do()
117
118     oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
119     newPosition = GeomAPI_Pnt2d(120., 90.)
120     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
121     model.do()
122
123     # plane is XOY, no need to project oldPosition point
124     dx = newPosition.x() - oldPosition.x()
125     dy = newPosition.y() - oldPosition.y()
126
127     newPoles = []
128     for pole in self.myPoles:
129       newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
130     newPoles[1].setX(newPoles[1].x() - dx)
131     newPoles[1].setY(newPoles[1].y() - dy)
132     self.checkPoles(self.mySpline, newPoles)
133
134
135   def test_move_bspline_with_fixed_segment(self):
136     """ Test 4. Movement of a B-spline curve with fixed control segment
137     """
138     [Line_1, Line_2, Line_3, Line_4, Line_5] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3, 4])
139     model.do()
140
141     self.mySketch.setFixed(Line_1.defaultResult())
142     self.myDOF -= 4
143     model.do()
144
145     oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
146     newPosition = GeomAPI_Pnt2d(120., 90.)
147     self.mySketch.move(self.mySpline.defaultResult(), newPosition)
148     model.do()
149
150     # plane is XOY, no need to project oldPosition point
151     dx = newPosition.x() - oldPosition.x()
152     dy = newPosition.y() - oldPosition.y()
153
154     newPoles = [self.myPoles[0], self.myPoles[1]]
155     for pole in self.myPoles[2:]:
156       newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
157     self.checkPoles(self.mySpline, newPoles)
158
159
160   def test_move_pole_of_free_bspline(self):
161     """ Test 5. Movement of a pole of a B-spline curve
162     """
163     [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
164     [Line_1, Line_2, Line_3, Line_4, Line_5] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3, 4])
165     model.do()
166
167     newPoles = self.myPoles
168
169     newPoles[2].setX(newPoles[2].x() + 20.)
170     newPoles[2].setY(newPoles[2].y() + 20.)
171     self.mySketch.move(SketchAPI_Point(Point_3).coordinates(), newPoles[2])
172     model.do()
173
174     self.checkPoles(self.mySpline, newPoles)
175
176   def test_move_segment_of_free_bspline(self):
177     """ Test 6. Movement of a control segment of a B-spline curve
178     """
179     [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
180     [Line_1, Line_2, Line_3, Line_4, Line_5] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3, 4])
181     model.do()
182
183     oldPosition = GeomAPI_Pnt2d(0.5 * (self.myPoles[2].x() + self.myPoles[3].x()),
184                                 0.5 * (self.myPoles[2].y() + self.myPoles[3].y()))
185     newPosition = GeomAPI_Pnt2d(120., 90.)
186     self.mySketch.move(SketchAPI_Line(Line_3).defaultResult(), newPosition)
187     model.do()
188
189     dx = newPosition.x() - oldPosition.x()
190     dy = newPosition.y() - oldPosition.y()
191
192     newPoles = self.myPoles
193     newPoles[2].setX(newPoles[2].x() + dx)
194     newPoles[2].setY(newPoles[2].y() + dy)
195     newPoles[3].setX(newPoles[3].x() + dx)
196     newPoles[3].setY(newPoles[3].y() + dy)
197
198     self.checkPoles(self.mySpline, newPoles)
199
200
201
202 if __name__ == "__main__":
203     test_program = unittest.main(exit=False)
204     assert test_program.result.wasSuccessful(), "Test failed"
205     assert model.checkPythonDump()