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