Salome HOME
[bos #42437] fixed problem where sketch validation icons in toolbar were not activated
[modules/shaper.git] / sketcher / tests.py
1 # Copyright (C) 2017-2019  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 from ModelAPI import *
21 from GeomDataAPI import *
22 import ModelHighAPI
23 import math
24 from salome.shaper.model.sketcher import tools
25
26 TOLERANCE = 1.e-7
27
28 def assertPoint(thePoint, theCoords, theTolerance = TOLERANCE):
29     """ Verifies coordinates of the point
30     """
31     aPoint = tools.toList(thePoint)
32     assert((aPoint[0]-theCoords[0])**2 + (aPoint[1]-theCoords[1])**2 < theTolerance**2), "Wrong '{}' point {}, expected {}".format(thePoint.id(), aPoint, theCoords)
33
34
35 def assertLine(theLine, theStart, theEnd, theTolerance = TOLERANCE):
36     """ Verifies coordinates of line extremities
37     """
38     aLine = tools.toSketchFeature(theLine)
39
40     aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
41     aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
42     if len(theStart):
43         assertPoint(aStartPnt, theStart, theTolerance)
44     if len(theEnd):
45         assertPoint(aEndPnt, theEnd, theTolerance)
46
47
48 def assertCircle(theCircle, theCenter, theRadius, theTolerance = TOLERANCE):
49     """ Verifies attributes of circle
50     """
51     aCircle = tools.toSketchFeature(theCircle)
52
53     aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
54     if len(theCenter):
55         assertPoint(aCenter, theCenter, theTolerance)
56
57     aRadius = aCircle.real("circle_radius")
58     assert aRadius.value() == theRadius, "Wrong circle radius {}, expected {}".format(aRadius.value(), theRadius)
59
60
61 def assertArc(theArc, theCenter, theStart, theEnd, theTolerance = TOLERANCE):
62     """ Verifies coordinates of arc points and the consistency of the arc.
63         Some of points may be empty lists.
64     """
65     anArc = tools.toSketchFeature(theArc)
66
67     aCenterPnt = geomDataAPI_Point2D(anArc.attribute("center_point"))
68     aStartPnt = geomDataAPI_Point2D(anArc.attribute("start_point"))
69     aEndPnt = geomDataAPI_Point2D(anArc.attribute("end_point"))
70     if len(theCenter):
71         assertPoint(aCenterPnt, theCenter, theTolerance)
72     if len(theStart):
73         assertPoint(aStartPnt, theStart, theTolerance)
74     if len(theEnd):
75         assertPoint(aEndPnt, theEnd, theTolerance)
76
77     assertArcValidity(anArc)
78
79
80 def assertArcValidity(theArc):
81     """ Tests whether the arc is correctly defined
82     """
83     anArc = tools.toSketchFeature(theArc)
84
85     aCenterPnt = geomDataAPI_Point2D(anArc.attribute("center_point"))
86     aStartPnt = geomDataAPI_Point2D(anArc.attribute("start_point"))
87     aEndPnt = geomDataAPI_Point2D(anArc.attribute("end_point"))
88     aRadius = anArc.real("radius")
89     aDistCS = tools.distancePointPoint(aCenterPnt, aStartPnt)
90     aDistCE = tools.distancePointPoint(aCenterPnt, aEndPnt)
91     assert math.fabs(aDistCS - aDistCE) < TOLERANCE, "Wrong arc: center-start distance {}, center-end distance {}".format(aDistCS, aDistCE)
92     assert math.fabs(aRadius.value() - aDistCS) < TOLERANCE, "Wrong arc: radius is {}, expected {}".format(aRadius.value(), aDistCS)
93
94
95 def checkSketch(theSketch, theDOF = -1):
96     """ Tests the sketch is valid and DoF is equal to the given
97     """
98     assert(theSketch.feature().error() == ""), "Sketch failed: {}".format(theSketch.feature().error())
99     assert(theSketch.solverError().value() == ""), "Sketch solver failed: {}".format(theSketch.solverError().value())
100     if theDOF != -1:
101         aDOF = tools.dof(theSketch)
102         assert(aDOF == theDOF), "Sketch DoF {} is wrong. Expected {}".format(aDOF, theDOF)
103
104
105 def checkSketchErrorDegenerated(theSketch):
106     """ Verify the sketch reports error about degenerated geometry
107     """
108     errorValue = theSketch.solverError().value()
109     assert(errorValue != "")
110     assert(errorValue.find("degenerated") >= 0)