]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_Group.cpp
Salome HOME
Issue #2139: Lines attached to Extern lines can be moved
[modules/shaper.git] / src / SketchSolver / SketchSolver_Group.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Group.cpp
4 // Created: 27 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchSolver_Group.h"
8 #include <SketchSolver_Error.h>
9 #include <SketchSolver_Manager.h>
10
11 #include <PlaneGCSSolver_Solver.h>
12 #include <PlaneGCSSolver_Storage.h>
13 #include <PlaneGCSSolver_Tools.h>
14
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>
22
23
24 static void sendMessage(const char* theMessageName)
25 {
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);
29 }
30
31 static void sendMessage(const char* theMessageName, const std::set<ObjectPtr>& theConflicting)
32 {
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);
38 }
39
40 static void sendMessage(const char* theMessageName,
41                         const CompositeFeaturePtr& theSketch,
42                         const int theDOF)
43 {
44   std::shared_ptr<ModelAPI_SolverFailedMessage> aMessage =
45       std::shared_ptr<ModelAPI_SolverFailedMessage>(
46       new ModelAPI_SolverFailedMessage(Events_Loop::eventByName(theMessageName)));
47
48   std::set<ObjectPtr> anObjects;
49   anObjects.insert(theSketch);
50   aMessage->setObjects(anObjects);
51   aMessage->dof(theDOF);
52
53   Events_Loop::loop()->send(aMessage);
54 }
55
56
57
58 // ========================================================
59 // =========  SketchSolver_Group  ===============
60 // ========================================================
61
62 SketchSolver_Group::SketchSolver_Group(const CompositeFeaturePtr& theWorkplane)
63   : mySketch(theWorkplane),
64     myPrevResult(PlaneGCSSolver_Solver::STATUS_UNKNOWN),
65     myDOF(0),
66     myIsEventsBlocked(false),
67     myMultiConstraintUpdateStack(0)
68 {
69   mySketchSolver = SolverPtr(new PlaneGCSSolver_Solver);
70   myStorage = StoragePtr(new PlaneGCSSolver_Storage(mySketchSolver));
71 }
72
73 SketchSolver_Group::~SketchSolver_Group()
74 {
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();
80   }
81 }
82
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)
90 {
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);
95     if (!aConstraint)
96       return false;
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();
102     }
103     myConstraints[theConstraint] = aConstraint;
104   }
105   else
106     myConstraints[theConstraint]->update();
107
108   // constraint is created/updated => reset stack of "multi" constraints updates
109   myMultiConstraintUpdateStack = 0;
110   return true;
111 }
112
113 bool SketchSolver_Group::updateFeature(FeaturePtr theFeature)
114 {
115   return myStorage->update(theFeature);
116 }
117
118 bool SketchSolver_Group::moveFeature(FeaturePtr theFeature)
119 {
120   bool isFeatureExists = (myStorage->entity(theFeature).get() != 0);
121   if (myDOF == 0 && isFeatureExists) {
122     // avoid moving elements of fully constrained sketch
123     myStorage->refresh();
124     return true;
125   }
126
127   // Create temporary Fixed constraint
128   SolverConstraintPtr aConstraint = PlaneGCSSolver_Tools::createMovementConstraint(theFeature);
129   if (!aConstraint)
130     return false;
131   aConstraint->process(myStorage, myIsEventsBlocked);
132   if (aConstraint->error().empty())
133     setTemporary(aConstraint);
134   else
135     myStorage->notify(theFeature);
136
137   return true;
138 }
139
140 // ============================================================================
141 //  Function: resolveConstraints
142 //  Class:    SketchSolver_Group
143 //  Purpose:  solve the set of constraints for the current group
144 #include <iostream>
145 // ============================================================================
146 bool SketchSolver_Group::resolveConstraints()
147 {
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);
157     return false;
158   }
159
160   bool aResolved = false;
161   bool isGroupEmpty = isEmpty() && myStorage->isEmpty();
162   if (myStorage->isNeedToResolve() &&
163       (!isGroupEmpty || !myConflictingConstraints.empty() ||
164         myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED)) {
165
166     PlaneGCSSolver_Solver::SolveStatus aResult = PlaneGCSSolver_Solver::STATUS_OK;
167     try {
168       if (!isGroupEmpty)
169         aResult = mySketchSolver->solve();
170       if (aResult == PlaneGCSSolver_Solver::STATUS_FAILED &&
171           !myTempConstraints.empty()) {
172         mySketchSolver->undo();
173         removeTemporaryConstraints();
174         aResult = mySketchSolver->solve();
175       }
176     } catch (...) {
177       getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
178         ->setValue(SketchSolver_Error::SOLVESPACE_CRASH());
179       if (myPrevResult == PlaneGCSSolver_Solver::STATUS_OK ||
180           myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) {
181         // the error message should be changed before sending the message
182         sendMessage(EVENT_SOLVER_FAILED);
183         myPrevResult = PlaneGCSSolver_Solver::STATUS_FAILED;
184       }
185       mySketchSolver->undo();
186       return false;
187     }
188     // solution succeeded, store results into correspondent attributes
189     if (aResult == PlaneGCSSolver_Solver::STATUS_OK ||
190         aResult == PlaneGCSSolver_Solver::STATUS_EMPTYSET) {
191       myStorage->setNeedToResolve(false);
192       myStorage->refresh();
193
194       // additional check that copied entities used in Mirror and other "Multi" constraints
195       // is not connected with their originals by constraints.
196       myMultiConstraintUpdateStack += 1;
197       aResolved = true;
198       if (myStorage->isNeedToResolve())
199         aResolved = resolveConstraints();
200
201       if (aResolved) {
202         myMultiConstraintUpdateStack -= 1;
203
204         if (myPrevResult != PlaneGCSSolver_Solver::STATUS_OK ||
205             myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN) {
206           getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
207           std::set<ObjectPtr> aConflicting = myConflictingConstraints;
208           myConflictingConstraints.clear();
209           myPrevResult = PlaneGCSSolver_Solver::STATUS_OK;
210           // the error message should be changed before sending the message
211           sendMessage(EVENT_SOLVER_REPAIRED, aConflicting);
212         }
213       }
214
215       // show degrees of freedom
216       computeDoF();
217     } else {
218       mySketchSolver->undo();
219       if (!myConstraints.empty()) {
220         // the error message should be changed before sending the message
221         getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
222           ->setValue(SketchSolver_Error::CONSTRAINTS());
223         if (myPrevResult != aResult ||
224             myPrevResult == PlaneGCSSolver_Solver::STATUS_UNKNOWN ||
225             myPrevResult == PlaneGCSSolver_Solver::STATUS_FAILED) {
226           // Obtain list of conflicting constraints
227           std::set<ObjectPtr> aConflicting = myStorage->getConflictingConstraints(mySketchSolver);
228
229           if (!myConflictingConstraints.empty()) {
230             std::set<ObjectPtr>::iterator anIt = aConflicting.begin();
231             for (; anIt != aConflicting.end(); ++anIt)
232               myConflictingConstraints.erase(*anIt);
233             if (!myConflictingConstraints.empty()) {
234               // some constraints does not conflict, send corresponding message
235               sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints);
236             }
237           }
238           myConflictingConstraints = aConflicting;
239           if (!myConflictingConstraints.empty())
240             sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints);
241           myPrevResult = aResult;
242         }
243       }
244     }
245
246   } else if (isGroupEmpty && isWorkplaneValid())
247     computeDoF();
248   removeTemporaryConstraints();
249   myStorage->setNeedToResolve(false);
250   return aResolved;
251 }
252
253 // ============================================================================
254 //  Function: computeDoF
255 //  Class:    SketchSolver_Group
256 //  Purpose:  compute DoF of the sketch and set corresponding field
257 // ============================================================================
258 void SketchSolver_Group::computeDoF()
259 {
260   std::ostringstream aDoFMsg;
261   int aDoF = mySketchSolver->dof();
262   /// "DoF = 0" content of string value is used in PartSet by Sketch edit
263   /// If it is changed, it should be corrected also there
264   if (aDoF == 0)
265     aDoFMsg << "Sketch is fully fixed (DoF = 0)";
266   else
267     aDoFMsg << "DoF (degrees of freedom) = " << aDoF;
268   mySketch->string(SketchPlugin_Sketch::SOLVER_DOF())->setValue(aDoFMsg.str());
269
270   if (aDoF > 0 && myDOF == 0)
271     sendMessage(EVENT_SKETCH_UNDER_CONSTRAINED, mySketch, aDoF);
272   else if (aDoF == 0 && myDOF > 0)
273     sendMessage(EVENT_SKETCH_FULLY_CONSTRAINED, mySketch, aDoF);
274   else if (aDoF < 0)
275     sendMessage(EVENT_SKETCH_OVER_CONSTRAINED, mySketch, aDoF);
276
277   myDOF = aDoF;
278 }
279
280 // ============================================================================
281 //  Function: repairConsistency
282 //  Class:    SketchSolver_Group
283 //  Purpose:  search removed entities and constraints
284 // ============================================================================
285 void SketchSolver_Group::repairConsistency()
286 {
287   if (!areConstraintsValid() || !myStorage->areFeaturesValid()) {
288     // remove invalid constraints
289     std::set<ConstraintPtr> anInvalidConstraints;
290     ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
291     for (; aCIter != myConstraints.end(); ++aCIter) {
292       if (!aCIter->first->data() || !aCIter->first->data()->isValid())
293         anInvalidConstraints.insert(aCIter->first);
294     }
295     std::set<ConstraintPtr>::const_iterator aRemoveIt = anInvalidConstraints.begin();
296     for (; aRemoveIt != anInvalidConstraints.end(); ++aRemoveIt)
297       removeConstraint(*aRemoveIt);
298
299     // remove invalid features
300     myStorage->removeInvalidEntities();
301
302     // show DoF
303     computeDoF();
304   }
305 }
306
307 // ============================================================================
308 //  Function: removeTemporaryConstraints
309 //  Class:    SketchSolver_Group
310 //  Purpose:  remove all transient SLVS_C_WHERE_DRAGGED constraints after
311 //            resolving the set of constraints
312 // ============================================================================
313 void SketchSolver_Group::removeTemporaryConstraints()
314 {
315   if (!myTempConstraints.empty()) {
316     mySketchSolver->removeConstraint(CID_MOVEMENT);
317
318     std::set<SolverConstraintPtr>::iterator aTmpIt = myTempConstraints.begin();
319     for (; aTmpIt != myTempConstraints.end(); ++aTmpIt)
320       (*aTmpIt)->remove();
321
322     myTempConstraints.clear();
323   }
324
325   myStorage->setNeedToResolve(false);
326 }
327
328 // ============================================================================
329 //  Function: removeConstraint
330 //  Class:    SketchSolver_Group
331 //  Purpose:  remove constraint and all unused entities
332 // ============================================================================
333 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
334 {
335   ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
336   for (; aCIter != myConstraints.end(); aCIter++)
337     if (aCIter->first == theConstraint) {
338       aCIter->second->remove(); // the constraint is not fully removed
339
340       // constraint is removed => reset stack of "multi" constraints updates
341       myMultiConstraintUpdateStack = 0;
342       break;
343     }
344   if (aCIter != myConstraints.end())
345     myConstraints.erase(aCIter);
346 }
347
348 // ============================================================================
349 //  Function: setTemporary
350 //  Class:    SketchSolver_Group
351 //  Purpose:  append given constraint to the group of temporary constraints
352 // ============================================================================
353 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
354 {
355   myTempConstraints.insert(theConstraint);
356 }
357
358 // ============================================================================
359 //  Function: blockEvents
360 //  Class:    SketchSolver_Group
361 //  Purpose:  block or unblock events from features in this group
362 // ============================================================================
363 void SketchSolver_Group::blockEvents(bool isBlocked)
364 {
365   if (myIsEventsBlocked == isBlocked)
366     return;
367
368   // block/unblock events from the features in the storage
369   myStorage->blockEvents(isBlocked);
370
371   // block/unblock events from constraints
372   ConstraintConstraintMap::iterator aCIt = myConstraints.begin();
373   for (; aCIt != myConstraints.end(); ++aCIt)
374     aCIt->second->blockEvents(isBlocked);
375
376   myIsEventsBlocked = isBlocked;
377 }
378
379 bool SketchSolver_Group::areConstraintsValid() const
380 {
381   // Check the constraints are valid
382   ConstraintConstraintMap::const_iterator aCIter = myConstraints.begin();
383   for (; aCIter != myConstraints.end(); ++aCIter)
384     if (!aCIter->first->data() || !aCIter->first->data()->isValid())
385       return false;
386   return true;
387 }