1 # Copyright (C) 2021-2024 CEA, EDF
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 areCounterDirectedAndOfEqualLength(theLineA, theLineB):
44 aStartA = theLineA.startPoint().pnt()
45 aEndA = theLineA.endPoint().pnt()
46 aDirA_X = aEndA.x() - aStartA.x()
47 aDirA_Y = aEndA.y() - aStartA.y()
49 aStartB = theLineB.startPoint().pnt()
50 aEndB = theLineB.endPoint().pnt()
51 aDirB_X = aEndB.x() - aStartB.x()
52 aDirB_Y = aEndB.y() - aStartB.y()
54 return abs(aDirA_X + aDirB_X) < tolerance and abs(aDirA_Y + aDirB_Y) < tolerance
56 def arePerpendicular(theLineA, theLineB):
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()
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()
71 if (aLengthA < tolerance or aLengthB < tolerance):
74 return (aDirA_X * aDirB_X + aDirA_Y * aDirB_Y) / (aLengthA * aLengthB) < tolerance
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()
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])
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))
99 partSet = model.moduleDocument()
100 part = model.addPart(partSet).document()
102 centerPoint = geom.Pnt2d(50, 50)
103 endPoint = geom.Pnt2d(100, 100)
105 sketch = model.addSketch(part, model.defaultPlane("XOY"))
106 rectangle_1, center_of_rectangle_1 = sketch.addRectangleCentered(centerPoint, endPoint)
107 lines_1 = rectangle_1.lines()
110 checkRectangle(lines_1, centerPoint, endPoint)
113 projection_1 = sketch.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
114 point_1 = SketchAPI_Point(projection_1.createdFeature())
116 rectangle_2, _ = sketch.addRectangleCentered(point_1.coordinates(), endPoint)
117 lines_2 = rectangle_2.lines()
120 checkRectangle(lines_2, geom.Pnt2d(0.0, 0.0), endPoint)
123 rectangle_3, _ = sketch.addRectangleCentered(SketchAPI_Line(lines_1[0]).startPoint(), SketchAPI_Line(lines_2[0]).endPoint())
124 lines_3 = rectangle_3.lines()
127 checkRectangle(lines_3, SketchAPI_Line(lines_1[0]).startPoint().pnt(), SketchAPI_Line(lines_2[0]).endPoint().pnt())
129 # move center of rectangle
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)
136 sketch.move(SketchAPI_Line(lines_1[0]).startPoint(), center)
138 checkIfArbitraryAlignedRectangle(lines_3)
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)
146 sketch.move(SketchAPI_Line(lines_2[0]).endPoint(), corner)
148 checkIfArbitraryAlignedRectangle(lines_3)
150 assert(model.checkPythonDump())