1 # Copyright (C) 2017-2022 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 from ModelAPI import *
21 from GeomDataAPI import *
24 from salome.shaper.model.sketcher import tools
28 def assertPoint(thePoint, theCoords, theTolerance = TOLERANCE):
29 """ Verifies coordinates of the point
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)
35 def assertLine(theLine, theStart, theEnd, theTolerance = TOLERANCE):
36 """ Verifies coordinates of line extremities
38 aLine = tools.toSketchFeature(theLine)
40 aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
41 aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
43 assertPoint(aStartPnt, theStart, theTolerance)
45 assertPoint(aEndPnt, theEnd, theTolerance)
48 def assertCircle(theCircle, theCenter, theRadius, theTolerance = TOLERANCE):
49 """ Verifies attributes of circle
51 aCircle = tools.toSketchFeature(theCircle)
53 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
55 assertPoint(aCenter, theCenter, theTolerance)
57 aRadius = aCircle.real("circle_radius")
58 assert aRadius.value() == theRadius, "Wrong circle radius {}, expected {}".format(aRadius.value(), theRadius)
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.
65 anArc = tools.toSketchFeature(theArc)
67 aCenterPnt = geomDataAPI_Point2D(anArc.attribute("center_point"))
68 aStartPnt = geomDataAPI_Point2D(anArc.attribute("start_point"))
69 aEndPnt = geomDataAPI_Point2D(anArc.attribute("end_point"))
71 assertPoint(aCenterPnt, theCenter, theTolerance)
73 assertPoint(aStartPnt, theStart, theTolerance)
75 assertPoint(aEndPnt, theEnd, theTolerance)
77 assertArcValidity(anArc)
80 def assertArcValidity(theArc):
81 """ Tests whether the arc is correctly defined
83 anArc = tools.toSketchFeature(theArc)
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)
95 def checkSketch(theSketch, theDOF = -1):
96 """ Tests the sketch is valid and DoF is equal to the given
98 assert(theSketch.feature().error() == ""), "Sketch failed: {}".format(theSketch.feature().error())
99 assert(theSketch.solverError().value() == ""), "Sketch solver failed: {}".format(theSketch.solverError().value())
101 aDOF = tools.dof(theSketch)
102 assert(aDOF == theDOF), "Sketch DoF {} is wrong. Expected {}".format(aDOF, theDOF)
105 def checkSketchErrorDegenerated(theSketch):
106 """ Verify the sketch reports error about degenerated geometry
108 errorValue = theSketch.solverError().value()
109 assert(errorValue != "")
110 assert(errorValue.find("degenerated") >= 0)
113 def compareSketches(theReference, theSketch, TOLERANCE = 1.e-5):
114 """ Compare sketches for the sequence of features
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))
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())
126 aRefSketch = featureToCompositeFeature(theReference.feature())
127 anActualSketch = featureToCompositeFeature(theSketch.feature())
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)
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())
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()
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()
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)
194 errors += "\nWrong integer value '{}' index {} (feature = '{}', reference = '{}'): actual = {}, expected = {}".format(attr.id(), vInd, aFeature.name(), aRefFeature.name(), aVal, aRefVal)