Salome HOME
720255968580df63b6e50d337e6968a0b8437dfd
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistanceBehavior.py
1 # Copyright (C) 2017-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 from salome.shaper import model
21 from SketchAPI import *
22 import math
23
24 TOLERANCE = 1.e-7
25
26 model.begin()
27 partSet = model.moduleDocument()
28 Part_1 = model.addPart(partSet)
29 Part_1_doc = Part_1.document()
30 DistanceParam = model.addParameter(Part_1_doc, "distance", "10.")
31 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
32 SketchRectangle_1 = Sketch_1.addRectangle(20., 20., 70., 50.)
33 [SketchLine_1, SketchLine_2, SketchLine_3, SketchLine_4] = SketchRectangle_1.lines()
34 Sketch_1.setVertical(SketchLine_4.result())
35 firstPoint = SketchAPI_Line(SketchLine_2).startPoint()
36 secondPoint = SketchAPI_Line(SketchLine_3).endPoint()
37 model.do()
38
39 # =============================================================================
40 # Test 1.
41 # =============================================================================
42 # horizontal distance constraint
43 SketchConstraintDistanceHorizontal_1 = Sketch_1.setHorizontalDistance(firstPoint, secondPoint, 10)
44 SketchConstraintDistanceHorizontal_1.feature().real("ConstraintValue").setText(DistanceParam.name().value())
45 model.do()
46
47 # changing the parameter
48 for param in range(11, 31, 2):
49     if param == 0:
50         continue
51     DistanceParam.setValue(param)
52     model.do()
53     dist = secondPoint.x() - firstPoint.x()
54     assert math.fabs(dist - param) < TOLERANCE, "Incorrect horizontal distance {}, expected {}".format(dist, param)
55
56 model.testNbSubFeatures(Sketch_1, "SketchLine", 4)
57 model.testNbSubFeatures(Sketch_1, "SketchConstraintDistanceHorizontal", 1)
58
59 # remove horizontal distance constraint
60 Part_1_doc.removeFeature(SketchConstraintDistanceHorizontal_1.feature())
61 model.do()
62
63 # =============================================================================
64 # Test 2.
65 # =============================================================================
66 # Vertical distance constraint
67 SketchConstraintDistanceVertical_1 = Sketch_1.setVerticalDistance(firstPoint, secondPoint, 10)
68 SketchConstraintDistanceVertical_1.feature().real("ConstraintValue").setText(DistanceParam.name().value())
69 model.do()
70
71 # changing the parameter
72 for param in range(11, 31, 2):
73     if param == 0:
74         continue
75     DistanceParam.setValue(param)
76     model.do()
77     dist = secondPoint.y() - firstPoint.y()
78     assert math.fabs(dist - param) < TOLERANCE, "Incorrect vertical distance {}, expected {}".format(dist, param)
79
80 model.testNbSubFeatures(Sketch_1, "SketchLine", 4)
81 model.testNbSubFeatures(Sketch_1, "SketchConstraintDistanceVertical", 1)
82
83 # remove verticel distance constraint
84 Part_1_doc.removeFeature(SketchConstraintDistanceVertical_1.feature())
85 model.do()
86
87 # =============================================================================
88 # Test 3.
89 # =============================================================================
90 # distance constraint
91 SketchConstraintDistance_1 = Sketch_1.setDistance(firstPoint, secondPoint, "distance")
92 model.do()
93
94 # changing the parameter
95 for param in range(11, 31, 2):
96     DistanceParam.setValue(param)
97     model.do()
98     if param < 0:
99         assert SketchConstraintDistance_1.feature().error() != '', "ERROR: Sketch should not be valid due to negative distance value"
100     elif param == 0: # constraint is valid, but lead to degenerated geometry
101         assert Sketch_1.feature().error() != '', "ERROR: Sketch should not be valid due to negative distance value"
102     else:
103         dist = model.distancePointPoint(firstPoint, secondPoint)
104         assert math.fabs(dist - math.fabs(param)) < TOLERANCE, "Incorrect distance {}, expected {}".format(dist, math.fabs(param))
105
106 model.testNbSubFeatures(Sketch_1, "SketchLine", 4)
107 model.testNbSubFeatures(Sketch_1, "SketchConstraintDistance", 1)
108 # leave distance constraint alive
109
110 model.end()
111