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