Salome HOME
Update removing point-point coincidences.
[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
60 void SketchSolver_ConstraintFixed::getAttributes(
61     ParameterWrapperPtr& theValue,
62     std::vector<EntityWrapperPtr>& theAttributes)
63 {
64   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
65
66   EntityWrapperPtr aSolverEntity;
67   if (myBaseFeature) {
68     // The feature is fixed.
69     myStorage->update(myBaseFeature, myGroupID);
70     aSolverEntity = myStorage->entity(myBaseFeature);
71   } else if (myBaseConstraint) {
72     // Constraint Fixed is added by user.
73     // Get the attribute of constraint (it should be alone in the list of constraints).
74     AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A());
75     AttributeRefAttrPtr aRefAttr =
76         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
77     if (!aRefAttr || !aRefAttr->isInitialized()) {
78       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
79       return;
80     }
81
82     myStorage->update(anAttr, myGroupID);
83     aSolverEntity = myStorage->entity(anAttr);
84   } else
85     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
86
87   if (aSolverEntity)
88     theAttributes.push_back(aSolverEntity);
89 }
90
91
92 bool SketchSolver_ConstraintFixed::remove()
93 {
94   cleanErrorMsg();
95   // Move fixed entities back to the current group
96   FeaturePtr aFeature = myBaseFeature;
97   if (myBaseConstraint && myFixedAttribute && myFixedAttribute->isObject())
98     aFeature = ModelAPI_Feature::feature(myFixedAttribute->object());
99   if (aFeature)
100     myStorage->update(aFeature, myGroupID);
101   else if (myFixedAttribute)
102     myStorage->update(myFixedAttribute, myGroupID);
103
104   // Remove constraint or base feature
105   if (myBaseConstraint) {
106     bool isRemoved = false;
107     if (aFeature)
108       isRemoved = myStorage->removeEntity(aFeature);
109     return SketchSolver_Constraint::remove() || isRemoved;
110   } else if (myBaseFeature)
111     myStorage->removeEntity(myBaseFeature);
112   return true;
113 }