Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintFixed.py
1 # Copyright (C) 2014-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 """
21     TestConstraintRigid.py
22     Unit test of SketchPlugin_ConstraintRigid class
23
24     SketchPlugin_Constraint
25         static const std::string MY_CONSTRAINT_VALUE("ConstraintValue");
26         static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt");
27         static const std::string MY_ENTITY_A("ConstraintEntityA");
28         static const std::string MY_ENTITY_B("ConstraintEntityB");
29         static const std::string MY_ENTITY_C("ConstraintEntityC");
30         static const std::string MY_ENTITY_D("ConstraintEntityD");
31
32     SketchPlugin_ConstraintRigid
33         static const std::string MY_CONSTRAINT_RIGID_ID("SketchConstraintRigid");
34         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
35
36 """
37 from GeomDataAPI import *
38 from ModelAPI import *
39 from GeomAPI import *
40 from salome.shaper import model
41
42 #=========================================================================
43 # Initialization of the test
44 #=========================================================================
45
46 __updated__ = "2014-11-21"
47
48 aSession = ModelAPI_Session.get()
49 aDocument = aSession.moduleDocument()
50 #=========================================================================
51 # Creation of a sketch
52 #=========================================================================
53 aSession.startOperation()
54 aSketchCommonFeature = aDocument.addFeature("Sketch")
55 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
56 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
57 origin.setValue(0, 0, 0)
58 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
59 dirx.setValue(1, 0, 0)
60 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
61 norm.setValue(0, 0, 1)
62 aSession.finishOperation()
63 #=========================================================================
64 # Create a triangle ABC
65 #=========================================================================
66 aSession.startOperation()
67 aSketchLineA = aSketchFeature.addFeature("SketchLine")
68 aSketchLineB = aSketchFeature.addFeature("SketchLine")
69 aSketchLineC = aSketchFeature.addFeature("SketchLine")
70 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
71 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
72 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
73 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
74 aLineCStartPoint = geomDataAPI_Point2D(aSketchLineC.attribute("StartPoint"))
75 aLineCEndPoint = geomDataAPI_Point2D(aSketchLineC.attribute("EndPoint"))
76 aLineAStartPoint.setValue(25., 25.)
77 aLineAEndPoint.setValue(100., 25.)
78 aLineBStartPoint.setValue(100., 25.)
79 aLineBEndPoint.setValue(60., 75.)
80 aLineCStartPoint.setValue(60., 75.)
81 aLineCEndPoint.setValue(25., 25.)
82 aSession.finishOperation()
83 assert (model.dof(aSketchFeature) == 12)
84 # Store initial values of lines
85 kLineAStart = (aLineAStartPoint.x(), aLineAStartPoint.y())
86 kLineAEnd = (aLineAEndPoint.x(),   aLineAEndPoint.y())
87 kLineBStart = (aLineBStartPoint.x(), aLineBStartPoint.y())
88 kLineBEnd = (aLineBEndPoint.x(),   aLineBEndPoint.y())
89 kLineCStart = (aLineCStartPoint.x(), aLineCStartPoint.y())
90 kLineCEnd = (aLineCEndPoint.x(),   aLineCEndPoint.y())
91 #=========================================================================
92 # Link triange lines with concidence
93 #=========================================================================
94 concidenceLinks = list(zip([aLineBStartPoint, aLineCStartPoint, aLineAStartPoint],
95                       [aLineAEndPoint, aLineBEndPoint, aLineCEndPoint]))
96 aSession.startOperation()
97 for eachLink in concidenceLinks:
98     aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
99     reflistA = aConstraint.refattr("ConstraintEntityA")
100     reflistB = aConstraint.refattr("ConstraintEntityB")
101     reflistA.setAttr(eachLink[0])
102     reflistB.setAttr(eachLink[1])
103     aConstraint.execute()
104 aSession.finishOperation()
105 assert (model.dof(aSketchFeature) == 6)
106
107 # Check that constarints doesn't affected lines' values
108 assert (kLineAStart == (aLineAStartPoint.x(), aLineAStartPoint.y()))
109 assert (kLineAEnd == (aLineAEndPoint.x(),   aLineAEndPoint.y()))
110 assert (kLineBStart == (aLineBStartPoint.x(), aLineBStartPoint.y()))
111 assert (kLineBEnd == (aLineBEndPoint.x(),   aLineBEndPoint.y()))
112 assert (kLineCStart == (aLineCStartPoint.x(), aLineCStartPoint.y()))
113 assert (kLineCEnd == (aLineCEndPoint.x(),   aLineCEndPoint.y()))
114 #=========================================================================
115 # Make line A rigid
116 #=========================================================================
117 aSession.startOperation()
118 aRigidConstraint = aSketchFeature.addFeature("SketchConstraintRigid")
119 eachRefattr = aRigidConstraint.refattr("ConstraintEntityA")
120 lineResults = aSketchLineA.results()
121 for eachResult in lineResults:
122     if eachResult.shape().isEdge():
123         break
124 aResult = modelAPI_ResultConstruction(eachResult)
125 assert (aResult is not None)
126 eachRefattr.setObject(aResult)
127 # aRigidConstraint.execute()
128 aSession.finishOperation()
129
130 # Check that constarints doesn't affected lines' values
131 assert ((aLineAStartPoint.x(), aLineAStartPoint.y()) == (aLineCEndPoint.x(), aLineCEndPoint.y()))
132 assert ((aLineBStartPoint.x(), aLineBStartPoint.y()) == (aLineAEndPoint.x(), aLineAEndPoint.y()))
133 assert ((aLineCStartPoint.x(), aLineCStartPoint.y()) == (aLineBEndPoint.x(), aLineBEndPoint.y()))
134 assert (model.dof(aSketchFeature) == 2)
135
136 #=========================================================================
137 # Check that moving line A does not affect lines
138 #=========================================================================
139 aSession.startOperation()
140 aLineAEndPoint.setValue(90., 0.)
141 aSession.finishOperation()
142 # Check that constarint keep features' values
143 assert ((aLineAStartPoint.x(), aLineAStartPoint.y()) == (aLineCEndPoint.x(), aLineCEndPoint.y()))
144 assert ((aLineBStartPoint.x(), aLineBStartPoint.y()) == (aLineAEndPoint.x(), aLineAEndPoint.y()))
145 assert ((aLineCStartPoint.x(), aLineCStartPoint.y()) == (aLineBEndPoint.x(), aLineBEndPoint.y()))
146 assert (model.dof(aSketchFeature) == 2)
147
148 #=========================================================================
149 # Check that moving line B does not affect lines
150 #=========================================================================
151 aSession.startOperation()
152 aLineBEndPoint.setValue(90., 150.)
153 aSession.finishOperation()
154 # Check that constarint keep features' values
155 assert ((aLineAStartPoint.x(), aLineAStartPoint.y()) == (aLineCEndPoint.x(), aLineCEndPoint.y()))
156 assert ((aLineBStartPoint.x(), aLineBStartPoint.y()) == (aLineAEndPoint.x(), aLineAEndPoint.y()))
157 assert ((aLineCStartPoint.x(), aLineCStartPoint.y()) == (aLineBEndPoint.x(), aLineBEndPoint.y()))
158 assert (model.dof(aSketchFeature) == 2)
159
160 #=========================================================================
161 # Remove constraint, move line to check that it is not fixed
162 #=========================================================================
163 aSession.startOperation()
164 aDocument.removeFeature(aRigidConstraint)
165 aLineBEndPoint.setValue(90., 150.)
166 aSession.finishOperation()
167 assert ((aLineAStartPoint.x(), aLineAStartPoint.y()) == (aLineCEndPoint.x(), aLineCEndPoint.y()))
168 assert ((aLineBStartPoint.x(), aLineBStartPoint.y()) == (aLineAEndPoint.x(), aLineAEndPoint.y()))
169 assert ((aLineCStartPoint.x(), aLineCStartPoint.y()) == (aLineBEndPoint.x(), aLineBEndPoint.y()))
170 assert (model.dof(aSketchFeature) == 6)
171
172 #=========================================================================
173 # Create circle, fix it and check the circle is not moved
174 #=========================================================================
175 aCenter = [10., 10.]
176 aRadius = 5.
177 aSession.startOperation()
178 aCircle = aSketchFeature.addFeature("SketchCircle")
179 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
180 aCircleRadius = aCircle.real("circle_radius")
181 aCircleCenter.setValue(aCenter[0], aCenter[1])
182 aCircleRadius.setValue(aRadius)
183 aSession.finishOperation()
184 assert (model.dof(aSketchFeature) == 9)
185 # fixed constraints
186 aSession.startOperation()
187 aRigidConstraint = aSketchFeature.addFeature("SketchConstraintRigid")
188 aRigidConstraint.refattr("ConstraintEntityA").setObject(aCircle.lastResult())
189 aSession.finishOperation()
190 assert (model.dof(aSketchFeature) == 6)
191 # move center of circle
192 aSession.startOperation()
193 aCircleCenter.setValue(aCenter[0] + 1., aCenter[1] - 1.)
194 aSession.finishOperation()
195 assert (aCircleCenter.x() == aCenter[0] and aCircleCenter.y() == aCenter[1])
196 assert (aCircleRadius.value() == aRadius)
197 assert (model.dof(aSketchFeature) == 6)
198 # change radius of circle
199 aSession.startOperation()
200 aCircleRadius.setValue(aRadius + 3.)
201 aSession.finishOperation()
202 assert (aCircleCenter.x() == aCenter[0] and aCircleCenter.y() == aCenter[1])
203 assert (aCircleRadius.value() == aRadius)
204 assert (model.dof(aSketchFeature) == 6)
205
206 #=========================================================================
207 # Remove Fixed constraint and check the circle can be moved
208 #=========================================================================
209 aSession.startOperation()
210 aDocument.removeFeature(aRigidConstraint)
211 aSession.finishOperation()
212 assert (model.dof(aSketchFeature) == 9)
213 # move center of circle
214 aCenter = [aCenter[0] + 1., aCenter[1] - 1.]
215 aSession.startOperation()
216 aCircleCenter.setValue(aCenter[0], aCenter[1])
217 aSession.finishOperation()
218 assert (aCircleCenter.x() == aCenter[0] and aCircleCenter.y() == aCenter[1])
219 assert (aCircleRadius.value() == aRadius)
220 assert (model.dof(aSketchFeature) == 9)
221 # change radius of circle
222 aRadius = aRadius + 3.
223 aSession.startOperation()
224 aCircleRadius.setValue(aRadius)
225 aSession.finishOperation()
226 assert (aCircleCenter.x() == aCenter[0] and aCircleCenter.y() == aCenter[1])
227 assert (aCircleRadius.value() == aRadius)
228 assert (model.dof(aSketchFeature) == 9)
229
230 #=========================================================================
231 # Create arc, fix it and check it is not moved
232 #=========================================================================
233 aCenter = [10., 10.]
234 aStart = [5., 10.]
235 aEnd = [10., 15.]
236 aSession.startOperation()
237 anArc = aSketchFeature.addFeature("SketchArc")
238 anArcCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
239 anArcStart = geomDataAPI_Point2D(anArc.attribute("start_point"))
240 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point"))
241 anArcCenter.setValue(aCenter[0], aCenter[1])
242 anArcStart.setValue(aStart[0], aStart[1])
243 anArcEnd.setValue(aEnd[0], aEnd[1])
244 aSession.finishOperation()
245 assert (model.dof(aSketchFeature) == 14)
246 # fixed constraints
247 aSession.startOperation()
248 aRigidConstraint = aSketchFeature.addFeature("SketchConstraintRigid")
249 aRigidConstraint.refattr("ConstraintEntityA").setObject(anArc.lastResult())
250 aSession.finishOperation()
251 assert (model.dof(aSketchFeature) == 9)
252 # move center of arc
253 aSession.startOperation()
254 anArcCenter.setValue(aCenter[0] + 1., aCenter[1] - 1.)
255 aSession.finishOperation()
256 assert (anArcCenter.x() == aCenter[0] and anArcCenter.y() == aCenter[1])
257 assert (anArcStart.x() == aStart[0] and anArcStart.y() == aStart[1])
258 assert (anArcEnd.x() == aEnd[0] and anArcEnd.y() == aEnd[1])
259 assert (model.dof(aSketchFeature) == 9)
260 # move start point of arc
261 aSession.startOperation()
262 anArcStart.setValue(aStart[0] + 1., aStart[1] - 1.)
263 aSession.finishOperation()
264 assert (anArcCenter.x() == aCenter[0] and anArcCenter.y() == aCenter[1])
265 assert (anArcStart.x() == aStart[0] and anArcStart.y() == aStart[1])
266 assert (anArcEnd.x() == aEnd[0] and anArcEnd.y() == aEnd[1])
267 assert (model.dof(aSketchFeature) == 9)
268 # move end point of arc
269 aSession.startOperation()
270 anArcEnd.setValue(aEnd[0] + 1., aEnd[1] - 1.)
271 aSession.finishOperation()
272 assert (anArcCenter.x() == aCenter[0] and anArcCenter.y() == aCenter[1])
273 assert (anArcStart.x() == aStart[0] and anArcStart.y() == aStart[1])
274 assert (anArcEnd.x() == aEnd[0] and anArcEnd.y() == aEnd[1])
275 assert (model.dof(aSketchFeature) == 9)
276
277 #=========================================================================
278 # Remove Fixed constraint and check the arc can be moved
279 #=========================================================================
280 aSession.startOperation()
281 aDocument.removeFeature(aRigidConstraint)
282 aSession.finishOperation()
283 assert (model.dof(aSketchFeature) == 14)
284 # move center of arc
285 aCenter = [anArcCenter.x(), anArcCenter.y()]
286 aSession.startOperation()
287 anArcCenter.setValue(aCenter[0] + 1., aCenter[1] - 1.)
288 aSession.finishOperation()
289 assert (anArcCenter.x() != aCenter[0] or anArcCenter.y() != aCenter[1])
290 assert (model.dof(aSketchFeature) == 14)
291 # move start point of arc
292 aStart = [anArcStart.x(), anArcStart.y()]
293 aSession.startOperation()
294 anArcStart.setValue(aStart[0] + 1., aStart[1] - 1.)
295 aSession.finishOperation()
296 assert (anArcStart.x() != aStart[0] or anArcStart.y() != aStart[1])
297 assert (model.dof(aSketchFeature) == 14)
298 # move end point of arc
299 aEnd = [anArcEnd.x(), anArcEnd.y()]
300 aSession.startOperation()
301 anArcEnd.setValue(aEnd[0] + 1., aEnd[1] - 1.)
302 aSession.finishOperation()
303 assert (anArcEnd.x() != aEnd[0] or anArcEnd.y() != aEnd[1])
304 assert (model.dof(aSketchFeature) == 14)
305
306 #=========================================================================
307 # End of test
308 #=========================================================================
309
310 assert(model.checkPythonDump())