From 7129b3ca0e0ee2a5937cf2f681db41c30efe7cd9 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Mon, 29 Oct 2018 11:07:12 +0100 Subject: [PATCH] Sketcher: Mass operations on dimensionals ========================================= This commit introduces two functions to operate on all datum constraints exclusively: - Make all datum constraints driving or not driving, depending on the argument. - Move all datum constraints at the end of the constraint list. The practical use an ongoing effort towards improving the block constraint behaviour, that will be defered to 0.19 --- src/Mod/Sketcher/App/SketchObject.cpp | 113 ++++++++++++++++++++++---- src/Mod/Sketcher/App/SketchObject.h | 13 ++- 2 files changed, 110 insertions(+), 16 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 63646962b5..e94e1d90cd 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -326,14 +326,10 @@ int SketchObject::setDriving(int ConstrId, bool isdriving) { const std::vector &vals = this->Constraints.getValues(); - if (ConstrId < 0 || ConstrId >= int(vals.size())) - return -1; + int ret = testDrivingChange(ConstrId, isdriving); - if (!vals[ConstrId]->isDimensional()) - return -2; - - if (!(vals[ConstrId]->First>=0 || vals[ConstrId]->Second>=0 || vals[ConstrId]->Third>=0) && isdriving==true) - return -3; // a constraint that does not have at least one element as not-external-geometry can never be driving. + if(ret < 0) + return ret; // copy the list std::vector newVals(vals); @@ -370,14 +366,10 @@ int SketchObject::toggleDriving(int ConstrId) { const std::vector &vals = this->Constraints.getValues(); - if (ConstrId < 0 || ConstrId >= int(vals.size())) - return -1; + int ret = testDrivingChange(ConstrId,!vals[ConstrId]->isDriving); - if (!vals[ConstrId]->isDimensional()) - return -2; - - if (!(vals[ConstrId]->First>=0 || vals[ConstrId]->Second>=0 || vals[ConstrId]->Third>=0) && vals[ConstrId]->isDriving==false) - return -3; // a constraint that does not have at least one element as not-external-geometry can never be driving. + if(ret<0) + return ret; const Part::Geometry * geo1 = getGeometry(vals[ConstrId]->First); const Part::Geometry * geo2 = getGeometry(vals[ConstrId]->Second); @@ -389,7 +381,7 @@ int SketchObject::toggleDriving(int ConstrId) if (extorconstructionpoint1 && extorconstructionpoint2 && extorconstructionpoint3 && vals[ConstrId]->isDriving==false) return -4; - + // copy the list std::vector newVals(vals); // clone the changed Constraint @@ -407,6 +399,97 @@ int SketchObject::toggleDriving(int ConstrId) return 0; } +int SketchObject::testDrivingChange(int ConstrId, bool isdriving) +{ + const std::vector &vals = this->Constraints.getValues(); + + if (ConstrId < 0 || ConstrId >= int(vals.size())) + return -1; + + if (!vals[ConstrId]->isDimensional()) + return -2; + + if (!(vals[ConstrId]->First>=0 || vals[ConstrId]->Second>=0 || vals[ConstrId]->Third>=0) && isdriving==true) + return -3; // a constraint that does not have at least one element as not-external-geometry can never be driving. + + + return 0; +} + + +/// Make all dimensionals Driving/non-Driving +int SketchObject::setDatumsDriving(bool isdriving) +{ + const std::vector &vals = this->Constraints.getValues(); + std::vector newVals(vals); + + std::vector< Constraint * > tbd; // list of dynamically allocated memory that need to be deleted; + + for(size_t i=0; iclone(); + constNew->isDriving = isdriving; + newVals[i] = constNew; + tbd.push_back(constNew); + } + } + + + this->Constraints.setValues(newVals); + + + for(size_t i = 0; i < newVals.size(); i++) { + if (!isdriving && newVals[i]->isDimensional()) + setExpression(Constraints.createPath(i), boost::shared_ptr()); + } + + for(auto &t : tbd) + delete t; + + + if(noRecomputes) // if we do not have a recompute, the sketch must be solved to update the DoF of the solver + solve(); + + return 0; +} + +int SketchObject::moveDatumsToEnd(void) +{ + const std::vector &vals = this->Constraints.getValues(); + + std::vector copy(vals); + std::vector newVals(vals.size()); + + int addindex= copy.size()-1; + + // add the dimensionals at the end + for(int i= copy.size()-1 ; i >= 0; i--) { + + if(copy[i]->isDimensional()) { + newVals[addindex] = copy[i]; + addindex--; + } + } + + // add the non-dimensionals + for(int i = copy.size()-1; i >= 0; i--) { + + if(!copy[i]->isDimensional()) { + newVals[addindex] = copy[i]; + addindex--; + } + } + + this->Constraints.setValues(newVals); + + if(noRecomputes) // if we do not have a recompute, the sketch must be solved to update the DoF of the solver + solve(); + + return 0; +} + int SketchObject::setVirtualSpace(int ConstrId, bool isinvirtualspace) { const std::vector &vals = this->Constraints.getValues(); diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index 7ee39d29b7..50d8c0b707 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -113,7 +113,7 @@ public: /// Copy the constraints instead of cloning them and copying the expressions if any int addCopyOfConstraints(const SketchObject &orig); /// add constraint - int addConstraint(const Constraint *constraint); + int addConstraint(const Constraint *constraint); /// delete constraint int delConstraint(int ConstrId); int delConstraints(std::vector ConstrIds, bool updategeometry=true); @@ -179,6 +179,12 @@ public: int getDriving(int ConstrId, bool &isdriving); /// toggle the driving status of this constraint int toggleDriving(int ConstrId); + + /// Make all dimensionals Driving/non-Driving + int setDatumsDriving(bool isdriving); + /// Move Dimensional constraints at the end of the properties array + int moveDatumsToEnd(void); + /// set the driving status of this constraint and solve int setVirtualSpace(int ConstrId, bool isinvirtualspace); /// get the driving status of this constraint @@ -408,6 +414,11 @@ protected: \retval list - the supported geometry list */ std::vector supportedGeometry(const std::vector &geoList) const; + + + // refactoring functions + // check whether constraint may be changed driving status + int testDrivingChange(int ConstrId, bool isdriving); private: /// Flag to allow external geometry from other bodies than the one this sketch belongs to