Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistanceBehavior.py
1 ## Copyright (C) 2017  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
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 from salome.shaper import model
22 from SketchAPI import *
23 import math
24
25 TOLERANCE = 1.e-7
26
27 model.begin()
28 partSet = model.moduleDocument()
29 Part_1 = model.addPart(partSet)
30 Part_1_doc = Part_1.document()
31 DistanceParam = model.addParameter(Part_1_doc, "distance", "10.")
32 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
33 SketchRectangle_1 = Sketch_1.addRectangle(20., 20., 70., 50.)
34 [SketchLine_1, SketchLine_2, SketchLine_3, SketchLine_4] = SketchRectangle_1.lines()
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, "distance")
44 model.do()
45
46 # changing the parameter
47 for param in range(-30, 31, 2):
48     DistanceParam.setValue(param)
49     model.do()
50     dist = secondPoint.x() - firstPoint.x()
51     assert math.fabs(dist - param) < TOLERANCE, "Incorrect horizontal distance {}, expected {}".format(dist, param)
52
53 model.testNbSubFeatures(Sketch_1, "SketchLine", 4)
54 model.testNbSubFeatures(Sketch_1, "SketchConstraintDistanceHorizontal", 1)
55
56 # remove horizontal distance constraint
57 Part_1_doc.removeFeature(SketchConstraintDistanceHorizontal_1.feature())
58 model.do()
59
60 # =============================================================================
61 # Test 2.
62 # =============================================================================
63 # Vertical distance constraint
64 SketchConstraintDistanceVertical_1 = Sketch_1.setVerticalDistance(firstPoint, secondPoint, "distance")
65 model.do()
66
67 # changing the parameter
68 for param in range(-30, 31, 2):
69     DistanceParam.setValue(param)
70     model.do()
71     dist = secondPoint.y() - firstPoint.y()
72     assert math.fabs(dist - param) < TOLERANCE, "Incorrect vertical distance {}, expected {}".format(dist, param)
73
74 model.testNbSubFeatures(Sketch_1, "SketchLine", 4)
75 model.testNbSubFeatures(Sketch_1, "SketchConstraintDistanceVertical", 1)
76
77 # remove verticel distance constraint
78 Part_1_doc.removeFeature(SketchConstraintDistanceVertical_1.feature())
79 model.do()
80
81 # =============================================================================
82 # Test 3.
83 # =============================================================================
84 # distance constraint
85 SketchConstraintDistance_1 = Sketch_1.setDistance(firstPoint, secondPoint, "distance")
86 model.do()
87
88 # changing the parameter
89 for param in range(-30, 31, 2):
90     DistanceParam.setValue(param)
91     model.do()
92     if param <= 0:
93         assert SketchConstraintDistance_1.feature().error() != '', "ERROR: Sketch should not be valid due to negative distance value"
94     else:
95         dist = model.distancePointPoint(firstPoint, secondPoint)
96         assert math.fabs(dist - math.fabs(param)) < TOLERANCE, "Incorrect distance {}, expected {}".format(dist, math.fabs(param))
97
98 model.testNbSubFeatures(Sketch_1, "SketchLine", 4)
99 model.testNbSubFeatures(Sketch_1, "SketchConstraintDistance", 1)
100 # leave distance constraint alive
101
102 model.end()