Salome HOME
Copyright update 2022
[modules/shaper.git] / src / PythonAPI / model / sketcher / tests.py
1 # Copyright (C) 2017-2022  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)
111
112
113 def compareSketches(theReference, theSketch, TOLERANCE = 1.e-5):
114     """ Compare sketches for the sequence of features
115     """
116     errors = ""
117
118     # compare sketches degree of freedom
119     if tools.dof(theReference) != tools.dof(theSketch):
120         errors += "\nError in DoF (actual = {}, expected = {})".format(tools.dof(theSketch), tools.dof(theReference))
121
122     # compare sketch solver error
123     if theReference.solverError().value() != theSketch.solverError().value():
124         errors += "\nError in solver message (actual = '{}', expected = '{}')".format(theSketch.solverError().value(), theReference.solverError().value())
125
126     aRefSketch = featureToCompositeFeature(theReference.feature())
127     anActualSketch = featureToCompositeFeature(theSketch.feature())
128
129     # compare number of subs
130     aRefNbSubs = aRefSketch.numberOfSubs()
131     anActualNbSubs = anActualSketch.numberOfSubs()
132     if aRefNbSubs != anActualNbSubs:
133         errors += "\nError in number of sub-features (actual = {}, expected = {})".format(anActualNbSubs, aRefNbSubs)
134
135     for index in range(min(aRefNbSubs, anActualNbSubs)):
136         aRefFeature = aRefSketch.subFeature(index)
137         aFeature = anActualSketch.subFeature(index)
138         # compare types of subs
139         if aFeature.getKind() != aRefFeature.getKind():
140             errors += "\nWrong sketch feature (actual = '{}', expected = '{}')".format(aFeature.name(), aRefFeature.name())
141             continue
142         # compare attributes
143         aRefAttrs = aRefFeature.data().attributes("")
144         anAttrs = aFeature.data().attributes("")
145         for ref, attr in zip(aRefAttrs, anAttrs):
146             if ref.attributeType() != attr.attributeType():
147                 errors += "\nWrong sequence of attributes (feature = '{}', reference = '{}')".format(aFeature.name(), aRefFeature.name())
148             elif not attr.isInitialized() or not ref.isInitialized():
149                 if attr.isInitialized() != ref.isInitialized():
150                     errors += "\nAttribute '{}' initialization is different (feature = '{}', reference = '{}'): actual = {}, expected = {}".format(attr.id(), aFeature.name(), aRefFeature.name(), attr.isInitialized(), ref.isInitialized())
151             elif ref.attributeType() == GeomDataAPI_Point2D.typeId():
152                 aRefPoint = geomDataAPI_Point2D(ref)
153                 aPoint = geomDataAPI_Point2D(attr)
154                 if tools.distancePointPoint(aPoint, aRefPoint) > TOLERANCE:
155                     errors += "\nWrong coordinates '{}' (feature = '{}', reference = '{}'): actual = ({}, {}), expected = ({}, {})".format(attr.id(), aFeature.name(), aRefFeature.name(), aPoint.x(), aPoint.y(), aRefPoint.x(), aRefPoint.y())
156             elif ref.attributeType() == GeomDataAPI_Point2DArray.typeId():
157                 aRefPoints = geomDataAPI_Point2DArray(ref)
158                 aPoints = geomDataAPI_Point2DArray(attr)
159                 for pInd in range(aRefPoints.size()):
160                     aRefPoint = aRefPoints.pnt(pInd)
161                     aPoint = aPoints.pnt(pInd)
162                     if tools.distancePointPoint(aPoint, aRefPoint) > TOLERANCE:
163                         errors += "\nWrong coordinates '{}', index {} (feature = '{}', reference = '{}'): actual = ({}, {}), expected = ({}, {})".format(attr.id(), pInd, aFeature.name(), aRefFeature.name(), aPoint.x(), aPoint.y(), aRefPoint.x(), aRefPoint.y())
164             elif ref.attributeType() == ModelAPI_AttributeBoolean.typeId():
165                 aRefVal = modelAPI_AttributeBoolean(ref).value()
166                 aVal = modelAPI_AttributeBoolean(attr).value()
167                 if aVal != aRefVal:
168                     errors += "\nWrong boolean value '{}' (feature = '{}', reference = '{}'): actual = {}, expected = {}".format(attr.id(), aFeature.name(), aRefFeature.name(), aVal, aRefVal)
169             elif ref.attributeType() == ModelAPI_AttributeDouble.typeId():
170                 aRefVal = modelAPI_AttributeDouble(ref).value()
171                 aVal = modelAPI_AttributeDouble(attr).value()
172                 if math.fabs(aVal - aRefVal) > TOLERANCE:
173                     errors += "\nWrong real value '{}' (feature = '{}', reference = '{}'): actual = {}, expected = {}".format(attr.id(), aFeature.name(), aRefFeature.name(), aVal, aRefVal)
174             elif ref.attributeType() == ModelAPI_AttributeDoubleArray.typeId():
175                 aRefArray = modelAPI_AttributeDoubleArray(ref)
176                 anArray = modelAPI_AttributeDoubleArray(attr)
177                 for vInd in range(aRefArray.size()):
178                     aRefVal = aRefArray.value(vInd)
179                     aVal = anArray.value(vInd)
180                     if math.fabs(aVal - aRefVal) > TOLERANCE:
181                         errors += "\nWrong real value '{}' index {} (feature = '{}', reference = '{}'): actual = {}, expected = {}".format(attr.id(), vInd, aFeature.name(), aRefFeature.name(), aVal, aRefVal)
182             elif ref.attributeType() == ModelAPI_AttributeInteger.typeId():
183                 aRefVal = modelAPI_AttributeInteger(ref).value()
184                 aVal = modelAPI_AttributeInteger(attr).value()
185                 if aVal != aRefVal:
186                     errors += "\nWrong integer value '{}' (feature = '{}', reference = '{}'): actual = {}, expected = {}".format(attr.id(), aFeature.name(), aRefFeature.name(), aVal, aRefVal)
187             elif ref.attributeType() == ModelAPI_AttributeIntArray.typeId():
188                 aRefArray = modelAPI_AttributeIntArray(ref)
189                 anArray = modelAPI_AttributeIntArray(attr)
190                 for vInd in range(aRefArray.size()):
191                     aRefVal = aRefArray.value(vInd)
192                     aVal = anArray.value(vInd)
193                     if aVal != aRefVal:
194                         errors += "\nWrong integer value '{}' index {} (feature = '{}', reference = '{}'): actual = {}, expected = {}".format(attr.id(), vInd, aFeature.name(), aRefFeature.name(), aVal, aRefVal)
195
196     return errors