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