Salome HOME
8501289fd2b475d9532f2d77cc5eba1796d783d6
[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 <SketchSolver_Manager.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   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
27   mySolverConstraint = aBuilder->createConstraint(
28       myBaseConstraint, getType(),
29       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
30
31   myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
32   myStorage->notify(myBaseConstraint);
33 }
34
35 void SketchSolver_ConstraintCoincidence::getAttributes(
36     EntityWrapperPtr& theValue,
37     std::vector<EntityWrapperPtr>& theAttributes)
38 {
39   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
40   if (!myErrorMsg.empty() || !theAttributes[0]) {
41     theAttributes.clear();
42     return;
43   }
44
45   if (theAttributes[1])
46     myType = CONSTRAINT_PT_PT_COINCIDENT;
47   else if (theAttributes[2]) {
48     // check the type of entity (line or circle)
49     SketchSolver_EntityType anEntType = theAttributes[2]->type();
50     if (anEntType == ENTITY_LINE)
51       myType = CONSTRAINT_PT_ON_LINE;
52     else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
53       myType = CONSTRAINT_PT_ON_CIRCLE;
54     else
55       myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
56   } else
57     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
58 }
59
60 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr&      theFeature,
61                                                 PlaneGCSSolver_Update* theUpdater)
62 {
63   PlaneGCSSolver_UpdateCoincidence* anUpdater =
64       static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
65   bool isAccepted = anUpdater->checkCoincidence(myAttributes.front(), myAttributes.back());
66   if (isAccepted) {
67     if (!myInSolver) {
68       myInSolver = true;
69       myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
70     }
71   } else {
72     if (myInSolver) {
73       myInSolver = false;
74       myStorage->removeConstraint(myBaseConstraint);
75     }
76   }
77 }