Salome HOME
Simplification and refactoring of unit tests for SketchPlugin
[modules/shaper.git] / src / PythonAPI / model / sketcher / tests.py
1 # Author: Artem Zhidkov
2 # Copyright (C) 2017-20xx CEA/DEN, EDF R&D
3
4 from ModelAPI import *
5 from GeomDataAPI import *
6 import ModelHighAPI
7 import math
8 from salome.shaper.model.sketcher import tools
9
10 TOLERANCE = 1.e-7
11
12 def assertPoint(thePoint, theCoords):
13     """ Verifies coordinates of the point
14     """
15     aPoint = tools.toPoint(thePoint)
16     assert aPoint.x() == theCoords[0] and aPoint.y() == theCoords[1], "Wrong '{}' point ({}, {}), expected ({}, {})".format(thePoint.id(), aPoint.x(), aPoint.y(), theCoords[0], theCoords[1])
17
18
19 def assertLine(theLine, theStart, theEnd):
20     """ Verifies coordinates of line extremities
21     """
22     aLine = tools.toSketchFeature(theLine)
23
24     aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
25     aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
26     if len(theStart):
27         assertPoint(aStartPnt, theStart)
28     if len(theEnd):
29         assertPoint(aEndPnt, theEnd)
30
31
32 def assertCircle(theCircle, theCenter, theRadius):
33     """ Verifies attributes of circle
34     """
35     aCircle = tools.toSketchFeature(theCircle)
36
37     aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
38     if len(theCenter):
39         assertPoint(aCenter, theCenter)
40
41     aRadius = aCircle.real("circle_radius")
42     assert aRadius.value() == theRadius, "Wrong circle radius {}, expected {}".format(aRadius.value(), theRadius)
43
44
45 def assertArc(theArc, theCenter, theStart, theEnd):
46     """ Verifies coordinates of arc points and the consistency of the arc.
47         Some of points may be empty lists.
48     """
49     anArc = tools.toSketchFeature(theArc)
50
51     aCenterPnt = geomDataAPI_Point2D(anArc.attribute("center_point"))
52     aStartPnt = geomDataAPI_Point2D(anArc.attribute("start_point"))
53     aEndPnt = geomDataAPI_Point2D(anArc.attribute("end_point"))
54     if len(theCenter):
55         assertPoint(aCenterPnt, theCenter)
56     if len(theStart):
57         assertPoint(aStartPnt, theStart)
58     if len(theEnd):
59         assertPoint(aEndPnt, theEnd)
60
61     assertArcValidity(anArc)
62
63
64 def assertArcValidity(theArc):
65     """ Tests whether the arc is correctly defined
66     """
67     anArc = tools.toSketchFeature(theArc)
68
69     aCenterPnt = geomDataAPI_Point2D(anArc.attribute("center_point"))
70     aStartPnt = geomDataAPI_Point2D(anArc.attribute("start_point"))
71     aEndPnt = geomDataAPI_Point2D(anArc.attribute("end_point"))
72     aRadius = anArc.real("radius")
73     aDistCS = tools.distancePointPoint(aCenterPnt, aStartPnt)
74     aDistCE = tools.distancePointPoint(aCenterPnt, aEndPnt)
75     assert math.fabs(aDistCS - aDistCE) < TOLERANCE, "Wrong arc: center-start distance {}, center-end distance {}".format(aDistCS, aDistCE)
76     assert math.fabs(aRadius.value() - aDistCS) < TOLERANCE, "Wrong arc: radius is {0}, expected {1}".format(aRadius.value(), aDistCS)