Salome HOME
[EDF] (2023-T1) Sketch middle point constrain should create point if missing
[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 from SketchAPI 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 geomDataAPI_Point(aFeature.attribute("point3d")).setValue(0., 0., 0.)
71 aFeature.string("creation_method").setValue("by_xyz")
72 anOriginName = aFeature.name()
73 aSession.finishOperation()
74 #=========================================================================
75 # Creation of a sketch
76 #=========================================================================
77 aSession.startOperation()
78 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
79 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
80 origin.setValue(0, 0, 0)
81 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
82 dirx.setValue(1, 0, 0)
83 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
84 norm.setValue(0, 0, 1)
85 aSession.finishOperation()
86 #=========================================================================
87 # Create a two lines
88 #=========================================================================
89 aSession.startOperation()
90 aLine1 = aSketchFeature.addFeature("SketchLine")
91 aStartPoint1 = geomDataAPI_Point2D(aLine1.attribute("StartPoint"))
92 aEndPoint1 = geomDataAPI_Point2D(aLine1.attribute("EndPoint"))
93 aStartPoint1.setValue(50., 0.)
94 aEndPoint1.setValue(100., 25.)
95 aLine2 = aSketchFeature.addFeature("SketchLine")
96 aStartPoint2 = geomDataAPI_Point2D(aLine2.attribute("StartPoint"))
97 aEndPoint2 = geomDataAPI_Point2D(aLine2.attribute("EndPoint"))
98 aStartPoint2.setValue(10., 100.)
99 aEndPoint2.setValue(100., 25.)
100 aSession.finishOperation()
101 assert (model.dof(aSketchFeature) == 8)
102 #=========================================================================
103 # Make end point of second line middle point on first line
104 #=========================================================================
105 aSession.startOperation()
106 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
107 reflistA = aConstraint.refattr("ConstraintEntityA")
108 reflistB = aConstraint.refattr("ConstraintEntityB")
109 anAlgoType = aConstraint.string("middle_type")
110 anAlgoType.setValue("middle_type_by_line_and_point")
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 anAlgoType = aConstraint.string("middle_type")
129 anAlgoType.setValue("middle_type_by_line_and_point")
130 reflistA.setAttr(aEndPoint2)
131 reflistB.setObject(aLine1.lastResult())
132 aConstraint.execute()
133 aSession.finishOperation()
134 assert (model.dof(aSketchFeature) == 6)
135
136 #=========================================================================
137 # Check values and move one constrainted object
138 #=========================================================================
139 checkMiddlePoint(aEndPoint2, aLine1)
140 deltaX, deltaY = -80.0, -40.0
141 aSession.startOperation()
142 aStartPoint1.setValue(aStartPoint1.x() + deltaX, aStartPoint1.y() + deltaY)
143 aSession.finishOperation()
144 checkMiddlePoint(aEndPoint2, aLine1)
145 assert (model.dof(aSketchFeature) == 6)
146 #=========================================================================
147 # Remove constraint and move the line
148 #=========================================================================
149 aCurX, aCurY = aEndPoint2.x(), aEndPoint2.y()
150 aSession.startOperation()
151 aDocument.removeFeature(aConstraint)
152 aSession.finishOperation()
153 assert (model.dof(aSketchFeature) == 8)
154 aSession.startOperation()
155 aEndPoint1.setValue(90., 0.)
156 aSession.finishOperation()
157 assert (aEndPoint2.x() == aCurX and aEndPoint2.y() == aCurY)
158 assert (model.dof(aSketchFeature) == 8)
159
160 #=========================================================================
161 # Set external point as a middle point
162 #=========================================================================
163 # create origin
164 aSession.startOperation()
165 anOrigRes = modelAPI_Result(aDocument.objectByName("Construction", anOriginName))
166 assert (anOrigRes)
167 anOrigShape = anOrigRes.shape()
168 assert (anOrigShape)
169 anOrigin = aSketchFeature.addFeature("SketchPoint")
170 anOriginCoord = geomDataAPI_Point2D(anOrigin.attribute("PointCoordinates"))
171 anOriginCoord.setValue(0., 0.)
172 anOrigin.selection("External").setValue(anOrigRes, anOrigShape)
173 aSession.finishOperation()
174 assert (model.dof(aSketchFeature) == 8)
175 # middle point constraint
176 aSession.startOperation()
177 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
178 reflistA = aConstraint.refattr("ConstraintEntityA")
179 reflistB = aConstraint.refattr("ConstraintEntityB")
180 anAlgoType = aConstraint.string("middle_type")
181 anAlgoType.setValue("middle_type_by_line_and_point")
182 reflistA.setObject(aLine2.lastResult())
183 reflistB.setObject(anOrigin.lastResult())
184 aSession.finishOperation()
185 checkMiddlePoint(anOriginCoord, aLine2)
186 assert (model.dof(aSketchFeature) == 6)
187 #=========================================================================
188 # Check origin coordinates does not changed
189 #=========================================================================
190 assert (anOriginCoord.x() == 0. and anOriginCoord.y() == 0.)
191
192 #=========================================================================
193 # Add other line with one extremity coincident to the first line
194 #=========================================================================
195 aSession.startOperation()
196 aLine3 = aSketchFeature.addFeature("SketchLine")
197 aStartPoint3 = geomDataAPI_Point2D(aLine3.attribute("StartPoint"))
198 aEndPoint3 = geomDataAPI_Point2D(aLine3.attribute("EndPoint"))
199 aStartPoint3.setValue(50., 50.)
200 aEndPoint3.setValue(50., 0.)
201 aCoincidence = aSketchFeature.addFeature("SketchConstraintCoincidence")
202 reflistA = aCoincidence.refattr("ConstraintEntityA")
203 reflistB = aCoincidence.refattr("ConstraintEntityB")
204 reflistA.setAttr(aEndPoint3)
205 reflistB.setObject(aLine1.lastResult())
206 aSession.finishOperation()
207 assert (model.dof(aSketchFeature) == 9)
208 #=========================================================================
209 # Set Middle point
210 #=========================================================================
211 aSession.startOperation()
212 aMiddle = aSketchFeature.addFeature("SketchConstraintMiddle")
213 reflistA = aMiddle.refattr("ConstraintEntityA")
214 reflistB = aMiddle.refattr("ConstraintEntityB")
215 anAlgoType = aMiddle.string("middle_type")
216 anAlgoType.setValue("middle_type_by_line_and_point")
217 reflistA.setAttr(aEndPoint3)
218 reflistB.setObject(aLine1.lastResult())
219 aSession.finishOperation()
220 # check the point, and no error message
221 assert aSketchFeature.string("SolverError").value() == ""
222 assert (model.dof(aSketchFeature) == 8)
223 checkMiddlePoint(aEndPoint3, aLine1)
224 #=========================================================================
225 # Remove coincidence and move one line
226 #=========================================================================
227 aSession.startOperation()
228 aDocument.removeFeature(aCoincidence)
229 aSession.finishOperation()
230 deltaX, deltaY = 10.0, -10.0
231 aSession.startOperation()
232 aStartPoint1.setValue(aStartPoint1.x() + deltaX, aStartPoint1.y() + deltaY)
233 aEndPoint1.setValue(aEndPoint1.x() + deltaX, aEndPoint1.y() + deltaY)
234 aSession.finishOperation()
235 checkMiddlePoint(aEndPoint3, aLine1)
236 assert (model.dof(aSketchFeature) == 8)
237 #=========================================================================
238 # CreateLine
239 #=========================================================================
240 aSession.startOperation()
241 aLine4 = aSketchFeature.addFeature("SketchLine")
242 aStartPoint4 = geomDataAPI_Point2D(aLine4.attribute("StartPoint"))
243 aEndPoint4 = geomDataAPI_Point2D(aLine4.attribute("EndPoint"))
244 aStartPoint4.setValue(2., 8.)
245 aEndPoint4.setValue(20., 14.)
246 aRigidConstraint1 = aSketchFeature.addFeature("SketchConstraintRigid")
247 aRigidConstraint1.refattr("ConstraintEntityA").setAttr(aStartPoint4)
248 aRigidConstraint2 = aSketchFeature.addFeature("SketchConstraintRigid")
249 aRigidConstraint2.refattr("ConstraintEntityA").setAttr(aEndPoint4)
250 aSession.finishOperation()
251 #=========================================================================
252 # Set middle point on line
253 #=========================================================================
254 aSession.startOperation()
255 aMiddle = aSketchFeature.addFeature("SketchConstraintMiddle")
256 reflistA = aMiddle.refattr("ConstraintEntityA")
257 anAlgoType = aMiddle.string("middle_type")
258 anAlgoType.setValue("middle_type_by_line")
259 reflistA.setObject(aLine4.lastResult())
260 aSession.finishOperation()
261 arefB = aMiddle.refattr("ConstraintEntityB")
262 aPointRes = ModelAPI_Feature.feature(arefB.object())
263 aMidPoint = geomDataAPI_Point2D(SketchAPI_Point(aPointRes).coordinates())
264
265 # check the point, and no error message
266 checkMiddlePoint(aMidPoint, aLine4)
267
268
269 #=========================================================================
270 # End of test
271 #=========================================================================
272
273 assert(model.checkPythonDump())