Salome HOME
Update incorrect units tests and add missing tests (issues #2838, #2839, #2840, ...
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintCoincidence.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     TestConstraintCoincidence.py
23     Unit test of SketchPlugin_ConstraintCoincidence 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_ConstraintCoincidence
34         static const std::string MY_CONSTRAINT_COINCIDENCE_ID("SketchConstraintCoincidence");
35         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
36         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
37
38 """
39 from GeomDataAPI import *
40 from ModelAPI import *
41 import math
42 from salome.shaper import model
43
44 #=========================================================================
45 # Initialization of the test
46 #=========================================================================
47
48 __updated__ = "2014-10-28"
49 TOLERANCE = 1.e-7
50
51
52 #=========================================================================
53 # Auxiliary functions
54 #=========================================================================
55 def checkPointOnLine(point, line):
56     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
57     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
58     aDirX = aEnd.x() - aStart.x()
59     aDirY = aEnd.y() - aStart.y()
60     aVecX = point.x() - aStart.x()
61     aVecY = point.y() - aStart.y()
62     assert (math.fabs(aDirX * aVecY - aDirY * aVecX) <= TOLERANCE)
63
64 def checkPointOnCircle(point, circle):
65     aCenter = geomDataAPI_Point2D(circle.attribute("circle_center"))
66     aRadius = circle.real("circle_radius").value()
67     aDist = math.hypot(point.x() - aCenter.x(), point.y() - aCenter.y())
68     assert (math.fabs(aDist - aRadius) <= TOLERANCE)
69
70 def checkPointOnArc(point, arc):
71     aStart  = geomDataAPI_Point2D(arc.attribute("start_point"))
72     aCenter = geomDataAPI_Point2D(arc.attribute("center_point"))
73     aRadius = math.hypot(aStart.x() - aCenter.x(), aStart.y() - aCenter.y())
74     aDist = math.hypot(point.x() - aCenter.x(), point.y() - aCenter.y())
75     assert (math.fabs(aDist - aRadius) <= TOLERANCE)
76
77
78 #=========================================================================
79 # Start of test
80 #=========================================================================
81 aSession = ModelAPI_Session.get()
82 aDocument = aSession.moduleDocument()
83 # add an origin
84 aSession.startOperation()
85 aFeature = aDocument.addFeature("Point")
86 geomDataAPI_Point(aFeature.attribute("point3d")).setValue(0., 0., 0.)
87 aFeature.string("creation_method").setValue("by_xyz")
88 anOriginName = aFeature.name()
89 aSession.finishOperation()
90 #=========================================================================
91 # Creation of a sketch
92 #=========================================================================
93 aSession.startOperation()
94 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
95 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
96 origin.setValue(0, 0, 0)
97 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
98 dirx.setValue(1, 0, 0)
99 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
100 norm.setValue(0, 0, 1)
101 aSession.finishOperation()
102 #=========================================================================
103 # Create a line and an arc
104 #=========================================================================
105 aSession.startOperation()
106 aSketchArc = aSketchFeature.addFeature("SketchArc")
107 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
108 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
109 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
110 anArcCentr.setValue(10., 10.)
111 anArcStartPoint.setValue(0., 50.)
112 anArcEndPoint.setValue(50., 0.)
113 aSketchLine = aSketchFeature.addFeature("SketchLine")
114 aLineStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
115 aLineEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
116 # Lets initialize line start at circle's end:
117 aLineStartPoint.setValue(50., 0.)
118 aLineEndPoint.setValue(100., 25.)
119 aSession.finishOperation()
120 assert (model.dof(aSketchFeature) == 9)
121 #=========================================================================
122 # Link arc's end and line's start points with concidence constraint
123 #=========================================================================
124 aSession.startOperation()
125 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
126 reflistA = aConstraint.refattr("ConstraintEntityA")
127 reflistB = aConstraint.refattr("ConstraintEntityB")
128 reflistA.setAttr(anArcEndPoint)
129 reflistB.setAttr(aLineStartPoint)
130 aConstraint.execute()
131 aSession.finishOperation()
132 assert (model.dof(aSketchFeature) == 7)
133 #=========================================================================
134 # Check values and move one constrainted object
135 #=========================================================================
136 assert (anArcEndPoint.x() == aLineStartPoint.x())
137 assert (anArcEndPoint.y() == aLineStartPoint.y())
138 deltaX = deltaY = 40.
139 #  move line
140 aSession.startOperation()
141 aLineStartPoint.setValue(aLineStartPoint.x() + deltaX,
142                          aLineStartPoint.y() + deltaY)
143 aLineEndPoint.setValue(aLineEndPoint.x() + deltaX,
144                        aLineEndPoint.y() + deltaY)
145 aSession.finishOperation()
146 # check that arc's points are moved also
147 assert (anArcEndPoint.x() == aLineStartPoint.x())
148 assert (anArcEndPoint.y() == aLineStartPoint.y())
149 assert (model.dof(aSketchFeature) == 7)
150 #=========================================================================
151 # Remove coincidence and move the line
152 #=========================================================================
153 aSession.startOperation()
154 aDocument.removeFeature(aConstraint)
155 aSession.finishOperation()
156 aSession.startOperation()
157 aLineStartPoint.setValue(70., 0.)
158 aSession.finishOperation()
159 assert (anArcEndPoint.x() != aLineStartPoint.x() or anArcEndPoint.y() != aLineStartPoint.y())
160 assert (model.dof(aSketchFeature) == 9)
161
162 #=========================================================================
163 # Add constraint point-on-line
164 #=========================================================================
165 aSession.startOperation()
166 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
167 reflistA = aConstraint.refattr("ConstraintEntityA")
168 reflistB = aConstraint.refattr("ConstraintEntityB")
169 reflistA.setAttr(anArcStartPoint)
170 reflistB.setObject(aSketchLine.lastResult())
171 aConstraint.execute()
172 aSession.finishOperation()
173 checkPointOnLine(anArcStartPoint, aSketchLine)
174 assert (model.dof(aSketchFeature) == 8)
175 #=========================================================================
176 # Add constraint point-on-circle
177 #=========================================================================
178 aSession.startOperation()
179 # create circle with center coincident with origin
180 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
181 aCircleCenter = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
182 aCircleRadius = aSketchCircle.real("circle_radius")
183 aCircleCenter.setValue(10., 10.)
184 aCircleRadius.setValue(25.)
185 aSession.finishOperation()
186 assert (model.dof(aSketchFeature) == 11)
187 # create origin
188 aSession.startOperation()
189 anOrigRes = modelAPI_Result(aDocument.objectByName("Construction", anOriginName))
190 assert (anOrigRes)
191 anOrigShape = anOrigRes.shape()
192 assert (anOrigShape)
193 anOrigin = aSketchFeature.addFeature("SketchPoint")
194 anOriginCoord = geomDataAPI_Point2D(anOrigin.attribute("PointCoordinates"))
195 anOriginCoord.setValue(0., 0.)
196 anOrigin.selection("External").setValue(anOrigRes, anOrigShape)
197 aSession.finishOperation()
198 assert (model.dof(aSketchFeature) == 11)
199 # coincidence between center of circle and the origin
200 aSession.startOperation()
201 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
202 reflistA = aConstraint.refattr("ConstraintEntityA")
203 reflistB = aConstraint.refattr("ConstraintEntityB")
204 reflistA.setAttr(aCircleCenter)
205 reflistB.setObject(anOrigin.lastResult())
206 aSession.finishOperation()
207 assert (model.dof(aSketchFeature) == 9)
208 # point-on-circle
209 aSession.startOperation()
210 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
211 reflistA = aConstraint.refattr("ConstraintEntityA")
212 reflistB = aConstraint.refattr("ConstraintEntityB")
213 reflistA.setObject(aSketchCircle.lastResult())
214 reflistB.setAttr(aLineEndPoint)
215 aConstraint.execute()
216 aSession.finishOperation()
217 checkPointOnCircle(aLineEndPoint, aSketchCircle)
218 assert (model.dof(aSketchFeature) == 8)
219 #=========================================================================
220 # Add constraint point-on-arc
221 #=========================================================================
222 aSession.startOperation("constraint point-on-arc")
223 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
224 reflistA = aConstraint.refattr("ConstraintEntityA")
225 reflistB = aConstraint.refattr("ConstraintEntityB")
226 reflistA.setAttr(aCircleCenter)
227 reflistB.setObject(aSketchArc.lastResult())
228 aConstraint.execute()
229 aSession.finishOperation()
230 checkPointOnArc(aCircleCenter, aSketchArc)
231 # check center of circle is still in origin
232 assert (aCircleCenter.x() == 0. and aCircleCenter.y() == 0.)
233 assert (model.dof(aSketchFeature) == 7)
234
235 #=========================================================================
236 # Create two more lines and set multi-coincidence between their extremities
237 #=========================================================================
238 aSession.startOperation()
239 # line 2
240 aLine2 = aSketchFeature.addFeature("SketchLine")
241 aLine2StartPoint = geomDataAPI_Point2D(aLine2.attribute("StartPoint"))
242 aLine2EndPoint = geomDataAPI_Point2D(aLine2.attribute("EndPoint"))
243 aLine2StartPoint.setValue(50., 0.)
244 aLine2EndPoint.setValue(100., 0.)
245 # line 3
246 aLine3 = aSketchFeature.addFeature("SketchLine")
247 aLine3StartPoint = geomDataAPI_Point2D(aLine3.attribute("StartPoint"))
248 aLine3EndPoint = geomDataAPI_Point2D(aLine3.attribute("EndPoint"))
249 aLine3StartPoint.setValue(50., 0.)
250 aLine3EndPoint.setValue(0., 100.)
251 aSession.finishOperation()
252 assert (model.dof(aSketchFeature) == 15)
253 # coincidences between extremities of lines
254 aSession.startOperation()
255 aConstraint12 = aSketchFeature.addFeature("SketchConstraintCoincidence")
256 refAttrA = aConstraint12.refattr("ConstraintEntityA")
257 refAttrB = aConstraint12.refattr("ConstraintEntityB")
258 refAttrA.setAttr(aLineStartPoint)
259 refAttrB.setAttr(aLine2StartPoint)
260 aConstraint23 = aSketchFeature.addFeature("SketchConstraintCoincidence")
261 refAttrA = aConstraint23.refattr("ConstraintEntityA")
262 refAttrB = aConstraint23.refattr("ConstraintEntityB")
263 refAttrA.setAttr(aLine2StartPoint)
264 refAttrB.setAttr(aLine3StartPoint)
265 aConstraint31 = aSketchFeature.addFeature("SketchConstraintCoincidence")
266 refAttrA = aConstraint31.refattr("ConstraintEntityA")
267 refAttrB = aConstraint31.refattr("ConstraintEntityB")
268 refAttrA.setAttr(aLine3StartPoint)
269 refAttrB.setAttr(aLineStartPoint)
270 aSession.finishOperation()
271 # check the points have same coordinates
272 assert (aLineStartPoint.x() == aLine2StartPoint.x() and aLineStartPoint.y() == aLine2StartPoint.y())
273 assert (aLineStartPoint.x() == aLine3StartPoint.x() and aLineStartPoint.y() == aLine3StartPoint.y())
274 assert (model.dof(aSketchFeature) == 11)
275 #=========================================================================
276 # Move one line and check other have been updated too
277 #=========================================================================
278 aSession.startOperation()
279 aLine3StartPoint.setValue(aLine3StartPoint.x() + deltaX,
280                           aLine3StartPoint.y() + deltaY)
281 aLine3EndPoint.setValue(aLine3EndPoint.x() + deltaX,
282                         aLine3EndPoint.y() + deltaY)
283 aSession.finishOperation()
284 assert (aLineStartPoint.x() == aLine2StartPoint.x() and aLineStartPoint.y() == aLine2StartPoint.y())
285 assert (aLineStartPoint.x() == aLine3StartPoint.x() and aLineStartPoint.y() == aLine3StartPoint.y())
286 assert (model.dof(aSketchFeature) == 11)
287 #=========================================================================
288 # Fix a line and move another connected segment
289 #=========================================================================
290 coordX = aLineStartPoint.x()
291 coordY = aLineStartPoint.y()
292 aSession.startOperation()
293 aFixed = aSketchFeature.addFeature("SketchConstraintRigid")
294 refAttrA = aFixed.refattr("ConstraintEntityA")
295 refAttrA.setObject(aLine2.lastResult())
296 aSession.finishOperation()
297 # move another line
298 aSession.startOperation()
299 aLine3StartPoint.setValue(aLine3StartPoint.x() + deltaX,
300                           aLine3StartPoint.y() + deltaY)
301 aLine3EndPoint.setValue(aLine3EndPoint.x() + deltaX,
302                         aLine3EndPoint.y() + deltaY)
303 aSession.finishOperation()
304 assert (aLineStartPoint.x() == coordX and aLineStartPoint.y() == coordY)
305 assert (aLine2StartPoint.x() == coordX and aLine2StartPoint.y() == coordY)
306 assert (aLine3StartPoint.x() == coordX and aLine3StartPoint.y() == coordY)
307 assert (model.dof(aSketchFeature) == 7)
308 #=========================================================================
309 # Detach fixed line and move one of remaining
310 #=========================================================================
311 aSession.startOperation()
312 aDocument.removeFeature(aConstraint12)
313 aDocument.removeFeature(aConstraint23)
314 aSession.finishOperation()
315 # move line
316 deltaX = 1.
317 deltaY = 0.
318 aSession.startOperation()
319 aLineStartPoint.setValue(aLineStartPoint.x() + deltaX,
320                           aLineStartPoint.y() + deltaY)
321 aLineEndPoint.setValue(aLineEndPoint.x() + deltaX,
322                         aLineEndPoint.y() + deltaY)
323 aSession.finishOperation()
324 assert (aLineStartPoint.x() != aLine2StartPoint.x() or aLineStartPoint.y() != aLine2StartPoint.y())
325 assert (aLineStartPoint.x() == aLine3StartPoint.x() and aLineStartPoint.y() == aLine3StartPoint.y())
326 assert (model.dof(aSketchFeature) == 9)
327
328 #=========================================================================
329 # End of test
330 #=========================================================================
331
332 assert(model.checkPythonDump())