X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketchSolver%2FSketchSolver_Group.cpp;h=93bbfa69d406369bea25307cbe7a7ea0eb3d498e;hb=1e63ae393cf1ce1923329c90aa66fa33f957f268;hp=f4ba30c83ba97cd9ad720712f6be25c5eace4781;hpb=e3483e0c2eaddb88f2e55a72bd81ca0fb67cc4ce;p=modules%2Fshaper.git diff --git a/src/SketchSolver/SketchSolver_Group.cpp b/src/SketchSolver/SketchSolver_Group.cpp index f4ba30c83..93bbfa69d 100644 --- a/src/SketchSolver/SketchSolver_Group.cpp +++ b/src/SketchSolver/SketchSolver_Group.cpp @@ -1,136 +1,97 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: SketchSolver_Group.cpp -// Created: 27 May 2014 -// Author: Artem ZHIDKOV +// Copyright (C) 2014-2017 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or +// email : webmaster.salome@opencascade.com +// #include "SketchSolver_Group.h" - -#include -#include -#include #include -////#include #include -#include -#include -////#include -////#include -////#include -////#include -////#include -////#include -////#include +#include +#include +#include + +#include #include -////#include #include -////#include -#include -#include -//// -////#include -#include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include #include -#include -#include -////#include #include #include -////#include -//// -////#include -////#include -////#include -////#include -////#include -#include -#include - -/// \brief This class is used to give unique index to the groups -class GroupIndexer +static void sendMessage(const char* theMessageName) { -public: - /// \brief Return vacant index - static GroupID NEW_GROUP() { return ++myGroupIndex; } - /// \brief Removes the index - static void REMOVE_GROUP(const GroupID& theIndex) { - if (myGroupIndex == theIndex) - myGroupIndex--; - } - -private: - GroupIndexer() {}; + std::shared_ptr aMessage = std::shared_ptr( + new Events_Message(Events_Loop::eventByName(theMessageName))); + Events_Loop::loop()->send(aMessage); +} - static GroupID myGroupIndex; ///< index of the group -}; +static void sendMessage(const char* theMessageName, const std::set& theConflicting) +{ + std::shared_ptr aMessage = + std::shared_ptr( + new ModelAPI_SolverFailedMessage(Events_Loop::eventByName(theMessageName))); + aMessage->setObjects(theConflicting); + Events_Loop::loop()->send(aMessage); +} -GroupID GroupIndexer::myGroupIndex = GID_OUTOFGROUP; +static void sendMessage(const char* theMessageName, + const CompositeFeaturePtr& theSketch, + const int theDOF) +{ + std::shared_ptr aMessage = + std::shared_ptr( + new ModelAPI_SolverFailedMessage(Events_Loop::eventByName(theMessageName))); + std::set anObjects; + anObjects.insert(theSketch); + aMessage->setObjects(anObjects); + aMessage->dof(theDOF); -static void sendMessage(const char* theMessageName) -{ - std::shared_ptr aMessage = std::shared_ptr( - new Events_Message(Events_Loop::eventByName(theMessageName))); Events_Loop::loop()->send(aMessage); } // ======================================================== -// ========= SketchSolver_Group =============== +// ========= SketchSolver_Group =============== // ======================================================== -SketchSolver_Group::SketchSolver_Group( - std::shared_ptr theWorkplane) - : myID(GroupIndexer::NEW_GROUP()), - myPrevSolved(true) +SketchSolver_Group::SketchSolver_Group(const CompositeFeaturePtr& theWorkplane) + : mySketch(theWorkplane), + myPrevResult(PlaneGCSSolver_Solver::STATUS_UNKNOWN), + myDOF(0), + myIsEventsBlocked(false), + myMultiConstraintUpdateStack(0) { - // Initialize workplane - myWorkplaneID = EID_UNKNOWN; - addWorkplane(theWorkplane); + mySketchSolver = SolverPtr(new PlaneGCSSolver_Solver); + myStorage = StoragePtr(new PlaneGCSSolver_Storage(mySketchSolver)); } SketchSolver_Group::~SketchSolver_Group() { myConstraints.clear(); - GroupIndexer::REMOVE_GROUP(myID); -} - -// ============================================================================ -// Function: isBaseWorkplane -// Class: SketchSolver_Group -// Purpose: verify the group is based on the given workplane -// ============================================================================ -bool SketchSolver_Group::isBaseWorkplane(CompositeFeaturePtr theWorkplane) const -{ - return theWorkplane == mySketch; -} - -// ============================================================================ -// Function: isInteract -// Class: SketchSolver_Group -// Purpose: verify are there any entities in the group used by given constraint -// ============================================================================ -bool SketchSolver_Group::isInteract(FeaturePtr theFeature) const -{ - // Empty group interacts with everything - if (isEmpty()) - return true; - // Check interaction with the storage - return myStorage->isInteract(theFeature); + // send the message that there is no more conflicting constraints + if (!myConflictingConstraints.empty()) { + sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints); + myConflictingConstraints.clear(); + } } // ============================================================================ @@ -141,260 +102,84 @@ bool SketchSolver_Group::isInteract(FeaturePtr theFeature) const bool SketchSolver_Group::changeConstraint( std::shared_ptr theConstraint) { - // There is no workplane yet, something wrong - if (myWorkplaneID == EID_UNKNOWN) - return false; - - if (!theConstraint || !theConstraint->data()) - return false; - - if (!checkFeatureValidity(theConstraint)) - return false; - - BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder(); - bool isNewConstraint = myConstraints.find(theConstraint) == myConstraints.end(); if (isNewConstraint) { // Add constraint to the current group - SolverConstraintPtr aConstraint = aBuilder->createConstraint(theConstraint); + SolverConstraintPtr aConstraint = PlaneGCSSolver_Tools::createConstraint(theConstraint); if (!aConstraint) return false; - aConstraint->process(myStorage, getId(), getWorkplaneId()); + aConstraint->process(myStorage, myIsEventsBlocked); if (!aConstraint->error().empty()) { if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED()) return false; // some attribute are not initialized yet, don't show message - Events_Error::send(aConstraint->error(), this); + Events_InfoMessage("SketchSolver_Group", aConstraint->error(), this).send(); } - -//// // Additional verification of coincidence of several points -//// if (aConstraint->getType() == CONSTRAINT_PT_PT_COINCIDENT) { -//// bool hasMultiCoincidence = false; -//// std::shared_ptr aCoincidence = -//// std::dynamic_pointer_cast(aConstraint); -//// -//// ConstraintConstraintMap::iterator aCIter = myConstraints.begin(); -//// for (; aCIter != myConstraints.end(); ++aCIter) { -//// if (aCIter->second->getType() != CONSTRAINT_PT_PT_COINCIDENT) -//// continue; -//// -//// std::shared_ptr aCurCoinc = -//// std::dynamic_pointer_cast(aCIter->second); -//// if (aCoincidence != aCurCoinc && aCurCoinc->isCoincide(aCoincidence)) { -//// aCoincidence->attach(aCurCoinc); -//////// // update other coincidences -//////// ConstraintConstraintMap::iterator anIt = aCIter; -//////// for (++anIt; anIt != myConstraints.end(); ++anIt) -//////// if (anIt->second == aCIter->second) -//////// anIt->second = aCoincidence; -//// aCIter->second = aCoincidence; -//// hasMultiCoincidence = true; -//// } -//// } -//// -//// if (hasMultiCoincidence) -//// notifyMultiConstraints(); -//// } myConstraints[theConstraint] = aConstraint; } else myConstraints[theConstraint]->update(); -//// // Fix base features for fillet -//// if (isNewConstraint && theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) { -//// std::list anAttrList = -//// theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId()); -//// std::list::iterator anAttrIter = anAttrList.begin(); -//// for (; anAttrIter != anAttrList.end(); anAttrIter++) { -//// AttributeRefAttrPtr aRefAttr = -//// std::dynamic_pointer_cast(*anAttrIter); -//// if (!aRefAttr || !aRefAttr->isObject()) -//// continue; -//// FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object()); -//// SolverConstraintPtr aConstraint = aBuilder->createRigidConstraint(aFeature); -//// if (!aConstraint) -//// continue; -//// aConstraint->setGroup(this); -//// aConstraint->setStorage(myStorage); -//// setTemporary(aConstraint); -//// } -//// } - - // Fix mirror line - if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) { - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( - theConstraint->attribute(SketchPlugin_ConstraintMirror::ENTITY_A())); - if (aRefAttr && aRefAttr->isObject()) { - std::shared_ptr aFeature = - std::dynamic_pointer_cast( - ModelAPI_Feature::feature(aRefAttr->object())); - if (aFeature) { - SolverConstraintPtr aConstraint = aBuilder->createFixedConstraint(aFeature); - if (aConstraint) { - aConstraint->process(myStorage, getId(), getWorkplaneId()); - setTemporary(aConstraint); - } - } - } - } - -//// // Check the attributes of constraint are given by parametric expression -//// std::list anAttributes = -//// theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId()); -//// std::list::iterator anAttrIt = anAttributes.begin(); -//// for (; anAttrIt != anAttributes.end(); ++anAttrIt) { -//// AttributeRefAttrPtr aRefAttr = -//// std::dynamic_pointer_cast(*anAttrIt); -//// if (!aRefAttr) -//// continue; -//// -//// std::shared_ptr aPoint; -//// if (aRefAttr->isObject()) { -//// FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object()); -//// if (aFeat->getKind() != SketchPlugin_Point::ID()) -//// continue; -//// aPoint = std::dynamic_pointer_cast( -//// aFeat->attribute(SketchPlugin_Point::COORD_ID())); -//// } else -//// aPoint = std::dynamic_pointer_cast(aRefAttr->attr()); -//// -//// if (!aPoint || (aPoint->textX().empty() && aPoint->textY().empty())) -//// continue; -//// -//// std::map::iterator aFound = -//// myParametricConstraints.find(aPoint); -//// if (aFound == myParametricConstraints.end()) { -//// SolverConstraintPtr aConstraint = aBuilder->createParametricConstraint(aPoint); -//// if (!aConstraint) -//// continue; -//// aConstraint->setGroup(this); -//// aConstraint->setStorage(myStorage); -//// myParametricConstraints[aPoint] = aConstraint; -//// } else -//// aFound->second->update(); -//// } - + // constraint is created/updated => reset stack of "multi" constraints updates + myMultiConstraintUpdateStack = 0; return true; } - -void SketchSolver_Group::updateConstraints() -{ - std::set aPostponed; // postponed constraints Multi-Rotation and Multi-Translation - - ConstraintConstraintMap::iterator anIt = myConstraints.begin(); - for (; anIt != myConstraints.end(); ++anIt) { - if (myChangedConstraints.find(anIt->first) == myChangedConstraints.end()) - continue; - if (anIt->first->getKind() == SketchPlugin_MultiRotation::ID() || - anIt->first->getKind() == SketchPlugin_MultiTranslation::ID()) - aPostponed.insert(anIt->second); - else - anIt->second->update(); - } - - // Update postponed constraints - std::set::iterator aSCIter = aPostponed.begin(); - for (; aSCIter != aPostponed.end(); ++aSCIter) - (*aSCIter)->update(); - - myChangedConstraints.clear(); -} - bool SketchSolver_Group::updateFeature(FeaturePtr theFeature) { - if (!checkFeatureValidity(theFeature)) - return false; - - bool isUpdated = myStorage->update(theFeature); - -//// // Search attributes of the feature in the set of parametric constraints and update them -//// std::list anAttrList = -//// theFeature->data()->attributes(GeomDataAPI_Point2D::typeId()); -//// std::list::iterator anAttrIt = anAttrList.begin(); -//// for (; anAttrIt != anAttrList.end(); ++anAttrIt) { -//// std::map::iterator aFound = -//// myParametricConstraints.find(*anAttrIt); -//// if (aFound != myParametricConstraints.end()) -//// aFound->second->update(); -//// else { -//// std::shared_ptr aPoint = -//// std::dynamic_pointer_cast(*anAttrIt); -//// if (aPoint && (!aPoint->textX().empty() || !aPoint->textY().empty())) { -//// // Create new parametric constraint -//// SolverConstraintPtr aConstraint = -//// SketchSolver_Builder::getInstance()->createParametricConstraint(*anAttrIt); -//// if (!aConstraint) -//// continue; -//// aConstraint->setGroup(this); -//// aConstraint->setStorage(myStorage); -//// myParametricConstraints[*anAttrIt] = aConstraint; -//// } -//// } -//// } - return isUpdated; + return myStorage->update(theFeature); } -void SketchSolver_Group::moveFeature(FeaturePtr theFeature) +template +static SolverConstraintPtr move(StoragePtr theStorage, + SolverPtr theSketchSolver, + int theSketchDOF, + bool theEventsBlocked, + Type theFeatureOrPoint, + const std::shared_ptr& theFrom, + const std::shared_ptr& theTo) { - BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder(); - - // Firstly, revert changes in the fixed entities - myStorage->refresh(true); + bool isEntityExists = (theStorage->entity(theFeatureOrPoint).get() != 0); + if (theSketchDOF == 0 && isEntityExists) { + // avoid moving elements of fully constrained sketch + theStorage->refresh(); + return SolverConstraintPtr(); + } - // Secondly, search attributes of the feature in the list of the Multi constraints and update them - ConstraintConstraintMap::iterator aCIt = myConstraints.begin(); - for (; aCIt != myConstraints.end(); ++aCIt) { - if ((aCIt->second->getType() == CONSTRAINT_MULTI_ROTATION || - aCIt->second->getType() == CONSTRAINT_MULTI_TRANSLATION) - && aCIt->second->isUsed(theFeature)) - std::dynamic_pointer_cast(aCIt->second)->update(true); + // Create temporary Fixed constraint + std::shared_ptr aConstraint = + PlaneGCSSolver_Tools::createMovementConstraint(theFeatureOrPoint); + if (aConstraint) { + SolverConstraintPtr(aConstraint)->process(theStorage, theEventsBlocked); + if (aConstraint->error().empty()) { + aConstraint->startPoint(theFrom); + theSketchSolver->initialize(); + aConstraint->moveTo(theTo); + theStorage->setNeedToResolve(true); + } else + theStorage->notify(aConstraint->movedFeature()); } - // Then, create temporary rigid constraint - SolverConstraintPtr aConstraint = aBuilder->createMovementConstraint(theFeature); - if (!aConstraint) - return; - aConstraint->process(myStorage, getId(), getWorkplaneId()); - if (aConstraint->error().empty()) - setTemporary(aConstraint); + return aConstraint; } -// ============================================================================ -// Function: addWorkplane -// Class: SketchSolver_Group -// Purpose: create workplane for the group -// ============================================================================ -bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch) +bool SketchSolver_Group::moveFeature(FeaturePtr theFeature, + const std::shared_ptr& theFrom, + const std::shared_ptr& theTo) { - if (myWorkplaneID != EID_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID()) - return false; // the workplane already exists or the function parameter is not Sketch - - mySketch = theSketch; - if (!updateWorkplane()) { - mySketch = CompositeFeaturePtr(); - return false; - } + SolverConstraintPtr aConstraint = + move(myStorage, mySketchSolver, myDOF, myIsEventsBlocked, theFeature, theFrom, theTo); + setTemporary(aConstraint); return true; } -// ============================================================================ -// Function: updateWorkplane -// Class: SketchSolver_Group -// Purpose: update parameters of workplane -// ============================================================================ -bool SketchSolver_Group::updateWorkplane() +bool SketchSolver_Group::movePoint(AttributePtr theAttribute, + const std::shared_ptr& theFrom, + const std::shared_ptr& theTo) { - BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder(); - if (!myStorage) // Create storage if not exists - myStorage = aBuilder->createStorage(getId()); - - // sketch should be unchanged, set it out of current group - bool isUpdated = myStorage->update(FeaturePtr(mySketch), GID_OUTOFGROUP); - if (isUpdated) { - EntityWrapperPtr anEntity = myStorage->entity(FeaturePtr(mySketch)); - myWorkplaneID = anEntity->id(); - } - return isUpdated; + SolverConstraintPtr aConstraint = + move(myStorage, mySketchSolver, myDOF, myIsEventsBlocked, theAttribute, theFrom, theTo); + setTemporary(aConstraint); + return true; } // ============================================================================ @@ -404,214 +189,146 @@ bool SketchSolver_Group::updateWorkplane() // ============================================================================ bool SketchSolver_Group::resolveConstraints() { - if (!myChangedConstraints.empty()) - updateConstraints(); + static const int MAX_STACK_SIZE = 5; + // check the "Multi" constraints do not drop sketch into infinite loop + if (myMultiConstraintUpdateStack > MAX_STACK_SIZE) { + myMultiConstraintUpdateStack = 0; + myPrevResult = PlaneGCSSolver_Solver::STATUS_FAILED; + // generate error message due to loop update of the sketch + getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR()) + ->setValue(SketchSolver_Error::INFINITE_LOOP()); + sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints); + return false; + } bool aResolved = false; - bool isGroupEmpty = isEmpty(); - if (myStorage->isNeedToResolve() && !isGroupEmpty) { - if (!mySketchSolver) - mySketchSolver = SketchSolver_Manager::instance()->builder()->createSolver(); - - mySketchSolver->setGroup(myID); - mySketchSolver->calculateFailedConstraints(false); - myStorage->initializeSolver(mySketchSolver); + bool isGroupEmpty = isEmpty() && myStorage->isEmpty(); + if (myStorage->isNeedToResolve() && + (!isGroupEmpty || !myConflictingConstraints.empty() || + myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED)) { - SketchSolver_SolveStatus aResult = STATUS_OK; + PlaneGCSSolver_Solver::SolveStatus aResult = PlaneGCSSolver_Solver::STATUS_OK; try { - if (myStorage->hasDuplicatedConstraint()) - aResult = STATUS_INCONSISTENT; - else { - // To avoid overconstraint situation, we will remove temporary constraints one-by-one - // and try to find the case without overconstraint - bool isLastChance = false; - size_t aNbTemp = myStorage->nbTemporary(); - while (true) { - aResult = mySketchSolver->solve(); - if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET || isLastChance) - break; - if (aNbTemp == 0) { - // try to update parameters and resolve once again - ConstraintConstraintMap::iterator aConstrIt = myConstraints.begin(); - for (; aConstrIt != myConstraints.end(); ++aConstrIt) - aConstrIt->second->update(); - isLastChance = true; - } else - aNbTemp = myStorage->removeTemporary(); - mySketchSolver->calculateFailedConstraints(true); // something failed => need to find it - myStorage->initializeSolver(mySketchSolver); - } + if (!isGroupEmpty) + aResult = mySketchSolver->solve(); + if (aResult == PlaneGCSSolver_Solver::STATUS_FAILED && + !myTempConstraints.empty()) { + mySketchSolver->undo(); + removeTemporaryConstraints(); + aResult = mySketchSolver->solve(); } } catch (...) { -// Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this); - getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::SOLVESPACE_CRASH()); - if (myPrevSolved) { + getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR()) + ->setValue(SketchSolver_Error::SOLVESPACE_CRASH()); + if (myPrevResult == PlaneGCSSolver_Solver::STATUS_OK || + myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) { // the error message should be changed before sending the message sendMessage(EVENT_SOLVER_FAILED); - myPrevSolved = false; + myPrevResult = PlaneGCSSolver_Solver::STATUS_FAILED; } + mySketchSolver->undo(); return false; } - if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET) { // solution succeeded, store results into correspondent attributes + // solution succeeded, store results into correspondent attributes + if (aResult == PlaneGCSSolver_Solver::STATUS_OK || + aResult == PlaneGCSSolver_Solver::STATUS_EMPTYSET) { + myStorage->setNeedToResolve(false); myStorage->refresh(); -//// myFeatureStorage->blockEvents(true); -//// // First refresh parametric constraints to satisfy parameters -//// std::map::iterator aParIter = myParametricConstraints.begin(); -//// for (; aParIter != myParametricConstraints.end(); ++aParIter) -//// aParIter->second->refresh(); -//// // Update all other constraints -//// ConstraintConstraintMap::iterator aConstrIter = myConstraints.begin(); -//// for (; aConstrIter != myConstraints.end(); ++aConstrIter) -//// aConstrIter->second->refresh(); -//// myFeatureStorage->blockEvents(false); - if (!myPrevSolved) { - getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(""); - // the error message should be changed before sending the message - sendMessage(EVENT_SOLVER_REPAIRED); - myPrevSolved = true; + + // additional check that copied entities used in Mirror and other "Multi" constraints + // is not connected with their originals by constraints. + myMultiConstraintUpdateStack += 1; + aResolved = true; + if (myStorage->isNeedToResolve()) + aResolved = resolveConstraints(); + + if (aResolved) { + myMultiConstraintUpdateStack -= 1; + + if (myPrevResult != PlaneGCSSolver_Solver::STATUS_OK || + myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) { + getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(""); + std::set aConflicting = myConflictingConstraints; + myConflictingConstraints.clear(); + myPrevResult = PlaneGCSSolver_Solver::STATUS_OK; + // the error message should be changed before sending the message + sendMessage(EVENT_SOLVER_REPAIRED, aConflicting); + } } - } else if (!myConstraints.empty()) { -// Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this); - getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::CONSTRAINTS()); - if (myPrevSolved) { + + // show degrees of freedom + computeDoF(); + } else { + mySketchSolver->undo(); + if (!myConstraints.empty()) { // the error message should be changed before sending the message - sendMessage(EVENT_SOLVER_FAILED); - myPrevSolved = false; + getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR()) + ->setValue(SketchSolver_Error::CONSTRAINTS()); + if (myPrevResult != aResult || + myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN || + myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED) { + // Obtain list of conflicting constraints + std::set aConflicting = myStorage->getConflictingConstraints(mySketchSolver); + + if (!myConflictingConstraints.empty()) { + std::set::iterator anIt = aConflicting.begin(); + for (; anIt != aConflicting.end(); ++anIt) + myConflictingConstraints.erase(*anIt); + if (!myConflictingConstraints.empty()) { + // some constraints does not conflict, send corresponding message + sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints); + } + } + myConflictingConstraints = aConflicting; + if (!myConflictingConstraints.empty()) + sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints); + myPrevResult = aResult; + } } } - aResolved = true; - } else if (!isGroupEmpty) { -//// myFeatureStorage->blockEvents(true); - // Check there are constraints Fixed. If they exist, update parameters by stored values - ConstraintConstraintMap::iterator aCIt = myConstraints.begin(); - for (; aCIt != myConstraints.end(); ++aCIt) - if (aCIt->first->getKind() == SketchPlugin_ConstraintRigid::ID()) { -//// aCIt->second->refresh(); - aResolved = true; - break; - } -//// myFeatureStorage->blockEvents(false); - if (aCIt != myConstraints.end()) - myStorage->refresh(); - } + } else if (isGroupEmpty && isWorkplaneValid()) + computeDoF(); removeTemporaryConstraints(); myStorage->setNeedToResolve(false); return aResolved; } // ============================================================================ -// Function: mergeGroups +// Function: computeDoF // Class: SketchSolver_Group -// Purpose: append specified group to the current group +// Purpose: compute DoF of the sketch and set corresponding field // ============================================================================ -void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup) +void SketchSolver_Group::computeDoF() { - // If specified group is empty, no need to merge - if (theGroup.isEmpty()) - return; - - std::set aConstraints; - ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin(); - for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++) - aConstraints.insert(aConstrIter->first); - - std::list aSortedConstraints = selectApplicableFeatures(aConstraints); - std::list::iterator aSCIter = aSortedConstraints.begin(); - for (; aSCIter != aSortedConstraints.end(); ++aSCIter) { - ConstraintPtr aConstr = std::dynamic_pointer_cast(*aSCIter); - if (!aConstr) - continue; - changeConstraint(aConstr); - } -} - -// ============================================================================ -// Function: splitGroup -// Class: SketchSolver_Group -// Purpose: divide the group into several subgroups -// ============================================================================ -void SketchSolver_Group::splitGroup(std::list& theCuts) -{ - // New storage will be used in trimmed way to store the list of constraint interacted together. - StoragePtr aNewStorage = SketchSolver_Manager::instance()->builder()->createStorage(getId()); - std::list aDummyVec; // empty vector to avoid creation of solver's constraints - - // Obtain constraints, which should be separated - std::list anUnusedConstraints; - ConstraintConstraintMap::iterator aCIter = myConstraints.begin(); - for ( ; aCIter != myConstraints.end(); aCIter++) { - if (aNewStorage->isInteract(FeaturePtr(aCIter->first))) - aNewStorage->addConstraint(aCIter->first, aDummyVec); - else - anUnusedConstraints.push_back(aCIter->first); - } + std::ostringstream aDoFMsg; + int aDoF = mySketchSolver->dof(); + /// "DoF = 0" content of string value is used in PartSet by Sketch edit + /// If it is changed, it should be corrected also there + if (aDoF == 0) + aDoFMsg << "Sketch is fully fixed (DoF = 0)"; + else + aDoFMsg << "DoF (degrees of freedom) = " << aDoF; + mySketch->string(SketchPlugin_Sketch::SOLVER_DOF())->setValue(aDoFMsg.str()); - // Check the unused constraints once again, because they may become interacted with new storage since adding constraints - std::list::iterator aUnuseIt = anUnusedConstraints.begin(); - while (aUnuseIt != anUnusedConstraints.end()) { - if (aNewStorage->isInteract(FeaturePtr(*aUnuseIt))) { - aNewStorage->addConstraint(*aUnuseIt, aDummyVec); - anUnusedConstraints.erase(aUnuseIt); - aUnuseIt = anUnusedConstraints.begin(); - continue; - } - aUnuseIt++; - } + if (aDoF > 0 && myDOF == 0) + sendMessage(EVENT_SKETCH_UNDER_CONSTRAINED, mySketch, aDoF); + else if (aDoF == 0 && myDOF > 0) + sendMessage(EVENT_SKETCH_FULLY_CONSTRAINED, mySketch, aDoF); + else if (aDoF < 0) + sendMessage(EVENT_SKETCH_OVER_CONSTRAINED, mySketch, aDoF); - std::list::iterator aCutsIter; - aUnuseIt = anUnusedConstraints.begin(); - for ( ; aUnuseIt != anUnusedConstraints.end(); ++aUnuseIt) { - // Remove unused constraints - removeConstraint(*aUnuseIt); - // Try to append constraint to already existent group - for (aCutsIter = theCuts.begin(); aCutsIter != theCuts.end(); ++aCutsIter) - if ((*aCutsIter)->isInteract(*aUnuseIt)) { - (*aCutsIter)->changeConstraint(*aUnuseIt); - break; - } - if (aCutsIter == theCuts.end()) { - // Add new group - SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch); - aGroup->changeConstraint(*aUnuseIt); - theCuts.push_back(aGroup); - } else { - // Find other groups interacting with constraint - std::list::iterator aBaseGroupIt = aCutsIter; - for (++aCutsIter; aCutsIter != theCuts.end(); ++aCutsIter) - if ((*aCutsIter)->isInteract(*aUnuseIt)) { - (*aBaseGroupIt)->mergeGroups(**aCutsIter); - std::list::iterator aRemoveIt = aCutsIter--; - theCuts.erase(aRemoveIt); - } - } - } + myDOF = aDoF; } // ============================================================================ -// Function: isConsistent +// Function: repairConsistency // Class: SketchSolver_Group // Purpose: search removed entities and constraints // ============================================================================ -bool SketchSolver_Group::isConsistent() +void SketchSolver_Group::repairConsistency() { - if (isEmpty()) // no one constraint is initialized yet - return true; - - // Check the features and constraint is the storage are valid - bool aResult = myStorage->isConsistent(); - if (aResult) { - // additional check of consistency of the Fixed constraint, - // because they are not added to the storage - ConstraintConstraintMap::iterator aCIter = myConstraints.begin(); - for (; aCIter != myConstraints.end(); ++aCIter) - if (aCIter->first->getKind() == SketchPlugin_ConstraintRigid::ID() && - (!aCIter->first->data() || !aCIter->first->data()->isValid())) { - aResult = false; - break; - } - } - if (!aResult) { + if (!areConstraintsValid() || !myStorage->areFeaturesValid()) { // remove invalid constraints std::set anInvalidConstraints; ConstraintConstraintMap::iterator aCIter = myConstraints.begin(); @@ -622,10 +339,13 @@ bool SketchSolver_Group::isConsistent() std::set::const_iterator aRemoveIt = anInvalidConstraints.begin(); for (; aRemoveIt != anInvalidConstraints.end(); ++aRemoveIt) removeConstraint(*aRemoveIt); + // remove invalid features myStorage->removeInvalidEntities(); + + // show DoF + computeDoF(); } - return aResult; } // ============================================================================ @@ -636,23 +356,17 @@ bool SketchSolver_Group::isConsistent() // ============================================================================ void SketchSolver_Group::removeTemporaryConstraints() { - std::set::iterator aTmpIt = myTempConstraints.begin(); - for (; aTmpIt != myTempConstraints.end(); ++aTmpIt) - (*aTmpIt)->remove(); - - size_t aNbTemp = myStorage->nbTemporary(); - if (aNbTemp > 0) - myStorage->removeTemporary(aNbTemp); -//// // Clean lists of removed entities in the storage -//// std::set aRemPar; -//// std::set aRemEnt; -//// std::set aRemCon; -//// myStorage->getRemoved(aRemPar, aRemEnt, aRemCon); - - if (!myTempConstraints.empty()) - myStorage->verifyFixed(); + if (!myTempConstraints.empty()) { + mySketchSolver->removeConstraint(CID_MOVEMENT); + + std::set::iterator aTmpIt = myTempConstraints.begin(); + for (; aTmpIt != myTempConstraints.end(); ++aTmpIt) + (*aTmpIt)->remove(); + + myTempConstraints.clear(); + } + myStorage->setNeedToResolve(false); - myTempConstraints.clear(); } // ============================================================================ @@ -662,57 +376,17 @@ void SketchSolver_Group::removeTemporaryConstraints() // ============================================================================ void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint) { - bool isFullyRemoved = true; ConstraintConstraintMap::iterator aCIter = myConstraints.begin(); for (; aCIter != myConstraints.end(); aCIter++) if (aCIter->first == theConstraint) { - if (!aCIter->second->remove()) // the constraint is not fully removed - isFullyRemoved = false; + aCIter->second->remove(); // the constraint is not fully removed + + // constraint is removed => reset stack of "multi" constraints updates + myMultiConstraintUpdateStack = 0; break; } - if (aCIter == myConstraints.end()) - return; - -//// // Remove entities not used by constraints -//// myStorage->removeUnusedEntities(); - - if (isFullyRemoved) + if (aCIter != myConstraints.end()) myConstraints.erase(aCIter); - else if (aCIter != myConstraints.end() && - aCIter->first->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { - // Update multicoincidence - std::list aMultiCoinc; - SolverConstraintPtr aCoincidence = aCIter->second; - while (aCIter != myConstraints.end()) { - if (aCIter->second != aCoincidence) { - ++aCIter; - continue; - } - if (aCIter->first != theConstraint) - aMultiCoinc.push_back(aCIter->first); - aCIter->second->remove(); - ConstraintConstraintMap::iterator aRemoveIt = aCIter++; - myConstraints.erase(aRemoveIt); - } - - std::list::iterator anIt = aMultiCoinc.begin(); - for (; anIt != aMultiCoinc.end(); ++anIt) - changeConstraint(*anIt); - - notifyMultiConstraints(); - } -} - -// ============================================================================ -// Function: isComplexConstraint -// Class: SketchSolver_Group -// Purpose: verifies the constraint is complex, i.e. it needs another constraints to be created before -// ============================================================================ -bool SketchSolver_Group::isComplexConstraint(FeaturePtr theConstraint) -{ - return theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID() || - theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID() || - theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID(); } // ============================================================================ @@ -722,122 +396,37 @@ bool SketchSolver_Group::isComplexConstraint(FeaturePtr theConstraint) // ============================================================================ void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint) { - theConstraint->makeTemporary(); - myTempConstraints.insert(theConstraint); + if (theConstraint) + myTempConstraints.insert(theConstraint); } - // ============================================================================ -// Function: checkFeatureValidity +// Function: blockEvents // Class: SketchSolver_Group -// Purpose: verifies is the feature valid +// Purpose: block or unblock events from features in this group // ============================================================================ -bool SketchSolver_Group::checkFeatureValidity(FeaturePtr theFeature) +void SketchSolver_Group::blockEvents(bool isBlocked) { - if (!theFeature || !theFeature->data()->isValid()) - return true; - - SessionPtr aMgr = ModelAPI_Session::get(); - ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); - return aFactory->validate(theFeature); -} - -// ============================================================================ -// Function: notifyMultiConstraints -// Class: SketchSolver_Group -// Purpose: Update Multi-Translation/-Rotation constraints due to multi coincidence appears/disappears -// ============================================================================ -void SketchSolver_Group::notifyMultiConstraints() -{ -//// ConstraintConstraintMap::iterator aCIter = myConstraints.begin(); -//// for (; aCIter != myConstraints.end(); ++aCIter) { -//// if (aCIter->first->getKind() == SketchPlugin_MultiRotation::ID() || -//// aCIter->first->getKind() == SketchPlugin_MultiTranslation::ID()) { -//// std::shared_ptr aMulti = -//// std::dynamic_pointer_cast(aCIter->second); -//// aMulti->checkCoincidence(); -//// } -//// } -} - - + if (myIsEventsBlocked == isBlocked) + return; + // block/unblock events from the features in the storage + myStorage->blockEvents(isBlocked); -// =========== Auxiliary functions ======================================== -static double featureToVal(FeaturePtr theFeature) -{ - if (theFeature->getKind() == SketchPlugin_Sketch::ID()) - return 0.0; // sketch - ConstraintPtr aConstraint = std::dynamic_pointer_cast(theFeature); - if (!aConstraint) - return 1.0; // features (arc, circle, line, point) - - const std::string& anID = aConstraint->getKind(); - if (anID == SketchPlugin_ConstraintCoincidence::ID()) { - AttributeRefAttrPtr anAttrA = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - AttributeRefAttrPtr anAttrB = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); - if (anAttrA && anAttrB && (anAttrA->isObject() || anAttrB->isObject())) - return 2.0; // point-on-line and point-on-circle should go before points coincidence constraint - return 2.5; - } - if (anID == SketchPlugin_ConstraintDistance::ID() || - anID == SketchPlugin_ConstraintLength::ID() || - anID == SketchPlugin_ConstraintRadius::ID()) - return 3.0; - if (anID == SketchPlugin_ConstraintAngle::ID()) - return 3.5; - if (anID == SketchPlugin_ConstraintHorizontal::ID() || - anID == SketchPlugin_ConstraintVertical::ID() || - anID == SketchPlugin_ConstraintParallel::ID() || - anID == SketchPlugin_ConstraintPerpendicular::ID()) - return 4.0; - if (anID == SketchPlugin_ConstraintEqual::ID()) - return 5.0; - if (anID == SketchPlugin_ConstraintTangent::ID() || - anID == SketchPlugin_ConstraintMirror::ID()) - return 6.0; - if (anID == SketchPlugin_ConstraintRigid::ID()) - return 7.0; - if (anID == SketchPlugin_MultiRotation::ID() || - anID == SketchPlugin_MultiTranslation::ID()) - return 8.0; - - // all other constraints are placed between Equal and Tangent constraints - return 5.5; -} + // block/unblock events from constraints + ConstraintConstraintMap::iterator aCIt = myConstraints.begin(); + for (; aCIt != myConstraints.end(); ++aCIt) + aCIt->second->blockEvents(isBlocked); -static bool isLess(FeaturePtr theFeature1, FeaturePtr theFeature2) -{ - return featureToVal(theFeature1) < featureToVal(theFeature2); + myIsEventsBlocked = isBlocked; } -std::list SketchSolver_Group::selectApplicableFeatures(const std::set& theObjects) +bool SketchSolver_Group::areConstraintsValid() const { - std::list aResult; - std::list::iterator aResIt; - - std::set::const_iterator anObjIter = theObjects.begin(); - for (; anObjIter != theObjects.end(); ++anObjIter) { - // Operate sketch itself and SketchPlugin features only. - // Also, the Fillet need to be skipped, because there are several separated constraints composing it. - FeaturePtr aFeature = std::dynamic_pointer_cast(*anObjIter); - if (!aFeature) - continue; - std::shared_ptr aSketchFeature = - std::dynamic_pointer_cast(aFeature); - if ((aFeature->getKind() != SketchPlugin_Sketch::ID() && !aSketchFeature) || - aFeature->getKind() == SketchPlugin_ConstraintFillet::ID()) - continue; - - // Find the place where to insert a feature - for (aResIt = aResult.begin(); aResIt != aResult.end(); ++aResIt) - if (isLess(aFeature, *aResIt)) - break; - aResult.insert(aResIt, aFeature); - } - - return aResult; + // Check the constraints are valid + ConstraintConstraintMap::const_iterator aCIter = myConstraints.begin(); + for (; aCIter != myConstraints.end(); ++aCIter) + if (!aCIter->first->data() || !aCIter->first->data()->isValid()) + return false; + return true; } -