Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into azv/SketchSolver_Refactoring
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintMiddlePoint.py
1 """
2     TestConstraintCoincidence.py
3     Unit test of SketchPlugin_ConstraintCoincidence class
4
5     SketchPlugin_Constraint
6         static const std::string MY_CONSTRAINT_VALUE("ConstraintValue");
7         static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt");
8         static const std::string MY_ENTITY_A("ConstraintEntityA");
9         static const std::string MY_ENTITY_B("ConstraintEntityB");
10         static const std::string MY_ENTITY_C("ConstraintEntityC");
11         static const std::string MY_ENTITY_D("ConstraintEntityD");
12
13     SketchPlugin_ConstraintCoincidence
14         static const std::string MY_CONSTRAINT_COINCIDENCE_ID("SketchConstraintCoincidence");
15         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
16         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
17
18 """
19 from GeomDataAPI import *
20 from ModelAPI import *
21 import math
22 from salome.shaper import model
23
24 #=========================================================================
25 # Initialization of the test
26 #=========================================================================
27
28 __updated__ = "2016-01-29"
29 TOLERANCE = 1.e-7
30
31
32 #=========================================================================
33 # Auxiliary functions
34 #=========================================================================
35 def checkMiddlePoint(point, line):
36     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
37     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
38     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())
39     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())
40
41
42 #=========================================================================
43 # Start of test
44 #=========================================================================
45 aSession = ModelAPI_Session.get()
46 aDocument = aSession.moduleDocument()
47 # add an origin
48 aSession.startOperation()
49 aFeature = aDocument.addFeature("Point")
50 # aFeature.string("creation_method").setValue("by_xyz")
51 aFeature.real("x").setValue(0.)
52 aFeature.real("y").setValue(0.)
53 aFeature.real("z").setValue(0.)
54 anOriginName = aFeature.name()
55 aSession.finishOperation()
56 #=========================================================================
57 # Creation of a sketch
58 #=========================================================================
59 aSession.startOperation()
60 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
61 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
62 origin.setValue(0, 0, 0)
63 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
64 dirx.setValue(1, 0, 0)
65 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
66 norm.setValue(0, 0, 1)
67 aSession.finishOperation()
68 #=========================================================================
69 # Create a two lines
70 #=========================================================================
71 aSession.startOperation()
72 aLine1 = aSketchFeature.addFeature("SketchLine")
73 aStartPoint1 = geomDataAPI_Point2D(aLine1.attribute("StartPoint"))
74 aEndPoint1 = geomDataAPI_Point2D(aLine1.attribute("EndPoint"))
75 aStartPoint1.setValue(50., 0.)
76 aEndPoint1.setValue(100., 25.)
77 aLine2 = aSketchFeature.addFeature("SketchLine")
78 aStartPoint2 = geomDataAPI_Point2D(aLine2.attribute("StartPoint"))
79 aEndPoint2 = geomDataAPI_Point2D(aLine2.attribute("EndPoint"))
80 aStartPoint2.setValue(10., 100.)
81 aEndPoint2.setValue(100., 25.)
82 aSession.finishOperation()
83 assert (model.dof(aSketchFeature) == 8)
84 #=========================================================================
85 # Make end point of second line middle point on first line
86 #=========================================================================
87 aSession.startOperation()
88 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
89 reflistA = aConstraint.refattr("ConstraintEntityA")
90 reflistB = aConstraint.refattr("ConstraintEntityB")
91 reflistA.setAttr(aEndPoint2)
92 reflistB.setObject(aLine1.lastResult())
93 aConstraint.execute()
94 aSession.finishOperation()
95 #=========================================================================
96 # Check error message (this message is not a error but a limitation of PlaneGCS)
97 # If the problem will be resolved, following block may be removed
98 #=========================================================================
99 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"
100 aSession.startOperation()
101 aDocument.removeFeature(aConstraint)
102 aSession.finishOperation()
103 aSession.startOperation()
104 aEndPoint2.setValue(80., 25.)
105 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
106 reflistA = aConstraint.refattr("ConstraintEntityA")
107 reflistB = aConstraint.refattr("ConstraintEntityB")
108 reflistA.setAttr(aEndPoint2)
109 reflistB.setObject(aLine1.lastResult())
110 aConstraint.execute()
111 aSession.finishOperation()
112 assert (model.dof(aSketchFeature) == 6)
113
114 #=========================================================================
115 # Check values and move one constrainted object
116 #=========================================================================
117 checkMiddlePoint(aEndPoint2, aLine1)
118 deltaX, deltaY = -80.0, -40.0
119 aSession.startOperation()
120 aStartPoint1.setValue(aStartPoint1.x() + deltaX, aStartPoint1.y() + deltaY)
121 aSession.finishOperation()
122 checkMiddlePoint(aEndPoint2, aLine1)
123 assert (model.dof(aSketchFeature) == 6)
124 #=========================================================================
125 # Remove constraint and move the line
126 #=========================================================================
127 aCurX, aCurY = aEndPoint2.x(), aEndPoint2.y()
128 aSession.startOperation()
129 aDocument.removeFeature(aConstraint)
130 aSession.finishOperation()
131 assert (model.dof(aSketchFeature) == 8)
132 aSession.startOperation()
133 aEndPoint1.setValue(90., 0.)
134 aSession.finishOperation()
135 assert (aEndPoint2.x() == aCurX and aEndPoint2.y() == aCurY)
136 assert (model.dof(aSketchFeature) == 8)
137
138 #=========================================================================
139 # Set external point as a middle point
140 #=========================================================================
141 # create origin
142 aSession.startOperation()
143 anOrigRes = modelAPI_Result(aDocument.objectByName("Construction", anOriginName))
144 assert (anOrigRes)
145 anOrigShape = anOrigRes.shape()
146 assert (anOrigShape)
147 anOrigin = aSketchFeature.addFeature("SketchPoint")
148 anOriginCoord = geomDataAPI_Point2D(anOrigin.attribute("PointCoordindates"))
149 anOriginCoord.setValue(0., 0.)
150 anOrigin.selection("External").setValue(anOrigRes, anOrigShape)
151 aSession.finishOperation()
152 assert (model.dof(aSketchFeature) == 8)
153 # middle point constraint
154 aSession.startOperation()
155 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
156 reflistA = aConstraint.refattr("ConstraintEntityA")
157 reflistB = aConstraint.refattr("ConstraintEntityB")
158 reflistA.setObject(aLine2.lastResult())
159 reflistB.setObject(anOrigin.lastResult())
160 aSession.finishOperation()
161 checkMiddlePoint(anOriginCoord, aLine2)
162 assert (model.dof(aSketchFeature) == 6)
163 #=========================================================================
164 # Check origin coordinates does not changed
165 #=========================================================================
166 assert (anOriginCoord.x() == 0. and anOriginCoord.y() == 0.)
167
168 #=========================================================================
169 # Add other line with one extremity coincident to the first line
170 #=========================================================================
171 aSession.startOperation()
172 aLine3 = aSketchFeature.addFeature("SketchLine")
173 aStartPoint3 = geomDataAPI_Point2D(aLine3.attribute("StartPoint"))
174 aEndPoint3 = geomDataAPI_Point2D(aLine3.attribute("EndPoint"))
175 aStartPoint3.setValue(50., 50.)
176 aEndPoint3.setValue(50., 0.)
177 aCoincidence = aSketchFeature.addFeature("SketchConstraintCoincidence")
178 reflistA = aCoincidence.refattr("ConstraintEntityA")
179 reflistB = aCoincidence.refattr("ConstraintEntityB")
180 reflistA.setAttr(aEndPoint3)
181 reflistB.setObject(aLine1.lastResult())
182 aSession.finishOperation()
183 assert (model.dof(aSketchFeature) == 9)
184 #=========================================================================
185 # Set Middle point
186 #=========================================================================
187 aSession.startOperation()
188 aMiddle = aSketchFeature.addFeature("SketchConstraintMiddle")
189 reflistA = aMiddle.refattr("ConstraintEntityA")
190 reflistB = aMiddle.refattr("ConstraintEntityB")
191 reflistA.setAttr(aEndPoint3)
192 reflistB.setObject(aLine1.lastResult())
193 aSession.finishOperation()
194 # check the point, and no error message
195 assert aSketchFeature.string("SolverError").value() == ""
196 assert (model.dof(aSketchFeature) == 8)
197 checkMiddlePoint(aEndPoint3, aLine1)
198 #=========================================================================
199 # Remove coincidence and move one line
200 #=========================================================================
201 aSession.startOperation()
202 aDocument.removeFeature(aCoincidence)
203 aSession.finishOperation()
204 deltaX, deltaY = 10.0, -10.0
205 aSession.startOperation()
206 aStartPoint1.setValue(aStartPoint1.x() + deltaX, aStartPoint1.y() + deltaY)
207 aEndPoint1.setValue(aEndPoint1.x() + deltaX, aEndPoint1.y() + deltaY)
208 aSession.finishOperation()
209 checkMiddlePoint(aEndPoint3, aLine1)
210 assert (model.dof(aSketchFeature) == 8)
211
212 #=========================================================================
213 # End of test
214 #=========================================================================
215
216 assert(model.checkPythonDump())