#include <SketchSolver_ConstraintCoincidence.h>
#include <SketchSolver_ConstraintMulti.h>
#include <SketchSolver_Error.h>
-////#include <SketchSolver_Builder.h>
#include <SketchSolver_Manager.h>
#include <Events_Error.h>
#include <Events_Loop.h>
-////#include <GeomAPI_XY.h>
-////#include <GeomAPI_Dir2d.h>
-////#include <GeomAPI_Pnt2d.h>
-////#include <GeomDataAPI_Dir.h>
-////#include <GeomDataAPI_Point.h>
-////#include <GeomDataAPI_Point2D.h>
-////#include <ModelAPI_AttributeDouble.h>
#include <ModelAPI_AttributeString.h>
-////#include <ModelAPI_Document.h>
#include <ModelAPI_Events.h>
-////#include <ModelAPI_ResultConstruction.h>
#include <ModelAPI_Session.h>
#include <ModelAPI_Validator.h>
-////
-////#include <SketchPlugin_Constraint.h>
+
#include <SketchPlugin_ConstraintAngle.h>
#include <SketchPlugin_ConstraintCoincidence.h>
#include <SketchPlugin_ConstraintDistance.h>
#include <SketchPlugin_ConstraintRigid.h>
#include <SketchPlugin_ConstraintTangent.h>
#include <SketchPlugin_ConstraintVertical.h>
-////#include <SketchPlugin_Feature.h>
#include <SketchPlugin_MultiRotation.h>
#include <SketchPlugin_MultiTranslation.h>
-////#include <SketchPlugin_Sketch.h>
-////
-////#include <SketchPlugin_Arc.h>
-////#include <SketchPlugin_Circle.h>
-////#include <SketchPlugin_Line.h>
-////#include <SketchPlugin_Point.h>
-////#include <SketchPlugin_Sketch.h>
#include <math.h>
#include <assert.h>
SketchSolver_Group::SketchSolver_Group(
std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
: myID(GroupIndexer::NEW_GROUP()),
- myPrevSolved(true)
+ myPrevResult(STATUS_UNKNOWN)
{
// Initialize workplane
myWorkplaneID = EID_UNKNOWN;
return false;
BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
+ myStorage->blockEvents(true);
bool isNewConstraint = myConstraints.find(theConstraint) == myConstraints.end();
if (isNewConstraint) {
return false; // some attribute are not initialized yet, don't show message
Events_Error::send(aConstraint->error(), this);
}
-
-//// // Additional verification of coincidence of several points
-//// if (aConstraint->getType() == CONSTRAINT_PT_PT_COINCIDENT) {
-//// bool hasMultiCoincidence = false;
-//// std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoincidence =
-//// std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aConstraint);
-////
-//// ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
-//// for (; aCIter != myConstraints.end(); ++aCIter) {
-//// if (aCIter->second->getType() != CONSTRAINT_PT_PT_COINCIDENT)
-//// continue;
-////
-//// std::shared_ptr<SketchSolver_ConstraintCoincidence> aCurCoinc =
-//// std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(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<AttributePtr> anAttrList =
-//// theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
-//// std::list<AttributePtr>::iterator anAttrIter = anAttrList.begin();
-//// for (; anAttrIter != anAttrList.end(); anAttrIter++) {
-//// AttributeRefAttrPtr aRefAttr =
-//// std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*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<ModelAPI_AttributeRefAttr>(
}
}
}
-
-//// // Check the attributes of constraint are given by parametric expression
-//// std::list<AttributePtr> anAttributes =
-//// theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
-//// std::list<AttributePtr>::iterator anAttrIt = anAttributes.begin();
-//// for (; anAttrIt != anAttributes.end(); ++anAttrIt) {
-//// AttributeRefAttrPtr aRefAttr =
-//// std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIt);
-//// if (!aRefAttr)
-//// continue;
-////
-//// std::shared_ptr<GeomDataAPI_Point2D> aPoint;
-//// if (aRefAttr->isObject()) {
-//// FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
-//// if (aFeat->getKind() != SketchPlugin_Point::ID())
-//// continue;
-//// aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-//// aFeat->attribute(SketchPlugin_Point::COORD_ID()));
-//// } else
-//// aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
-////
-//// if (!aPoint || (aPoint->textX().empty() && aPoint->textY().empty()))
-//// continue;
-////
-//// std::map<AttributePtr, SolverConstraintPtr>::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();
-//// }
-
return true;
}
-
-void SketchSolver_Group::updateConstraints()
+static void updateMultiConstraints(ConstraintConstraintMap& theConstraints, FeaturePtr theFeature)
{
- std::set<SolverConstraintPtr> 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();
+ ConstraintConstraintMap::iterator aCIt = theConstraints.begin();
+ for (; aCIt != theConstraints.end(); ++aCIt) {
+ if ((aCIt->second->getType() == CONSTRAINT_MULTI_ROTATION ||
+ aCIt->second->getType() == CONSTRAINT_MULTI_TRANSLATION)
+ && aCIt->second->isUsed(theFeature))
+ std::dynamic_pointer_cast<SketchSolver_ConstraintMulti>(aCIt->second)->update(true);
}
-
- // Update postponed constraints
- std::set<SolverConstraintPtr>::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 isBlocked = myStorage->isEventsBlocked();
+ if (!isBlocked)
+ myStorage->blockEvents(true);
+
+ myStorage->refresh(true);
bool isUpdated = myStorage->update(theFeature);
-//// // Search attributes of the feature in the set of parametric constraints and update them
-//// std::list<AttributePtr> anAttrList =
-//// theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
-//// std::list<AttributePtr>::iterator anAttrIt = anAttrList.begin();
-//// for (; anAttrIt != anAttrList.end(); ++anAttrIt) {
-//// std::map<AttributePtr, SolverConstraintPtr>::iterator aFound =
-//// myParametricConstraints.find(*anAttrIt);
-//// if (aFound != myParametricConstraints.end())
-//// aFound->second->update();
-//// else {
-//// std::shared_ptr<GeomDataAPI_Point2D> aPoint =
-//// std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*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;
-//// }
-//// }
-//// }
+ updateMultiConstraints(myConstraints, theFeature);
+
+ // events were not blocked before, the feature has not been updated,
+ // so it is necessary to revert blocking
+ if (!isUpdated && !isBlocked)
+ myStorage->blockEvents(false);
return isUpdated;
}
BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
// Firstly, revert changes in the fixed entities
+ myStorage->blockEvents(true);
myStorage->refresh(true);
- // 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<SketchSolver_ConstraintMulti>(aCIt->second)->update(true);
- }
-
- // Then, create temporary rigid constraint
+ // Then, create temporary Fixed constraint
SolverConstraintPtr aConstraint = aBuilder->createMovementConstraint(theFeature);
if (!aConstraint)
return;
aConstraint->process(myStorage, getId(), getWorkplaneId());
if (aConstraint->error().empty())
setTemporary(aConstraint);
+
+ // Secondly, search attributes of the feature in the list of the Multi constraints and update them
+ updateMultiConstraints(myConstraints, theFeature);
}
// ============================================================================
// ============================================================================
bool SketchSolver_Group::resolveConstraints()
{
- if (!myChangedConstraints.empty())
- updateConstraints();
-
bool aResolved = false;
bool isGroupEmpty = isEmpty();
if (myStorage->isNeedToResolve() && !isGroupEmpty) {
mySketchSolver->setGroup(myID);
mySketchSolver->calculateFailedConstraints(false);
myStorage->initializeSolver(mySketchSolver);
+ mySketchSolver->prepare();
SketchSolver_SolveStatus aResult = STATUS_OK;
try {
// 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();
+ // try to update parameters and resolve once again
+ ConstraintConstraintMap::iterator aConstrIt = myConstraints.begin();
+ for (; aConstrIt != myConstraints.end(); ++aConstrIt)
+ aConstrIt->second->update();
+ isLastChance = true;
+
+ removeTemporaryConstraints();
mySketchSolver->calculateFailedConstraints(true); // something failed => need to find it
myStorage->initializeSolver(mySketchSolver);
}
} catch (...) {
// Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this);
getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::SOLVESPACE_CRASH());
- if (myPrevSolved) {
+ if (myPrevResult == STATUS_OK || myPrevResult == STATUS_UNKNOWN) {
// the error message should be changed before sending the message
sendMessage(EVENT_SOLVER_FAILED);
- myPrevSolved = false;
+ myPrevResult = STATUS_FAILED;
}
+ mySketchSolver->undo();
return false;
}
if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET) { // solution succeeded, store results into correspondent attributes
myStorage->refresh();
-//// myFeatureStorage->blockEvents(true);
-//// // First refresh parametric constraints to satisfy parameters
-//// std::map<AttributePtr, SolverConstraintPtr>::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) {
+ if (myPrevResult != STATUS_OK || myPrevResult == STATUS_UNKNOWN) {
getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
// the error message should be changed before sending the message
sendMessage(EVENT_SOLVER_REPAIRED);
- myPrevSolved = true;
+ myPrevResult = STATUS_OK;
}
- } else if (!myConstraints.empty()) {
+ } else {
+ mySketchSolver->undo();
+ if (!myConstraints.empty()) {
// Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
- getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::CONSTRAINTS());
- if (myPrevSolved) {
- // 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 == STATUS_UNKNOWN) {
+ // the error message should be changed before sending the message
+ sendMessage(EVENT_SOLVER_FAILED);
+ 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();
}
removeTemporaryConstraints();
+ myStorage->blockEvents(false);
myStorage->setNeedToResolve(false);
return aResolved;
}
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<Slvs_hParam> aRemPar;
-//// std::set<Slvs_hEntity> aRemEnt;
-//// std::set<Slvs_hConstraint> aRemCon;
-//// myStorage->getRemoved(aRemPar, aRemEnt, aRemCon);
-
if (!myTempConstraints.empty())
myStorage->verifyFixed();
myStorage->setNeedToResolve(false);
// ============================================================================
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
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<ConstraintPtr> 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<ConstraintPtr>::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();
}
// ============================================================================
// ============================================================================
void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
{
- theConstraint->makeTemporary();
myTempConstraints.insert(theConstraint);
}
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<SketchSolver_ConstraintMulti> aMulti =
-//// std::dynamic_pointer_cast<SketchSolver_ConstraintMulti>(aCIter->second);
-//// aMulti->checkCoincidence();
-//// }
-//// }
-}
-