1 # Copyright (C) 2019-2023 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 movement of the periodic B-spline curve
27 from GeomDataAPI import geomDataAPI_Point2DArray
28 from SketchAPI import *
29 from salome.shaper import model
31 __updated__ = "2020-01-20"
33 class TestMoveBSpline(unittest.TestCase):
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
53 self.assertEqual(model.dof(self.mySketch), self.myDOF)
55 def checkPointCoordinates(self, thePoint, theCoordinates):
57 if issubclass(type(theCoordinates), GeomAPI_Pnt2d):
58 aCoord = [theCoordinates.x(), theCoordinates.y()]
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)
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)
70 def fixPoint(self, thePoint):
71 self.mySketch.setFixed(thePoint)
77 def test_move_free_bspline(self):
78 """ Test 1. Movement of a free B-spline dragging the edge
80 oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
81 newPosition = GeomAPI_Pnt2d(120., 90.)
82 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
85 # plane is XOY, no need to project oldPosition point
86 dx = newPosition.x() - oldPosition.x()
87 dy = newPosition.y() - oldPosition.y()
90 for pole in self.myPoles:
91 newPoles.append(GeomAPI_Pnt2d(pole.x() + dx, pole.y() + dy))
92 self.checkPoles(self.mySpline, newPoles)
95 def test_move_fixed_bspline(self):
96 """ Test 2. Movement of a fully fixed B-spline
98 self.mySketch.setFixed(self.mySpline.defaultResult())
102 newPosition = GeomAPI_Pnt2d(120., 90.)
103 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
106 self.checkPoles(self.mySpline, self.myPoles)
109 def test_move_bspline_with_fixed_pole(self):
110 """ Test 3. Movement of a B-spline curve with fixed pole
112 [Point_1, Point_2, Point_3, Point_4, Point_5] = self.mySpline.controlPoles(auxiliary = [0, 1, 2, 3, 4])
115 self.fixPoint(Point_2.defaultResult())
118 oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
119 newPosition = GeomAPI_Pnt2d(120., 90.)
120 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
123 # plane is XOY, no need to project oldPosition point
124 dx = newPosition.x() - oldPosition.x()
125 dy = newPosition.y() - oldPosition.y()
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)
135 def test_move_bspline_with_fixed_segment(self):
136 """ Test 4. Movement of a B-spline curve with fixed control segment
138 [Line_1, Line_2, Line_3, Line_4, Line_5] = self.mySpline.controlPolygon(auxiliary = [0, 1, 2, 3, 4])
141 self.mySketch.setFixed(Line_1.defaultResult())
145 oldPosition = GeomAPI_Edge(self.mySpline.defaultResult().shape()).middlePoint()
146 newPosition = GeomAPI_Pnt2d(120., 90.)
147 self.mySketch.move(self.mySpline.defaultResult(), newPosition)
150 # plane is XOY, no need to project oldPosition point
151 dx = newPosition.x() - oldPosition.x()
152 dy = newPosition.y() - oldPosition.y()
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)
160 def test_move_pole_of_free_bspline(self):
161 """ Test 5. Movement of a pole of a B-spline curve
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])
167 newPoles = self.myPoles
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])
174 self.checkPoles(self.mySpline, newPoles)
176 def test_move_segment_of_free_bspline(self):
177 """ Test 6. Movement of a control segment of a B-spline curve
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])
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)
189 dx = newPosition.x() - oldPosition.x()
190 dy = newPosition.y() - oldPosition.y()
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)
198 self.checkPoles(self.mySpline, newPoles)
202 if __name__ == "__main__":
203 test_program = unittest.main(exit=False)
204 assert test_program.result.wasSuccessful(), "Test failed"
205 assert model.checkPythonDump()