Salome HOME
Implement the Collinear constraint
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
1 #include <SketchSolver_Constraint.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4 #include <SketchSolver_Manager.h>
5
6 #include <SketchPlugin_Arc.h>
7 #include <SketchPlugin_Circle.h>
8 #include <SketchPlugin_Line.h>
9 #include <SketchPlugin_Point.h>
10
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_ConstraintCoincidence.h>
13 #include <SketchPlugin_ConstraintCollinear.h>
14 #include <SketchPlugin_ConstraintDistance.h>
15 #include <SketchPlugin_ConstraintEqual.h>
16 #include <SketchPlugin_ConstraintHorizontal.h>
17 #include <SketchPlugin_ConstraintLength.h>
18 #include <SketchPlugin_ConstraintMirror.h>
19 #include <SketchPlugin_ConstraintParallel.h>
20 #include <SketchPlugin_ConstraintPerpendicular.h>
21 #include <SketchPlugin_ConstraintRadius.h>
22 #include <SketchPlugin_ConstraintRigid.h>
23 #include <SketchPlugin_ConstraintTangent.h>
24 #include <SketchPlugin_ConstraintVertical.h>
25
26 #include <GeomAPI_Dir2d.h>
27 #include <GeomDataAPI_Point.h>
28 #include <GeomDataAPI_Point2D.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_ResultConstruction.h>
31
32 #include <math.h>
33
34 SketchSolver_Constraint::SketchSolver_Constraint(
35     ConstraintPtr  theConstraint)
36   : myBaseConstraint(theConstraint),
37     myGroupID(GID_UNKNOWN),
38     myType(CONSTRAINT_UNKNOWN)
39 {
40 }
41
42 void SketchSolver_Constraint::process(StoragePtr theStorage,
43                                       const GroupID& theGroupID,
44                                       const EntityID& theSketchID)
45 {
46   myStorage = theStorage;
47   myGroupID = theGroupID;
48   mySketchID = theSketchID;
49   // Process constraint according to its type
50   process();
51 }
52
53
54 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
55 {
56   const std::string& aType = theConstraint->getKind();
57   if (aType == SketchPlugin_ConstraintCoincidence::ID())
58     return CONSTRAINT_COINCIDENCE;
59   else if (aType == SketchPlugin_ConstraintRigid::ID())
60     return CONSTRAINT_FIXED;
61   else if (aType == SketchPlugin_ConstraintHorizontal::ID())
62     return CONSTRAINT_HORIZONTAL;
63   else if (aType == SketchPlugin_ConstraintVertical::ID())
64     return CONSTRAINT_VERTICAL;
65   else if (aType == SketchPlugin_ConstraintAngle::ID())
66     return CONSTRAINT_ANGLE;
67   else if (aType == SketchPlugin_ConstraintDistance::ID())
68     return CONSTRAINT_DISTANCE;
69   else if (aType == SketchPlugin_ConstraintEqual::ID())
70     return CONSTRAINT_EQUAL;
71   else if (aType == SketchPlugin_ConstraintLength::ID())
72     return CONSTRAINT_PT_PT_DISTANCE;
73   else if (aType == SketchPlugin_ConstraintMirror::ID())
74     return CONSTRAINT_SYMMETRIC;
75   else if (aType == SketchPlugin_ConstraintParallel::ID())
76     return CONSTRAINT_PARALLEL;
77   else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
78     return CONSTRAINT_PERPENDICULAR;
79   else if (aType == SketchPlugin_ConstraintRadius::ID())
80     return CONSTRAINT_RADIUS;
81   else if (aType == SketchPlugin_ConstraintTangent::ID())
82     return CONSTRAINT_TANGENT;
83   else if (aType == SketchPlugin_ConstraintCollinear::ID())
84     return CONSTRAINT_COLLINEAR;
85   return CONSTRAINT_UNKNOWN;
86 }
87
88 void SketchSolver_Constraint::process()
89 {
90   cleanErrorMsg();
91   if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
92     // Not enough parameters are assigned
93     return;
94   }
95
96   SketchSolver_ConstraintType aConstrType = getType();
97   double aValue;
98   std::vector<EntityWrapperPtr> anAttributes;
99   getAttributes(aValue, anAttributes);
100   if (!myErrorMsg.empty())
101     return;
102   if (anAttributes.empty()) {
103     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
104     return;
105   }
106   if (aConstrType == CONSTRAINT_UNKNOWN)
107     aConstrType = getType();
108
109   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
110   std::list<ConstraintWrapperPtr> aNewConstraints = aBuilder->createConstraint(
111       myBaseConstraint, myGroupID, mySketchID, aConstrType,
112       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
113   myStorage->addConstraint(myBaseConstraint, aNewConstraints);
114
115   adjustConstraint();
116 }
117
118 void SketchSolver_Constraint::update()
119 {
120   cleanErrorMsg();
121   std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
122   std::list<ConstraintWrapperPtr>::iterator aWIt = aWrapper.begin();
123
124   // Check if attributes of constraint are changed, rebuild constraint
125   std::set<AttributePtr> anAttributes;
126   std::set<AttributePtr>::iterator aFoundAttr;
127   std::set<FeaturePtr> aFeatures;
128   std::set<FeaturePtr>::iterator aFoundFeat;
129   for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
130     AttributePtr anAttr =
131         myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
132     if (!anAttr)
133       continue;
134
135     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
136     if (aRefAttr) {
137       if (aRefAttr->isObject()) {
138         FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
139         if (myBaseConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
140           aFeatures.insert(aFeat);
141         else {
142           // Workaround for the Length constraint: add points of line, not line itself
143           anAttributes.insert(aFeat->attribute(SketchPlugin_Line::START_ID()));
144           anAttributes.insert(aFeat->attribute(SketchPlugin_Line::END_ID()));
145         }
146       } else
147         anAttributes.insert(aRefAttr->attr());
148     } else
149       anAttributes.insert(anAttr);
150   }
151   bool hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
152   for (; hasNewAttr && aWIt != aWrapper.end(); ++ aWIt) {
153     const std::list<EntityWrapperPtr>& aSubs = (*aWIt)->entities();
154     std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
155     for (; hasNewAttr && aSIt != aSubs.end(); ++aSIt) {
156       if ((*aSIt)->baseAttribute()) {
157         aFoundAttr = anAttributes.find((*aSIt)->baseAttribute());
158         if (aFoundAttr != anAttributes.end())
159           anAttributes.erase(aFoundAttr);
160       } else {
161         aFoundFeat = aFeatures.find((*aSIt)->baseFeature());
162         if (aFoundFeat != aFeatures.end())
163           aFeatures.erase(aFoundFeat);
164       }
165       hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
166     }
167   }
168   if (hasNewAttr) {
169     remove();
170     process();
171     return;
172   }
173
174   AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
175       myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
176   if (aValueAttr) {
177     for (aWIt = aWrapper.begin(); aWIt != aWrapper.end(); ++aWIt)
178       if (fabs((*aWIt)->value() - aValueAttr->value()) > tolerance) {
179         (*aWIt)->setValue(aValueAttr->value());
180         myStorage->setNeedToResolve(true);
181       }
182   }
183   myStorage->addConstraint(myBaseConstraint, aWrapper);
184
185   adjustConstraint();
186 }
187
188 bool SketchSolver_Constraint::remove()
189 {
190   cleanErrorMsg();
191   myType = CONSTRAINT_UNKNOWN;
192   return myStorage->removeConstraint(myBaseConstraint);
193 }
194
195 void SketchSolver_Constraint::getAttributes(
196     double& theValue,
197     std::vector<EntityWrapperPtr>& theAttributes)
198 {
199   static const int anInitNbOfAttr = 4;
200   theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
201
202   DataPtr aData = myBaseConstraint->data();
203   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
204
205   myType = TYPE(myBaseConstraint);
206
207   AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
208       aData->attribute(SketchPlugin_Constraint::VALUE()));
209   theValue = aValueAttr ? aValueAttr->value() : 0.0;
210
211   int aPtInd = 0; // index of first point in the list of attributes
212   int aEntInd = 2; // index of first entity in the list of attributes
213   std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
214   std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
215   for (; anIter != aConstrAttrs.end(); anIter++) {
216     AttributeRefAttrPtr aRefAttr =
217         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
218     if (!aRefAttr || !aRefAttr->isInitialized()) {
219       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
220       return;
221     }
222
223     myStorage->update(*anIter/*, myGroupID*/);
224     EntityWrapperPtr anEntity = myStorage->entity(*anIter);
225
226     SketchSolver_EntityType aType = anEntity->type();
227     if (aType == ENTITY_UNKNOWN)
228       continue;
229     else if (aType == ENTITY_POINT)
230       theAttributes[aPtInd++] = anEntity; // the point is created
231     else { // another entity (not a point) is created
232       if (aEntInd < anInitNbOfAttr)
233         theAttributes[aEntInd] = anEntity;
234       else
235         theAttributes.push_back(anEntity);
236       aEntInd++;
237     }
238   }
239 }
240
241 bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
242 {
243   const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
244   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
245   for (; aCIt != aCList.end(); ++aCIt)
246     if ((*aCIt)->isUsed(theFeature))
247       return true;
248
249   std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
250   std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
251   for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
252     if (isUsed(*anAttrIt))
253       return true;
254
255   return false;
256 }
257
258 bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
259 {
260   AttributePtr anAttribute = theAttribute;
261   AttributeRefAttrPtr aRefAttr =
262       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
263   if (aRefAttr) {
264     if (aRefAttr->isObject())
265       return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
266     else
267       anAttribute = aRefAttr->attr();
268   }
269
270   const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
271   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
272   for (; aCIt != aCList.end(); ++aCIt)
273     if ((*aCIt)->isUsed(theAttribute))
274       return true;
275   return false;
276 }