Salome HOME
082de9f9ae165153b9df42f80e7e3242e047744f
[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 # aFeature.string("creation_method").setValue("by_xyz")
87 aFeature.real("x").setValue(0.)
88 aFeature.real("y").setValue(0.)
89 aFeature.real("z").setValue(0.)
90 anOriginName = aFeature.name()
91 aSession.finishOperation()
92 #=========================================================================
93 # Creation of a sketch
94 #=========================================================================
95 aSession.startOperation()
96 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
97 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
98 origin.setValue(0, 0, 0)
99 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
100 dirx.setValue(1, 0, 0)
101 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
102 norm.setValue(0, 0, 1)
103 aSession.finishOperation()
104 #=========================================================================
105 # Create a line and an arc
106 #=========================================================================
107 aSession.startOperation()
108 aSketchArc = aSketchFeature.addFeature("SketchArc")
109 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
110 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
111 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
112 anArcCentr.setValue(10., 10.)
113 anArcStartPoint.setValue(0., 50.)
114 anArcEndPoint.setValue(50., 0.)
115 aSketchLine = aSketchFeature.addFeature("SketchLine")
116 aLineStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
117 aLineEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
118 # Lets initialize line start at circle's end:
119 aLineStartPoint.setValue(50., 0.)
120 aLineEndPoint.setValue(100., 25.)
121 aSession.finishOperation()
122 assert (model.dof(aSketchFeature) == 9)
123 #=========================================================================
124 # Link arc's end and line's start points with concidence constraint
125 #=========================================================================
126 aSession.startOperation()
127 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
128 reflistA = aConstraint.refattr("ConstraintEntityA")
129 reflistB = aConstraint.refattr("ConstraintEntityB")
130 reflistA.setAttr(anArcEndPoint)
131 reflistB.setAttr(aLineStartPoint)
132 aConstraint.execute()
133 aSession.finishOperation()
134 assert (model.dof(aSketchFeature) == 7)
135 #=========================================================================
136 # Check values and move one constrainted object
137 #=========================================================================
138 assert (anArcEndPoint.x() == aLineStartPoint.x())
139 assert (anArcEndPoint.y() == aLineStartPoint.y())
140 deltaX = deltaY = 40.
141 #  move line
142 aSession.startOperation()
143 aLineStartPoint.setValue(aLineStartPoint.x() + deltaX,
144                          aLineStartPoint.y() + deltaY)
145 aLineEndPoint.setValue(aLineEndPoint.x() + deltaX,
146                        aLineEndPoint.y() + deltaY)
147 aSession.finishOperation()
148 # check that arc's points are moved also
149 assert (anArcEndPoint.x() == aLineStartPoint.x())
150 assert (anArcEndPoint.y() == aLineStartPoint.y())
151 assert (model.dof(aSketchFeature) == 7)
152 #=========================================================================
153 # Remove coincidence and move the line
154 #=========================================================================
155 aSession.startOperation()
156 aDocument.removeFeature(aConstraint)
157 aSession.finishOperation()
158 aSession.startOperation()
159 aLineStartPoint.setValue(70., 0.)
160 aSession.finishOperation()
161 assert (anArcEndPoint.x() != aLineStartPoint.x() or anArcEndPoint.y() != aLineStartPoint.y())
162 assert (model.dof(aSketchFeature) == 9)
163
164 #=========================================================================
165 # Add constraint point-on-line
166 #=========================================================================
167 aSession.startOperation()
168 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
169 reflistA = aConstraint.refattr("ConstraintEntityA")
170 reflistB = aConstraint.refattr("ConstraintEntityB")
171 reflistA.setAttr(anArcStartPoint)
172 reflistB.setObject(aSketchLine.lastResult())
173 aConstraint.execute()
174 aSession.finishOperation()
175 checkPointOnLine(anArcStartPoint, aSketchLine)
176 assert (model.dof(aSketchFeature) == 8)
177 #=========================================================================
178 # Add constraint point-on-circle
179 #=========================================================================
180 aSession.startOperation()
181 # create circle with center coincident with origin
182 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
183 aCircleCenter = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
184 aCircleRadius = aSketchCircle.real("circle_radius")
185 aCircleCenter.setValue(10., 10.)
186 aCircleRadius.setValue(25.)
187 aSession.finishOperation()
188 assert (model.dof(aSketchFeature) == 11)
189 # create origin
190 aSession.startOperation()
191 anOrigRes = modelAPI_Result(aDocument.objectByName("Construction", anOriginName))
192 assert (anOrigRes)
193 anOrigShape = anOrigRes.shape()
194 assert (anOrigShape)
195 anOrigin = aSketchFeature.addFeature("SketchPoint")
196 anOriginCoord = geomDataAPI_Point2D(anOrigin.attribute("PointCoordinates"))
197 anOriginCoord.setValue(0., 0.)
198 anOrigin.selection("External").setValue(anOrigRes, anOrigShape)
199 aSession.finishOperation()
200 assert (model.dof(aSketchFeature) == 11)
201 # coincidence between center of circle and the origin
202 aSession.startOperation()
203 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
204 reflistA = aConstraint.refattr("ConstraintEntityA")
205 reflistB = aConstraint.refattr("ConstraintEntityB")
206 reflistA.setAttr(aCircleCenter)
207 reflistB.setObject(anOrigin.lastResult())
208 aSession.finishOperation()
209 assert (model.dof(aSketchFeature) == 9)
210 # point-on-circle
211 aSession.startOperation()
212 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
213 reflistA = aConstraint.refattr("ConstraintEntityA")
214 reflistB = aConstraint.refattr("ConstraintEntityB")
215 reflistA.setObject(aSketchCircle.lastResult())
216 reflistB.setAttr(aLineEndPoint)
217 aConstraint.execute()
218 aSession.finishOperation()
219 checkPointOnCircle(aLineEndPoint, aSketchCircle)
220 assert (model.dof(aSketchFeature) == 8)
221 #=========================================================================
222 # Add constraint point-on-arc
223 #=========================================================================
224 aSession.startOperation("constraint point-on-arc")
225 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
226 reflistA = aConstraint.refattr("ConstraintEntityA")
227 reflistB = aConstraint.refattr("ConstraintEntityB")
228 reflistA.setAttr(aCircleCenter)
229 reflistB.setObject(aSketchArc.lastResult())
230 aConstraint.execute()
231 aSession.finishOperation()
232 checkPointOnArc(aCircleCenter, aSketchArc)
233 # check center of circle is still in origin
234 assert (aCircleCenter.x() == 0. and aCircleCenter.y() == 0.)
235 assert (model.dof(aSketchFeature) == 7)
236
237 #=========================================================================
238 # Create two more lines and set multi-coincidence between their extremities
239 #=========================================================================
240 aSession.startOperation()
241 # line 2
242 aLine2 = aSketchFeature.addFeature("SketchLine")
243 aLine2StartPoint = geomDataAPI_Point2D(aLine2.attribute("StartPoint"))
244 aLine2EndPoint = geomDataAPI_Point2D(aLine2.attribute("EndPoint"))
245 aLine2StartPoint.setValue(50., 0.)
246 aLine2EndPoint.setValue(100., 0.)
247 # line 3
248 aLine3 = aSketchFeature.addFeature("SketchLine")
249 aLine3StartPoint = geomDataAPI_Point2D(aLine3.attribute("StartPoint"))
250 aLine3EndPoint = geomDataAPI_Point2D(aLine3.attribute("EndPoint"))
251 aLine3StartPoint.setValue(50., 0.)
252 aLine3EndPoint.setValue(0., 100.)
253 aSession.finishOperation()
254 assert (model.dof(aSketchFeature) == 15)
255 # coincidences between extremities of lines
256 aSession.startOperation()
257 aConstraint12 = aSketchFeature.addFeature("SketchConstraintCoincidence")
258 refAttrA = aConstraint12.refattr("ConstraintEntityA")
259 refAttrB = aConstraint12.refattr("ConstraintEntityB")
260 refAttrA.setAttr(aLineStartPoint)
261 refAttrB.setAttr(aLine2StartPoint)
262 aConstraint23 = aSketchFeature.addFeature("SketchConstraintCoincidence")
263 refAttrA = aConstraint23.refattr("ConstraintEntityA")
264 refAttrB = aConstraint23.refattr("ConstraintEntityB")
265 refAttrA.setAttr(aLine2StartPoint)
266 refAttrB.setAttr(aLine3StartPoint)
267 aConstraint31 = aSketchFeature.addFeature("SketchConstraintCoincidence")
268 refAttrA = aConstraint31.refattr("ConstraintEntityA")
269 refAttrB = aConstraint31.refattr("ConstraintEntityB")
270 refAttrA.setAttr(aLine3StartPoint)
271 refAttrB.setAttr(aLineStartPoint)
272 aSession.finishOperation()
273 # check the points have same coordinates
274 assert (aLineStartPoint.x() == aLine2StartPoint.x() and aLineStartPoint.y() == aLine2StartPoint.y())
275 assert (aLineStartPoint.x() == aLine3StartPoint.x() and aLineStartPoint.y() == aLine3StartPoint.y())
276 assert (model.dof(aSketchFeature) == 11)
277 #=========================================================================
278 # Move one line and check other have been updated too
279 #=========================================================================
280 aSession.startOperation()
281 aLine3StartPoint.setValue(aLine3StartPoint.x() + deltaX,
282                           aLine3StartPoint.y() + deltaY)
283 aLine3EndPoint.setValue(aLine3EndPoint.x() + deltaX,
284                         aLine3EndPoint.y() + deltaY)
285 aSession.finishOperation()
286 assert (aLineStartPoint.x() == aLine2StartPoint.x() and aLineStartPoint.y() == aLine2StartPoint.y())
287 assert (aLineStartPoint.x() == aLine3StartPoint.x() and aLineStartPoint.y() == aLine3StartPoint.y())
288 assert (model.dof(aSketchFeature) == 11)
289 #=========================================================================
290 # Fix a line and move another connected segment
291 #=========================================================================
292 coordX = aLineStartPoint.x()
293 coordY = aLineStartPoint.y()
294 aSession.startOperation()
295 aFixed = aSketchFeature.addFeature("SketchConstraintRigid")
296 refAttrA = aFixed.refattr("ConstraintEntityA")
297 refAttrA.setObject(aLine2.lastResult())
298 aSession.finishOperation()
299 # move another line
300 aSession.startOperation()
301 aLine3StartPoint.setValue(aLine3StartPoint.x() + deltaX,
302                           aLine3StartPoint.y() + deltaY)
303 aLine3EndPoint.setValue(aLine3EndPoint.x() + deltaX,
304                         aLine3EndPoint.y() + deltaY)
305 aSession.finishOperation()
306 assert (aLineStartPoint.x() == coordX and aLineStartPoint.y() == coordY)
307 assert (aLine2StartPoint.x() == coordX and aLine2StartPoint.y() == coordY)
308 assert (aLine3StartPoint.x() == coordX and aLine3StartPoint.y() == coordY)
309 assert (model.dof(aSketchFeature) == 7)
310 #=========================================================================
311 # Detach fixed line and move one of remaining
312 #=========================================================================
313 aSession.startOperation()
314 aDocument.removeFeature(aConstraint12)
315 aDocument.removeFeature(aConstraint23)
316 aSession.finishOperation()
317 # move line
318 deltaX = 1.
319 deltaY = 0.
320 aSession.startOperation()
321 aLineStartPoint.setValue(aLineStartPoint.x() + deltaX,
322                           aLineStartPoint.y() + deltaY)
323 aLineEndPoint.setValue(aLineEndPoint.x() + deltaX,
324                         aLineEndPoint.y() + deltaY)
325 aSession.finishOperation()
326 assert (aLineStartPoint.x() != aLine2StartPoint.x() or aLineStartPoint.y() != aLine2StartPoint.y())
327 assert (aLineStartPoint.x() == aLine3StartPoint.x() and aLineStartPoint.y() == aLine3StartPoint.y())
328 assert (model.dof(aSketchFeature) == 9)
329
330 #=========================================================================
331 # End of test
332 #=========================================================================
333
334 assert(model.checkPythonDump())