Salome HOME
Issues #2150 and #2151: Frequently appeared "Conflicting constraints" message for...
[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 #include <SketchPlugin_Arc.h>
9 #include <SketchPlugin_Line.h>
10
11 static void getCoincidentFeatureExtremities(const ConstraintPtr& theConstraint,
12                                             const StoragePtr& theStorage,
13                                             EntityWrapperPtr theExtremities[2])
14 {
15   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
16     AttributeRefAttrPtr aRefAttr = theConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
17     if (!aRefAttr || !aRefAttr->isObject())
18       continue;
19
20     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
21     if (!aFeature)
22       continue;
23
24     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
25       theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::START_ID()));
26       theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::END_ID()));
27     } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
28       theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::START_ID()));
29       theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::END_ID()));
30     }
31   }
32 }
33
34
35 void SketchSolver_ConstraintCoincidence::process()
36 {
37   cleanErrorMsg();
38   if (!myBaseConstraint || !myStorage) {
39     // Not enough parameters are assigned
40     return;
41   }
42
43   EntityWrapperPtr aValue;
44   std::vector<EntityWrapperPtr> anAttributes;
45   getAttributes(aValue, anAttributes);
46   if (!myErrorMsg.empty())
47     return;
48   if (anAttributes.empty()) {
49     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
50     return;
51   }
52
53   mySolverConstraint = PlaneGCSSolver_Tools::createConstraint(
54       myBaseConstraint, getType(),
55       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
56
57   myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
58   myStorage->notify(myBaseConstraint);
59 }
60
61 bool SketchSolver_ConstraintCoincidence::remove()
62 {
63   myInSolver = false;
64   myFeatureExtremities[0] = EntityWrapperPtr();
65   myFeatureExtremities[1] = EntityWrapperPtr();
66   return SketchSolver_Constraint::remove();
67 }
68
69 void SketchSolver_ConstraintCoincidence::getAttributes(
70     EntityWrapperPtr& theValue,
71     std::vector<EntityWrapperPtr>& theAttributes)
72 {
73   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
74   if (!myErrorMsg.empty() || !theAttributes[0]) {
75     theAttributes.clear();
76     return;
77   }
78
79   if (theAttributes[1])
80     myType = CONSTRAINT_PT_PT_COINCIDENT;
81   else if (theAttributes[2]) {
82     // check the type of entity (line or circle)
83     SketchSolver_EntityType anEntType = theAttributes[2]->type();
84     if (anEntType == ENTITY_LINE)
85       myType = CONSTRAINT_PT_ON_LINE;
86     else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
87       myType = CONSTRAINT_PT_ON_CIRCLE;
88     else
89       myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
90
91     // obtain extremity points of the coincident feature for further checking of multi-coincidence
92     getCoincidentFeatureExtremities(myBaseConstraint, myStorage, myFeatureExtremities);
93   } else
94     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
95 }
96
97 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr&      theFeature,
98                                                 PlaneGCSSolver_Update* theUpdater)
99 {
100   PlaneGCSSolver_UpdateCoincidence* anUpdater =
101       static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
102   bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
103
104   // additionally check the point is coincident to extremity of coincident feature
105   if (myFeatureExtremities[0] && myFeatureExtremities[1]) {
106     EntityWrapperPtr aPoint =
107         myAttributes.front()->type() == ENTITY_POINT ? myAttributes.front() : myAttributes.back();
108
109     for (int i = 0; i < 2; ++i)
110       isAccepted = isAccepted && !anUpdater->isPointOnEntity(aPoint, myFeatureExtremities[i]);
111   }
112
113   if (isAccepted) {
114     if (!myInSolver) {
115       myInSolver = true;
116       myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
117     }
118   } else {
119     if (myInSolver) {
120       myInSolver = false;
121       myStorage->removeConstraint(myBaseConstraint);
122     }
123   }
124 }