Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchPlugin / Test / TestCenteredRectangle.py
1 # Copyright (C) 2021-2022  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
22 model.begin()
23 partSet = model.moduleDocument()
24
25 ### Create Part
26 Part_1 = model.addPart(partSet)
27 Part_1_doc = Part_1.document()
28 l = model.addParameter(Part_1_doc, "l", "50")
29 h = model.addParameter(Part_1_doc, "h", "20")
30
31 ### Create Sketch
32 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
33
34 ### Create SketchProjection
35 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
36 SketchPoint_1 = SketchProjection_1.createdFeature()
37
38 ### Create SketchLine
39 SketchLine_1 = Sketch_1.addLine(-25, -10, 25, -10)
40
41 ### Create SketchLine
42 SketchLine_2 = Sketch_1.addLine(-25, -10, 25, 10)
43 SketchLine_2.setAuxiliary(True)
44
45 ### Create SketchLine
46 SketchLine_3 = Sketch_1.addLine(25, -10, -25, 10)
47 SketchLine_3.setAuxiliary(True)
48
49 ### Create SketchLine
50 SketchLine_4 = Sketch_1.addLine(25, -10, 25, 10)
51
52 ### Create SketchLine
53 SketchLine_5 = Sketch_1.addLine(25, 10, -25, 10)
54
55 ### Create SketchLine
56 SketchLine_6 = Sketch_1.addLine(-25, 10, -25, -10)
57
58 ### Create SketchPoint
59 SketchPoint_2 = Sketch_1.addPoint(0, 0)
60 SketchPoint_2.setAuxiliary(True)
61 Sketch_1.setCoincident(SketchPoint_2.coordinates(), SketchLine_2.result())
62 Sketch_1.setCoincident(SketchPoint_2.coordinates(), SketchLine_3.result())
63 Sketch_1.setCoincident(SketchLine_6.endPoint(), SketchLine_1.startPoint())
64 Sketch_1.setCoincident(SketchLine_2.startPoint(), SketchLine_1.startPoint())
65 Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_4.startPoint())
66 Sketch_1.setCoincident(SketchLine_3.startPoint(), SketchLine_4.startPoint())
67 Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_5.startPoint())
68 Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_5.startPoint())
69 Sketch_1.setCoincident(SketchLine_5.endPoint(), SketchLine_6.startPoint())
70 Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_6.startPoint())
71 Sketch_1.setCoincident(SketchPoint_1.result(), SketchPoint_2.result())
72 Sketch_1.setHorizontal(SketchLine_1.result())
73 Sketch_1.setVertical(SketchLine_4.result())
74 Sketch_1.setHorizontal(SketchLine_5.result())
75 Sketch_1.setVertical(SketchLine_6.result())
76 Sketch_1.setLength(SketchLine_5.result(), "l")
77 Sketch_1.setLength(SketchLine_6.result(), "h")
78 model.do()
79
80 ### Create Extrusion
81 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchLine_1f-SketchLine_4f-SketchLine_5f-SketchLine_6f")], model.selection(), 40, 0, "Faces|Wires")
82
83 model.end()
84
85 #=============================================================================================
86 # Tests :
87 #=============================================================================================
88 from GeomDataAPI import *
89 from ModelAPI import *
90 from GeomAPI import *
91 from SketchAPI import SketchAPI_Sketch, SketchAPI_SketchEntity
92
93 def isSymetricX(line):
94     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
95     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
96     return aStart.x() == -aEnd.x()
97
98 def isSymetricY(line):
99     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
100     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
101     return aStart.y() == -aEnd.y()
102
103 def isSymetricXY(line):
104     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
105     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
106     return (aStart.x() == -aEnd.x()) and (aStart.y() == -aEnd.y())
107
108 #=========================================================================
109 def checkLinesSymetry(aSketchFeature):
110 #=========================================================================
111 # Checks the lines of rectangle are symetric around the origin
112         aNbSubs = aSketchFeature.numberOfSubs()
113         aNbLines = 0
114         nbAux = 0
115         for i in range (0, aNbSubs):
116                 aFeature = objectToFeature(aSketchFeature.subFeature(i))
117                 if aFeature.getKind() == "SketchLine":
118                         aLastLine = aFeature
119                         entity = SketchAPI_SketchEntity(aLastLine)
120                         if entity.auxiliary().value(): # auxiliary line
121                                 assert isSymetricXY(aLastLine)
122                                 nbAux +=1
123                         else:
124                                 assert (isSymetricX(aLastLine) or isSymetricY(aLastLine))
125                                 aNbLines = aNbLines + 1
126         assert (nbAux == 2)
127         assert (aNbLines == 4)
128         assert (model.dof(aSketchFeature) == 0)
129
130 Part_doc = model.activeDocument()
131 for feat in Part_doc.allFeatures():
132         if feat.getKind() == "Sketch":
133                 sketch = feat
134
135 aSketchFeature = featureToCompositeFeature(sketch)
136 #=========================================================================
137 # Check the lines of rectangle are symetric around the origin
138 #=========================================================================
139 checkLinesSymetry(aSketchFeature)
140 #=========================================================================
141 # Change the dimensions of the rectangle
142 #=========================================================================
143 aSession = ModelAPI_Session.get()
144 aSession.startOperation()
145 l.setValue(80.)
146 h.setValue(55.)
147 aSession.finishOperation()
148 #=========================================================================
149 # Check the lines of rectangle are symetric around the origin
150 #=========================================================================
151 checkLinesSymetry(aSketchFeature)
152
153 assert(model.checkPythonDump())