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