X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketchPlugin%2FTest%2FTestConstraintDistance.py;h=3b0181b13ac608aaa6a7ac2d2fb954b493dee569;hb=06e7f5859095193fc7f498bd89a7d28009794f53;hp=e9703fa63e5911217ed65ce9cccf409095a14740;hpb=d73b65e0b6e8c38fc7fdcbd302e69b51db4082d8;p=modules%2Fshaper.git diff --git a/src/SketchPlugin/Test/TestConstraintDistance.py b/src/SketchPlugin/Test/TestConstraintDistance.py index e9703fa63..3b0181b13 100644 --- a/src/SketchPlugin/Test/TestConstraintDistance.py +++ b/src/SketchPlugin/Test/TestConstraintDistance.py @@ -1,7 +1,26 @@ +# Copyright (C) 2014-2023 CEA, EDF +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + """ TestConstraintDistance.py Unit test of SketchPlugin_ConstraintDistance class - + SketchPlugin_Constraint static const std::string MY_CONSTRAINT_VALUE("ConstraintValue"); static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt"); @@ -9,19 +28,21 @@ static const std::string MY_ENTITY_B("ConstraintEntityB"); static const std::string MY_ENTITY_C("ConstraintEntityC"); static const std::string MY_ENTITY_D("ConstraintEntityD"); - + SketchPlugin_ConstraintDistance static const std::string MY_CONSTRAINT_DISTANCE_ID("SketchConstraintDistance"); data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId()); data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId()); data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId()); data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId()); - - + + """ from GeomDataAPI import * from ModelAPI import * import math +from salome.shaper import model + #========================================================================= # Initialization of the test #========================================================================= @@ -29,32 +50,6 @@ import math __updated__ = "2014-10-28" -def distancePointPoint(pointA, pointB): - """ - subroutine to calculate distance between two points - result of calculated distance is has 10**-5 precision - """ - xdiff = math.pow((pointA.x() - pointB.x()), 2) - ydiff = math.pow((pointA.y() - pointB.y()), 2) - return round(math.sqrt(xdiff + ydiff), 5) - -def distancePointLine(point, line): - """ - subroutine to calculate distance between point and line - result of calculated distance is has 10**-5 precision - """ - aStartPoint = geomDataAPI_Point2D(line.attribute("StartPoint")) - aEndPoint = geomDataAPI_Point2D(line.attribute("EndPoint")) - # orthogonal direction - aDirX = -(aEndPoint.y() - aStartPoint.y()) - aDirY = (aEndPoint.x() - aStartPoint.x()) - aLen = math.sqrt(aDirX**2 + aDirY**2) - aDirX = aDirX / aLen - aDirY = aDirY / aLen - aVecX = point.x() - aStartPoint.x() - aVecY = point.y() - aStartPoint.y() - return round(math.fabs(aVecX * aDirX + aVecY * aDirY), 5) - aSession = ModelAPI_Session.get() aDocument = aSession.moduleDocument() #========================================================================= @@ -76,7 +71,7 @@ aSession.finishOperation() aSession.startOperation() aSketchPoint = aSketchFeature.addFeature("SketchPoint") aSketchPointCoords = geomDataAPI_Point2D( - aSketchPoint.attribute("PointCoordindates")) + aSketchPoint.attribute("PointCoordinates")) aSketchPointCoords.setValue(50., 50.) aSketchLine = aSketchFeature.addFeature("SketchLine") aLineAStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint")) @@ -84,11 +79,12 @@ aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint")) aLineAStartPoint.setValue(0., 25.) aLineAEndPoint.setValue(100., 25.) aSession.finishOperation() +assert (model.dof(aSketchFeature) == 6) #========================================================================= # Make a constraint to keep the distance #========================================================================= PT_PT_DIST = 25. -aDist = distancePointPoint(aSketchPointCoords, aLineAStartPoint); +aDist = model.distancePointPoint(aSketchPointCoords, aLineAStartPoint); assert (aDist != PT_PT_DIST) aSession.startOperation() aConstraint = aSketchFeature.addFeature("SketchConstraintDistance") @@ -105,17 +101,25 @@ aConstraint.execute() refattrA.setAttr(aSketchPointCoords) refattrB.setAttr(aLineAStartPoint) aSession.finishOperation() +assert (model.dof(aSketchFeature) == 5) +# set flyout point then abort operation, after that check the Distance is correct +aSession.startOperation() +aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt")) +aFlyoutPoint.setValue(50.0, 100.0) +aSession.abortOperation() assert (refattrA.isInitialized()) assert (refattrB.isInitialized()) assert (aDistance.isInitialized()) assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist) +assert (model.dof(aSketchFeature) == 5) #========================================================================= # Change distance value #========================================================================= aSession.startOperation() aDistance.setValue(PT_PT_DIST) aSession.finishOperation() -assert (distancePointPoint(aSketchPointCoords, aLineAStartPoint) == PT_PT_DIST) +assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10) +assert (model.dof(aSketchFeature) == 5) #========================================================================= # Move line, check that distance is constant #========================================================================= @@ -123,7 +127,8 @@ aSession.startOperation() aLineAStartPoint.setValue(0., 40.) aLineAEndPoint.setValue(100., 40.) aSession.finishOperation() -assert (distancePointPoint(aSketchPointCoords, aLineAStartPoint) == PT_PT_DIST) +assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10) +assert (model.dof(aSketchFeature) == 5) #========================================================================= # Remove constraint, check the points are unconstrained now #========================================================================= @@ -133,13 +138,14 @@ aSession.finishOperation() aSession.startOperation() aSketchPointCoords.setValue(0., 0.) aSession.finishOperation() -assert (distancePointPoint(aSketchPointCoords, aLineAStartPoint) != PT_PT_DIST) +assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) > 1.e-10) +assert (model.dof(aSketchFeature) == 6) #========================================================================= # Add distance between point and line #========================================================================= PT_LINE_DIST = 50. -aDist = distancePointLine(aSketchPointCoords, aSketchLine) +aDist = model.distancePointLine(aSketchPointCoords, aSketchLine) aSession.startOperation() aConstraint = aSketchFeature.addFeature("SketchConstraintDistance") aDistance = aConstraint.real("ConstraintValue") @@ -153,17 +159,55 @@ assert (aLineResult is not None) refattrA.setObject(aLineResult) refattrB.setAttr(aSketchPointCoords) aSession.finishOperation() +assert (model.dof(aSketchFeature) == 5) +# set flyout point then abort operation, after that check the Distance is correct +aSession.startOperation() +aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt")) +aFlyoutPoint.setValue(50.0, 100.0) +aSession.abortOperation() +assert (refattrA.isInitialized()) +assert (refattrB.isInitialized()) +assert (aDistance.isInitialized()) +assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist) +assert (model.dof(aSketchFeature) == 5) + +#========================================================================= +# Swap point and line in the distance +#========================================================================= +aSession.startOperation() +refattrA.setAttr(aSketchPointCoords) +refattrB.setObject(aLineResult) +aSession.finishOperation() +assert (model.dof(aSketchFeature) == 5) +# set flyout point then abort operation, after that check the Distance is correct +aSession.startOperation() +aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt")) +aFlyoutPoint.setValue(100.0, 100.0) +aSession.abortOperation() assert (refattrA.isInitialized()) assert (refattrB.isInitialized()) assert (aDistance.isInitialized()) assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist) +assert (model.dof(aSketchFeature) == 5) #========================================================================= # Change distance value #========================================================================= aSession.startOperation() aDistance.setValue(PT_LINE_DIST) aSession.finishOperation() -assert (distancePointLine(aSketchPointCoords, aSketchLine) == PT_LINE_DIST) +assert (math.fabs(model.distancePointLine(aSketchPointCoords, aSketchLine) - PT_LINE_DIST) < 1.e-10) +assert (model.dof(aSketchFeature) == 5) +#========================================================================= +# Set distance between line boundaries +#========================================================================= +aSession.startOperation() +refattrA.setAttr(aLineAStartPoint) +refattrB.setAttr(aLineAEndPoint) +aSession.finishOperation() +assert (math.fabs(model.distancePointPoint(aLineAStartPoint, aLineAEndPoint) - PT_LINE_DIST) < 1.e-10) +assert (model.dof(aSketchFeature) == 5) #========================================================================= # End of test #========================================================================= + +assert(model.checkPythonDump())