Salome HOME
73c1a52202dbe1dece1003a9518798178f9cac4e
[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   AttributeRefAttrPtr anAttribute =
24       theConstraint->refattr(SketchPlugin_ConstraintRigid::ENTITY_A());
25   if (anAttribute->isObject())
26     myFixedFeature = ModelAPI_Feature::feature(anAttribute->object());
27   else
28     myFixedAttribute = anAttribute->attr();
29 }
30
31 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(FeaturePtr theFeature)
32   : SketchSolver_Constraint(),
33     myBaseFeature(theFeature)
34 {
35   myType = CONSTRAINT_FIXED;
36   process();
37 }
38
39 void SketchSolver_ConstraintFixed::process()
40 {
41   cleanErrorMsg();
42   if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroupID == GID_UNKNOWN) {
43     // Not enough parameters are assigned
44     return;
45   }
46
47   ParameterWrapperPtr aValue;
48   std::vector<EntityWrapperPtr> anEntities;
49   getAttributes(aValue, anEntities);
50   if (!myErrorMsg.empty() || anEntities.empty())
51     return;
52   fixFeature(anEntities.front());
53 }
54
55 void SketchSolver_ConstraintFixed::fixFeature(EntityWrapperPtr theFeature)
56 {
57   // extract feature from the group
58   if (theFeature->baseAttribute())
59     myStorage->update(theFeature->baseAttribute(), GID_OUTOFGROUP);
60   else if (theFeature->baseFeature())
61     myStorage->update(theFeature->baseFeature(), GID_OUTOFGROUP);
62
63   if (myBaseConstraint)
64     myStorage->addConstraint(myBaseConstraint, std::list<ConstraintWrapperPtr>());
65 }
66
67 void SketchSolver_ConstraintFixed::getAttributes(
68     ParameterWrapperPtr& theValue,
69     std::vector<EntityWrapperPtr>& theAttributes)
70 {
71   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
72
73   EntityWrapperPtr aSolverEntity;
74   if (myBaseFeature) {
75     // The feature is fixed.
76     myStorage->update(myBaseFeature/*, myGroupID*/);
77     aSolverEntity = myStorage->entity(myBaseFeature);
78   } else if (myBaseConstraint) {
79     // Constraint Fixed is added by user.
80     // Get the attribute of constraint (it should be alone in the list of constraints).
81     AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A());
82     AttributeRefAttrPtr aRefAttr =
83         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
84     if (!aRefAttr || !aRefAttr->isInitialized()) {
85       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
86       return;
87     }
88
89     myStorage->update(anAttr, myGroupID);
90     aSolverEntity = myStorage->entity(anAttr);
91   } else
92     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
93
94   if (aSolverEntity)
95     theAttributes.push_back(aSolverEntity);
96 }
97
98
99 bool SketchSolver_ConstraintFixed::remove()
100 {
101   cleanErrorMsg();
102   // Move fixed entities back to the current group
103   FeaturePtr aFeature = myBaseFeature;
104   if (myBaseConstraint) {
105     if (myFixedFeature)
106       aFeature = myFixedFeature;
107     else if (myFixedAttribute)
108       myStorage->update(AttributePtr(myFixedAttribute), myGroupID);
109   }
110   if (aFeature)
111     myStorage->update(aFeature, myGroupID);
112   myStorage->setNeedToResolve(true);
113
114   // Remove constraint or base feature
115   if (myBaseConstraint) {
116     bool isRemoved = false;
117     if (aFeature)
118       isRemoved = myStorage->removeEntity(aFeature);
119     return SketchSolver_Constraint::remove() || isRemoved;
120   } else if (myBaseFeature)
121     myStorage->removeEntity(myBaseFeature);
122   return true;
123 }