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