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