Salome HOME
22c5fb4f775824c63e2a5b601c4be60c7c6cdee4
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByCenterStartEnd.py
1 # Copyright (C) 2014-2023  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 email : webmaster.salome@opencascade.com
18 #
19
20 """
21     TestCreateArc.py
22
23     static const std::string MY_SKETCH_ARC_ID("SketchArc");
24     static const std::string MY_CENTER_ID = "center_point";
25     static const std::string MY_START_ID = "start_point";
26     static const std::string MY_END_ID = "end_point";
27     data()->addAttribute(SketchPlugin_Arc::CENTER_ID(), GeomDataAPI_Point2D::typeId());
28     data()->addAttribute(SketchPlugin_Arc::START_ID(),  GeomDataAPI_Point2D::typeId());
29     data()->addAttribute(SketchPlugin_Arc::END_ID(),    GeomDataAPI_Point2D::typeId());
30 """
31
32 #=========================================================================
33 # Initialization of the test
34 #=========================================================================
35 from GeomDataAPI import *
36 from ModelAPI import *
37 from SketchAPI import SketchAPI_Sketch
38 import math
39 from salome.shaper import model
40
41 __updated__ = "2017-03-28"
42
43 TOLERANCE = 1.e-7
44
45 #=========================================================================
46 # Auxiliary functions
47 #=========================================================================
48
49 def verifyLastArc(theSketch, theCenter, theStart, theEnd):
50     """
51     subroutine to verify position of last arc in the sketch
52     """
53     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
54     model.assertArc(aLastArc, theCenter, theStart, theEnd)
55
56 def verifyPointOnLine(thePoint, theLine):
57     aDistance = model.distancePointLine(thePoint, theLine)
58     assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
59
60 def verifyPointOnCircle(thePoint, theCircular):
61     if theCircular.getKind() == "SketchArc":
62         aCenterPoint = geomDataAPI_Point2D(theCircular.attribute("center_point"))
63         aStartPoint = geomDataAPI_Point2D(theCircular.attribute("start_point"))
64         aRadius = model.distancePointPoint(aCenterPoint, aStartPoint)
65     elif theCircular.getKind() == "SketchCircle":
66         aCenterPoint = geomDataAPI_Point2D(theCircular.attribute("circle_center"))
67         aRadius = theCircular.real("circle_radius").value()
68     else:
69         return
70     assert math.fabs(model.distancePointPoint(aCenterPoint, thePoint) - aRadius) < TOLERANCE
71
72
73 aSession = ModelAPI_Session.get()
74 aDocument = aSession.moduleDocument()
75 #=========================================================================
76 # Creation of a sketch
77 #=========================================================================
78 aSession.startOperation()
79 aSketchCommonFeature = aDocument.addFeature("Sketch")
80 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
81 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
82 origin.setValue(0, 0, 0)
83 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
84 dirx.setValue(1, 0, 0)
85 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
86 norm.setValue(0, 0, 1)
87 aSession.finishOperation()
88 aSketch = SketchAPI_Sketch(aSketchFeature)
89
90 #=========================================================================
91 # Test 1. Create an arc by center, start and end points
92 #=========================================================================
93 aCenter = [10., 10.]
94 aStart  = [0., 50.]
95 aEnd    = [50., 0.]
96 aSession.startOperation()
97 aSketchArc = aSketchFeature.addFeature("SketchArc")
98 assert (aSketchArc.getKind() == "SketchArc")
99 anArcCenter = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
100 assert (not anArcCenter.isInitialized())
101 anArcCenter.setValue(aCenter[0], aCenter[1])
102 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
103 assert (not anArcStartPoint.isInitialized())
104 anArcStartPoint.setValue(aStart[0], aStart[1])
105 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
106 assert (not anArcEndPoint.isInitialized())
107 anArcEndPoint.setValue(aEnd[0], aEnd[1])
108 aSession.finishOperation()
109 verifyLastArc(aSketchFeature, aCenter, aStart, aEnd)
110 # Move center
111 aCenter = [15., 20.]
112 aSession.startOperation()
113 anArcCenter.setValue(aCenter[0], aCenter[1])
114 aSession.finishOperation()
115 verifyLastArc(aSketchFeature, [], [], [])
116 # Move start point
117 deltaX, deltaY = 5., 2.
118 aStart = [anArcStartPoint.x() + deltaX, anArcStartPoint.y() + deltaY]
119 aSession.startOperation()
120 anArcStartPoint.setValue(aStart[0], aStart[1])
121 aSession.finishOperation()
122 verifyLastArc(aSketchFeature, [], [], [])
123 # Move end point
124 aEnd = [anArcEndPoint.x() - deltaX, anArcEndPoint.y() - deltaY]
125 aSession.startOperation()
126 anArcEndPoint.setValue(aEnd[0], aEnd[1])
127 aSession.finishOperation()
128 verifyLastArc(aSketchFeature, [], [], [])
129 # Check that changing the radius does not affect arc
130 aSession.startOperation()
131 anArcRadius = aSketchArc.real("radius")
132 aPrevRadius = anArcRadius.value();
133 anArcRadius.setValue(aPrevRadius + 10.)
134 aSession.finishOperation()
135 assert (math.fabs(anArcRadius.value() - aPrevRadius) < TOLERANCE)
136 verifyLastArc(aSketchFeature, [], [], [])
137 # Check that changing the angle does not affect arc
138 aSession.startOperation()
139 anArcAngle = aSketchArc.real("angle")
140 aPrevAngle = anArcAngle.value()
141 anArcAngle.setValue(aPrevAngle + 10.)
142 aSession.finishOperation()
143 assert (math.fabs(anArcAngle.value() - aPrevAngle) < TOLERANCE)
144 verifyLastArc(aSketchFeature, [], [], [])
145
146 #=========================================================================
147 # Test 2. Create an arc as a macro-feature by center, start and end points
148 #=========================================================================
149 aSession.startOperation()
150 anArc = aSketchFeature.addFeature("SketchMacroArc")
151 assert (anArc.getKind() == "SketchMacroArc")
152 anArcCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
153 assert (not anArcCenter.isInitialized())
154 anArcStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
155 assert (not anArcStart.isInitialized())
156 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
157 assert (not anArcEnd.isInitialized())
158 anArcType = anArc.string("arc_type")
159 assert (not anArcType.isInitialized())
160 anArcType.setValue("by_center_and_points")
161 anArcCenter.setValue(aCenter[0], aCenter[1])
162 anArcStart.setValue(aStart[0], aStart[1])
163 anArcEnd.setValue(aEnd[0], aEnd[1])
164 aSession.finishOperation()
165 assert (aSketchFeature.numberOfSubs() == 2)
166 verifyLastArc(aSketchFeature, [], [], [])
167
168 #=========================================================================
169 # Test 3. Create an arc by center and two points coincident to other points
170 #=========================================================================
171 # get previous arc
172 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
173 aPrevArcStart = geomDataAPI_Point2D(aPrevArc.attribute("start_point"))
174 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
175 # create additional point
176 aPointCoordinates = [0., 0.]
177 aSession.startOperation()
178 aPoint = aSketchFeature.addFeature("SketchPoint")
179 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
180 aPointCoord.setValue(aPointCoordinates[0], aPointCoordinates[1])
181 aPoint.selection("External").selectSubShape("VERTEX", "Origin")
182 aSession.finishOperation()
183 # create additional line
184 aLineStart = [20., -5.]
185 aLineEnd = [20., 20]
186 aSession.startOperation()
187 aLine = aSketchFeature.addFeature("SketchLine")
188 aLineStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
189 aLineEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
190 aLineStartPoint.setValue(aLineStart[0], aLineStart[1])
191 aLineEndPoint.setValue(aLineEnd[0], aLineEnd[1])
192 aSession.finishOperation()
193 # create new arc
194 aSession.startOperation()
195 anArc = aSketchFeature.addFeature("SketchMacroArc")
196 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
197 aCenterRef = anArc.refattr("center_point_ref")
198 assert (not aCenterRef.isInitialized())
199 aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
200 aStartRef = anArc.refattr("start_point_ref")
201 assert (not aStartRef.isInitialized())
202 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
203 aEndRef = anArc.refattr("end_point_ref")
204 assert (not aEndRef.isInitialized())
205 anArcType = anArc.string("arc_type")
206 assert (not anArcType.isInitialized())
207 # initialize attributes
208 anArcType.setValue("by_center_and_points")
209 aCenterRef.setAttr(aPrevArcStart)
210 aCenter.setValue(aPrevArcStart.pnt())
211 aStartRef.setObject(aPoint.lastResult())
212 aStart.setValue(aPointCoord.pnt())
213 aEndRef.setAttr(aPrevArcEnd)
214 aEnd.setValue(aPrevArcEnd.pnt())
215 aSession.finishOperation()
216 # check the MacroArc is not valid (selection of end point is not possible)
217 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
218 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
219 # change end point reference to a line
220 aSession.startOperation()
221 aEndRef.setObject(aLine.lastResult())
222 aEnd.setValue(aLineStart[0], aLineStart[1])
223 aSession.finishOperation()
224 assert (aSketchFeature.numberOfSubs() == 8), "Number of subs {}".format(aSketchFeature.numberOfSubs())
225 model.assertPoint(aPointCoord, aPointCoordinates)
226 verifyLastArc(aSketchFeature, [aPrevArcStart.x(), aPrevArcStart.y()], aPointCoordinates, [])
227 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
228
229 #=========================================================================
230 # Test 4. Create an arc by center and points coincident to other features
231 #=========================================================================
232 # get previous arc
233 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
234 aPrevArcCenter = geomDataAPI_Point2D(aPrevArc.attribute("center_point"))
235 aPrevArcStart = geomDataAPI_Point2D(aPrevArc.attribute("start_point"))
236 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
237 aPrevArcCenterXY = [aPrevArcCenter.x(), aPrevArcCenter.y()]
238 aPrevArcStartXY = [aPrevArcStart.x(), aPrevArcStart.y()]
239 aPrevArcEndXY = [aPrevArcEnd.x(), aPrevArcEnd.y()]
240 # create new arc
241 aSession.startOperation()
242 anArc = aSketchFeature.addFeature("SketchMacroArc")
243 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
244 aCenterRef = anArc.refattr("center_point_ref")
245 aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
246 aStartRef = anArc.refattr("start_point_ref")
247 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
248 aEndRef = anArc.refattr("end_point_ref")
249 anArcType = anArc.string("arc_type")
250 # initialize attributes
251 anArcType.setValue("by_center_and_points")
252 aCenterRef.setObject(aLine.lastResult())
253 aCenter.setValue(aLineStartPoint.pnt())
254 aStartRef.setObject(aPrevArc.lastResult())
255 aStart.setValue(aPrevArcStart.pnt())
256 aEndRef.setObject(aLine.lastResult())
257 aEnd.setValue(aLineEndPoint.pnt())
258 aSession.finishOperation()
259 assert (aSketchFeature.numberOfSubs() == 12), "Number of subs {}".format(aSketchFeature.numberOfSubs())
260 # check connected features do not change their positions
261 model.assertArc(aPrevArc, aPrevArcCenterXY, aPrevArcStartXY, aPrevArcEndXY)
262 model.assertLine(aLine, aLineStart, aLineEnd)
263 # verify newly created arc
264 anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
265 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
266 aStart = geomDataAPI_Point2D(anArc.attribute("start_point"))
267 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point"))
268 verifyPointOnLine(aCenter, aLine)
269 verifyPointOnLine(aEnd, aLine)
270 verifyPointOnCircle(aStart, aPrevArc)
271 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 6)
272 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
273
274 #=========================================================================
275 # Test 5. Create another arc by center and points coincident to other features
276 #=========================================================================
277 # create new arc
278 aSession.startOperation()
279 anArc = aSketchFeature.addFeature("SketchMacroArc")
280 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
281 aCenterRef = anArc.refattr("center_point_ref")
282 aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
283 aStartRef = anArc.refattr("start_point_ref")
284 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
285 aEndRef = anArc.refattr("end_point_ref")
286 anArcType = anArc.string("arc_type")
287 # initialize attributes
288 anArcType.setValue("by_center_and_points")
289 aCenterRef.setObject(aLine.lastResult())
290 aCenter.setValue(aLineStartPoint.pnt())
291 aStartRef.setObject(aLine.lastResult())
292 aStart.setValue(aLineEndPoint.pnt())
293 aEndRef.setObject(aPrevArc.lastResult())
294 aEnd.setValue(aPrevArcStart.pnt())
295 aSession.finishOperation()
296 assert (aSketchFeature.numberOfSubs() == 16), "Number of subs {}".format(aSketchFeature.numberOfSubs())
297 # check connected features do not change their positions
298 model.assertArc(aPrevArc, aPrevArcCenterXY, aPrevArcStartXY, aPrevArcEndXY)
299 model.assertLine(aLine, aLineStart, aLineEnd)
300 # verify newly created arc
301 anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
302 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
303 aStart = geomDataAPI_Point2D(anArc.attribute("start_point"))
304 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point"))
305 verifyPointOnLine(aCenter, aLine)
306 verifyPointOnLine(aStart, aLine)
307 verifyPointOnCircle(aEnd, aPrevArc)
308 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 9)
309 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
310
311 #=========================================================================
312 # Test 6. Create one more arc by center and points coincident to other features
313 #=========================================================================
314 # get previous arc
315 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
316 aPrevArcCenter = geomDataAPI_Point2D(aPrevArc.attribute("center_point"))
317 aPrevArcStart = geomDataAPI_Point2D(aPrevArc.attribute("start_point"))
318 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
319 aPrevArcCenterXY = [aPrevArcCenter.x(), aPrevArcCenter.y()]
320 aPrevArcStartXY = [aPrevArcStart.x(), aPrevArcStart.y()]
321 aPrevArcEndXY = [aPrevArcEnd.x(), aPrevArcEnd.y()]
322 # create new arc
323 aSession.startOperation()
324 anArc = aSketchFeature.addFeature("SketchMacroArc")
325 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
326 aCenterRef = anArc.refattr("center_point_ref")
327 aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
328 aStartRef = anArc.refattr("start_point_ref")
329 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
330 aEndRef = anArc.refattr("end_point_ref")
331 anArcType = anArc.string("arc_type")
332 # initialize attributes
333 anArcType.setValue("by_center_and_points")
334 delta = [aPrevArcEndXY[0] - aPrevArcCenterXY[0], aPrevArcEndXY[1] - aPrevArcCenterXY[1]]
335 aCenter.setValue(aPrevArcCenterXY[0] + 2 * delta[0], aPrevArcCenterXY[1] + 2 * delta[1])
336 aStart.setValue(aPrevArcCenterXY[0] + 2 * delta[0] - 0.9 * delta[1], aPrevArcCenterXY[1] + 2 * delta[1] + 0.9 * delta[0])
337 aEndRef.setObject(aPrevArc.lastResult())
338 aEnd.setValue(aPrevArcCenterXY[0] + 1.1 * delta[0], aPrevArcCenterXY[1] + 1.1 * delta[1])
339 aSession.finishOperation()
340 assert (aSketchFeature.numberOfSubs() == 18), "Number of subs {}".format(aSketchFeature.numberOfSubs())
341 # verify newly created arc
342 anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
343 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
344 aStart = geomDataAPI_Point2D(anArc.attribute("start_point"))
345 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point"))
346 verifyPointOnCircle(aEnd, aPrevArc)
347 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 10)
348 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
349
350 #=========================================================================
351 # Test 7. Create one more arc by center and points coincident to other features
352 #=========================================================================
353 aPrevArcCenterXY = [aPrevArcCenter.x(), aPrevArcCenter.y()]
354 aPrevArcStartXY = [aPrevArcStart.x(), aPrevArcStart.y()]
355 aPrevArcEndXY = [aPrevArcEnd.x(), aPrevArcEnd.y()]
356 # create new arc
357 aSession.startOperation()
358 anArc = aSketchFeature.addFeature("SketchMacroArc")
359 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
360 aCenterRef = anArc.refattr("center_point_ref")
361 aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
362 aStartRef = anArc.refattr("start_point_ref")
363 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
364 aEndRef = anArc.refattr("end_point_ref")
365 anArcType = anArc.string("arc_type")
366 # initialize attributes
367 anArcType.setValue("by_center_and_points")
368 delta = [aPrevArcEndXY[0] - aPrevArcCenterXY[0], aPrevArcEndXY[1] - aPrevArcCenterXY[1]]
369 aCenter.setValue(aPrevArcCenterXY[0] + 2 * delta[0], aPrevArcCenterXY[1] + 2 * delta[1])
370 aStart.setValue(aPrevArcCenterXY[0] + 2 * delta[0] - delta[1], aPrevArcCenterXY[1] + 2 * delta[1] + delta[0])
371 aEndRef.setObject(aPrevArc.lastResult())
372 aEnd.setValue(aPrevArcCenterXY[0] + delta[0], aPrevArcCenterXY[1] + delta[1])
373 aSession.finishOperation()
374 assert (aSketchFeature.numberOfSubs() == 20), "Number of subs {}".format(aSketchFeature.numberOfSubs())
375 # verify newly created arc
376 anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
377 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
378 aStart = geomDataAPI_Point2D(anArc.attribute("start_point"))
379 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point"))
380 verifyPointOnCircle(aEnd, aPrevArc)
381 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 11)
382 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
383
384 #=========================================================================
385 # End of test
386 #=========================================================================
387
388 assert(model.checkPythonDump())