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 bool SketchSolver_Group::moveFeature(FeaturePtr theFeature)
120 bool isFeatureExists = (myStorage->entity(theFeature).get() != 0);
121 if (myDOF == 0 && isFeatureExists) {
122 // avoid moving elements of fully constrained sketch
123 myStorage->refresh();
127 // Create temporary Fixed constraint
128 SolverConstraintPtr aConstraint = PlaneGCSSolver_Tools::createMovementConstraint(theFeature);
131 aConstraint->process(myStorage, myIsEventsBlocked);
132 if (aConstraint->error().empty())
133 setTemporary(aConstraint);
135 myStorage->notify(theFeature);
140 // ============================================================================
141 // Function: resolveConstraints
142 // Class: SketchSolver_Group
143 // Purpose: solve the set of constraints for the current group
145 // ============================================================================
146 bool SketchSolver_Group::resolveConstraints()
148 static const int MAX_STACK_SIZE = 5;
149 // check the "Multi" constraints do not drop sketch into infinite loop
150 if (myMultiConstraintUpdateStack > MAX_STACK_SIZE) {
151 myMultiConstraintUpdateStack = 0;
152 myPrevResult = PlaneGCSSolver_Solver::STATUS_FAILED;
153 // generate error message due to loop update of the sketch
154 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
155 ->setValue(SketchSolver_Error::INFINITE_LOOP());
156 sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints);
160 bool aResolved = false;
161 bool isGroupEmpty = isEmpty() && myStorage->isEmpty();
162 if (myStorage->isNeedToResolve() &&
163 (!isGroupEmpty || !myConflictingConstraints.empty() ||
164 myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED)) {
166 PlaneGCSSolver_Solver::SolveStatus aResult = PlaneGCSSolver_Solver::STATUS_OK;
169 aResult = mySketchSolver->solve();
170 if (aResult == PlaneGCSSolver_Solver::STATUS_FAILED &&
171 !myTempConstraints.empty()) {
172 removeTemporaryConstraints();
173 aResult = mySketchSolver->solve();
176 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
177 ->setValue(SketchSolver_Error::SOLVESPACE_CRASH());
178 if (myPrevResult == PlaneGCSSolver_Solver::STATUS_OK ||
179 myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) {
180 // the error message should be changed before sending the message
181 sendMessage(EVENT_SOLVER_FAILED);
182 myPrevResult = PlaneGCSSolver_Solver::STATUS_FAILED;
184 mySketchSolver->undo();
187 // solution succeeded, store results into correspondent attributes
188 if (aResult == PlaneGCSSolver_Solver::STATUS_OK ||
189 aResult == PlaneGCSSolver_Solver::STATUS_EMPTYSET) {
190 myStorage->setNeedToResolve(false);
191 myStorage->refresh();
193 // additional check that copied entities used in Mirror and other "Multi" constraints
194 // is not connected with their originals by constraints.
195 myMultiConstraintUpdateStack += 1;
197 if (myStorage->isNeedToResolve())
198 aResolved = resolveConstraints();
201 myMultiConstraintUpdateStack -= 1;
203 if (myPrevResult != PlaneGCSSolver_Solver::STATUS_OK ||
204 myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) {
205 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
206 std::set<ObjectPtr> aConflicting = myConflictingConstraints;
207 myConflictingConstraints.clear();
208 myPrevResult = PlaneGCSSolver_Solver::STATUS_OK;
209 // the error message should be changed before sending the message
210 sendMessage(EVENT_SOLVER_REPAIRED, aConflicting);
214 // show degrees of freedom
217 mySketchSolver->undo();
218 if (!myConstraints.empty()) {
219 // the error message should be changed before sending the message
220 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
221 ->setValue(SketchSolver_Error::CONSTRAINTS());
222 if (myPrevResult != aResult ||
223 myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN ||
224 myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED) {
225 // Obtain list of conflicting constraints
226 std::set<ObjectPtr> aConflicting = myStorage->getConflictingConstraints(mySketchSolver);
228 if (!myConflictingConstraints.empty()) {
229 std::set<ObjectPtr>::iterator anIt = aConflicting.begin();
230 for (; anIt != aConflicting.end(); ++anIt)
231 myConflictingConstraints.erase(*anIt);
232 if (!myConflictingConstraints.empty()) {
233 // some constraints does not conflict, send corresponding message
234 sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints);
237 myConflictingConstraints = aConflicting;
238 if (!myConflictingConstraints.empty())
239 sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints);
240 myPrevResult = aResult;
245 } else if (isGroupEmpty && isWorkplaneValid())
247 removeTemporaryConstraints();
248 myStorage->setNeedToResolve(false);
252 // ============================================================================
253 // Function: computeDoF
254 // Class: SketchSolver_Group
255 // Purpose: compute DoF of the sketch and set corresponding field
256 // ============================================================================
257 void SketchSolver_Group::computeDoF()
259 std::ostringstream aDoFMsg;
260 int aDoF = mySketchSolver->dof();
261 /// "DoF = 0" content of string value is used in PartSet by Sketch edit
262 /// If it is changed, it should be corrected also there
264 aDoFMsg << "Sketch is fully fixed (DoF = 0)";
266 aDoFMsg << "DoF (degrees of freedom) = " << aDoF;
267 mySketch->string(SketchPlugin_Sketch::SOLVER_DOF())->setValue(aDoFMsg.str());
269 if (aDoF > 0 && myDOF == 0)
270 sendMessage(EVENT_SKETCH_UNDER_CONSTRAINED, mySketch, aDoF);
271 else if (aDoF == 0 && myDOF > 0)
272 sendMessage(EVENT_SKETCH_FULLY_CONSTRAINED, mySketch, aDoF);
274 sendMessage(EVENT_SKETCH_OVER_CONSTRAINED, mySketch, aDoF);
279 // ============================================================================
280 // Function: repairConsistency
281 // Class: SketchSolver_Group
282 // Purpose: search removed entities and constraints
283 // ============================================================================
284 void SketchSolver_Group::repairConsistency()
286 if (!areConstraintsValid() || !myStorage->areFeaturesValid()) {
287 // remove invalid constraints
288 std::set<ConstraintPtr> anInvalidConstraints;
289 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
290 for (; aCIter != myConstraints.end(); ++aCIter) {
291 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
292 anInvalidConstraints.insert(aCIter->first);
294 std::set<ConstraintPtr>::const_iterator aRemoveIt = anInvalidConstraints.begin();
295 for (; aRemoveIt != anInvalidConstraints.end(); ++aRemoveIt)
296 removeConstraint(*aRemoveIt);
298 // remove invalid features
299 myStorage->removeInvalidEntities();
306 // ============================================================================
307 // Function: removeTemporaryConstraints
308 // Class: SketchSolver_Group
309 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
310 // resolving the set of constraints
311 // ============================================================================
312 void SketchSolver_Group::removeTemporaryConstraints()
314 if (!myTempConstraints.empty()) {
315 mySketchSolver->removeConstraint(CID_MOVEMENT);
317 std::set<SolverConstraintPtr>::iterator aTmpIt = myTempConstraints.begin();
318 for (; aTmpIt != myTempConstraints.end(); ++aTmpIt)
321 myTempConstraints.clear();
324 myStorage->setNeedToResolve(false);
327 // ============================================================================
328 // Function: removeConstraint
329 // Class: SketchSolver_Group
330 // Purpose: remove constraint and all unused entities
331 // ============================================================================
332 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
334 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
335 for (; aCIter != myConstraints.end(); aCIter++)
336 if (aCIter->first == theConstraint) {
337 aCIter->second->remove(); // the constraint is not fully removed
339 // constraint is removed => reset stack of "multi" constraints updates
340 myMultiConstraintUpdateStack = 0;
343 if (aCIter != myConstraints.end())
344 myConstraints.erase(aCIter);
347 // ============================================================================
348 // Function: setTemporary
349 // Class: SketchSolver_Group
350 // Purpose: append given constraint to the group of temporary constraints
351 // ============================================================================
352 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
354 myTempConstraints.insert(theConstraint);
357 // ============================================================================
358 // Function: blockEvents
359 // Class: SketchSolver_Group
360 // Purpose: block or unblock events from features in this group
361 // ============================================================================
362 void SketchSolver_Group::blockEvents(bool isBlocked)
364 if (myIsEventsBlocked == isBlocked)
367 // block/unblock events from the features in the storage
368 myStorage->blockEvents(isBlocked);
370 // block/unblock events from constraints
371 ConstraintConstraintMap::iterator aCIt = myConstraints.begin();
372 for (; aCIt != myConstraints.end(); ++aCIt)
373 aCIt->second->blockEvents(isBlocked);
375 myIsEventsBlocked = isBlocked;
378 bool SketchSolver_Group::areConstraintsValid() const
380 // Check the constraints are valid
381 ConstraintConstraintMap::const_iterator aCIter = myConstraints.begin();
382 for (; aCIter != myConstraints.end(); ++aCIter)
383 if (!aCIter->first->data() || !aCIter->first->data()->isValid())