-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File: SketchSolver_Manager.cpp
-// Created: 08 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<mailto:webmaster.salome@opencascade.com>
+//
#include "SketchSolver_Manager.h"
#include "SketchSolver_Error.h"
#include <Events_Loop.h>
+#include <GeomDataAPI_Point2D.h>
#include <ModelAPI_Events.h>
#include <ModelAPI_ResultConstruction.h>
#include <ModelAPI_Session.h>
#include <ModelAPI_Validator.h>
+#include <SketchPlugin_Constraint.h>
#include <SketchPlugin_Sketch.h>
/// Global constraint manager object
Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_MOVED));
- ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
- ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_PREPARED));
}
void SketchSolver_Manager::processEvent(
const std::shared_ptr<Events_Message>& theMessage)
{
+ bool needToResolve = false;
+ bool isUpdateFlushed = false;
+ bool isMovedEvt = false;
+
static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
static const Events_ID aSketchPreparedEvent = Events_Loop::eventByName(EVENT_SKETCH_PREPARED);
// sketch is prepared for resolve: all the needed events
// are collected and must be processed by the solver
if (theMessage->eventID() == aSketchPreparedEvent) {
flushGrouped(anUpdateEvent);
- return;
+ needToResolve = true;
}
if (myIsComputed)
myIsComputed = true;
if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
- || theMessage->eventID() == anUpdateEvent
- || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
+ || theMessage->eventID() == anUpdateEvent) {
std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
std::set<ObjectPtr> aFeatures = anUpdateMsg->objects();
- bool isUpdateFlushed = stopSendUpdate();
-
- bool isMovedEvt = theMessage->eventID()
- == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
-
- // Shows that the message has at least one feature applicable for solver
- bool hasProperFeature = false;
+ isUpdateFlushed = stopSendUpdate();
// update sketch features only
std::set<ObjectPtr>::iterator aFeatIter;
if (!aFeature || aFeature->isMacro())
continue;
- hasProperFeature = updateFeature(aFeature, isMovedEvt) || hasProperFeature;
- }
-
- if (isMovedEvt && !hasProperFeature) {
- // in this iteration it will compute nothing, so, no problem with recursion
- // it is important that solver flushes signal updated after processing move signal as there
- // is optimization that relies on this update, might be found by key "optimization"
- myIsComputed = false;
+ updateFeature(aFeature);
}
- // Solve the set of constraints
- bool needToUpdate = resolveConstraints();
+ } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
+ std::shared_ptr<ModelAPI_ObjectMovedMessage> aMoveMsg =
+ std::dynamic_pointer_cast<ModelAPI_ObjectMovedMessage>(theMessage);
- // Features may be updated => now send events, but for all changed at once
- if (isUpdateFlushed)
- allowSendUpdate();
+ ObjectPtr aMovedObject = aMoveMsg->movedObject();
+ std::shared_ptr<GeomDataAPI_Point2D> aMovedPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMoveMsg->movedAttribute());
- myIsComputed = false;
+ const std::shared_ptr<GeomAPI_Pnt2d>& aFrom = aMoveMsg->originalPosition();
+ const std::shared_ptr<GeomAPI_Pnt2d>& aTo = aMoveMsg->currentPosition();
- // send update for movement in any case
- if (needToUpdate || isMovedEvt)
- Events_Loop::loop()->flush(anUpdateEvent);
+ if (aMovedObject) {
+ FeaturePtr aMovedFeature = ModelAPI_Feature::feature(aMovedObject);
+ std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(aMovedFeature);
+ if (aSketchFeature && !aSketchFeature->isMacro())
+ needToResolve = moveFeature(aSketchFeature, aFrom, aTo);
+ } else if (aMovedPoint)
+ needToResolve = moveAttribute(aMovedPoint, aFrom, aTo);
} else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
(*aGroupIter)->repairConsistency();
++aGroupIter;
}
-
- resolveConstraints();
}
myIsComputed = false;
}
+
+ // resolve constraints if needed
+ bool needToUpdate = needToResolve && resolveConstraints();
+ releaseFeaturesIfEventsBlocked();
+
+ // Features may be updated => now send events, but for all changed at once
+ if (isUpdateFlushed)
+ allowSendUpdate();
+
+ myIsComputed = false;
+
+ // send update for movement in any case
+ if (needToUpdate || isMovedEvt)
+ Events_Loop::loop()->flush(anUpdateEvent);
}
// ============================================================================
-// Function: changeConstraintOrEntity
-// Purpose: create/update the constraint or the feature and place it into appropriate group
+// Function: updateFeature
+// Purpose: create/update constraint or feature in appropriate group
// ============================================================================
-bool SketchSolver_Manager::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature,
- bool theMoved)
+bool SketchSolver_Manager::updateFeature(const std::shared_ptr<SketchPlugin_Feature>& theFeature)
{
// Check feature validity and find a group to place it.
// If the feature is not valid, the returned group will be empty.
bool isOk = false;
if (aConstraint)
isOk = aGroup->changeConstraint(aConstraint);
- else if (theMoved)
- isOk = aGroup->moveFeature(theFeature);
else
isOk = aGroup->updateFeature(theFeature);
return isOk;
}
+// ============================================================================
+// Function: moveFeature
+// Purpose: move given feature in appropriate group
+// ============================================================================
+bool SketchSolver_Manager::moveFeature(
+ const std::shared_ptr<SketchPlugin_Feature>& theMovedFeature,
+ const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
+ const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
+{
+ SketchGroupPtr aGroup = findGroup(theMovedFeature);
+ if (!aGroup)
+ return false;
+
+ std::shared_ptr<SketchPlugin_Constraint> aConstraint =
+ std::dynamic_pointer_cast<SketchPlugin_Constraint>(theMovedFeature);
+ if (aConstraint)
+ {
+ std::shared_ptr<GeomDataAPI_Point2D> aPntAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
+ (aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+ aPntAttr->setValue(theTo);
+ Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+ return true;
+ }
+
+ aGroup->blockEvents(true);
+ return aGroup->moveFeature(theMovedFeature, theFrom, theTo);
+}
+
+// ============================================================================
+// Function: moveAttribute
+// Purpose: move given attribute in appropriate group
+// ============================================================================
+bool SketchSolver_Manager::moveAttribute(
+ const std::shared_ptr<GeomDataAPI_Point2D>& theMovedAttribute,
+ const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
+ const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
+{
+ FeaturePtr anOwner = ModelAPI_Feature::feature(theMovedAttribute->owner());
+ std::shared_ptr<SketchPlugin_Constraint> aConstraint =
+ std::dynamic_pointer_cast<SketchPlugin_Constraint>(anOwner);
+ if (aConstraint)
+ {
+ theMovedAttribute->setValue(theTo);
+ Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+ return true;
+ }
+
+ std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(anOwner);
+ SketchGroupPtr aGroup;
+ if (aSketchFeature)
+ aGroup = findGroup(aSketchFeature);
+ if (!aGroup) {
+ theMovedAttribute->setValue(theTo);
+ return false;
+ }
+
+ aGroup->blockEvents(true);
+ return aGroup->movePoint(theMovedAttribute, theFrom, theTo);
+}
+
// ============================================================================
// Function: findGroup
// Purpose: search groups of entities interacting with given feature
for (; aGroupIter != myGroups.end(); ++aGroupIter) {
if ((*aGroupIter)->resolveConstraints())
needToUpdate = true;
- (*aGroupIter)->blockEvents(false);
}
return needToUpdate;
}
+void SketchSolver_Manager::releaseFeaturesIfEventsBlocked() const
+{
+ std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
+ for (; aGroupIter != myGroups.end(); ++aGroupIter)
+ (*aGroupIter)->blockEvents(false);
+}
+
bool SketchSolver_Manager::stopSendUpdate() const
{
static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);