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
144 // ============================================================================
145 bool SketchSolver_Group::resolveConstraints()
147 static const int MAX_STACK_SIZE = 3;
148 // check the "Multi" constraints do not drop sketch into infinite loop
149 if (myMultiConstraintUpdateStack > MAX_STACK_SIZE) {
150 myPrevResult = PlaneGCSSolver_Solver::STATUS_FAILED;
151 // generate error message due to loop update of the sketch
152 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
153 ->setValue(SketchSolver_Error::INFINITE_LOOP());
154 sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints);
158 bool aResolved = false;
159 bool isGroupEmpty = isEmpty() && myStorage->isEmpty();
160 if (myStorage->isNeedToResolve() &&
161 (!isGroupEmpty || !myConflictingConstraints.empty() ||
162 myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED)) {
164 PlaneGCSSolver_Solver::SolveStatus aResult = PlaneGCSSolver_Solver::STATUS_OK;
166 if (!isGroupEmpty && myMultiConstraintUpdateStack <= 1)
167 aResult = mySketchSolver->solve();
169 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
170 ->setValue(SketchSolver_Error::SOLVESPACE_CRASH());
171 if (myPrevResult == PlaneGCSSolver_Solver::STATUS_OK ||
172 myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) {
173 // the error message should be changed before sending the message
174 sendMessage(EVENT_SOLVER_FAILED);
175 myPrevResult = PlaneGCSSolver_Solver::STATUS_FAILED;
177 mySketchSolver->undo();
180 // solution succeeded, store results into correspondent attributes
181 if (aResult == PlaneGCSSolver_Solver::STATUS_OK ||
182 aResult == PlaneGCSSolver_Solver::STATUS_EMPTYSET) {
183 myStorage->setNeedToResolve(false);
184 myStorage->refresh();
186 // additional check that copied entities used in Mirror and other "Multi" constraints
187 // is not connected with their originals by constraints.
188 myMultiConstraintUpdateStack += 1;
190 if (myStorage->isNeedToResolve())
191 aResolved = resolveConstraints();
194 myMultiConstraintUpdateStack -= 1;
196 if (myPrevResult != PlaneGCSSolver_Solver::STATUS_OK ||
197 myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) {
198 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
199 std::set<ObjectPtr> aConflicting = myConflictingConstraints;
200 myConflictingConstraints.clear();
201 myPrevResult = PlaneGCSSolver_Solver::STATUS_OK;
202 // the error message should be changed before sending the message
203 sendMessage(EVENT_SOLVER_REPAIRED, aConflicting);
207 // show degrees of freedom
210 mySketchSolver->undo();
211 if (!myConstraints.empty()) {
212 // the error message should be changed before sending the message
213 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
214 ->setValue(SketchSolver_Error::CONSTRAINTS());
215 if (myPrevResult != aResult ||
216 myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN ||
217 myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED) {
218 // Obtain list of conflicting constraints
219 std::set<ObjectPtr> aConflicting = myStorage->getConflictingConstraints(mySketchSolver);
221 if (!myConflictingConstraints.empty()) {
222 std::set<ObjectPtr>::iterator anIt = aConflicting.begin();
223 for (; anIt != aConflicting.end(); ++anIt)
224 myConflictingConstraints.erase(*anIt);
225 if (!myConflictingConstraints.empty()) {
226 // some constraints does not conflict, send corresponding message
227 sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints);
230 myConflictingConstraints = aConflicting;
231 if (!myConflictingConstraints.empty())
232 sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints);
233 myPrevResult = aResult;
238 } else if (isGroupEmpty && isWorkplaneValid())
240 removeTemporaryConstraints();
241 myStorage->setNeedToResolve(false);
245 // ============================================================================
246 // Function: computeDoF
247 // Class: SketchSolver_Group
248 // Purpose: compute DoF of the sketch and set corresponding field
249 // ============================================================================
250 void SketchSolver_Group::computeDoF()
252 std::ostringstream aDoFMsg;
253 int aDoF = mySketchSolver->dof();
254 /// "DoF = 0" content of string value is used in PartSet by Sketch edit
255 /// If it is changed, it should be corrected also there
257 aDoFMsg << "Sketch is fully fixed (DoF = 0)";
259 aDoFMsg << "DoF (degrees of freedom) = " << aDoF;
260 mySketch->string(SketchPlugin_Sketch::SOLVER_DOF())->setValue(aDoFMsg.str());
262 if (aDoF > 0 && myDOF == 0)
263 sendMessage(EVENT_SKETCH_UNDER_CONSTRAINED, mySketch, aDoF);
264 else if (aDoF == 0 && myDOF > 0)
265 sendMessage(EVENT_SKETCH_FULLY_CONSTRAINED, mySketch, aDoF);
267 sendMessage(EVENT_SKETCH_OVER_CONSTRAINED, mySketch, aDoF);
272 // ============================================================================
273 // Function: repairConsistency
274 // Class: SketchSolver_Group
275 // Purpose: search removed entities and constraints
276 // ============================================================================
277 void SketchSolver_Group::repairConsistency()
279 if (!areConstraintsValid() || !myStorage->areFeaturesValid()) {
280 // remove invalid constraints
281 std::set<ConstraintPtr> anInvalidConstraints;
282 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
283 for (; aCIter != myConstraints.end(); ++aCIter) {
284 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
285 anInvalidConstraints.insert(aCIter->first);
287 std::set<ConstraintPtr>::const_iterator aRemoveIt = anInvalidConstraints.begin();
288 for (; aRemoveIt != anInvalidConstraints.end(); ++aRemoveIt)
289 removeConstraint(*aRemoveIt);
291 // remove invalid features
292 myStorage->removeInvalidEntities();
299 // ============================================================================
300 // Function: removeTemporaryConstraints
301 // Class: SketchSolver_Group
302 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
303 // resolving the set of constraints
304 // ============================================================================
305 void SketchSolver_Group::removeTemporaryConstraints()
307 if (!myTempConstraints.empty()) {
308 mySketchSolver->removeConstraint(CID_MOVEMENT);
310 std::set<SolverConstraintPtr>::iterator aTmpIt = myTempConstraints.begin();
311 for (; aTmpIt != myTempConstraints.end(); ++aTmpIt)
314 myTempConstraints.clear();
317 myStorage->setNeedToResolve(false);
320 // ============================================================================
321 // Function: removeConstraint
322 // Class: SketchSolver_Group
323 // Purpose: remove constraint and all unused entities
324 // ============================================================================
325 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
327 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
328 for (; aCIter != myConstraints.end(); aCIter++)
329 if (aCIter->first == theConstraint) {
330 aCIter->second->remove(); // the constraint is not fully removed
332 // constraint is removed => reset stack of "multi" constraints updates
333 myMultiConstraintUpdateStack = 0;
336 if (aCIter != myConstraints.end())
337 myConstraints.erase(aCIter);
340 // ============================================================================
341 // Function: setTemporary
342 // Class: SketchSolver_Group
343 // Purpose: append given constraint to the group of temporary constraints
344 // ============================================================================
345 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
347 myTempConstraints.insert(theConstraint);
350 // ============================================================================
351 // Function: blockEvents
352 // Class: SketchSolver_Group
353 // Purpose: block or unblock events from features in this group
354 // ============================================================================
355 void SketchSolver_Group::blockEvents(bool isBlocked)
357 if (myIsEventsBlocked == isBlocked)
360 // block/unblock events from the features in the storage
361 myStorage->blockEvents(isBlocked);
363 // block/unblock events from constraints
364 ConstraintConstraintMap::iterator aCIt = myConstraints.begin();
365 for (; aCIt != myConstraints.end(); ++aCIt)
366 aCIt->second->blockEvents(isBlocked);
368 myIsEventsBlocked = isBlocked;
371 bool SketchSolver_Group::areConstraintsValid() const
373 // Check the constraints are valid
374 ConstraintConstraintMap::const_iterator aCIter = myConstraints.begin();
375 for (; aCIter != myConstraints.end(); ++aCIter)
376 if (!aCIter->first->data() || !aCIter->first->data()->isValid())