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