1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Group.cpp
4 // Created: 27 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchSolver_Group.h"
8 #include <SketchSolver_Error.h>
9 #include <SketchSolver_Manager.h>
11 #include <PlaneGCSSolver_Solver.h>
12 #include <PlaneGCSSolver_Storage.h>
13 #include <PlaneGCSSolver_Tools.h>
15 #include <Events_InfoMessage.h>
16 #include <ModelAPI_AttributeString.h>
17 #include <ModelAPI_Events.h>
18 #include <SketchPlugin_ConstraintMirror.h>
19 #include <SketchPlugin_ConstraintRigid.h>
20 #include <SketchPlugin_MultiRotation.h>
21 #include <SketchPlugin_MultiTranslation.h>
24 static void sendMessage(const char* theMessageName)
26 std::shared_ptr<Events_Message> aMessage = std::shared_ptr<Events_Message>(
27 new Events_Message(Events_Loop::eventByName(theMessageName)));
28 Events_Loop::loop()->send(aMessage);
31 static void sendMessage(const char* theMessageName, const std::set<ObjectPtr>& theConflicting)
33 std::shared_ptr<ModelAPI_SolverFailedMessage> aMessage =
34 std::shared_ptr<ModelAPI_SolverFailedMessage>(
35 new ModelAPI_SolverFailedMessage(Events_Loop::eventByName(theMessageName)));
36 aMessage->setObjects(theConflicting);
37 Events_Loop::loop()->send(aMessage);
40 static void sendMessage(const char* theMessageName,
41 const CompositeFeaturePtr& theSketch,
44 std::shared_ptr<ModelAPI_SolverFailedMessage> aMessage =
45 std::shared_ptr<ModelAPI_SolverFailedMessage>(
46 new ModelAPI_SolverFailedMessage(Events_Loop::eventByName(theMessageName)));
48 std::set<ObjectPtr> anObjects;
49 anObjects.insert(theSketch);
50 aMessage->setObjects(anObjects);
51 aMessage->dof(theDOF);
53 Events_Loop::loop()->send(aMessage);
58 // ========================================================
59 // ========= SketchSolver_Group ===============
60 // ========================================================
62 SketchSolver_Group::SketchSolver_Group(const CompositeFeaturePtr& theWorkplane)
63 : mySketch(theWorkplane),
64 myPrevResult(PlaneGCSSolver_Solver::STATUS_UNKNOWN),
66 myIsEventsBlocked(false),
67 myMultiConstraintUpdateStack(0)
69 mySketchSolver = SolverPtr(new PlaneGCSSolver_Solver);
70 myStorage = StoragePtr(new PlaneGCSSolver_Storage(mySketchSolver));
73 SketchSolver_Group::~SketchSolver_Group()
75 myConstraints.clear();
76 // send the message that there is no more conflicting constraints
77 if (!myConflictingConstraints.empty()) {
78 sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints);
79 myConflictingConstraints.clear();
83 // ============================================================================
84 // Function: changeConstraint
85 // Class: SketchSolver_Group
86 // Purpose: create/update the constraint in the group
87 // ============================================================================
88 bool SketchSolver_Group::changeConstraint(
89 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
91 bool isNewConstraint = myConstraints.find(theConstraint) == myConstraints.end();
92 if (isNewConstraint) {
93 // Add constraint to the current group
94 SolverConstraintPtr aConstraint = PlaneGCSSolver_Tools::createConstraint(theConstraint);
97 aConstraint->process(myStorage, myIsEventsBlocked);
98 if (!aConstraint->error().empty()) {
99 if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
100 return false; // some attribute are not initialized yet, don't show message
101 Events_InfoMessage("SketchSolver_Group", aConstraint->error(), this).send();
103 myConstraints[theConstraint] = aConstraint;
106 myConstraints[theConstraint]->update();
108 // constraint is created/updated => reset stack of "multi" constraints updates
109 myMultiConstraintUpdateStack = 0;
113 bool SketchSolver_Group::updateFeature(FeaturePtr theFeature)
115 return myStorage->update(theFeature);
118 template <class Type>
119 static SolverConstraintPtr move(StoragePtr theStorage,
120 SolverPtr theSketchSolver,
122 bool theEventsBlocked,
123 Type theFeatureOrPoint,
124 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
125 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
127 bool isEntityExists = (theStorage->entity(theFeatureOrPoint).get() != 0);
128 if (theSketchDOF == 0 && isEntityExists) {
129 // avoid moving elements of fully constrained sketch
130 theStorage->refresh();
131 return SolverConstraintPtr();
134 // Create temporary Fixed constraint
135 std::shared_ptr<SketchSolver_ConstraintMovement> aConstraint =
136 PlaneGCSSolver_Tools::createMovementConstraint(theFeatureOrPoint);
138 SolverConstraintPtr(aConstraint)->process(theStorage, theEventsBlocked);
139 if (aConstraint->error().empty()) {
140 if (!theStorage->isEmpty())
141 theStorage->setNeedToResolve(true);
143 theSketchSolver->initialize();
144 aConstraint->startPoint(theFrom);
145 aConstraint->moveTo(theTo);
147 theStorage->notify(aConstraint->movedFeature());
153 bool SketchSolver_Group::moveFeature(FeaturePtr theFeature,
154 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
155 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
157 SolverConstraintPtr aConstraint =
158 move(myStorage, mySketchSolver, myDOF, myIsEventsBlocked, theFeature, theFrom, theTo);
159 setTemporary(aConstraint);
163 bool SketchSolver_Group::movePoint(AttributePtr theAttribute,
164 const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
165 const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
167 SolverConstraintPtr aConstraint =
168 move(myStorage, mySketchSolver, myDOF, myIsEventsBlocked, theAttribute, theFrom, theTo);
169 setTemporary(aConstraint);
173 // ============================================================================
174 // Function: resolveConstraints
175 // Class: SketchSolver_Group
176 // Purpose: solve the set of constraints for the current group
177 // ============================================================================
178 bool SketchSolver_Group::resolveConstraints()
180 static const int MAX_STACK_SIZE = 5;
181 // check the "Multi" constraints do not drop sketch into infinite loop
182 if (myMultiConstraintUpdateStack > MAX_STACK_SIZE) {
183 myMultiConstraintUpdateStack = 0;
184 myPrevResult = PlaneGCSSolver_Solver::STATUS_FAILED;
185 // generate error message due to loop update of the sketch
186 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
187 ->setValue(SketchSolver_Error::INFINITE_LOOP());
188 sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints);
192 bool aResolved = false;
193 bool isGroupEmpty = isEmpty() && myStorage->isEmpty();
194 if (myStorage->isNeedToResolve() &&
195 (!isGroupEmpty || !myConflictingConstraints.empty() ||
196 myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED)) {
198 PlaneGCSSolver_Solver::SolveStatus aResult = PlaneGCSSolver_Solver::STATUS_OK;
201 aResult = mySketchSolver->solve();
202 if (aResult == PlaneGCSSolver_Solver::STATUS_FAILED &&
203 !myTempConstraints.empty()) {
204 mySketchSolver->undo();
205 removeTemporaryConstraints();
206 aResult = mySketchSolver->solve();
209 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
210 ->setValue(SketchSolver_Error::SOLVESPACE_CRASH());
211 if (myPrevResult == PlaneGCSSolver_Solver::STATUS_OK ||
212 myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) {
213 // the error message should be changed before sending the message
214 sendMessage(EVENT_SOLVER_FAILED);
215 myPrevResult = PlaneGCSSolver_Solver::STATUS_FAILED;
217 mySketchSolver->undo();
220 // solution succeeded, store results into correspondent attributes
221 if (aResult == PlaneGCSSolver_Solver::STATUS_OK ||
222 aResult == PlaneGCSSolver_Solver::STATUS_EMPTYSET) {
223 myStorage->setNeedToResolve(false);
224 myStorage->refresh();
226 // additional check that copied entities used in Mirror and other "Multi" constraints
227 // is not connected with their originals by constraints.
228 myMultiConstraintUpdateStack += 1;
230 if (myStorage->isNeedToResolve())
231 aResolved = resolveConstraints();
234 myMultiConstraintUpdateStack -= 1;
236 if (myPrevResult != PlaneGCSSolver_Solver::STATUS_OK ||
237 myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) {
238 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
239 std::set<ObjectPtr> aConflicting = myConflictingConstraints;
240 myConflictingConstraints.clear();
241 myPrevResult = PlaneGCSSolver_Solver::STATUS_OK;
242 // the error message should be changed before sending the message
243 sendMessage(EVENT_SOLVER_REPAIRED, aConflicting);
247 // show degrees of freedom
250 mySketchSolver->undo();
251 if (!myConstraints.empty()) {
252 // the error message should be changed before sending the message
253 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
254 ->setValue(SketchSolver_Error::CONSTRAINTS());
255 if (myPrevResult != aResult ||
256 myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN ||
257 myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED) {
258 // Obtain list of conflicting constraints
259 std::set<ObjectPtr> aConflicting = myStorage->getConflictingConstraints(mySketchSolver);
261 if (!myConflictingConstraints.empty()) {
262 std::set<ObjectPtr>::iterator anIt = aConflicting.begin();
263 for (; anIt != aConflicting.end(); ++anIt)
264 myConflictingConstraints.erase(*anIt);
265 if (!myConflictingConstraints.empty()) {
266 // some constraints does not conflict, send corresponding message
267 sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints);
270 myConflictingConstraints = aConflicting;
271 if (!myConflictingConstraints.empty())
272 sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints);
273 myPrevResult = aResult;
278 } else if (isGroupEmpty && isWorkplaneValid())
280 removeTemporaryConstraints();
281 myStorage->setNeedToResolve(false);
285 // ============================================================================
286 // Function: computeDoF
287 // Class: SketchSolver_Group
288 // Purpose: compute DoF of the sketch and set corresponding field
289 // ============================================================================
290 void SketchSolver_Group::computeDoF()
292 std::ostringstream aDoFMsg;
293 int aDoF = mySketchSolver->dof();
294 /// "DoF = 0" content of string value is used in PartSet by Sketch edit
295 /// If it is changed, it should be corrected also there
297 aDoFMsg << "Sketch is fully fixed (DoF = 0)";
299 aDoFMsg << "DoF (degrees of freedom) = " << aDoF;
300 mySketch->string(SketchPlugin_Sketch::SOLVER_DOF())->setValue(aDoFMsg.str());
302 if (aDoF > 0 && myDOF == 0)
303 sendMessage(EVENT_SKETCH_UNDER_CONSTRAINED, mySketch, aDoF);
304 else if (aDoF == 0 && myDOF > 0)
305 sendMessage(EVENT_SKETCH_FULLY_CONSTRAINED, mySketch, aDoF);
307 sendMessage(EVENT_SKETCH_OVER_CONSTRAINED, mySketch, aDoF);
312 // ============================================================================
313 // Function: repairConsistency
314 // Class: SketchSolver_Group
315 // Purpose: search removed entities and constraints
316 // ============================================================================
317 void SketchSolver_Group::repairConsistency()
319 if (!areConstraintsValid() || !myStorage->areFeaturesValid()) {
320 // remove invalid constraints
321 std::set<ConstraintPtr> anInvalidConstraints;
322 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
323 for (; aCIter != myConstraints.end(); ++aCIter) {
324 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
325 anInvalidConstraints.insert(aCIter->first);
327 std::set<ConstraintPtr>::const_iterator aRemoveIt = anInvalidConstraints.begin();
328 for (; aRemoveIt != anInvalidConstraints.end(); ++aRemoveIt)
329 removeConstraint(*aRemoveIt);
331 // remove invalid features
332 myStorage->removeInvalidEntities();
339 // ============================================================================
340 // Function: removeTemporaryConstraints
341 // Class: SketchSolver_Group
342 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
343 // resolving the set of constraints
344 // ============================================================================
345 void SketchSolver_Group::removeTemporaryConstraints()
347 if (!myTempConstraints.empty()) {
348 mySketchSolver->removeConstraint(CID_MOVEMENT);
350 std::set<SolverConstraintPtr>::iterator aTmpIt = myTempConstraints.begin();
351 for (; aTmpIt != myTempConstraints.end(); ++aTmpIt)
354 myTempConstraints.clear();
357 myStorage->setNeedToResolve(false);
360 // ============================================================================
361 // Function: removeConstraint
362 // Class: SketchSolver_Group
363 // Purpose: remove constraint and all unused entities
364 // ============================================================================
365 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
367 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
368 for (; aCIter != myConstraints.end(); aCIter++)
369 if (aCIter->first == theConstraint) {
370 aCIter->second->remove(); // the constraint is not fully removed
372 // constraint is removed => reset stack of "multi" constraints updates
373 myMultiConstraintUpdateStack = 0;
376 if (aCIter != myConstraints.end())
377 myConstraints.erase(aCIter);
380 // ============================================================================
381 // Function: setTemporary
382 // Class: SketchSolver_Group
383 // Purpose: append given constraint to the group of temporary constraints
384 // ============================================================================
385 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
388 myTempConstraints.insert(theConstraint);
391 // ============================================================================
392 // Function: blockEvents
393 // Class: SketchSolver_Group
394 // Purpose: block or unblock events from features in this group
395 // ============================================================================
396 void SketchSolver_Group::blockEvents(bool isBlocked)
398 if (myIsEventsBlocked == isBlocked)
401 // block/unblock events from the features in the storage
402 myStorage->blockEvents(isBlocked);
404 // block/unblock events from constraints
405 ConstraintConstraintMap::iterator aCIt = myConstraints.begin();
406 for (; aCIt != myConstraints.end(); ++aCIt)
407 aCIt->second->blockEvents(isBlocked);
409 myIsEventsBlocked = isBlocked;
412 bool SketchSolver_Group::areConstraintsValid() const
414 // Check the constraints are valid
415 ConstraintConstraintMap::const_iterator aCIter = myConstraints.begin();
416 for (; aCIter != myConstraints.end(); ++aCIter)
417 if (!aCIter->first->data() || !aCIter->first->data()->isValid())