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