Salome HOME
Fix conflict on middle-point constraint when the point is already on the line (issue...
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMiddle.cpp
1 #include <SketchSolver_ConstraintMiddle.h>
2
3 SketchSolver_ConstraintMiddle::SketchSolver_ConstraintMiddle(ConstraintPtr theConstraint)
4   : SketchSolver_Constraint(theConstraint)
5 {
6 }
7
8 void SketchSolver_ConstraintMiddle::notifyCoincidenceChanged(
9     EntityWrapperPtr theCoincAttr1,
10     EntityWrapperPtr theCoincAttr2)
11 {
12   // Check the coincidence between point and line has been changed
13   AttributePtr aPoint;
14   FeaturePtr aLine;
15   EntityWrapperPtr anEntities[2] = {theCoincAttr1, theCoincAttr2};
16   for (int i = 0; i < 2; ++i) {
17     if (anEntities[i]->type() == ENTITY_POINT)
18       aPoint = anEntities[i]->baseAttribute();
19     else if (anEntities[i]->type() == ENTITY_LINE)
20       aLine = anEntities[i]->baseFeature();
21   }
22   if (!aPoint || !aLine)
23     return;
24
25   // Check the attributes of middle-point constraint are the same point and line
26   bool isSameAttr = true;
27   for (int i = 0; i < 2 && isSameAttr; ++i) {
28     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
29         myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i)));
30     if (aRefAttr->isObject()) {
31       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
32       isSameAttr = (aFeature == aLine);
33     } else
34       isSameAttr = (aRefAttr->attr() == aPoint);
35   }
36
37   if (isSameAttr) {
38     remove();
39     process();
40   }
41 }