Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into azv/SketchSolver_Refactoring
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintCoincidence.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <SketchSolver_ConstraintCoincidence.h>
4 #include <SketchSolver_Error.h>
5 #include <PlaneGCSSolver_Tools.h>
6 #include <PlaneGCSSolver_UpdateCoincidence.h>
7
8 void SketchSolver_ConstraintCoincidence::process()
9 {
10   cleanErrorMsg();
11   if (!myBaseConstraint || !myStorage) {
12     // Not enough parameters are assigned
13     return;
14   }
15
16   EntityWrapperPtr aValue;
17   std::vector<EntityWrapperPtr> anAttributes;
18   getAttributes(aValue, anAttributes);
19   if (!myErrorMsg.empty())
20     return;
21   if (anAttributes.empty()) {
22     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
23     return;
24   }
25
26   mySolverConstraint = PlaneGCSSolver_Tools::createConstraint(
27       myBaseConstraint, getType(),
28       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
29
30   myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
31   myStorage->notify(myBaseConstraint);
32 }
33
34 bool SketchSolver_ConstraintCoincidence::remove()
35 {
36   myInSolver = false;
37   return SketchSolver_Constraint::remove();
38 }
39
40 void SketchSolver_ConstraintCoincidence::getAttributes(
41     EntityWrapperPtr& theValue,
42     std::vector<EntityWrapperPtr>& theAttributes)
43 {
44   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
45   if (!myErrorMsg.empty() || !theAttributes[0]) {
46     theAttributes.clear();
47     return;
48   }
49
50   if (theAttributes[1])
51     myType = CONSTRAINT_PT_PT_COINCIDENT;
52   else if (theAttributes[2]) {
53     // check the type of entity (line or circle)
54     SketchSolver_EntityType anEntType = theAttributes[2]->type();
55     if (anEntType == ENTITY_LINE)
56       myType = CONSTRAINT_PT_ON_LINE;
57     else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
58       myType = CONSTRAINT_PT_ON_CIRCLE;
59     else
60       myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
61   } else
62     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
63 }
64
65 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr&      theFeature,
66                                                 PlaneGCSSolver_Update* theUpdater)
67 {
68   PlaneGCSSolver_UpdateCoincidence* anUpdater =
69       static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
70   bool isAccepted = anUpdater->checkCoincidence(myAttributes.front(), myAttributes.back());
71   if (isAccepted) {
72     if (!myInSolver) {
73       myInSolver = true;
74       myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
75     }
76   } else {
77     if (myInSolver) {
78       myInSolver = false;
79       myStorage->removeConstraint(myBaseConstraint);
80     }
81   }
82 }