Salome HOME
Show conflicting constraints when fixing constrained entity
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintFixed.cpp
1 #include <SketchSolver_ConstraintFixed.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
4
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_Circle.h>
7 #include <SketchPlugin_ConstraintRigid.h>
8 #include <SketchPlugin_Line.h>
9 #include <SketchPlugin_Point.h>
10
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomAPI_XY.h>
13 #include <GeomDataAPI_Point2D.h>
14 #include <ModelAPI_AttributeDouble.h>
15
16 #include <math.h>
17
18 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(ConstraintPtr theConstraint)
19   : SketchSolver_Constraint()
20 {
21   myBaseConstraint = theConstraint;
22   myType = CONSTRAINT_FIXED;
23   myFixedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
24       theConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
25 }
26
27 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(FeaturePtr theFeature)
28   : SketchSolver_Constraint(),
29     myBaseFeature(theFeature)
30 {
31   myType = CONSTRAINT_FIXED;
32   process();
33 }
34
35 void SketchSolver_ConstraintFixed::process()
36 {
37   cleanErrorMsg();
38   if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroupID == GID_UNKNOWN) {
39     // Not enough parameters are assigned
40     return;
41   }
42
43   ParameterWrapperPtr aValue;
44   std::vector<EntityWrapperPtr> anEntities;
45   getAttributes(aValue, anEntities);
46   if (!myErrorMsg.empty() || anEntities.empty())
47     return;
48   fixFeature(anEntities.front());
49 }
50
51 void SketchSolver_ConstraintFixed::fixFeature(EntityWrapperPtr theFeature)
52 {
53   // extract feature from the group
54   if (theFeature->baseAttribute())
55     myStorage->update(theFeature->baseAttribute(), GID_OUTOFGROUP);
56   else if (theFeature->baseFeature())
57     myStorage->update(theFeature->baseFeature(), GID_OUTOFGROUP);
58
59   if (myBaseConstraint)
60     myStorage->addConstraint(myBaseConstraint, std::list<ConstraintWrapperPtr>());
61 }
62
63 void SketchSolver_ConstraintFixed::getAttributes(
64     ParameterWrapperPtr& theValue,
65     std::vector<EntityWrapperPtr>& theAttributes)
66 {
67   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
68
69   EntityWrapperPtr aSolverEntity;
70   if (myBaseFeature) {
71     // The feature is fixed.
72     myStorage->update(myBaseFeature/*, myGroupID*/);
73     aSolverEntity = myStorage->entity(myBaseFeature);
74   } else if (myBaseConstraint) {
75     // Constraint Fixed is added by user.
76     // Get the attribute of constraint (it should be alone in the list of constraints).
77     AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A());
78     AttributeRefAttrPtr aRefAttr =
79         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
80     if (!aRefAttr || !aRefAttr->isInitialized()) {
81       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
82       return;
83     }
84
85     myStorage->update(anAttr, myGroupID);
86     aSolverEntity = myStorage->entity(anAttr);
87   } else
88     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
89
90   if (aSolverEntity)
91     theAttributes.push_back(aSolverEntity);
92 }
93
94
95 bool SketchSolver_ConstraintFixed::remove()
96 {
97   cleanErrorMsg();
98   // Move fixed entities back to the current group
99   FeaturePtr aFeature = myBaseFeature;
100   if (myBaseConstraint && myFixedAttribute) {
101     if (myFixedAttribute->isObject())
102       aFeature = ModelAPI_Feature::feature(myFixedAttribute->object());
103     else {
104       if (myFixedAttribute->attr().get())
105         myStorage->update(AttributePtr(myFixedAttribute), myGroupID);
106     }
107   }
108   if (aFeature)
109     myStorage->update(aFeature, myGroupID);
110   myStorage->setNeedToResolve(true);
111
112   // Remove constraint or base feature
113   if (myBaseConstraint) {
114     bool isRemoved = false;
115     if (aFeature)
116       isRemoved = myStorage->removeEntity(aFeature);
117     return SketchSolver_Constraint::remove() || isRemoved;
118   } else if (myBaseFeature)
119     myStorage->removeEntity(myBaseFeature);
120   return true;
121 }