Salome HOME
macro tuyauterie
[modules/shaper.git] / src / PythonAddons / Test / TestRectangleCentered.py
1 # Copyright (C) 2021  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 import math
26
27 def checkRectangle(lines, center, corner, tolerance = 1.e-7):
28   dx = corner.x() - center.x()
29   dy = corner.y() - center.y()
30   points = [geom.Pnt2d(center.x() - dx, center.y() - dy),
31             geom.Pnt2d(center.x() - dx, center.y() + dy),
32             geom.Pnt2d(center.x() + dx, center.y() + dy),
33             geom.Pnt2d(center.x() + dx, center.y() - dy)]
34   for i in range(0, 4):
35     line = SketchAPI_Line(lines[i])
36     sp = line.startPoint().pnt()
37     sp_ref = points[i-1]
38     assert(sp.distance(sp_ref) <= tolerance)
39     ep = line.endPoint().pnt()
40     ep_ref = points[i]
41     assert(ep.distance(ep_ref) <= tolerance)
42
43
44 model.begin()
45 partSet = model.moduleDocument()
46 part = model.addPart(partSet).document()
47
48 centerPoint = geom.Pnt2d(50, 50)
49 endPoint = geom.Pnt2d(100, 100)
50
51 sketch = model.addSketch(part, model.defaultPlane("XOY"))
52 rectangle_1 = sketch.addRectangleCentered(centerPoint, endPoint)
53 lines_1 = rectangle_1.lines()
54 model.end()
55
56 checkRectangle(lines_1, centerPoint, endPoint)
57
58 model.begin()
59 projection_1 = sketch.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
60 point_1 = SketchAPI_Point(projection_1.createdFeature())
61
62 rectangle_2 = sketch.addRectangleCentered(point_1.coordinates(), endPoint)
63 lines_2 = rectangle_2.lines()
64 model.end()
65
66 checkRectangle(lines_2, geom.Pnt2d(0.0, 0.0), endPoint)
67
68 model.begin()
69 rectangle_3 = sketch.addRectangleCentered(SketchAPI_Line(lines_1[0]).startPoint(), SketchAPI_Line(lines_2[0]).endPoint())
70 lines_3 = rectangle_3.lines()
71 model.end()
72
73 checkRectangle(lines_3, SketchAPI_Line(lines_1[0]).startPoint().pnt(), SketchAPI_Line(lines_2[0]).endPoint().pnt())
74
75 # move center of rectangle
76 SHIFT = 1.0
77 center = SketchAPI_Line(lines_1[0]).startPoint().pnt()
78 for i in range(0, 20):
79   center.setX(center.x() + SHIFT)
80   center.setY(center.y() + SHIFT)
81   model.begin()
82   sketch.move(SketchAPI_Line(lines_1[0]).startPoint(), center)
83   model.end()
84   checkRectangle(lines_3, center, SketchAPI_Line(lines_2[0]).endPoint().pnt())
85
86 # move corner of rectangle
87 corner = SketchAPI_Line(lines_2[0]).endPoint().pnt()
88 for i in range(0, 20):
89   corner.setX(corner.x() + SHIFT)
90   corner.setY(corner.y() + SHIFT)
91   model.begin()
92   sketch.move(SketchAPI_Line(lines_2[0]).endPoint(), corner)
93   model.end()
94   checkRectangle(lines_3, SketchAPI_Line(lines_1[0]).startPoint().pnt(), corner)
95
96 assert(model.checkPythonDump())