Salome HOME
Fix for the #1757 with full split of the selected sketch-face.
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintMiddlePoint.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__ = "2016-01-29"
49 TOLERANCE = 1.e-7
50
51
52 #=========================================================================
53 # Auxiliary functions
54 #=========================================================================
55 def checkMiddlePoint(point, line):
56     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
57     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
58     assert math.fabs(aStart.x() + aEnd.x() - 2.0 * point.x()) <= TOLERANCE, "x1={0}, x2={1}, mid={2}".format(aStart.x(), aEnd.x(), point.x())
59     assert math.fabs(aStart.y() + aEnd.y() - 2.0 * point.y()) <= TOLERANCE, "y1={0}, y2={1}, mid={2}".format(aStart.y(), aEnd.y(), point.y())
60
61
62 #=========================================================================
63 # Start of test
64 #=========================================================================
65 aSession = ModelAPI_Session.get()
66 aDocument = aSession.moduleDocument()
67 # add an origin
68 aSession.startOperation()
69 aFeature = aDocument.addFeature("Point")
70 geomDataAPI_Point(aFeature.attribute("point3d")).setValue(0., 0., 0.)
71 aFeature.string("creation_method").setValue("by_xyz")
72 anOriginName = aFeature.name()
73 aSession.finishOperation()
74 #=========================================================================
75 # Creation of a sketch
76 #=========================================================================
77 aSession.startOperation()
78 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
79 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
80 origin.setValue(0, 0, 0)
81 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
82 dirx.setValue(1, 0, 0)
83 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
84 norm.setValue(0, 0, 1)
85 aSession.finishOperation()
86 #=========================================================================
87 # Create a two lines
88 #=========================================================================
89 aSession.startOperation()
90 aLine1 = aSketchFeature.addFeature("SketchLine")
91 aStartPoint1 = geomDataAPI_Point2D(aLine1.attribute("StartPoint"))
92 aEndPoint1 = geomDataAPI_Point2D(aLine1.attribute("EndPoint"))
93 aStartPoint1.setValue(50., 0.)
94 aEndPoint1.setValue(100., 25.)
95 aLine2 = aSketchFeature.addFeature("SketchLine")
96 aStartPoint2 = geomDataAPI_Point2D(aLine2.attribute("StartPoint"))
97 aEndPoint2 = geomDataAPI_Point2D(aLine2.attribute("EndPoint"))
98 aStartPoint2.setValue(10., 100.)
99 aEndPoint2.setValue(100., 25.)
100 aSession.finishOperation()
101 assert (model.dof(aSketchFeature) == 8)
102 #=========================================================================
103 # Make end point of second line middle point on first line
104 #=========================================================================
105 aSession.startOperation()
106 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
107 reflistA = aConstraint.refattr("ConstraintEntityA")
108 reflistB = aConstraint.refattr("ConstraintEntityB")
109 reflistA.setAttr(aEndPoint2)
110 reflistB.setObject(aLine1.lastResult())
111 aConstraint.execute()
112 aSession.finishOperation()
113 #=========================================================================
114 # Check error message (this message is not a error but a limitation of PlaneGCS)
115 # If the problem will be resolved, following block may be removed
116 #=========================================================================
117 assert aSketchFeature.string("SolverError").value() != "", "PlaneGCS limitation: if you see this message, then PlaneGCS has solved conflicting constraints when applying Middle constraint for the point out of the segment"
118 aSession.startOperation()
119 aDocument.removeFeature(aConstraint)
120 aSession.finishOperation()
121 aSession.startOperation()
122 aEndPoint2.setValue(80., 25.)
123 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
124 reflistA = aConstraint.refattr("ConstraintEntityA")
125 reflistB = aConstraint.refattr("ConstraintEntityB")
126 reflistA.setAttr(aEndPoint2)
127 reflistB.setObject(aLine1.lastResult())
128 aConstraint.execute()
129 aSession.finishOperation()
130 assert (model.dof(aSketchFeature) == 6)
131
132 #=========================================================================
133 # Check values and move one constrainted object
134 #=========================================================================
135 checkMiddlePoint(aEndPoint2, aLine1)
136 deltaX, deltaY = -80.0, -40.0
137 aSession.startOperation()
138 aStartPoint1.setValue(aStartPoint1.x() + deltaX, aStartPoint1.y() + deltaY)
139 aSession.finishOperation()
140 checkMiddlePoint(aEndPoint2, aLine1)
141 assert (model.dof(aSketchFeature) == 6)
142 #=========================================================================
143 # Remove constraint and move the line
144 #=========================================================================
145 aCurX, aCurY = aEndPoint2.x(), aEndPoint2.y()
146 aSession.startOperation()
147 aDocument.removeFeature(aConstraint)
148 aSession.finishOperation()
149 assert (model.dof(aSketchFeature) == 8)
150 aSession.startOperation()
151 aEndPoint1.setValue(90., 0.)
152 aSession.finishOperation()
153 assert (aEndPoint2.x() == aCurX and aEndPoint2.y() == aCurY)
154 assert (model.dof(aSketchFeature) == 8)
155
156 #=========================================================================
157 # Set external point as a middle point
158 #=========================================================================
159 # create origin
160 aSession.startOperation()
161 anOrigRes = modelAPI_Result(aDocument.objectByName("Construction", anOriginName))
162 assert (anOrigRes)
163 anOrigShape = anOrigRes.shape()
164 assert (anOrigShape)
165 anOrigin = aSketchFeature.addFeature("SketchPoint")
166 anOriginCoord = geomDataAPI_Point2D(anOrigin.attribute("PointCoordinates"))
167 anOriginCoord.setValue(0., 0.)
168 anOrigin.selection("External").setValue(anOrigRes, anOrigShape)
169 aSession.finishOperation()
170 assert (model.dof(aSketchFeature) == 8)
171 # middle point constraint
172 aSession.startOperation()
173 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
174 reflistA = aConstraint.refattr("ConstraintEntityA")
175 reflistB = aConstraint.refattr("ConstraintEntityB")
176 reflistA.setObject(aLine2.lastResult())
177 reflistB.setObject(anOrigin.lastResult())
178 aSession.finishOperation()
179 checkMiddlePoint(anOriginCoord, aLine2)
180 assert (model.dof(aSketchFeature) == 6)
181 #=========================================================================
182 # Check origin coordinates does not changed
183 #=========================================================================
184 assert (anOriginCoord.x() == 0. and anOriginCoord.y() == 0.)
185
186 #=========================================================================
187 # Add other line with one extremity coincident to the first line
188 #=========================================================================
189 aSession.startOperation()
190 aLine3 = aSketchFeature.addFeature("SketchLine")
191 aStartPoint3 = geomDataAPI_Point2D(aLine3.attribute("StartPoint"))
192 aEndPoint3 = geomDataAPI_Point2D(aLine3.attribute("EndPoint"))
193 aStartPoint3.setValue(50., 50.)
194 aEndPoint3.setValue(50., 0.)
195 aCoincidence = aSketchFeature.addFeature("SketchConstraintCoincidence")
196 reflistA = aCoincidence.refattr("ConstraintEntityA")
197 reflistB = aCoincidence.refattr("ConstraintEntityB")
198 reflistA.setAttr(aEndPoint3)
199 reflistB.setObject(aLine1.lastResult())
200 aSession.finishOperation()
201 assert (model.dof(aSketchFeature) == 9)
202 #=========================================================================
203 # Set Middle point
204 #=========================================================================
205 aSession.startOperation()
206 aMiddle = aSketchFeature.addFeature("SketchConstraintMiddle")
207 reflistA = aMiddle.refattr("ConstraintEntityA")
208 reflistB = aMiddle.refattr("ConstraintEntityB")
209 reflistA.setAttr(aEndPoint3)
210 reflistB.setObject(aLine1.lastResult())
211 aSession.finishOperation()
212 # check the point, and no error message
213 assert aSketchFeature.string("SolverError").value() == ""
214 assert (model.dof(aSketchFeature) == 8)
215 checkMiddlePoint(aEndPoint3, aLine1)
216 #=========================================================================
217 # Remove coincidence and move one line
218 #=========================================================================
219 aSession.startOperation()
220 aDocument.removeFeature(aCoincidence)
221 aSession.finishOperation()
222 deltaX, deltaY = 10.0, -10.0
223 aSession.startOperation()
224 aStartPoint1.setValue(aStartPoint1.x() + deltaX, aStartPoint1.y() + deltaY)
225 aEndPoint1.setValue(aEndPoint1.x() + deltaX, aEndPoint1.y() + deltaY)
226 aSession.finishOperation()
227 checkMiddlePoint(aEndPoint3, aLine1)
228 assert (model.dof(aSketchFeature) == 8)
229
230 #=========================================================================
231 # End of test
232 #=========================================================================
233
234 assert(model.checkPythonDump())