]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintCoincidence.cpp
Salome HOME
Fix the frequently appeared "conflicting constraints" message when the point is coinc...
[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   return SketchSolver_Constraint::remove();
65 }
66
67 void SketchSolver_ConstraintCoincidence::getAttributes(
68     EntityWrapperPtr& theValue,
69     std::vector<EntityWrapperPtr>& theAttributes)
70 {
71   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
72   if (!myErrorMsg.empty() || !theAttributes[0]) {
73     theAttributes.clear();
74     return;
75   }
76
77   if (theAttributes[1])
78     myType = CONSTRAINT_PT_PT_COINCIDENT;
79   else if (theAttributes[2]) {
80     // check the type of entity (line or circle)
81     SketchSolver_EntityType anEntType = theAttributes[2]->type();
82     if (anEntType == ENTITY_LINE)
83       myType = CONSTRAINT_PT_ON_LINE;
84     else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
85       myType = CONSTRAINT_PT_ON_CIRCLE;
86     else
87       myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
88
89     // obtain extremity points of the coincident feature for further checking of multi-coincidence
90     getCoincidentFeatureExtremities(myBaseConstraint, myStorage, myFeatureExtremities);
91   } else
92     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
93 }
94
95 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr&      theFeature,
96                                                 PlaneGCSSolver_Update* theUpdater)
97 {
98   PlaneGCSSolver_UpdateCoincidence* anUpdater =
99       static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
100   bool isAccepted = anUpdater->checkCoincidence(myAttributes.front(), myAttributes.back());
101
102   // additionally check the point is coincident to extremity of coincident feature
103   if (myFeatureExtremities[0] && myFeatureExtremities[1]) {
104     EntityWrapperPtr aPoint =
105         myAttributes.front()->type() == ENTITY_POINT ? myAttributes.front() : myAttributes.back();
106
107     for (int i = 0; i < 2; ++i)
108       isAccepted = isAccepted && !anUpdater->isPointOnEntity(aPoint, myFeatureExtremities[i]);
109   }
110
111   if (isAccepted) {
112     if (!myInSolver) {
113       myInSolver = true;
114       myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
115     }
116   } else {
117     if (myInSolver) {
118       myInSolver = false;
119       myStorage->removeConstraint(myBaseConstraint);
120     }
121   }
122 }