Salome HOME
ae1fdbb26c7977fd49575c2ac454dd5d4814f111
[modules/shaper.git] / src / PythonAddons / Test / TestRectangleCentered.py
1 # Copyright (C) 2021-2024  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 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 areCounterDirectedAndOfEqualLength(theLineA, theLineB):
42   tolerance = 1.e-5
43
44   aStartA = theLineA.startPoint().pnt()
45   aEndA   = theLineA.endPoint().pnt()
46   aDirA_X = aEndA.x() - aStartA.x()
47   aDirA_Y = aEndA.y() - aStartA.y()
48
49   aStartB = theLineB.startPoint().pnt()
50   aEndB   = theLineB.endPoint().pnt()
51   aDirB_X = aEndB.x() - aStartB.x()
52   aDirB_Y = aEndB.y() - aStartB.y()
53
54   return abs(aDirA_X + aDirB_X) < tolerance and abs(aDirA_Y + aDirB_Y) < tolerance
55
56 def arePerpendicular(theLineA, theLineB):
57   tolerance = 1.e-5
58
59   aStartA = theLineA.startPoint().pnt()
60   aEndA   = theLineA.endPoint().pnt()
61   aDirA_X = aEndA.x() - aStartA.x()
62   aDirA_Y = aEndA.y() - aStartA.y()
63   aLengthA = theLineA.defaultResult().shape().edge().length()
64
65   aStartB = theLineB.startPoint().pnt()
66   aEndB   = theLineB.startPoint().pnt()
67   aDirB_X = aEndB.x() - aStartB.x()
68   aDirB_Y = aEndB.y() - aStartB.y()
69   aLengthB = theLineB.defaultResult().shape().edge().length()
70
71   if (aLengthA < tolerance or aLengthB < tolerance):
72     return True
73
74   return (aDirA_X * aDirB_X + aDirA_Y * aDirB_Y) / (aLengthA * aLengthB) < tolerance
75
76 def areConnected(theLineA, theLineB):
77   aEndA   = theLineA.endPoint().pnt()
78   aStartB = theLineB.startPoint().pnt()
79   return aEndA.x() == aStartB.x() and aEndA.y() == aStartB.y()
80
81 def checkIfArbitraryAlignedRectangle(theEdgeLines):
82   aLine0 = SketchAPI_Line(theEdgeLines[0])
83   aLine1 = SketchAPI_Line(theEdgeLines[1])
84   aLine2 = SketchAPI_Line(theEdgeLines[2])
85   aLine3 = SketchAPI_Line(theEdgeLines[3])
86
87   assert (areCounterDirectedAndOfEqualLength(aLine0, aLine2))
88   assert (areCounterDirectedAndOfEqualLength(aLine1, aLine3))
89   assert (arePerpendicular(aLine0, aLine1))
90   assert (arePerpendicular(aLine2, aLine3))
91   assert (areConnected(aLine0, aLine1))
92   assert (areConnected(aLine1, aLine2))
93   assert (areConnected(aLine2, aLine3))
94   assert (areConnected(aLine3, aLine0))       
95
96
97
98 model.begin()
99 partSet = model.moduleDocument()
100 part = model.addPart(partSet).document()
101
102 centerPoint = geom.Pnt2d(50, 50)
103 endPoint = geom.Pnt2d(100, 100)
104
105 sketch = model.addSketch(part, model.defaultPlane("XOY"))
106 rectangle_1 = sketch.addRectangleCentered(centerPoint, endPoint)
107 lines_1 = rectangle_1.lines()
108 model.end()
109
110 checkRectangle(lines_1, centerPoint, endPoint)
111
112 model.begin()
113 projection_1 = sketch.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
114 point_1 = SketchAPI_Point(projection_1.createdFeature())
115
116 rectangle_2 = sketch.addRectangleCentered(point_1.coordinates(), endPoint)
117 lines_2 = rectangle_2.lines()
118 model.end()
119
120 checkRectangle(lines_2, geom.Pnt2d(0.0, 0.0), endPoint)
121
122 model.begin()
123 rectangle_3 = sketch.addRectangleCentered(SketchAPI_Line(lines_1[0]).startPoint(), SketchAPI_Line(lines_2[0]).endPoint())
124 lines_3 = rectangle_3.lines()
125 model.end()
126
127 checkRectangle(lines_3, SketchAPI_Line(lines_1[0]).startPoint().pnt(), SketchAPI_Line(lines_2[0]).endPoint().pnt())
128
129 # move center of rectangle
130 SHIFT = 1.0
131 center = SketchAPI_Line(lines_1[0]).startPoint().pnt()
132 for i in range(0, 5):
133   center.setX(center.x() + SHIFT)
134   center.setY(center.y() + SHIFT)
135   model.begin()
136   sketch.move(SketchAPI_Line(lines_1[0]).startPoint(), center)
137   model.end()
138   checkIfArbitraryAlignedRectangle(lines_3)
139
140 # move corner of rectangle
141 corner = SketchAPI_Line(lines_2[0]).endPoint().pnt()
142 for i in range(0, 5):
143   corner.setX(corner.x() + SHIFT)
144   corner.setY(corner.y() + SHIFT)
145   model.begin()
146   sketch.move(SketchAPI_Line(lines_2[0]).endPoint(), corner)
147   model.end()
148   checkIfArbitraryAlignedRectangle(lines_3)
149
150 assert(model.checkPythonDump())