From 89b2089e16ead86659ed51b3dd310d28e96d85b4 Mon Sep 17 00:00:00 2001 From: azv Date: Wed, 22 Apr 2015 11:40:40 +0300 Subject: [PATCH] Issue #483: Fix the problem with distance constraint updating --- .../SketchSolver_ConstraintDistance.cpp | 23 ++++++++++++++++++- .../SketchSolver_ConstraintDistance.h | 8 +++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/SketchSolver/SketchSolver_ConstraintDistance.cpp b/src/SketchSolver/SketchSolver_ConstraintDistance.cpp index d256a25ed..f8898477f 100644 --- a/src/SketchSolver/SketchSolver_ConstraintDistance.cpp +++ b/src/SketchSolver/SketchSolver_ConstraintDistance.cpp @@ -54,14 +54,28 @@ void SketchSolver_ConstraintDistance::process() aConstraint.h = myStorage->addConstraint(aConstraint); mySlvsConstraints.push_back(aConstraint.h); + myPrevValue = 0.0; + adjustConstraint(); +} + +void SketchSolver_ConstraintDistance::adjustConstraint() +{ // Adjust point-line distance if (getType() != SLVS_C_PT_LINE_DISTANCE) return; + // Check the sign of distance is changed + Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front()); + if (fabs(myPrevValue) == fabs(aConstraint.valA)) { + aConstraint.valA = myPrevValue; + myStorage->updateConstraint(aConstraint); + return; + } + // Get constraint parameters and check the sign of constraint value std::vector::iterator aCIter = mySlvsConstraints.begin(); for (; aCIter != mySlvsConstraints.end(); aCIter++) { - Slvs_Constraint aConstraint = myStorage->getConstraint(*aCIter); + aConstraint = myStorage->getConstraint(*aCIter); Slvs_Entity aLine = myStorage->getEntity(aConstraint.entityA); // Obtain point and line coordinates Slvs_hEntity aPointID[3] = {aConstraint.ptA, aLine.point[0], aLine.point[1]}; @@ -81,3 +95,10 @@ void SketchSolver_ConstraintDistance::process() } } } + +void SketchSolver_ConstraintDistance::update(ConstraintPtr theConstraint) +{ + Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front()); + myPrevValue = aConstraint.valA; + SketchSolver_Constraint::update(theConstraint); +} diff --git a/src/SketchSolver/SketchSolver_ConstraintDistance.h b/src/SketchSolver/SketchSolver_ConstraintDistance.h index ff594c10c..ba1827208 100644 --- a/src/SketchSolver/SketchSolver_ConstraintDistance.h +++ b/src/SketchSolver/SketchSolver_ConstraintDistance.h @@ -21,6 +21,9 @@ public: SketchSolver_Constraint(theConstraint), myType(SLVS_C_UNKNOWN) {} + /// \brief Update constraint + virtual void update(ConstraintPtr theConstraint = ConstraintPtr()); + virtual int getType() const {return myType; } @@ -28,8 +31,13 @@ protected: /// \brief Converts SketchPlugin constraint to a list of SolveSpace constraints virtual void process(); + /// \brief This method is used in derived objects to check consistence of constraint. + /// E.g. the distance between line and point may be signed. + virtual void adjustConstraint(); + private: int myType; ///< type of constraint (applicable: SLVS_C_PT_PT_DISTANCE, SLVS_C_PT_LINE_DISTANCE) + double myPrevValue; ///< previous value of distance (for correct calculation of a distance sign) }; #endif -- 2.39.2