From 01ea17c79bfb3ea775eb282798fc6e5496f2f1c6 Mon Sep 17 00:00:00 2001 From: azv Date: Mon, 16 Mar 2015 12:33:24 +0300 Subject: [PATCH] Horizontal and vertical constraints 1. SketchPlugin featurea are implemented 2. SketchSolver integration of new constraints is made 3. Unit tests are created --- src/SketchPlugin/CMakeLists.txt | 6 ++ .../SketchPlugin_ConstraintHorizontal.cpp | 43 ++++++++++ .../SketchPlugin_ConstraintHorizontal.h | 50 ++++++++++++ .../SketchPlugin_ConstraintVertical.cpp | 43 ++++++++++ .../SketchPlugin_ConstraintVertical.h | 50 ++++++++++++ src/SketchPlugin/SketchPlugin_Plugin.cpp | 8 ++ .../Test/TestConstraintHorizontal.py | 78 +++++++++++++++++++ .../Test/TestConstraintVertical.py | 78 +++++++++++++++++++ src/SketchPlugin/plugin-Sketch.xml | 16 +++- src/SketchSolver/SketchSolver_Constraint.cpp | 18 +++++ 10 files changed, 389 insertions(+), 1 deletion(-) create mode 100644 src/SketchPlugin/SketchPlugin_ConstraintHorizontal.cpp create mode 100644 src/SketchPlugin/SketchPlugin_ConstraintHorizontal.h create mode 100644 src/SketchPlugin/SketchPlugin_ConstraintVertical.cpp create mode 100644 src/SketchPlugin/SketchPlugin_ConstraintVertical.h create mode 100644 src/SketchPlugin/Test/TestConstraintHorizontal.py create mode 100644 src/SketchPlugin/Test/TestConstraintVertical.py diff --git a/src/SketchPlugin/CMakeLists.txt b/src/SketchPlugin/CMakeLists.txt index 01c640568..21b0cfbec 100644 --- a/src/SketchPlugin/CMakeLists.txt +++ b/src/SketchPlugin/CMakeLists.txt @@ -22,6 +22,8 @@ SET(PROJECT_HEADERS SketchPlugin_ConstraintPerpendicular.h SketchPlugin_ConstraintRadius.h SketchPlugin_ConstraintRigid.h + SketchPlugin_ConstraintHorizontal.h + SketchPlugin_ConstraintVertical.h SketchPlugin_ShapeValidator.h SketchPlugin_Validators.h SketchPlugin_ResultValidators.h @@ -44,6 +46,8 @@ SET(PROJECT_SOURCES SketchPlugin_ConstraintPerpendicular.cpp SketchPlugin_ConstraintRadius.cpp SketchPlugin_ConstraintRigid.cpp + SketchPlugin_ConstraintHorizontal.cpp + SketchPlugin_ConstraintVertical.cpp SketchPlugin_ShapeValidator.cpp SketchPlugin_Validators.cpp SketchPlugin_ResultValidators.cpp @@ -89,5 +93,7 @@ ADD_UNIT_TESTS(TestSketchPointLine.py TestConstraintPerpendicular.py TestConstraintRadius.py TestConstraintRigid.py + TestConstraintHorizontal.py + TestConstraintVertical.py TestHighload.py TestSnowflake.py) diff --git a/src/SketchPlugin/SketchPlugin_ConstraintHorizontal.cpp b/src/SketchPlugin/SketchPlugin_ConstraintHorizontal.cpp new file mode 100644 index 000000000..722ea659a --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_ConstraintHorizontal.cpp @@ -0,0 +1,43 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> + +// File: SketchPlugin_ConstraintHorizontal.cpp +// Created: 16 Mar 2015 +// Author: Artem ZHIDKOV + +#include "SketchPlugin_ConstraintHorizontal.h" + +#include +#include +#include + +#include +#include + +#include + +#include + +SketchPlugin_ConstraintHorizontal::SketchPlugin_ConstraintHorizontal() +{ +} + +void SketchPlugin_ConstraintHorizontal::initAttributes() +{ + data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type()); +} + +void SketchPlugin_ConstraintHorizontal::execute() +{ +} + +AISObjectPtr SketchPlugin_ConstraintHorizontal::getAISObject(AISObjectPtr thePrevious) +{ + if (!sketch()) + return thePrevious; + + AISObjectPtr anAIS = thePrevious; + /// TODO: Horizontal constraint presentation should be put here + return anAIS; +} + + diff --git a/src/SketchPlugin/SketchPlugin_ConstraintHorizontal.h b/src/SketchPlugin/SketchPlugin_ConstraintHorizontal.h new file mode 100644 index 000000000..3014e2657 --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_ConstraintHorizontal.h @@ -0,0 +1,50 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> + +// File: SketchPlugin_ConstraintHorizontal.h +// Created: 16 Mar 2015 +// Author: Artem ZHIDKOV + +#ifndef SketchPlugin_ConstraintHorizontal_H_ +#define SketchPlugin_ConstraintHorizontal_H_ + +#include "SketchPlugin.h" +#include +#include "SketchPlugin_ConstraintBase.h" + +/** \class SketchPlugin_ConstraintHorizontal + * \ingroup Plugins + * \brief Feature for creation of a new constraint horizontality of a line + * + * This constraint has one attribute SketchPlugin_Constraint::ENTITY_A(), + * which specifies a line to be horizontal + */ +class SketchPlugin_ConstraintHorizontal : public SketchPlugin_ConstraintBase +{ + public: + /// Horizontal constraint kind + inline static const std::string& ID() + { + static const std::string MY_CONSTRAINT_HORIZONTAL_ID("SketchConstraintHorizontal"); + return MY_CONSTRAINT_HORIZONTAL_ID; + } + /// \brief Returns the kind of a feature + SKETCHPLUGIN_EXPORT virtual const std::string& getKind() + { + static std::string MY_KIND = SketchPlugin_ConstraintHorizontal::ID(); + return MY_KIND; + } + + /// \brief Creates a new part document if needed + SKETCHPLUGIN_EXPORT virtual void execute(); + + /// \brief Request for initialization of data model of the feature: adding all attributes + SKETCHPLUGIN_EXPORT virtual void initAttributes(); + + /// Returns the AIS preview + SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious); + + /// \brief Use plugin manager for features creation + SketchPlugin_ConstraintHorizontal(); +}; + +#endif diff --git a/src/SketchPlugin/SketchPlugin_ConstraintVertical.cpp b/src/SketchPlugin/SketchPlugin_ConstraintVertical.cpp new file mode 100644 index 000000000..ecfc91776 --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_ConstraintVertical.cpp @@ -0,0 +1,43 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> + +// File: SketchPlugin_ConstraintVertical.cpp +// Created: 16 Mar 2015 +// Author: Artem ZHIDKOV + +#include "SketchPlugin_ConstraintVertical.h" + +#include +#include +#include + +#include +#include + +#include + +#include + +SketchPlugin_ConstraintVertical::SketchPlugin_ConstraintVertical() +{ +} + +void SketchPlugin_ConstraintVertical::initAttributes() +{ + data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type()); +} + +void SketchPlugin_ConstraintVertical::execute() +{ +} + +AISObjectPtr SketchPlugin_ConstraintVertical::getAISObject(AISObjectPtr thePrevious) +{ + if (!sketch()) + return thePrevious; + + AISObjectPtr anAIS = thePrevious; + /// TODO: Horizontal constraint presentation should be put here + return anAIS; +} + + diff --git a/src/SketchPlugin/SketchPlugin_ConstraintVertical.h b/src/SketchPlugin/SketchPlugin_ConstraintVertical.h new file mode 100644 index 000000000..79b5c74e6 --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_ConstraintVertical.h @@ -0,0 +1,50 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> + +// File: SketchPlugin_ConstraintVertical.h +// Created: 16 Mar 2015 +// Author: Artem ZHIDKOV + +#ifndef SketchPlugin_ConstraintVertical_H_ +#define SketchPlugin_ConstraintVertical_H_ + +#include "SketchPlugin.h" +#include +#include "SketchPlugin_ConstraintBase.h" + +/** \class SketchPlugin_ConstraintVertical + * \ingroup Plugins + * \brief Feature for creation of a new constraint verticality of a line + * + * This constraint has one attribute SketchPlugin_Constraint::ENTITY_A(), + * which specifies a line to be vertical + */ +class SketchPlugin_ConstraintVertical : public SketchPlugin_ConstraintBase +{ + public: + /// Vertical constraint kind + inline static const std::string& ID() + { + static const std::string MY_CONSTRAINT_VERTICAL_ID("SketchConstraintVertical"); + return MY_CONSTRAINT_VERTICAL_ID; + } + /// \brief Returns the kind of a feature + SKETCHPLUGIN_EXPORT virtual const std::string& getKind() + { + static std::string MY_KIND = SketchPlugin_ConstraintVertical::ID(); + return MY_KIND; + } + + /// \brief Creates a new part document if needed + SKETCHPLUGIN_EXPORT virtual void execute(); + + /// \brief Request for initialization of data model of the feature: adding all attributes + SKETCHPLUGIN_EXPORT virtual void initAttributes(); + + /// Returns the AIS preview + SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious); + + /// \brief Use plugin manager for features creation + SketchPlugin_ConstraintVertical(); +}; + +#endif diff --git a/src/SketchPlugin/SketchPlugin_Plugin.cpp b/src/SketchPlugin/SketchPlugin_Plugin.cpp index c4188bf90..6aa24717d 100644 --- a/src/SketchPlugin/SketchPlugin_Plugin.cpp +++ b/src/SketchPlugin/SketchPlugin_Plugin.cpp @@ -8,11 +8,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -109,6 +111,10 @@ FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID) return FeaturePtr(new SketchPlugin_ConstraintRadius); } else if (theFeatureID == SketchPlugin_ConstraintRigid::ID()) { return FeaturePtr(new SketchPlugin_ConstraintRigid); + } else if (theFeatureID == SketchPlugin_ConstraintHorizontal::ID()) { + return FeaturePtr(new SketchPlugin_ConstraintHorizontal); + } else if (theFeatureID == SketchPlugin_ConstraintVertical::ID()) { + return FeaturePtr(new SketchPlugin_ConstraintVertical); } // feature of such kind is not found return FeaturePtr(); @@ -153,6 +159,8 @@ std::shared_ptr SketchPlugin_Plugin aMsg->setState(SketchPlugin_ConstraintPerpendicular::ID(), aHasSketchPlane); aMsg->setState(SketchPlugin_ConstraintRadius::ID(), aHasSketchPlane); aMsg->setState(SketchPlugin_ConstraintRigid::ID(), aHasSketchPlane); + aMsg->setState(SketchPlugin_ConstraintHorizontal::ID(), aHasSketchPlane); + aMsg->setState(SketchPlugin_ConstraintVertical::ID(), aHasSketchPlane); } } return aMsg; diff --git a/src/SketchPlugin/Test/TestConstraintHorizontal.py b/src/SketchPlugin/Test/TestConstraintHorizontal.py new file mode 100644 index 000000000..337315858 --- /dev/null +++ b/src/SketchPlugin/Test/TestConstraintHorizontal.py @@ -0,0 +1,78 @@ +""" + TestConstraintHorizontal.py + Unit test of SketchPlugin_ConstraintHorizontal class + + SketchPlugin_ConstraintHorizontal + static const std::string MY_CONSTRAINT_HORIZONTAL_ID("SketchConstraintHorizontal"); + data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type()); + +""" +from GeomDataAPI import * +from ModelAPI import * +#========================================================================= +# Initialization of the test +#========================================================================= + +__updated__ = "2015-03-16" + +aSession = ModelAPI_Session.get() +aDocument = aSession.moduleDocument() +#========================================================================= +# Creation of a sketch +#========================================================================= +aSession.startOperation() +aSketchCommonFeature = aDocument.addFeature("Sketch") +aSketchFeature = modelAPI_CompositeFeature(aSketchCommonFeature) +origin = geomDataAPI_Point(aSketchFeature.attribute("Origin")) +origin.setValue(0, 0, 0) +dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX")) +dirx.setValue(1, 0, 0) +diry = geomDataAPI_Dir(aSketchFeature.attribute("DirY")) +diry.setValue(0, 1, 0) +norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm")) +norm.setValue(0, 0, 1) +aSession.finishOperation() +#========================================================================= +# Create non-horizontal line +#========================================================================= +aSession.startOperation() +aSketchLine = aSketchFeature.addFeature("SketchLine") +aLineStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint")) +aLineEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint")) +aLineStartPoint.setValue(0., 15.) +aLineEndPoint.setValue(20., 25.) +aSession.finishOperation() +#========================================================================= +# Assign horizontal constraint for a line +#========================================================================= +aSession.startOperation() +aHorizontalConstraint = aSketchFeature.addFeature("SketchConstraintHorizontal") +refattrA = aHorizontalConstraint.refattr("ConstraintEntityA") +aResult = modelAPI_ResultConstruction(aSketchLine.firstResult()) +assert (aResult is not None) +refattrA.setObject(aResult) +aHorizontalConstraint.execute() +aSession.finishOperation() +assert(aLineStartPoint.y() == aLineEndPoint.y()) +#========================================================================= +# Move one of boundary points of a line +#========================================================================= +deltaX = deltaY = 10. +aSession.startOperation() +aLineStartPoint.setValue(aLineStartPoint.x() + deltaX, + aLineStartPoint.y() + deltaY) +aSession.finishOperation() +assert(aLineStartPoint.y() == aLineEndPoint.y()) +#========================================================================= +# Move other boundary point of a line +#========================================================================= +deltaX = -3. +deltaY = -10. +aSession.startOperation() +aLineEndPoint.setValue(aLineEndPoint.x() + deltaX, + aLineEndPoint.y() + deltaY) +aSession.finishOperation() +assert(aLineStartPoint.y() == aLineEndPoint.y()) +#========================================================================= +# End of test +#========================================================================= diff --git a/src/SketchPlugin/Test/TestConstraintVertical.py b/src/SketchPlugin/Test/TestConstraintVertical.py new file mode 100644 index 000000000..87ba28f97 --- /dev/null +++ b/src/SketchPlugin/Test/TestConstraintVertical.py @@ -0,0 +1,78 @@ +""" + TestConstraintVertical.py + Unit test of SketchPlugin_ConstraintVertical class + + SketchPlugin_ConstraintVertical + static const std::string MY_CONSTRAINT_VERTICAL_ID("SketchConstraintVertical"); + data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type()); + +""" +from GeomDataAPI import * +from ModelAPI import * +#========================================================================= +# Initialization of the test +#========================================================================= + +__updated__ = "2015-03-16" + +aSession = ModelAPI_Session.get() +aDocument = aSession.moduleDocument() +#========================================================================= +# Creation of a sketch +#========================================================================= +aSession.startOperation() +aSketchCommonFeature = aDocument.addFeature("Sketch") +aSketchFeature = modelAPI_CompositeFeature(aSketchCommonFeature) +origin = geomDataAPI_Point(aSketchFeature.attribute("Origin")) +origin.setValue(0, 0, 0) +dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX")) +dirx.setValue(1, 0, 0) +diry = geomDataAPI_Dir(aSketchFeature.attribute("DirY")) +diry.setValue(0, 1, 0) +norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm")) +norm.setValue(0, 0, 1) +aSession.finishOperation() +#========================================================================= +# Create non-vertical line +#========================================================================= +aSession.startOperation() +aSketchLine = aSketchFeature.addFeature("SketchLine") +aLineStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint")) +aLineEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint")) +aLineStartPoint.setValue(0., 15.) +aLineEndPoint.setValue(20., 25.) +aSession.finishOperation() +#========================================================================= +# Assign vertical constraint for a line +#========================================================================= +aSession.startOperation() +aVerticalConstraint = aSketchFeature.addFeature("SketchConstraintVertical") +refattrA = aVerticalConstraint.refattr("ConstraintEntityA") +aResult = modelAPI_ResultConstruction(aSketchLine.firstResult()) +assert (aResult is not None) +refattrA.setObject(aResult) +aVerticalConstraint.execute() +aSession.finishOperation() +assert(aLineStartPoint.x() == aLineEndPoint.x()) +#========================================================================= +# Move one of boundary points of a line +#========================================================================= +deltaX = deltaY = 10. +aSession.startOperation() +aLineStartPoint.setValue(aLineStartPoint.x() + deltaX, + aLineStartPoint.y() + deltaY) +aSession.finishOperation() +assert(aLineStartPoint.x() == aLineEndPoint.x()) +#========================================================================= +# Move other boundary point of a line +#========================================================================= +deltaX = -3. +deltaY = -10. +aSession.startOperation() +aLineEndPoint.setValue(aLineEndPoint.x() + deltaX, + aLineEndPoint.y() + deltaY) +aSession.finishOperation() +assert(aLineStartPoint.x() == aLineEndPoint.x()) +#========================================================================= +# End of test +#========================================================================= diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index fafdb2fde..894e3a88f 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -5,7 +5,7 @@ + + + + + + + + + + + + diff --git a/src/SketchSolver/SketchSolver_Constraint.cpp b/src/SketchSolver/SketchSolver_Constraint.cpp index 61a42b64c..b2c7185d7 100644 --- a/src/SketchSolver/SketchSolver_Constraint.cpp +++ b/src/SketchSolver/SketchSolver_Constraint.cpp @@ -13,11 +13,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include @@ -197,6 +199,22 @@ const int& SketchSolver_Constraint::getType( return getType(); } + // Constraint for horizontal/vertical line + bool isHorizontal = (aConstraintKind.compare(SketchPlugin_ConstraintHorizontal::ID()) == 0); + bool isVertical = (aConstraintKind.compare(SketchPlugin_ConstraintVertical::ID()) == 0); + if (isHorizontal || isVertical) { + int aNbEntities = 2; // lines in SolveSpace constraints should start from SketchPlugin_Constraint::ENTITY_C() attribute + for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) { + std::shared_ptr anAttr = + aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr)); + if (typeOfAttribute(anAttr) == LINE) + myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr); + } + if (aNbEntities == 3) + myType = isHorizontal ? SLVS_C_HORIZONTAL : SLVS_C_VERTICAL; + return getType(); + } + /// \todo Implement other kind of constraints return getType(); -- 2.30.2