1 # Copyright (C) 2021-2022 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
20 from salome.shaper import model
21 from salome.shaper import geom
23 from SketchAPI import *
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)]
33 line = SketchAPI_Line(lines[i])
34 sp = line.startPoint().pnt()
36 assert(sp.distance(sp_ref) <= tolerance)
37 ep = line.endPoint().pnt()
39 assert(ep.distance(ep_ref) <= tolerance)
41 def checkRectangleL(lines, valref, tolerance = 1.e-5):
43 line = SketchAPI_Line(lines[i])
44 #print (line.defaultResult().shape().edge().length())
46 #print (abs(line.defaultResult().shape().edge().length()-valref[i%2]))
47 assert(abs(line.defaultResult().shape().edge().length()-valref[i%2]) <= tolerance)
50 partSet = model.moduleDocument()
51 part = model.addPart(partSet).document()
53 centerPoint = geom.Pnt2d(50, 50)
54 endPoint = geom.Pnt2d(100, 100)
56 sketch = model.addSketch(part, model.defaultPlane("XOY"))
57 rectangle_1 = sketch.addRectangleCentered(centerPoint, endPoint)
58 lines_1 = rectangle_1.lines()
61 checkRectangle(lines_1, centerPoint, endPoint)
64 projection_1 = sketch.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
65 point_1 = SketchAPI_Point(projection_1.createdFeature())
67 rectangle_2 = sketch.addRectangleCentered(point_1.coordinates(), endPoint)
68 lines_2 = rectangle_2.lines()
71 checkRectangle(lines_2, geom.Pnt2d(0.0, 0.0), endPoint)
74 rectangle_3 = sketch.addRectangleCentered(SketchAPI_Line(lines_1[0]).startPoint(), SketchAPI_Line(lines_2[0]).endPoint())
75 lines_3 = rectangle_3.lines()
78 checkRectangle(lines_3, SketchAPI_Line(lines_1[0]).startPoint().pnt(), SketchAPI_Line(lines_2[0]).endPoint().pnt())
80 # move center of rectangle
82 center = SketchAPI_Line(lines_1[0]).startPoint().pnt()
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 ]
90 center.setX(center.x() + SHIFT)
91 center.setY(center.y() + SHIFT)
93 sketch.move(SketchAPI_Line(lines_1[0]).startPoint(), center)
95 checkRectangleL(lines_3, valref[2*i:])
97 # move corner of rectangle
98 corner = SketchAPI_Line(lines_2[0]).endPoint().pnt()
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)
109 sketch.move(SketchAPI_Line(lines_2[0]).endPoint(), corner)
111 checkRectangleL(lines_3, valref[2*i:])
113 assert(model.checkPythonDump())