]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintManager.cpp
Salome HOME
a543a181f433a03208d2c82d4b9efde9088c561b
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintManager.cpp
1 // File:    SketchSolver_ConstraintManager.cpp
2 // Created: 08 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchSolver_ConstraintManager.h"
6
7 #include <Events_Loop.h>
8 #include <GeomDataAPI_Dir.h>
9 #include <GeomDataAPI_Point.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_Data.h>
14 #include <Model_Events.h>
15
16 #include <SketchPlugin_Constraint.h>
17 #include <SketchPlugin_ConstraintCoincidence.h>
18 #include <SketchPlugin_Line.h>
19 #include <SketchPlugin_Point.h>
20 #include <SketchPlugin_Sketch.h>
21
22 #include <math.h>
23 #include <assert.h>
24
25 #include <set>
26
27 /// Tolerance for value of parameters
28 const double tolerance = 1.e-10;
29
30 // Initialization of constraint manager self pointer
31 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::_self = 0;
32
33 /// Global constraint manager object
34 SketchSolver_ConstraintManager* myManager = SketchSolver_ConstraintManager::Instance();
35
36 /// This value is used to give unique index to the groups
37 static Slvs_hGroup myGroupIndexer = 0;
38
39 /** \brief Search the entity/parameter with specified ID in the list of elements
40  *  \param[in] theEntityID unique ID of the element
41  *  \param[in] theEntities list of elements
42  *  \return position of the found element or -1 if the element is not found
43  */
44 template <typename T>
45 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
46
47
48
49 // ========================================================
50 // ========= SketchSolver_ConstraintManager ===============
51 // ========================================================
52 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::Instance()
53 {
54   if (!_self)
55     _self = new SketchSolver_ConstraintManager();
56   return _self;
57 }
58
59 SketchSolver_ConstraintManager::SketchSolver_ConstraintManager()
60 {
61   myGroups.clear();
62
63   // Register in event loop
64   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
65   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
66   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
67   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURES_MOVED));
68 }
69
70 SketchSolver_ConstraintManager::~SketchSolver_ConstraintManager()
71 {
72   myGroups.clear();
73 }
74
75 void SketchSolver_ConstraintManager::processEvent(const Events_Message* theMessage)
76 {
77   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED) ||
78       theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED) || 
79       theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURES_MOVED))
80   {
81     const Model_FeatureUpdatedMessage* anUpdateMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
82     std::set< boost::shared_ptr<ModelAPI_Feature> > aFeatures = anUpdateMsg->features();
83
84     if (!(theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURES_MOVED)))
85     {
86       std::set< boost::shared_ptr<ModelAPI_Feature> >::iterator aFeatIter;
87       for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++)
88       {
89         // Only sketches and constraints can be added by Create event
90         const std::string& aFeatureKind = (*aFeatIter)->getKind();
91         if (aFeatureKind.compare("Sketch") == 0)
92         {
93           boost::shared_ptr<SketchPlugin_Feature> aSketch =
94             boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
95           changeWorkplane(aSketch);
96           return ;
97         }
98         boost::shared_ptr<SketchPlugin_Constraint> aConstraint =
99           boost::dynamic_pointer_cast<SketchPlugin_Constraint>(*aFeatIter);
100         if (aConstraint)
101           changeConstraint(aConstraint);
102         else
103         {
104           // Sketch plugin features can be only updated
105           boost::shared_ptr<SketchPlugin_Feature> aFeature =
106             boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
107           if (aFeature)
108             updateEntity(aFeature);
109         }
110       }
111     }
112
113     // Solve the set of constraints
114     resolveConstraints();
115   }
116   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED))
117   {
118     const Model_FeatureDeletedMessage* aDeleteMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
119     const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
120
121     // Find "Sketch" in groups. The constraint groups should be updated when an object removed from Sketch
122     std::set<std::string>::const_iterator aFGrIter;
123     for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
124       if (aFGrIter->compare("Sketch") == 0)
125         break;
126     
127     if (aFGrIter != aFeatureGroups.end())
128     {
129       std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter = myGroups.begin();
130       while (aGroupIter != myGroups.end())
131       {
132         if ((*aGroupIter)->updateGroup())
133         { // the group should be removed
134           delete *aGroupIter;
135           int aShift = aGroupIter - myGroups.begin();
136           myGroups.erase(aGroupIter);
137           aGroupIter = myGroups.begin() + aShift;
138         }
139         else aGroupIter++;
140       }
141     }
142   }
143 }
144
145 bool SketchSolver_ConstraintManager::changeWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch)
146 {
147   bool aResult = true; // changed when a workplane wrongly updated
148   bool isUpdated = false;
149   // Try to update specified workplane in all groups
150   std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
151   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
152     if ((*aGroupIter)->isBaseWorkplane(theSketch))
153     {
154       isUpdated = true;
155       if (!(*aGroupIter)->updateWorkplane())
156         aResult = false;
157     }
158   // If the workplane is not updated, so this is a new workplane
159   if (!isUpdated)
160   {
161     SketchSolver_ConstraintGroup* aNewGroup = new SketchSolver_ConstraintGroup(theSketch);
162     // Verify that the group is created successfully
163     if (!aNewGroup->isBaseWorkplane(theSketch))
164     {
165       delete aNewGroup;
166       return false;
167     }
168     myGroups.push_back(aNewGroup);
169   }
170   return aResult;
171 }
172
173 bool SketchSolver_ConstraintManager::changeConstraint(
174               boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
175 {
176   // Search the groups which this constraint touchs
177   std::vector<Slvs_hGroup> aGroups;
178   findGroups(theConstraint, aGroups);
179
180   // Process the groups list
181   if (aGroups.size() == 0)
182   { // There are no groups applicable for this constraint => create new one
183     boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
184     if (!aWP) return false;
185     SketchSolver_ConstraintGroup* aGroup = new SketchSolver_ConstraintGroup(aWP);
186     if (!aGroup->changeConstraint(theConstraint))
187     {
188       delete aGroup;
189       return false;
190     }
191     myGroups.push_back(aGroup);
192     return true;
193   }
194   else if (aGroups.size() == 1)
195   { // Only one group => add constraint into it
196     Slvs_hGroup aGroupId = *(aGroups.begin());
197     std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
198     for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
199       if ((*aGroupIter)->getId() == aGroupId)
200         return (*aGroupIter)->changeConstraint(theConstraint);
201   }
202   else if (aGroups.size() > 1)
203   { // Several groups applicable for this constraint => need to merge them
204     /// \todo Implement merging of groups
205   }
206
207   // Something goes wrong
208   return false;
209 }
210
211 void SketchSolver_ConstraintManager::updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature)
212 {
213   // Create list of attributes depending on type of the feature
214   std::vector<std::string> anAttrList;
215   const std::string& aFeatureKind = theFeature->getKind();
216   // Point
217   if (aFeatureKind.compare("SketchPoint") == 0)
218     anAttrList.push_back(POINT_ATTR_COORD);
219   // Line
220   else if (aFeatureKind.compare("SketchLine") == 0)
221   {
222     anAttrList.push_back(LINE_ATTR_START);
223     anAttrList.push_back(LINE_ATTR_END);
224   }
225   /// \todo Other types of features should be implemented
226
227   // Check changing of feature's attributes (go through the groups and search usage of the attributes)
228   std::vector<std::string>::const_iterator anAttrIter;
229   for (anAttrIter = anAttrList.begin(); anAttrIter != anAttrList.end(); anAttrIter++)
230   {
231     std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
232     for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
233     {
234       boost::shared_ptr<ModelAPI_Attribute> anAttribute =
235         boost::dynamic_pointer_cast<ModelAPI_Attribute>(theFeature->data()->attribute(*anAttrIter));
236       (*aGroupIter)->updateEntityIfPossible(anAttribute);
237     }
238   }
239 }
240
241
242 void SketchSolver_ConstraintManager::findGroups(
243               boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
244               std::vector<Slvs_hGroup>&                  theGroupIDs) const
245 {
246   boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
247
248   std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
249   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
250     if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theConstraint))
251       theGroupIDs.push_back((*aGroupIter)->getId());
252 }
253
254 boost::shared_ptr<SketchPlugin_Feature> SketchSolver_ConstraintManager::findWorkplaneForConstraint(
255               boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
256 {
257   // Already verified workplanes
258   std::set< boost::shared_ptr<SketchPlugin_Feature> > aVerified;
259
260   std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
261   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
262   {
263     boost::shared_ptr<SketchPlugin_Feature> aWP = (*aGroupIter)->getWorkplane();
264     if (aVerified.find(aWP) != aVerified.end())
265       continue;
266
267     boost::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures =
268       boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aWP->data()->attribute(SKETCH_ATTR_FEATURES));
269     std::list< boost::shared_ptr<ModelAPI_Feature> > aFeaturesList = aWPFeatures->list();
270     std::list< boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIter;
271     for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
272       if (*anIter == theConstraint)
273         return aWP; // workplane is found
274     aVerified.insert(aWP);
275   }
276
277   return boost::shared_ptr<SketchPlugin_Feature>();
278 }
279
280 void SketchSolver_ConstraintManager::resolveConstraints()
281 {
282   std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
283   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
284     (*aGroupIter)->resolveConstraints();
285
286   // Features may be updated => send events
287   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
288 }
289
290
291
292 // ========================================================
293 // =========  SketchSolver_ConstraintGroup  ===============
294 // ========================================================
295
296 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::
297   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
298   : myID(++myGroupIndexer),
299     myParamMaxID(0),
300     myEntityMaxID(0),
301     myConstrMaxID(0),
302     myConstraintMap(),
303     myNeedToSolve(false),
304     myConstrSolver()
305 {
306   myParams.clear();
307   myEntities.clear();
308   myConstraints.clear();
309
310   // Initialize workplane
311   myWorkplane.h = SLVS_E_UNKNOWN;
312   assert(addWorkplane(theWorkplane));
313 }
314
315 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
316 {
317   myParams.clear();
318   myEntities.clear();
319   myConstraints.clear();
320   myConstraintMap.clear();
321
322   // If the group with maximal identifier is deleted, decrease the indexer
323   if (myID == myGroupIndexer)
324     myGroupIndexer--;
325 }
326
327 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isBaseWorkplane(
328                 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
329 {
330   return theWorkplane == mySketch;
331 }
332
333 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
334                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
335 {
336   // Check the group is empty
337   if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
338     return true;
339
340   // Go through constraint entities and verify if some of them already in the group
341   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
342   {
343     boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
344       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
345         theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[i])
346       );
347     if (!aCAttrRef) continue;
348     if (myEntityMap.find(aCAttrRef->attr()) != myEntityMap.end())
349       return true;
350   }
351
352   // Entities did not found
353   return false;
354 }
355
356 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeConstraint(
357                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
358 {
359   // There is no workplane yet, something wrong
360   if (myWorkplane.h == SLVS_E_UNKNOWN)
361     return false;
362
363   // Search this constraint in the current group to update it
364   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
365     aConstrMapIter = myConstraintMap.find(theConstraint);
366   std::vector<Slvs_Constraint>::iterator aConstrIter;
367   if (aConstrMapIter != myConstraintMap.end())
368   {
369     int aConstrPos = Search(aConstrMapIter->second, myConstraints);
370     aConstrIter = myConstraints.begin() + aConstrPos;
371   }
372
373   // Get constraint type and verify the constraint parameters are correct
374   int aConstrType = getConstraintType(theConstraint);
375   if (aConstrType == SLVS_C_UNKNOWN ||
376      (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
377     return false;
378
379   // Create constraint parameters
380   double aDistance = 0.0; // scalar value of the constraint
381   boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
382     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
383   if (aDistAttr)
384   {
385     aDistance = aDistAttr->value();
386     if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
387     {
388       myNeedToSolve = true;
389       aConstrIter->valA = aDistance;
390     }
391   }
392
393   Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
394   for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
395   {
396     aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
397     boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
398       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
399         theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
400       );
401     if (!aConstrAttr) continue;
402     aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
403   }
404
405   if (aConstrMapIter == myConstraintMap.end())
406   {
407     // Create SolveSpace constraint structure
408     Slvs_Constraint aConstraint =
409       Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
410                           aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
411     myConstraints.push_back(aConstraint);
412     myConstraintMap[theConstraint] = aConstraint.h;
413   }
414   return true;
415 }
416
417 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeEntity(
418                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
419 {
420   // If the entity is already in the group, try to find it
421   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
422     aEntIter = myEntityMap.find(theEntity);
423   std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
424   if (aEntIter == myEntityMap.end()) // no such entity => should be created
425     aParamIter = myParams.end();
426   else
427   { // the entity already exists
428     int aEntPos = Search(aEntIter->second, myEntities);
429     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
430     aParamIter = myParams.begin() + aParamPos;
431   }
432
433   // Look over supported types of entities
434
435   // Point in 3D
436   boost::shared_ptr<GeomDataAPI_Point> aPoint =
437     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
438   if (aPoint)
439   {
440     Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
441     Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
442     Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
443
444     if (aEntIter != myEntityMap.end()) // the entity already exists
445       return aEntIter->second;
446
447     // New entity
448     Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
449     myEntities.push_back(aPtEntity);
450     myEntityMap[theEntity] = aPtEntity.h;
451     return aPtEntity.h;
452   }
453
454   // Point in 2D
455   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
456     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
457   if (aPoint2D)
458   {
459     // The 2D points are created on workplane. So, if there is no workplane yet, then error
460     if (myWorkplane.h == SLVS_E_UNKNOWN)
461       return SLVS_E_UNKNOWN;
462     Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
463     Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
464
465     if (aEntIter != myEntityMap.end()) // the entity already exists
466       return aEntIter->second;
467
468     // New entity
469     Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
470     myEntities.push_back(aPt2DEntity);
471     myEntityMap[theEntity] = aPt2DEntity.h;
472     return aPt2DEntity.h;
473   }
474
475   /// \todo Other types of entities
476
477   // Unsupported or wrong entity type
478   return SLVS_E_UNKNOWN;
479 }
480
481 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeNormal(
482                 boost::shared_ptr<ModelAPI_Attribute> theDirX,
483                 boost::shared_ptr<ModelAPI_Attribute> theDirY,
484                 boost::shared_ptr<ModelAPI_Attribute> theNorm)
485 {
486   boost::shared_ptr<GeomDataAPI_Dir> aDirX =
487     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
488   boost::shared_ptr<GeomDataAPI_Dir> aDirY =
489     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
490   if (!aDirX || !aDirY ||
491      (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
492      (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
493     return SLVS_E_UNKNOWN;
494
495   // quaternion parameters of normal vector
496   double qw, qx, qy, qz;
497   Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
498                       aDirY->x(), aDirY->y(), aDirY->z(),
499                       &qw, &qx, &qy, &qz);
500   double aNormCoord[4] = {qw, qx, qy, qz};
501
502   // Try to find existent normal
503   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
504     aEntIter = myEntityMap.find(theNorm);
505   std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
506   if (aEntIter == myEntityMap.end()) // no such entity => should be created
507     aParamIter = myParams.end();
508   else
509   { // the entity already exists, update it
510     int aEntPos = Search(aEntIter->second, myEntities);
511     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
512     aParamIter = myParams.begin() + aParamPos;
513   }
514
515   // Change parameters of the normal
516   Slvs_hParam aNormParams[4];
517   for (int i = 0; i < 4; i++)
518     aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
519
520   if (aEntIter != myEntityMap.end()) // the entity already exists
521     return aEntIter->second;
522
523   // Create a normal
524   Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
525                 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
526   myEntities.push_back(aNormal);
527   myEntityMap[theNorm] = aNormal.h;
528   return aNormal.h;
529 }
530
531
532 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
533                 boost::shared_ptr<SketchPlugin_Feature> theSketch)
534 {
535   if (myWorkplane.h || theSketch->getKind().compare("Sketch") != 0)
536     return false; // the workplane already exists or the function parameter is not Sketch
537
538   mySketch = theSketch;
539   updateWorkplane();
540   return true;
541 }
542
543 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateWorkplane()
544 {
545   // Get parameters of workplane
546   boost::shared_ptr<ModelAPI_Attribute> aDirX    = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
547   boost::shared_ptr<ModelAPI_Attribute> aDirY    = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
548   boost::shared_ptr<ModelAPI_Attribute> aNorm    = mySketch->data()->attribute(SKETCH_ATTR_NORM);
549   boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
550   // Transform them into SolveSpace format
551   Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
552   if (!aNormalWP) return false;
553   Slvs_hEntity anOriginWP = changeEntity(anOrigin);
554   if (!anOriginWP) return false;
555
556   if (!myWorkplane.h)
557   {
558     // Create workplane
559     myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
560     // Workplane should be added to the list of entities
561     myEntities.push_back(myWorkplane);
562   }
563   return true;
564 }
565
566
567 Slvs_hParam SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeParameter(
568                 const double&                            theParam,
569                 std::vector<Slvs_Param>::const_iterator& thePrmIter)
570 {
571   if (thePrmIter != myParams.end())
572   { // Parameter should be updated
573     int aParamPos = thePrmIter - myParams.begin();
574     if (fabs(thePrmIter->val - theParam) > tolerance)
575     {
576       myNeedToSolve = true; // parameter is changed, need to resolve constraints
577       myParams[aParamPos].val = theParam;
578     }
579     thePrmIter++;
580     return myParams[aParamPos].h;
581   }
582
583   // Newly created parameter
584   Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
585   myParams.push_back(aParam);
586   myNeedToSolve = true;
587   // The list of parameters is changed, move iterator to the end of the list to avoid problems
588   thePrmIter = myParams.end();
589   return aParam.h;
590 }
591
592 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
593                 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
594 {
595   const std::string& aConstraintKind = theConstraint->getKind();
596   // Constraint for coincidence of two points
597   if (aConstraintKind.compare("SketchConstraintCoincidence") == 0)
598   {
599     // Verify the constraint has only two attributes and they are points
600     int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point
601     int aPt3d = 0; // bit-mapped field, the same information for 3D points
602     for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
603     {
604       boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
605         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
606           theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
607         );
608       if (!anAttr) continue;
609       // Verify the attribute is a 2D point
610       boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
611         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
612       if (aPoint2D)
613       {
614         aPt2d |= (1 << indAttr);
615         continue;
616       }
617       // Verify the attribute is a 3D point
618       boost::shared_ptr<GeomDataAPI_Point> aPoint3D =
619         boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
620       if (aPoint3D)
621       {
622         aPt3d |= (1 << indAttr);
623         continue;
624       }
625       // Attribute neither 2D nor 3D point is not supported by this type of constraint
626       return SLVS_C_UNKNOWN;
627     }
628     // The constrained points should be in first and second positions,
629     // so the expected value of aPt2d or aPt3d is 3
630     if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
631       return SLVS_C_POINTS_COINCIDENT;
632     // Constraint parameters are wrong
633     return SLVS_C_UNKNOWN;
634   }
635
636   /// \todo Implement other kind of constrtaints
637
638   return SLVS_C_UNKNOWN;
639 }
640
641 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::resolveConstraints()
642 {
643   if (!myNeedToSolve)
644     return;
645
646   myConstrSolver.setGroupID(myID);
647   myConstrSolver.setParameters(myParams);
648   myConstrSolver.setEntities(myEntities);
649   myConstrSolver.setConstraints(myConstraints);
650
651   if (myConstrSolver.solve() == SLVS_RESULT_OKAY)
652   { // solution succeeded, store results into correspondent attributes
653     // Obtain result into the same list of parameters
654     if (!myConstrSolver.getResult(myParams))
655       return;
656
657     std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
658       anEntIter = myEntityMap.begin();
659     for ( ; anEntIter != myEntityMap.end(); anEntIter++)
660       updateAttribute(anEntIter->first, anEntIter->second);
661   }
662   /// \todo Implement error handling
663
664   removeTemporaryConstraints();
665   myNeedToSolve = false;
666 }
667
668 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateGroup()
669 {
670   // Check for valid sketch
671   if (!mySketch->data()->isValid())
672     return true;
673
674   // Fast check for constraint validity. If all constraints are valid, no need to update the group
675   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
676     aConstrIter = myConstraintMap.rbegin();
677   bool isAllValid = true;
678   for ( ; isAllValid && aConstrIter != myConstraintMap.rend(); aConstrIter++)
679     if (!aConstrIter->first->data()->isValid())
680       isAllValid = false;
681   if (isAllValid)
682     return false;
683
684   // Remove invalid constraints.
685   // There only constraint will be deleted (parameters and entities) will be removed below
686   std::list< boost::shared_ptr<SketchPlugin_Constraint> > aConstrToDelete;
687   std::map<Slvs_hEntity, bool> anEntToDelete; // entities will be removed if no valid constraints use them
688   for (aConstrIter = myConstraintMap.rbegin(); aConstrIter != myConstraintMap.rend(); aConstrIter++)
689   {
690     bool isValid = aConstrIter->first->data()->isValid();
691
692     int aConstrPos = Search(aConstrIter->second, myConstraints);
693     if (aConstrPos < (int)myConstraints.size())
694     {
695       Slvs_hEntity aConstrEnt[] = {
696         myConstraints[aConstrPos].ptA,     myConstraints[aConstrPos].ptB,
697         myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
698       for (int i = 0; i < 4; i++)
699         if (aConstrEnt[i] != SLVS_E_UNKNOWN)
700         {
701           if (anEntToDelete.find(aConstrEnt[i]) == anEntToDelete.end())
702             anEntToDelete[aConstrEnt[i]] = !isValid;
703           else if (isValid) // constraint is valid => no need to remove its entities
704             anEntToDelete[aConstrEnt[i]] = false;
705         }
706       if (!isValid)
707       {
708         myConstraints.erase(myConstraints.begin() + aConstrPos);
709         if (aConstrIter->second == myConstrMaxID) // When the constraint with highest ID is removed, decrease indexer
710           myConstrMaxID--;
711         aConstrToDelete.push_front(aConstrIter->first);
712       }
713     }
714   }
715   std::list< boost::shared_ptr<SketchPlugin_Constraint> >::iterator aDelIter;
716   for (aDelIter = aConstrToDelete.begin(); aDelIter != aConstrToDelete.end(); aDelIter++)
717     myConstraintMap.erase(*aDelIter);
718
719   // Remove invalid and unused entities
720   std::map<Slvs_hEntity, bool>::reverse_iterator aEDelIter;
721   for (aEDelIter = anEntToDelete.rbegin(); aEDelIter != anEntToDelete.rend(); aEDelIter++)
722     if (aEDelIter->second)
723     {
724       int anEntPos = Search(aEDelIter->first, myEntities);
725       std::vector<Slvs_Entity>::iterator aEntIter = myEntities.begin() + anEntPos;
726       // Number of parameters for the entity
727       int aNbParams = 0;
728       while (aEntIter->param[aNbParams]) aNbParams++;
729       if (aNbParams == 0) continue;
730       // Decrease parameter indexer if there are deleted parameter with higher IDs
731       if (aEntIter->param[aNbParams-1] == myParamMaxID)
732         myParamMaxID -= aNbParams;
733       // Remove parameters of the entity
734       int aParamPos = Search(aEntIter->param[0], myParams);
735       myParams.erase(myParams.begin() + aParamPos,
736                      myParams.begin() + aParamPos + aNbParams);
737
738       // Remove entity
739       if (aEDelIter->first == myEntityMaxID)
740         myEntityMaxID--;
741       myEntities.erase(myEntities.begin() + anEntPos);
742       // Remove such entity from myEntityMap
743       std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
744         anEntMapIter = myEntityMap.begin();
745       for ( ; anEntMapIter != myEntityMap.end(); anEntMapIter++)
746         if (anEntMapIter->second == aEDelIter->first)
747           break;
748       if (anEntMapIter != myEntityMap.end())
749         myEntityMap.erase(anEntMapIter);
750     }
751
752   return false;
753 }
754
755 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateAttribute(
756                 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
757                 const Slvs_hEntity&                   theEntityID)
758 {
759   // Search the position of the first parameter of the entity
760   int anEntPos = Search(theEntityID, myEntities);
761   int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
762
763   // Look over supported types of entities
764
765   // Point in 3D
766   boost::shared_ptr<GeomDataAPI_Point> aPoint =
767     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
768   if (aPoint)
769   {
770     aPoint->setValue(myParams[aFirstParamPos].val,
771                      myParams[aFirstParamPos+1].val,
772                      myParams[aFirstParamPos+2].val);
773     return ;
774   }
775
776   // Point in 2D
777   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
778     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
779   if (aPoint2D)
780   {
781     aPoint2D->setValue(myParams[aFirstParamPos].val,
782                        myParams[aFirstParamPos+1].val);
783     return ;
784   }
785
786   /// \todo Support other types of entities
787 }
788
789 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateEntityIfPossible(
790                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
791 {
792   if (myEntityMap.find(theEntity) != myEntityMap.end())
793   {
794     // If the attribute is a point and it is changed (the group needs to rebuild),
795     // probably user has dragged this point into this position,
796     // so it is necessary to add constraint which will guarantee the point will not change
797
798     // Store myNeedToSolve flag to verify the entity is really changed
799     bool aNeedToSolveCopy = myNeedToSolve;
800     myNeedToSolve = false;
801
802     changeEntity(theEntity);
803
804     if (myNeedToSolve) // the entity is changed
805     {
806       // Verify the entity is a point and add temporary constraint of permanency
807       boost::shared_ptr<GeomDataAPI_Point> aPoint =
808         boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
809       boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
810         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
811       if (aPoint || aPoint2D)
812         addTemporaryConstraintWhereDragged(theEntity);
813     }
814
815     // Restore flag of changes
816     myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
817   }
818 }
819
820 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
821                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
822 {
823   // Find identifier of the entity
824   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
825     anEntIter = myEntityMap.find(theEntity);
826
827   // Create WHERE_DRAGGED constraint
828   Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
829                                                   myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
830   myConstraints.push_back(aWDConstr);
831   myTempConstraints.push_back(aWDConstr.h);
832 }
833
834 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::removeTemporaryConstraints()
835 {
836   std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
837   for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
838   {
839     int aConstrPos = Search(*aTmpConstrIter, myConstraints);
840     myConstraints.erase(myConstraints.begin() + aConstrPos);
841
842     // If the removing constraint has higher index, decrease the indexer
843     if (*aTmpConstrIter == myConstrMaxID)
844       myConstrMaxID--;
845   }
846   myTempConstraints.clear();
847 }
848
849
850
851 // ========================================================
852 // =========      Auxiliary functions       ===============
853 // ========================================================
854
855 template <typename T>
856 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
857 {
858   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
859   int aVecSize = theEntities.size();
860   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
861     aResIndex--;
862   while (aResIndex < aVecSize && theEntities[aResIndex].h < theEntityID)
863     aResIndex++;
864   if (aResIndex == -1)
865     aResIndex = aVecSize;
866   return aResIndex;
867 }