Salome HOME
Merge branch 'Pre_2.8.0_development'
[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 # aFeature.string("creation_method").setValue("by_xyz")
71 aFeature.real("x").setValue(0.)
72 aFeature.real("y").setValue(0.)
73 aFeature.real("z").setValue(0.)
74 anOriginName = aFeature.name()
75 aSession.finishOperation()
76 #=========================================================================
77 # Creation of a sketch
78 #=========================================================================
79 aSession.startOperation()
80 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
81 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
82 origin.setValue(0, 0, 0)
83 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
84 dirx.setValue(1, 0, 0)
85 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
86 norm.setValue(0, 0, 1)
87 aSession.finishOperation()
88 #=========================================================================
89 # Create a two lines
90 #=========================================================================
91 aSession.startOperation()
92 aLine1 = aSketchFeature.addFeature("SketchLine")
93 aStartPoint1 = geomDataAPI_Point2D(aLine1.attribute("StartPoint"))
94 aEndPoint1 = geomDataAPI_Point2D(aLine1.attribute("EndPoint"))
95 aStartPoint1.setValue(50., 0.)
96 aEndPoint1.setValue(100., 25.)
97 aLine2 = aSketchFeature.addFeature("SketchLine")
98 aStartPoint2 = geomDataAPI_Point2D(aLine2.attribute("StartPoint"))
99 aEndPoint2 = geomDataAPI_Point2D(aLine2.attribute("EndPoint"))
100 aStartPoint2.setValue(10., 100.)
101 aEndPoint2.setValue(100., 25.)
102 aSession.finishOperation()
103 assert (model.dof(aSketchFeature) == 8)
104 #=========================================================================
105 # Make end point of second line middle point on first line
106 #=========================================================================
107 aSession.startOperation()
108 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
109 reflistA = aConstraint.refattr("ConstraintEntityA")
110 reflistB = aConstraint.refattr("ConstraintEntityB")
111 reflistA.setAttr(aEndPoint2)
112 reflistB.setObject(aLine1.lastResult())
113 aConstraint.execute()
114 aSession.finishOperation()
115 #=========================================================================
116 # Check error message (this message is not a error but a limitation of PlaneGCS)
117 # If the problem will be resolved, following block may be removed
118 #=========================================================================
119 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"
120 aSession.startOperation()
121 aDocument.removeFeature(aConstraint)
122 aSession.finishOperation()
123 aSession.startOperation()
124 aEndPoint2.setValue(80., 25.)
125 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
126 reflistA = aConstraint.refattr("ConstraintEntityA")
127 reflistB = aConstraint.refattr("ConstraintEntityB")
128 reflistA.setAttr(aEndPoint2)
129 reflistB.setObject(aLine1.lastResult())
130 aConstraint.execute()
131 aSession.finishOperation()
132 assert (model.dof(aSketchFeature) == 6)
133
134 #=========================================================================
135 # Check values and move one constrainted object
136 #=========================================================================
137 checkMiddlePoint(aEndPoint2, aLine1)
138 deltaX, deltaY = -80.0, -40.0
139 aSession.startOperation()
140 aStartPoint1.setValue(aStartPoint1.x() + deltaX, aStartPoint1.y() + deltaY)
141 aSession.finishOperation()
142 checkMiddlePoint(aEndPoint2, aLine1)
143 assert (model.dof(aSketchFeature) == 6)
144 #=========================================================================
145 # Remove constraint and move the line
146 #=========================================================================
147 aCurX, aCurY = aEndPoint2.x(), aEndPoint2.y()
148 aSession.startOperation()
149 aDocument.removeFeature(aConstraint)
150 aSession.finishOperation()
151 assert (model.dof(aSketchFeature) == 8)
152 aSession.startOperation()
153 aEndPoint1.setValue(90., 0.)
154 aSession.finishOperation()
155 assert (aEndPoint2.x() == aCurX and aEndPoint2.y() == aCurY)
156 assert (model.dof(aSketchFeature) == 8)
157
158 #=========================================================================
159 # Set external point as a middle point
160 #=========================================================================
161 # create origin
162 aSession.startOperation()
163 anOrigRes = modelAPI_Result(aDocument.objectByName("Construction", anOriginName))
164 assert (anOrigRes)
165 anOrigShape = anOrigRes.shape()
166 assert (anOrigShape)
167 anOrigin = aSketchFeature.addFeature("SketchPoint")
168 anOriginCoord = geomDataAPI_Point2D(anOrigin.attribute("PointCoordinates"))
169 anOriginCoord.setValue(0., 0.)
170 anOrigin.selection("External").setValue(anOrigRes, anOrigShape)
171 aSession.finishOperation()
172 assert (model.dof(aSketchFeature) == 8)
173 # middle point constraint
174 aSession.startOperation()
175 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
176 reflistA = aConstraint.refattr("ConstraintEntityA")
177 reflistB = aConstraint.refattr("ConstraintEntityB")
178 reflistA.setObject(aLine2.lastResult())
179 reflistB.setObject(anOrigin.lastResult())
180 aSession.finishOperation()
181 checkMiddlePoint(anOriginCoord, aLine2)
182 assert (model.dof(aSketchFeature) == 6)
183 #=========================================================================
184 # Check origin coordinates does not changed
185 #=========================================================================
186 assert (anOriginCoord.x() == 0. and anOriginCoord.y() == 0.)
187
188 #=========================================================================
189 # Add other line with one extremity coincident to the first line
190 #=========================================================================
191 aSession.startOperation()
192 aLine3 = aSketchFeature.addFeature("SketchLine")
193 aStartPoint3 = geomDataAPI_Point2D(aLine3.attribute("StartPoint"))
194 aEndPoint3 = geomDataAPI_Point2D(aLine3.attribute("EndPoint"))
195 aStartPoint3.setValue(50., 50.)
196 aEndPoint3.setValue(50., 0.)
197 aCoincidence = aSketchFeature.addFeature("SketchConstraintCoincidence")
198 reflistA = aCoincidence.refattr("ConstraintEntityA")
199 reflistB = aCoincidence.refattr("ConstraintEntityB")
200 reflistA.setAttr(aEndPoint3)
201 reflistB.setObject(aLine1.lastResult())
202 aSession.finishOperation()
203 assert (model.dof(aSketchFeature) == 9)
204 #=========================================================================
205 # Set Middle point
206 #=========================================================================
207 aSession.startOperation()
208 aMiddle = aSketchFeature.addFeature("SketchConstraintMiddle")
209 reflistA = aMiddle.refattr("ConstraintEntityA")
210 reflistB = aMiddle.refattr("ConstraintEntityB")
211 reflistA.setAttr(aEndPoint3)
212 reflistB.setObject(aLine1.lastResult())
213 aSession.finishOperation()
214 # check the point, and no error message
215 assert aSketchFeature.string("SolverError").value() == ""
216 assert (model.dof(aSketchFeature) == 8)
217 checkMiddlePoint(aEndPoint3, aLine1)
218 #=========================================================================
219 # Remove coincidence and move one line
220 #=========================================================================
221 aSession.startOperation()
222 aDocument.removeFeature(aCoincidence)
223 aSession.finishOperation()
224 deltaX, deltaY = 10.0, -10.0
225 aSession.startOperation()
226 aStartPoint1.setValue(aStartPoint1.x() + deltaX, aStartPoint1.y() + deltaY)
227 aEndPoint1.setValue(aEndPoint1.x() + deltaX, aEndPoint1.y() + deltaY)
228 aSession.finishOperation()
229 checkMiddlePoint(aEndPoint3, aLine1)
230 assert (model.dof(aSketchFeature) == 8)
231
232 #=========================================================================
233 # End of test
234 #=========================================================================
235
236 assert(model.checkPythonDump())