From 5d42212e136ea60b2418bf6c8cfd56388c54f539 Mon Sep 17 00:00:00 2001 From: sbh Date: Mon, 28 Jul 2014 13:16:17 +0400 Subject: [PATCH] Test for Length and Concidence constraints added --- src/SketchPlugin/CMakeLists.txt | 4 +- .../Test/TestConstraintConcidence.py | 108 ++++++++++++++++++ src/SketchPlugin/Test/TestConstraintLength.py | 92 +++++++++++++++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 src/SketchPlugin/Test/TestConstraintConcidence.py create mode 100644 src/SketchPlugin/Test/TestConstraintLength.py diff --git a/src/SketchPlugin/CMakeLists.txt b/src/SketchPlugin/CMakeLists.txt index 990495c08..b6d8467a2 100644 --- a/src/SketchPlugin/CMakeLists.txt +++ b/src/SketchPlugin/CMakeLists.txt @@ -62,4 +62,6 @@ INSTALL(TARGETS SketchPlugin DESTINATION plugins) INSTALL(FILES ${XML_RESOURCES} DESTINATION plugins) ADD_UNIT_TESTS(TestSketchPointLine.py - TestSketchArcCircle.py) + TestSketchArcCircle.py + TestConstraintConcidence.py + TestConstraintLength.py) diff --git a/src/SketchPlugin/Test/TestConstraintConcidence.py b/src/SketchPlugin/Test/TestConstraintConcidence.py new file mode 100644 index 000000000..d14cb21e7 --- /dev/null +++ b/src/SketchPlugin/Test/TestConstraintConcidence.py @@ -0,0 +1,108 @@ +""" + TestConstraintCoincidence.py + Unit test of SketchPlugin_ConstraintCoincidence class + + SketchPlugin_Constraint + static const std::string MY_CONSTRAINT_VALUE("ConstraintValue"); + static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt"); + static const std::string MY_ENTITY_A("ConstraintEntityA"); + 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_ConstraintCoincidence + static const std::string MY_CONSTRAINT_COINCIDENCE_ID("SketchConstraintCoincidence"); + data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type()); + data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type()); + +""" +from GeomDataAPI import * +from ModelAPI import * +#========================================================================= +# Initialization of the test +#========================================================================= + +__updated__ = "2014-07-28" + +aPluginManager = ModelAPI_PluginManager.get() +aDocument = aPluginManager.rootDocument() +#========================================================================= +# Creation of a sketch +#========================================================================= +aDocument.startOperation() +aSketchFeature = aDocument.addFeature("Sketch") +aSketchFeatureData = aSketchFeature.data() +origin = geomDataAPI_Point(aSketchFeatureData.attribute("Origin")) +origin.setValue(0, 0, 0) +dirx = geomDataAPI_Dir(aSketchFeatureData.attribute("DirX")) +dirx.setValue(1, 0, 0) +diry = geomDataAPI_Dir(aSketchFeatureData.attribute("DirY")) +diry.setValue(0, 1, 0) +norm = geomDataAPI_Dir(aSketchFeatureData.attribute("Norm")) +norm.setValue(0, 0, 1) +aDocument.finishOperation() +#========================================================================= +# Create a line and an arc +#========================================================================= +aDocument.startOperation() +aSketchReflist = aSketchFeatureData.reflist("Features") +aSketchArc = aDocument.addFeature("SketchArc") +aSketchReflist.append(aSketchArc) +aSketchArcData = aSketchArc.data() +anArcCentr = geomDataAPI_Point2D(aSketchArcData.attribute("ArcCenter")) +anArcStartPoint = geomDataAPI_Point2D( + aSketchArcData.attribute("ArcStartPoint")) +anArcEndPoint = geomDataAPI_Point2D(aSketchArcData.attribute("ArcEndPoint")) +aSketchFeature = aDocument.addFeature("SketchConstraintCoincidence") +anArcCentr.setValue(10., 10.) +anArcStartPoint.setValue(0., 50.) +anArcEndPoint.setValue(50., 0.) +aSketchLine = aDocument.addFeature("SketchLine") +aSketchReflist.append(aSketchLine) +aSketchLineData = aSketchLine.data() +aLineStartPoint = geomDataAPI_Point2D(aSketchLineData.attribute("StartPoint")) +aLineEndPoint = geomDataAPI_Point2D(aSketchLineData.attribute("EndPoint")) +# Lets initialize line start at circle's end: +aLineStartPoint.setValue(50., 0.) +aLineEndPoint.setValue(100., 25.) +aDocument.finishOperation() +#========================================================================= +# Link arc's end and line's start points with concidence constraint +#========================================================================= +aDocument.startOperation() +aCoincidenceConstraint = aDocument.addFeature("SketchConstraintCoincidence") +aConstraintData = aCoincidenceConstraint.data() +reflistA = aConstraintData.refattr("ConstraintEntityA") +reflistB = aConstraintData.refattr("ConstraintEntityB") +reflistA.setAttr(anArcEndPoint) +reflistB.setAttr(aLineStartPoint) +aDocument.finishOperation() +#========================================================================= +# Check values and move one constrainted object +#========================================================================= +assert (anArcEndPoint.x() == 50) +assert (anArcEndPoint.y() == 0) +assert (aLineStartPoint.x() == 50) +assert (aLineStartPoint.y() == 0) +deltaX = deltaY = 40. +# move line +aDocument.startOperation() +aLineStartPoint.setValue(aLineStartPoint.x() + deltaX, + aLineStartPoint.y() + deltaY) +aLineEndPoint.setValue(aLineEndPoint.x() + deltaX, + aLineEndPoint.y() + deltaY) +aDocument.finishOperation() +# check that arc's points are moved also +print ("assert anArcEndPoint.x() == %d" % anArcEndPoint.x()) +print ("assert anArcEndPoint.y() == %d" % anArcEndPoint.y()) +print ("assert aLineStartPoint.x() == %d" % aLineStartPoint.x()) +print ("assert aLineStartPoint.y() == %d" % aLineStartPoint.y()) +#========================================================================= +# TODO: improve test +# 1. remove constraint, move line to check that constraint are not applied +# 2. make a new constraint when the points are distanced from each other, +# check that one from constrainted objects has moved +#========================================================================= +#========================================================================= +# End of test +#========================================================================= diff --git a/src/SketchPlugin/Test/TestConstraintLength.py b/src/SketchPlugin/Test/TestConstraintLength.py new file mode 100644 index 000000000..e9ebab020 --- /dev/null +++ b/src/SketchPlugin/Test/TestConstraintLength.py @@ -0,0 +1,92 @@ +""" + TestConstraints.py + Base for all constaints tests. + + SketchPlugin_ConstraintLength + static const std::string MY_CONSTRAINT_LENGTH_ID("SketchConstraintLength"); + data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type()); + data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type()); + data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type()); + +""" +from GeomDataAPI import * +from ModelAPI import * +#========================================================================= +# Initialization of the test +#========================================================================= + +__updated__ = "2014-07-28" + +aPluginManager = ModelAPI_PluginManager.get() +aDocument = aPluginManager.rootDocument() +#========================================================================= +# Creation of a sketch +#========================================================================= +aDocument.startOperation() +aSketchFeature = aDocument.addFeature("Sketch") +aSketchFeatureData = aSketchFeature.data() +origin = geomDataAPI_Point(aSketchFeatureData.attribute("Origin")) +origin.setValue(0, 0, 0) +dirx = geomDataAPI_Dir(aSketchFeatureData.attribute("DirX")) +dirx.setValue(1, 0, 0) +diry = geomDataAPI_Dir(aSketchFeatureData.attribute("DirY")) +diry.setValue(0, 1, 0) +norm = geomDataAPI_Dir(aSketchFeatureData.attribute("Norm")) +norm.setValue(0, 0, 1) +aDocument.finishOperation() +#========================================================================= +# Create a line with length 100 +#========================================================================= +aDocument.startOperation() +aSketchReflist = aSketchFeatureData.reflist("Features") +aSketchLineA = aDocument.addFeature("SketchLine") +aSketchReflist.append(aSketchLineA) +aLineAStartPoint = geomDataAPI_Point2D( + aSketchLineA.data().attribute("StartPoint")) +aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.data().attribute("EndPoint")) +aLineAStartPoint.setValue(0., 25.) +aLineAEndPoint.setValue(100., 25.) +aDocument.finishOperation() +#========================================================================= +# Make a constraint to keep the length +#========================================================================= +aDocument.startOperation() +aCoincidenceConstraint = aDocument.addFeature("SketchConstraintLength") +aConstraintData = aCoincidenceConstraint.data() +aLength = aConstraintData.real("ConstraintValue") +refattrA = aConstraintData.refattr("ConstraintEntityA") +assert (not aLength.isInitialized()) +assert (not refattrA.isInitialized()) +# aLength.setValue(100.) +# refattrA.setObject(aSketchLineA) +aResult = aSketchLineA.firstResult() +assert (aResult is not None) +refattrA.setObject(modelAPI_ResultConstruction(aResult)) +aDocument.finishOperation() +assert (aLength.isInitialized()) +assert (refattrA.isInitialized()) +#========================================================================= +# Check values and move one constrainted object +#========================================================================= +deltaX = 40. +# move start point, check that end point are moved also +assert (aLineAStartPoint.x() == 0) +assert (aLineAStartPoint.y() == 25) +assert (aLineAEndPoint.x() == 100) +assert (aLineAEndPoint.y() == 25) +aDocument.startOperation() +aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX, + aLineAStartPoint.y()) +aDocument.finishOperation() +assert (aLineAStartPoint.x() == 40) +assert (aLineAStartPoint.y() == 25) +assert (aLineAEndPoint.x() == 140) +assert (aLineAEndPoint.y() == 25) +#========================================================================= +# TODO: improve test +# 1. remove constraint, move line's start point to +# check that constraint are not applied +#========================================================================= +#========================================================================= +# End of test +#========================================================================= -- 2.39.2