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