Salome HOME
updated copyright message
[modules/shaper.git] / src / PythonAddons / Test / TestRectangleCentered.py
1 # Copyright (C) 2021-2023  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 from salome.shaper import model
21 from salome.shaper import geom
22
23 from SketchAPI import *
24
25 def checkRectangle(lines, center, corner, tolerance = 1.e-7):
26   dx = corner.x() - center.x()
27   dy = corner.y() - center.y()
28   points = [geom.Pnt2d(center.x() - dx, center.y() - dy),
29             geom.Pnt2d(center.x() - dx, center.y() + dy),
30             geom.Pnt2d(center.x() + dx, center.y() + dy),
31             geom.Pnt2d(center.x() + dx, center.y() - dy)]
32   for i in range(0, 4):
33     line = SketchAPI_Line(lines[i])
34     sp = line.startPoint().pnt()
35     sp_ref = points[i-1]
36     assert(sp.distance(sp_ref) <= tolerance)
37     ep = line.endPoint().pnt()
38     ep_ref = points[i]
39     assert(ep.distance(ep_ref) <= tolerance)
40
41 def checkRectangleL(lines, valref, tolerance = 1.e-5):
42   for i in range(0, 4):
43     line = SketchAPI_Line(lines[i])
44     #print (line.defaultResult().shape().edge().length())
45     #print (valref[i%2])
46     #print (abs(line.defaultResult().shape().edge().length()-valref[i%2]))
47     assert(abs(line.defaultResult().shape().edge().length()-valref[i%2]) <= tolerance)
48
49 model.begin()
50 partSet = model.moduleDocument()
51 part = model.addPart(partSet).document()
52
53 centerPoint = geom.Pnt2d(50, 50)
54 endPoint = geom.Pnt2d(100, 100)
55
56 sketch = model.addSketch(part, model.defaultPlane("XOY"))
57 rectangle_1 = sketch.addRectangleCentered(centerPoint, endPoint)
58 lines_1 = rectangle_1.lines()
59 model.end()
60
61 checkRectangle(lines_1, centerPoint, endPoint)
62
63 model.begin()
64 projection_1 = sketch.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
65 point_1 = SketchAPI_Point(projection_1.createdFeature())
66
67 rectangle_2 = sketch.addRectangleCentered(point_1.coordinates(), endPoint)
68 lines_2 = rectangle_2.lines()
69 model.end()
70
71 checkRectangle(lines_2, geom.Pnt2d(0.0, 0.0), endPoint)
72
73 model.begin()
74 rectangle_3 = sketch.addRectangleCentered(SketchAPI_Line(lines_1[0]).startPoint(), SketchAPI_Line(lines_2[0]).endPoint())
75 lines_3 = rectangle_3.lines()
76 model.end()
77
78 checkRectangle(lines_3, SketchAPI_Line(lines_1[0]).startPoint().pnt(), SketchAPI_Line(lines_2[0]).endPoint().pnt())
79
80 # move center of rectangle
81 SHIFT = 1.0
82 center = SketchAPI_Line(lines_1[0]).startPoint().pnt()
83 valref = [ \
84 400.86931 , 200.78509 , \
85 401.73886 , 201.57021 , \
86 402.60865 , 202.35537 , \
87 403.47866 , 203.14056 , \
88 404.34890 , 203.92580 ]
89 for i in range(0, 5):
90   center.setX(center.x() + SHIFT)
91   center.setY(center.y() + SHIFT)
92   model.begin()
93   sketch.move(SketchAPI_Line(lines_1[0]).startPoint(), center)
94   model.end()
95   checkRectangleL(lines_3, valref[2*i:])
96
97 # move corner of rectangle
98 corner = SketchAPI_Line(lines_2[0]).endPoint().pnt()
99 valref = [ \
100 403.11209 , 202.95437 , \
101 401.87551 , 201.98300 , \
102 400.63915 , 201.01169 , \
103 399.40301 , 200.04043 , \
104 398.16710 , 199.06922 ]
105 for i in range(0, 5):
106   corner.setX(corner.x() + SHIFT)
107   corner.setY(corner.y() + SHIFT)
108   model.begin()
109   sketch.move(SketchAPI_Line(lines_2[0]).endPoint(), corner)
110   model.end()
111   checkRectangleL(lines_3, valref[2*i:])
112
113 assert(model.checkPythonDump())