From c5fd2fc8919953233a7f98b26589cd0ed6280676 Mon Sep 17 00:00:00 2001 From: azv Date: Fri, 16 May 2014 09:22:35 +0400 Subject: [PATCH] Temporary constraint for correct line movement --- .../SketchSolver_ConstraintManager.cpp | 68 +++++++++++++++++-- .../SketchSolver_ConstraintManager.h | 15 +++- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/SketchSolver/SketchSolver_ConstraintManager.cpp b/src/SketchSolver/SketchSolver_ConstraintManager.cpp index 68bc58b3c..0ca5c3279 100644 --- a/src/SketchSolver/SketchSolver_ConstraintManager.cpp +++ b/src/SketchSolver/SketchSolver_ConstraintManager.cpp @@ -280,6 +280,10 @@ SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_Cons myEntities.clear(); myConstraints.clear(); myConstraintMap.clear(); + + // If the group with maximal identifier is deleted, decrease the indexer + if (myID == myGroupIndexer) + myGroupIndexer--; } bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isBaseWorkplane( @@ -329,7 +333,7 @@ bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeConstra if (aDistAttr) { aDistance = aDistAttr->value(); - if (aConstrMapIter != myConstraintMap.end() && aConstrIter->valA != aDistance) + if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance) { myNeedToSolve = true; aConstrIter->valA = aDistance; @@ -515,10 +519,12 @@ Slvs_hParam SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::change { if (thePrmIter != myParams.end()) { // Parameter should be updated - if (thePrmIter->val != theParam) - myNeedToSolve = true; // parameter is changed, need to resolve constraints int aParamPos = thePrmIter - myParams.begin(); - myParams[aParamPos].val = theParam; + if (fabs(thePrmIter->val - theParam) > tolerance) + { + myNeedToSolve = true; // parameter is changed, need to resolve constraints + myParams[aParamPos].val = theParam; + } thePrmIter++; return myParams[aParamPos].h; } @@ -605,6 +611,7 @@ void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::ResolveConstr } /// \todo Implement error handling + removeTemporaryConstraints(); myNeedToSolve = false; } @@ -647,7 +654,60 @@ void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateEntityI boost::shared_ptr theEntity) { if (myEntityMap.find(theEntity) != myEntityMap.end()) + { + // If the attribute is a point and it is changed (the group needs to rebuild), + // probably user has dragged this point into this position, + // so it is necessary to add constraint which will quarantee the point will not change + + // Store myNeedToSolve flag to verify the entity is really changed + bool aNeedToSolveCopy = myNeedToSolve; + myNeedToSolve = false; + changeEntity(theEntity); + + if (myNeedToSolve) // the entity is changed + { + // Verify the entity is a point and add temporary constraint of permanency + boost::shared_ptr aPoint = + boost::dynamic_pointer_cast(theEntity); + boost::shared_ptr aPoint2D = + boost::dynamic_pointer_cast(theEntity); + if (aPoint || aPoint2D) + addTemporaryConstraintWhereDragged(theEntity); + } + + // Restore flag of changes + myNeedToSolve = myNeedToSolve || aNeedToSolveCopy; + } +} + +void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged( + boost::shared_ptr theEntity) +{ + // Find identifier of the entity + std::map, Slvs_hEntity>::const_iterator + anEntIter = myEntityMap.find(theEntity); + + // Create WHERE_DRAGGED constraint + Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED, + myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0); + myConstraints.push_back(aWDConstr); + myTempConstraints.push_back(aWDConstr.h); +} + +void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::removeTemporaryConstraints() +{ + std::list::reverse_iterator aTmpConstrIter; + for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++) + { + int aConstrPos = Search(*aTmpConstrIter, myConstraints); + myConstraints.erase(myConstraints.begin() + aConstrPos); + + // If the removing constraint has higher index, decrease the indexer + if (*aTmpConstrIter == myConstrMaxID) + myConstrMaxID--; + } + myTempConstraints.clear(); } diff --git a/src/SketchSolver/SketchSolver_ConstraintManager.h b/src/SketchSolver/SketchSolver_ConstraintManager.h index 95512cf3b..5844d9319 100644 --- a/src/SketchSolver/SketchSolver_ConstraintManager.h +++ b/src/SketchSolver/SketchSolver_ConstraintManager.h @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -216,6 +217,15 @@ protected: */ void updateAttribute(boost::shared_ptr theAttribute, const Slvs_hEntity& theEntityID); + /** \brief Adds a constraint for a point which should not be changed during computations + * \param[in] theEntity the base for the constraint + */ + void addTemporaryConstraintWhereDragged(boost::shared_ptr theEntity); + + /** \brief Remove all temporary constraint after computation finished + */ + void removeTemporaryConstraints(); + private: /** \brief Creates a workplane from the sketch parameters * \param[in] theSketch parameters of workplane are the attributes of this sketch @@ -240,9 +250,10 @@ private: // SketchPlugin entities boost::shared_ptr mySketch; ///< Equivalent to workplane std::map, Slvs_hConstraint> - myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints + myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints + std::list myTempConstraints; ///< The list of identifiers of temporary constraints std::map, Slvs_hEntity> - myEntityMap; ///< The map between parameters of constraints and their equivalent SolveSpace entities + myEntityMap; ///< The map between parameters of constraints and their equivalent SolveSpace entities }; #endif -- 2.39.2