Salome HOME
Fix crash when removing point under Middle and Coincidence constraints (issues #1762...
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMiddle.cpp
1 #include <SketchSolver_ConstraintMiddle.h>
2
3 #include <SketchSolver_Builder.h>
4 #include <SketchSolver_Manager.h>
5
6 #include <GeomAPI_XY.h>
7
8 SketchSolver_ConstraintMiddle::SketchSolver_ConstraintMiddle(ConstraintPtr theConstraint)
9   : SketchSolver_Constraint(theConstraint)
10 {
11 }
12
13 void SketchSolver_ConstraintMiddle::notifyCoincidenceChanged(
14     EntityWrapperPtr theCoincAttr1,
15     EntityWrapperPtr theCoincAttr2)
16 {
17   // Check the coincidence between point and line has been changed
18   AttributePtr aPoint;
19   FeaturePtr aLine;
20   EntityWrapperPtr anEntities[2] = {theCoincAttr1, theCoincAttr2};
21   for (int i = 0; i < 2; ++i) {
22     if (anEntities[i]->type() == ENTITY_POINT)
23       aPoint = anEntities[i]->baseAttribute();
24     else if (anEntities[i]->type() == ENTITY_LINE)
25       aLine = anEntities[i]->baseFeature();
26   }
27   if (!aPoint || !aLine)
28     return;
29
30   // Check the attributes of middle-point constraint are the same point and line
31   bool isSameAttr = true;
32   for (int i = 0; i < 2 && isSameAttr; ++i) {
33     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
34         myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i)));
35     if (!aRefAttr)
36     {
37       // It seems the Middle point constraint has been deleted, so keep it unchanged.
38       // It will be removed later.
39       return;
40     }
41     if (aRefAttr->isObject()) {
42       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
43       isSameAttr = (aFeature == aLine);
44     } else
45       isSameAttr = (aRefAttr->attr() == aPoint);
46   }
47
48   if (isSameAttr) {
49     remove();
50     process();
51   }
52 }