]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SolveSpaceSolver/SolveSpaceSolver_Storage.cpp
Salome HOME
PlaneGCS: Fix the problem regarding update of a distance during moving of a line...
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_Storage.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SolveSpaceSolver_Storage.cpp
4 // Created: 18 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <SolveSpaceSolver_Storage.h>
8 #include <SolveSpaceSolver_ConstraintWrapper.h>
9 #include <SolveSpaceSolver_EntityWrapper.h>
10 #include <SolveSpaceSolver_ParameterWrapper.h>
11 #include <SolveSpaceSolver_Builder.h>
12
13 #include <GeomAPI_Dir2d.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAPI_XY.h>
16 #include <math.h>
17 #include <assert.h>
18
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <ModelAPI_AttributeDouble.h>
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <SketchPlugin_Arc.h>
24
25 /** \brief Search the entity/parameter with specified ID in the list of elements
26  *  \param[in] theEntityID unique ID of the element
27  *  \param[in] theEntities list of elements
28  *  \return position of the found element or -1 if the element is not found
29  */
30 template<typename T>
31 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
32
33 /// \brief Compare two parameters to be different
34 static bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2);
35 /// \brief Compare two entities to be different
36 static bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2);
37 /// \brief Compare two constraints to be different
38 static bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2);
39
40
41 SolveSpaceSolver_Storage::SolveSpaceSolver_Storage(const GroupID& theGroup)
42   : SketchSolver_Storage(theGroup),
43     myWorkplaneID(SLVS_E_UNKNOWN),
44     myParamMaxID(SLVS_E_UNKNOWN),
45     myEntityMaxID(SLVS_E_UNKNOWN),
46     myConstrMaxID(SLVS_C_UNKNOWN),
47     myDuplicatedConstraint(false)
48 {
49 }
50
51 bool SolveSpaceSolver_Storage::update(ConstraintWrapperPtr theConstraint)
52 {
53   bool isUpdated = false;
54   std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
55       std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
56   Slvs_Constraint aSlvsConstr = getConstraint((Slvs_hConstraint)aConstraint->id());
57   if (aSlvsConstr.h == SLVS_C_UNKNOWN)
58     aSlvsConstr = aConstraint->constraint();
59
60   // update value of constraint if exist
61   double aCoeff = aSlvsConstr.type == SLVS_C_DIAMETER ? 2.0 : 1.0;
62   if (fabs(aSlvsConstr.valA - theConstraint->value() * aCoeff) > tolerance) {
63     aSlvsConstr.valA = theConstraint->value() * aCoeff;
64     isUpdated = true;
65   }
66
67   // update constrained entities
68   Slvs_hEntity* aPnts[2] = {&aSlvsConstr.ptA, &aSlvsConstr.ptB};
69   Slvs_hEntity* anEnts[4] = {&aSlvsConstr.entityA, &aSlvsConstr.entityB,
70                              &aSlvsConstr.entityC, &aSlvsConstr.entityD};
71   int aPtInd = 0;
72   int aEntInd = 0;
73
74   std::list<EntityWrapperPtr> anEntities = theConstraint->entities();
75   std::list<EntityWrapperPtr>::iterator anIt = anEntities.begin();
76   for (; anIt != anEntities.end(); ++anIt) {
77     isUpdated = update(*anIt) || isUpdated;
78     // do not update constrained entities for Multi constraints
79     if (aSlvsConstr.type == SLVS_C_MULTI_ROTATION || aSlvsConstr.type == SLVS_C_MULTI_TRANSLATION)
80       continue;
81
82     Slvs_hEntity anID = (Slvs_hEntity)(*anIt)->id();
83     if ((*anIt)->type() == ENTITY_POINT) {
84       if (*(aPnts[aPtInd]) != anID) {
85         *(aPnts[aPtInd]) = anID;
86         isUpdated = true;
87       }
88       ++aPtInd;
89     } else {
90       if (*(anEnts[aEntInd]) != anID) {
91         *(anEnts[aEntInd]) = anID;
92         isUpdated = true;
93       }
94       ++aEntInd;
95     }
96   }
97
98   // update constraint itself (do not update constraints Multi)
99   if (aSlvsConstr.type != SLVS_C_MULTI_ROTATION && aSlvsConstr.type != SLVS_C_MULTI_TRANSLATION) {
100     if (aSlvsConstr.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
101       aSlvsConstr.wrkpl = myWorkplaneID;
102     if (aSlvsConstr.group == SLVS_G_UNKNOWN)
103       aSlvsConstr.group = (Slvs_hGroup)myGroupID;
104     bool hasDupConstraints = myDuplicatedConstraint;
105     Slvs_hConstraint aConstrID = updateConstraint(aSlvsConstr);
106     if (aSlvsConstr.h == SLVS_C_UNKNOWN) {
107       aConstraint->changeConstraint() = getConstraint(aConstrID);
108       isUpdated = true;
109       // check duplicated constraints based on different attributes
110       if (myDuplicatedConstraint && findSameConstraint(aConstraint) && !hasDupConstraints)
111         myDuplicatedConstraint = false;
112     }
113   }
114   return isUpdated;
115 }
116
117 bool SolveSpaceSolver_Storage::update(EntityWrapperPtr theEntity)
118 {
119   bool isUpdated = false;
120   std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity = 
121       std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
122   Slvs_Entity aSlvsEnt = getEntity((Slvs_hEntity)anEntity->id());
123   if (aSlvsEnt.h == SLVS_E_UNKNOWN)
124     aSlvsEnt = anEntity->entity();
125
126   std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
127   std::list<ParameterWrapperPtr>::iterator aPIt;
128   // if the entity is an attribute, need to update its coordinates
129   if (anEntity->baseAttribute()) {
130     BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
131     EntityWrapperPtr anUpdAttr = aBuilder->createAttribute(anEntity->baseAttribute(), GID_UNKNOWN);
132     if (anUpdAttr) {
133       std::list<ParameterWrapperPtr> anUpdParams = anUpdAttr->parameters();
134       std::list<ParameterWrapperPtr>::iterator anUpdIt = anUpdParams.begin();
135       for (aPIt = aParams.begin(); aPIt != aParams.end() && anUpdIt != anUpdParams.end();
136           ++aPIt, ++anUpdIt) {
137         (*aPIt)->update(*anUpdIt);
138       }
139     }
140   }
141
142   // update parameters
143   int anInd = 0;
144   for (aPIt = aParams.begin(); aPIt != aParams.end(); ++aPIt, ++anInd) {
145     assert(anInd < 4);
146     isUpdated = update(*aPIt) || isUpdated;
147     if (aSlvsEnt.param[anInd] != (Slvs_hEntity)(*aPIt)->id()) {
148       isUpdated = true;
149       aSlvsEnt.param[anInd] = (Slvs_hEntity)(*aPIt)->id();
150     }
151   }
152
153   // update sub-entities
154   std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
155   std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
156   for (anInd = 0; aSIt != aSubEntities.end(); ++aSIt, ++anInd) {
157     assert(anInd < 4);
158     isUpdated = update(*aSIt) || isUpdated;
159
160     Slvs_hEntity anID = Slvs_hEntity((*aSIt)->id());
161     if ((*aSIt)->type() == ENTITY_NORMAL)
162       aSlvsEnt.normal = anID;
163     else if ((*aSIt)->type() == ENTITY_SCALAR)
164       aSlvsEnt.distance = anID;
165     else if (aSlvsEnt.point[anInd] != anID) {
166       aSlvsEnt.point[anInd] = anID;
167       if ((*aSIt)->baseAttribute())
168         SketchSolver_Storage::addEntity((*aSIt)->baseAttribute(), *aSIt);
169       isUpdated = true;
170     }
171   }
172   if (theEntity->type() == ENTITY_POINT && aSubEntities.size() == 1) {
173     // theEntity is based on SketchPlugin_Point => need to substitute its attribute instead
174     bool isNew = (aSlvsEnt.h == SLVS_E_UNKNOWN);
175     aSlvsEnt = getEntity(aSlvsEnt.point[0]);
176     if (isNew) {
177       anEntity->changeEntity() = aSlvsEnt;
178       isUpdated = true;
179     }
180   }
181
182   // update entity itself
183   if (aSlvsEnt.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
184     aSlvsEnt.wrkpl = myWorkplaneID;
185   if (aSlvsEnt.group == SLVS_G_UNKNOWN)
186     aSlvsEnt.group = (Slvs_hGroup)myGroupID;
187   Slvs_hEntity anEntID = updateEntity(aSlvsEnt);
188   if (aSlvsEnt.h == SLVS_E_UNKNOWN || isUpdated) {
189     anEntity->changeEntity() = getEntity(anEntID);
190     isUpdated = true;
191
192     if (anEntity->type() == ENTITY_SKETCH)
193       storeWorkplane(anEntity);
194   }
195
196   return isUpdated;
197 }
198
199 bool SolveSpaceSolver_Storage::update(ParameterWrapperPtr theParameter)
200 {
201   std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aParameter = 
202       std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(theParameter);
203   const Slvs_Param& aParam = getParameter((Slvs_hParam)aParameter->id());
204   if (aParam.h != SLVS_E_UNKNOWN && fabs(aParam.val - aParameter->value()) < tolerance)
205     return false;
206   Slvs_Param aParamToUpd = aParameter->parameter();
207   if (aParamToUpd.group == SLVS_G_UNKNOWN)
208     aParamToUpd.group = aParameter->isParametric() ? (Slvs_hGroup)GID_OUTOFGROUP : (Slvs_hGroup)myGroupID;
209   Slvs_hParam anID = updateParameter(aParamToUpd);
210   if (aParam.h == SLVS_E_UNKNOWN) // new parameter
211     aParameter->changeParameter() = getParameter(anID);
212   return true;
213 }
214
215 void SolveSpaceSolver_Storage::storeWorkplane(EntityWrapperPtr theSketch)
216 {
217   mySketchID = theSketch->id();
218   myWorkplaneID = (Slvs_hEntity)mySketchID;
219
220   // Update sub-entities of the sketch
221   std::list<EntityWrapperPtr> aSubEntities = theSketch->subEntities();
222   std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
223   for (; aSIt != aSubEntities.end(); ++aSIt) {
224     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSub =
225         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*aSIt);
226     aSub->changeEntity().wrkpl = myWorkplaneID;
227   }
228
229   // Update all stored entities
230   std::vector<Slvs_Entity>::iterator anIt = myEntities.begin();
231   for (; anIt != myEntities.end(); ++anIt)
232     anIt->wrkpl = myWorkplaneID;
233 }
234
235 void SolveSpaceSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
236 {
237   std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
238   std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
239   for (; aPIt != aParams.end(); ++aPIt)
240     changeGroup(*aPIt, theGroup);
241
242   std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
243   std::list<EntityWrapperPtr>::iterator aSIt = aSubs.begin();
244   for (; aSIt != aSubs.end(); ++aSIt)
245     changeGroup(*aSIt, theGroup);
246
247   if (theEntity->group() != theGroup) {
248     theEntity->setGroup(theGroup);
249     int aPos = Search((Slvs_hEntity)theEntity->id(), myEntities);
250     if (aPos >= 0 && aPos < (int)myEntities.size()) {
251       myEntities[aPos].group = (Slvs_hGroup)theGroup;
252       setNeedToResolve(true);
253     }
254   }
255 }
256
257 void SolveSpaceSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
258 {
259   GroupID aGroup = theGroup;
260   if (theParam->isParametric())
261     aGroup = GID_OUTOFGROUP;
262   if (theParam->group() == aGroup)
263     return;
264
265   theParam->setGroup(aGroup);
266   int aPos = Search((Slvs_hParam)theParam->id(), myParameters);
267   if (aPos >= 0 && aPos < (int)myParameters.size()) {
268     myParameters[aPos].group = (Slvs_hGroup)aGroup;
269     setNeedToResolve(true);
270   }
271 }
272
273 void SolveSpaceSolver_Storage::addCoincidentPoints(
274     EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
275 {
276   if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
277     return;
278   if (!theMaster->subEntities().empty() || !theSlave->subEntities().empty()) {
279     EntityWrapperPtr aSubMaster = theMaster->subEntities().empty() ?
280         theMaster : theMaster->subEntities().front();
281     EntityWrapperPtr aSubSlave = theSlave->subEntities().empty() ?
282         theSlave : theSlave->subEntities().front();
283     return addCoincidentPoints(aSubMaster, aSubSlave);
284   }
285
286   // Search available coincidence
287   CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(theMaster);
288   CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(theSlave);
289   if (aMasterFound == myCoincidentPoints.end() || aSlaveFound == myCoincidentPoints.end()) {
290     // try to find master and slave points in the lists of slaves of already existent coincidences
291     CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
292     for (; anIt != myCoincidentPoints.end(); ++anIt) {
293       if (anIt->second.find(theMaster) != anIt->second.end())
294         aMasterFound = anIt;
295       else if (anIt->second.find(theSlave) != anIt->second.end())
296         aSlaveFound = anIt;
297
298       if (aMasterFound != myCoincidentPoints.end() &&  aSlaveFound != myCoincidentPoints.end())
299         break;
300     }
301   }
302
303   if (aMasterFound == myCoincidentPoints.end()) {
304     // create new group
305     myCoincidentPoints[theMaster] = std::set<EntityWrapperPtr>();
306     aMasterFound = myCoincidentPoints.find(theMaster);
307   } else if (aMasterFound == aSlaveFound)
308     return; // already coincident
309
310   if (aSlaveFound != myCoincidentPoints.end()) {
311     // A slave has been found, we need to attach all points coincident with it to the new master
312     std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
313     aNewSlaves.insert(aSlaveFound->first);
314     myCoincidentPoints.erase(aSlaveFound);
315
316     std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
317     for (; aSlIt != aNewSlaves.end(); ++aSlIt)
318       addCoincidentPoints(theMaster, *aSlIt);
319   } else {
320     // Update the slave if it was used in constraints and features
321     replaceInFeatures(theSlave, theMaster);
322     replaceInConstraints(theSlave, theMaster);
323
324     // Remove slave entity (if the IDs are equal no need to remove slave entity, just update it)
325     if (theMaster->id() != theSlave->id())
326       removeEntity((Slvs_hEntity)theSlave->id());
327
328     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointMaster = 
329         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMaster);
330     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointSlave = 
331         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSlave);
332     aPointSlave->changeEntity() = aPointMaster->entity();
333     aPointSlave->setParameters(aPointMaster->parameters());
334
335     aMasterFound->second.insert(theSlave);
336   }
337 }
338
339 void SolveSpaceSolver_Storage::replaceInFeatures(
340     EntityWrapperPtr theSource, EntityWrapperPtr theDest)
341 {
342   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator anIt = myFeatureMap.begin();
343   for (; anIt != myFeatureMap.end(); ++anIt) {
344     if (!anIt->second)
345       continue;
346     bool isUpdated = false;
347     std::list<EntityWrapperPtr> aSubs = anIt->second->subEntities();
348     std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
349     for (; aSubIt != aSubs.end(); ++aSubIt)
350       if ((*aSubIt)->id() == theSource->id()) {
351         (*aSubIt)->update(theDest);
352         isUpdated = true;
353       }
354
355     if (!isUpdated)
356       continue;
357
358     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aWrapper =
359         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(anIt->second);
360     // update SolveSpace entity
361     Slvs_Entity anEnt = aWrapper->entity();
362     for (int i = 0; i < 4; ++i)
363       if (anEnt.point[i] == (Slvs_hEntity)theSource->id())
364         anEnt.point[i] = (Slvs_hEntity)theDest->id();
365     anEnt.h = updateEntity(anEnt);
366     aWrapper->changeEntity() = anEnt;
367
368     // update sub-entities
369     aWrapper->setSubEntities(aSubs);
370   }
371 }
372
373 void SolveSpaceSolver_Storage::replaceInConstraints(
374     EntityWrapperPtr theSource, EntityWrapperPtr theDest)
375 {
376   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
377       anIt = myConstraintMap.begin();
378   std::list<ConstraintWrapperPtr>::const_iterator aCIt;
379   for (; anIt != myConstraintMap.end(); ++anIt)
380     for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt) {
381       // Do not process coincidence between points and "multi" constraints
382       // (these constraints are stored to keep the structure of constraints).
383       if ((*aCIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
384           (*aCIt)->type() == CONSTRAINT_MULTI_ROTATION ||
385           (*aCIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
386         continue;
387
388       bool isUpdated = false;
389       std::list<EntityWrapperPtr> aSubs = (*aCIt)->entities();
390       std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
391       for (; aSubIt != aSubs.end(); ++aSubIt)
392         if ((*aSubIt)->id() == theSource->id()) {
393           (*aSubIt)->update(theDest);
394           isUpdated = true;
395         }
396
397       if (!isUpdated)
398         continue;
399
400       std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aWrapper =
401           std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(*aCIt);
402       if (theSource->id() == theDest->id()) {
403         // No need to update SolveSpace constraint if the entities are the same
404         aWrapper->changeConstraint() = getConstraint((Slvs_hConstraint)aWrapper->id());
405         aWrapper->setEntities(aSubs);
406         continue;
407       }
408
409       // change constraint entities
410       Slvs_Constraint aConstr = aWrapper->constraint();
411       if (aConstr.ptA == (Slvs_hEntity)theSource->id())
412         aConstr.ptA = (Slvs_hEntity)theDest->id();
413       if (aConstr.ptB == (Slvs_hEntity)theSource->id())
414         aConstr.ptB = (Slvs_hEntity)theDest->id();
415
416       // check the constraint is duplicated
417       std::vector<Slvs_Constraint>::const_iterator aSlvsCIt = myConstraints.begin();
418       for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
419         if (aConstr.h != aSlvsCIt->h &&
420             aConstr.type == aSlvsCIt->type &&
421             aConstr.ptA == aSlvsCIt->ptA && aConstr.ptB == aSlvsCIt->ptB &&
422             aConstr.entityA == aSlvsCIt->entityA && aConstr.entityB == aSlvsCIt->entityB &&
423             aConstr.entityC == aSlvsCIt->entityC && aConstr.entityD == aSlvsCIt->entityD) {
424           Slvs_hConstraint anIDToRemove = aConstr.h;
425           aConstr = *aSlvsCIt;
426           int aShift = (int)(aSlvsCIt - myConstraints.begin());
427           removeConstraint(anIDToRemove);
428           aSlvsCIt = myConstraints.begin() + aShift - 1;
429           for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
430             if (aSlvsCIt->h == aConstr.h)
431               break;
432           break;
433         }
434
435       if (aSlvsCIt != myConstraints.end()) {
436         // constraint is duplicated, search its wrapper to add the mapping
437         std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
438             anIt2 = myConstraintMap.begin();
439         std::list<ConstraintWrapperPtr>::const_iterator aCIt2;
440         for (; anIt2 != myConstraintMap.end(); ++anIt2)
441           for (aCIt2 = anIt2->second.begin(); aCIt2 != anIt2->second.end(); ++aCIt2)
442             if ((Slvs_hConstraint)(*aCIt2)->id() == aConstr.h) {
443               addSameConstraints(*aCIt2, aWrapper);
444               break;
445             }
446       } else 
447         aConstr.h = updateConstraint(aConstr);
448       aWrapper->changeConstraint() = aConstr;
449
450       // update sub-entities
451       aWrapper->setEntities(aSubs);
452     }
453 }
454
455 void SolveSpaceSolver_Storage::addSameConstraints(ConstraintWrapperPtr theConstraint1,
456                                                   ConstraintWrapperPtr theConstraint2)
457 {
458   SameConstraintMap::iterator anIt = myEqualConstraints.begin();
459   for (; anIt != myEqualConstraints.end(); ++anIt) {
460     if (anIt->find(theConstraint1) != anIt->end()) {
461       anIt->insert(theConstraint2);
462       return;
463     }
464     else if (anIt->find(theConstraint2) != anIt->end()) {
465       anIt->insert(theConstraint1);
466       return;
467     }
468   }
469   // group not found => create new one
470   std::set<ConstraintWrapperPtr> aNewGroup;
471   aNewGroup.insert(theConstraint1);
472   aNewGroup.insert(theConstraint2);
473   myEqualConstraints.push_back(aNewGroup);
474 }
475
476 bool SolveSpaceSolver_Storage::findSameConstraint(ConstraintWrapperPtr theConstraint)
477 {
478   if (theConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT ||
479       theConstraint->type() == CONSTRAINT_MULTI_ROTATION ||
480       theConstraint->type() == CONSTRAINT_MULTI_TRANSLATION)
481     return false;
482
483   const Slvs_Constraint& aCBase =
484       std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint)->constraint();
485
486   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
487       aCIt = myConstraintMap.begin();
488   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
489   for (; aCIt != myConstraintMap.end(); ++aCIt)
490     for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt) {
491       if ((*aCWIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
492           (*aCWIt)->type() == CONSTRAINT_MULTI_ROTATION ||
493           (*aCWIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
494         continue;
495       if ((*aCWIt)->type() == theConstraint->type()) {
496         const Slvs_Constraint& aCComp = getConstraint((Slvs_hConstraint)(*aCWIt)->id());
497
498         if (aCBase.ptA == aCComp.ptA && aCBase.ptB == aCComp.ptB &&
499             aCBase.entityA == aCComp.entityA && aCBase.entityB == aCComp.entityB &&
500             aCBase.entityC == aCComp.entityC && aCBase.entityD == aCComp.entityD &&
501             fabs(aCBase.valA -aCComp.valA) < tolerance) {
502           addSameConstraints(*aCWIt, theConstraint);
503           return true;
504         }
505       }
506     }
507   return false;
508 }
509
510
511 EntityWrapperPtr SolveSpaceSolver_Storage::calculateMiddlePoint(
512     EntityWrapperPtr theBase, double theCoeff)
513 {
514   BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
515
516   std::shared_ptr<GeomAPI_XY> aMidPoint;
517   if (theBase->type() == ENTITY_LINE) {
518     std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
519     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
520     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
521     for (int i = 0; i < 2; ++i, ++anIt)
522       aPoints[i] = aBuilder->point(*anIt);
523     aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
524         aPoints[1]->xy()->multiplied(theCoeff));
525   }
526   else if (theBase->type() == ENTITY_ARC) {
527     double theX, theY;
528     double anArcPoint[3][2];
529     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
530     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
531     for (int i = 0; i < 3; ++i, ++anIt) {
532       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
533       anArcPoint[i][0] = aPoint->x();
534       anArcPoint[i][1] = aPoint->y();
535     }
536     // project last point of arc on the arc
537     double x = anArcPoint[1][0] - anArcPoint[0][0];
538     double y = anArcPoint[1][1] - anArcPoint[0][1];
539     double aRad = sqrt(x*x + y*y);
540     x = anArcPoint[2][0] - anArcPoint[0][0];
541     y = anArcPoint[2][1] - anArcPoint[0][1];
542     double aNorm = sqrt(x*x + y*y);
543     if (aNorm >= tolerance) {
544       anArcPoint[2][0] = x * aRad / aNorm;
545       anArcPoint[2][1] = y * aRad / aNorm;
546     }
547     anArcPoint[1][0] -= anArcPoint[0][0];
548     anArcPoint[1][1] -= anArcPoint[0][1];
549     if (theCoeff < tolerance) {
550       theX = anArcPoint[0][0] + anArcPoint[1][0];
551       theY = anArcPoint[0][1] + anArcPoint[1][1];
552     } else if (1 - theCoeff < tolerance) {
553       theX = anArcPoint[0][0] + anArcPoint[2][0];
554       theY = anArcPoint[0][1] + anArcPoint[2][1];
555     } else {
556       std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
557       std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
558       double anAngle = aStartDir->angle(aEndDir);
559       if (anAngle < 0)
560         anAngle += 2.0 * PI;
561       anAngle *= theCoeff;
562       double aCos = cos(anAngle);
563       double aSin = sin(anAngle);
564       theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
565       theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
566     }
567     aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
568   }
569
570   if (!aMidPoint)
571     return EntityWrapperPtr();
572
573   std::list<ParameterWrapperPtr> aParameters;
574   Slvs_Param aParam1 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->x());
575   aParam1.h = addParameter(aParam1);
576   aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam1)));
577   Slvs_Param aParam2 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->y());
578   aParam2.h = addParameter(aParam2);
579   aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam2)));
580   // Create entity (parameters are not filled)
581   Slvs_Entity anEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
582       (Slvs_hEntity)myWorkplaneID, aParam1.h, aParam2.h);
583   anEntity.h = addEntity(anEntity);
584
585   EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(AttributePtr(), anEntity));
586   aResult->setParameters(aParameters);
587   return aResult;
588 }
589
590
591
592
593
594
595 Slvs_hParam SolveSpaceSolver_Storage::addParameter(const Slvs_Param& theParam)
596 {
597   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
598     // parameter is already used, rewrite it
599     return updateParameter(theParam);
600   }
601
602   Slvs_Param aParam = theParam;
603   if (aParam.h > myParamMaxID)
604     myParamMaxID = aParam.h;
605   else
606     aParam.h = ++myParamMaxID;
607   myParameters.push_back(aParam);
608   myNeedToResolve = true;
609   return aParam.h;
610 }
611
612 Slvs_hParam SolveSpaceSolver_Storage::updateParameter(const Slvs_Param& theParam)
613 {
614   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
615     // parameter already used, rewrite it
616     int aPos = Search(theParam.h, myParameters);
617     if (aPos >= 0 && aPos < (int)myParameters.size()) {
618       if (IsNotEqual(myParameters[aPos], theParam))
619         setNeedToResolve(true);
620       myParameters[aPos] = theParam;
621       return theParam.h;
622     }
623   }
624
625   // Parameter is not found, add new one
626   Slvs_Param aParam = theParam;
627   aParam.h = 0;
628   return addParameter(aParam);
629 }
630
631 bool SolveSpaceSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
632 {
633   int aPos = Search(theParamID, myParameters);
634   if (aPos >= 0 && aPos < (int)myParameters.size()) {
635     // Firstly, search the parameter is not used elsewhere
636     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
637     for (; anEntIter != myEntities.end(); anEntIter++) {
638       for (int i = 0; i < 4; i++)
639         if (anEntIter->param[i] == theParamID)
640           return false;
641     }
642     // Remove parameter
643     myParameters.erase(myParameters.begin() + aPos);
644     myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
645     myNeedToResolve = true;
646   }
647   return true;
648 }
649
650 const Slvs_Param& SolveSpaceSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
651 {
652   int aPos = Search(theParamID, myParameters);
653   if (aPos >= 0 && aPos < (int)myParameters.size())
654     return myParameters[aPos];
655
656   // Parameter is not found, return empty object
657   static Slvs_Param aDummy;
658   aDummy.h = 0;
659   return aDummy;
660 }
661
662
663 Slvs_hEntity SolveSpaceSolver_Storage::addEntity(const Slvs_Entity& theEntity)
664 {
665   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
666     // Entity is already used, rewrite it
667     return updateEntity(theEntity);
668   }
669
670   Slvs_Entity aEntity = theEntity;
671   if (aEntity.h > myEntityMaxID)
672     myEntityMaxID = aEntity.h;
673   else
674     aEntity.h = ++myEntityMaxID;
675   myEntities.push_back(aEntity);
676   myNeedToResolve = true;
677   return aEntity.h;
678 }
679
680 Slvs_hEntity SolveSpaceSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
681 {
682   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
683     // Entity already used, rewrite it
684     int aPos = Search(theEntity.h, myEntities);
685     if (aPos >= 0 && aPos < (int)myEntities.size()) {
686       myNeedToResolve = myNeedToResolve || IsNotEqual(myEntities[aPos], theEntity);
687       myEntities[aPos] = theEntity;
688       return theEntity.h;
689     }
690   }
691
692   // Entity is not found, add new one
693   Slvs_Entity aEntity = theEntity;
694   aEntity.h = 0;
695   return addEntity(aEntity);
696 }
697
698 bool SolveSpaceSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
699 {
700   bool aResult = true;
701   int aPos = Search(theEntityID, myEntities);
702   if (aPos >= 0 && aPos < (int)myEntities.size()) {
703     // Firstly, check the entity and its attributes is not used elsewhere
704     std::set<Slvs_hEntity> anEntAndSubs;
705     anEntAndSubs.insert(theEntityID);
706     for (int i = 0; i < 4; i++)
707       if (myEntities[aPos].point[i] != SLVS_E_UNKNOWN)
708         anEntAndSubs.insert(myEntities[aPos].point[i]);
709
710     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
711     for (; anEntIter != myEntities.end(); anEntIter++) {
712       if (anEntAndSubs.find(anEntIter->h) != anEntAndSubs.end())
713         continue;
714       for (int i = 0; i < 4; i++)
715         if (anEntAndSubs.find(anEntIter->point[i]) != anEntAndSubs.end())
716           return false;
717       if (anEntAndSubs.find(anEntIter->distance) != anEntAndSubs.end())
718         return false;
719     }
720     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
721     for (; aConstrIter != myConstraints.end(); aConstrIter++) {
722       Slvs_hEntity anEntIDs[6] = {aConstrIter->ptA, aConstrIter->ptB,
723           aConstrIter->entityA, aConstrIter->entityB,
724           aConstrIter->entityC, aConstrIter->entityD};
725       for (int i = 0; i < 6; i++)
726         if (anEntAndSubs.find(anEntIDs[i]) != anEntAndSubs.end())
727           return false;
728     }
729     // The entity is not used, remove it and its parameters
730     Slvs_Entity anEntity = myEntities[aPos];
731     myEntities.erase(myEntities.begin() + aPos);
732     myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
733     if (anEntity.distance != SLVS_E_UNKNOWN)
734       aResult = aResult && removeEntity(anEntity.distance);
735     for (int i = 0; i < 4; i++)
736       if (anEntity.param[i] != SLVS_E_UNKNOWN)
737         aResult = removeParameter(anEntity.param[i]) && aResult;
738     myNeedToResolve = true;
739   }
740   return aResult;
741 }
742
743 const Slvs_Entity& SolveSpaceSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
744 {
745   int aPos = Search(theEntityID, myEntities);
746   if (aPos >= 0 && aPos < (int)myEntities.size())
747     return myEntities[aPos];
748
749   // Entity is not found, return empty object
750   static Slvs_Entity aDummy;
751   aDummy.h = SLVS_E_UNKNOWN;
752   return aDummy;
753 }
754
755
756 Slvs_hConstraint SolveSpaceSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
757 {
758   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
759     // Constraint is already used, rewrite it
760     return updateConstraint(theConstraint);
761   }
762
763   Slvs_Constraint aConstraint = theConstraint;
764
765   // Find a constraint with same type uses same arguments to show user overconstraint situation
766   std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
767   for (; aCIt != myConstraints.end(); aCIt++) {
768     if (aConstraint.type != aCIt->type)
769       continue;
770     if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
771         aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
772         aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
773       myDuplicatedConstraint = true;
774       break;
775     }
776   }
777
778   if (aConstraint.h > myConstrMaxID)
779     myConstrMaxID = aConstraint.h;
780   else
781     aConstraint.h = ++myConstrMaxID;
782   myConstraints.push_back(aConstraint);
783   myNeedToResolve = true;
784   return aConstraint.h;
785 }
786
787 Slvs_hConstraint SolveSpaceSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
788 {
789   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
790     // Constraint already used, rewrite it
791     int aPos = Search(theConstraint.h, myConstraints);
792     if (aPos >= 0 && aPos < (int)myConstraints.size()) {
793       myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
794       myConstraints[aPos] = theConstraint;
795       return theConstraint.h;
796     }
797   }
798
799   // Constraint is not found, add new one
800   Slvs_Constraint aConstraint = theConstraint;
801   aConstraint.h = 0;
802
803   // Firstly, check middle-point constraint conflicts with point-on-line
804   if (aConstraint.type == SLVS_C_AT_MIDPOINT) {
805     std::vector<Slvs_Constraint>::const_iterator anIt = myConstraints.begin();
806     for (; anIt != myConstraints.end(); ++anIt)
807       if (anIt->type == SLVS_C_PT_ON_LINE &&
808           anIt->ptA == aConstraint.ptA &&
809           anIt->entityA == aConstraint.entityA)
810         break;
811     if (anIt != myConstraints.end()) {
812       // change the constraint to the lengths equality to avoid conflicts
813       Slvs_Entity aLine = getEntity(aConstraint.entityA);
814       Slvs_Entity aNewLine1 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
815           myWorkplaneID, aLine.point[0], aConstraint.ptA);
816       aNewLine1.h = addEntity(aNewLine1);
817       Slvs_Entity aNewLine2 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
818           myWorkplaneID, aLine.point[1], aConstraint.ptA);
819       aNewLine2.h = addEntity(aNewLine2);
820       aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, SLVS_C_EQUAL_LENGTH_LINES,
821           myWorkplaneID, 0.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aNewLine1.h, aNewLine2.h);
822     }
823   }
824
825   return addConstraint(aConstraint);
826 }
827
828 bool SolveSpaceSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
829 {
830   int aPos = Search(theConstraintID, myConstraints);
831   if (aPos >= 0 && aPos < (int)myConstraints.size()) {
832     Slvs_Constraint aConstraint = myConstraints[aPos];
833     myConstraints.erase(myConstraints.begin() + aPos);
834     myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
835     myNeedToResolve = true;
836
837     if (myDuplicatedConstraint) {
838       // Find a constraint with same type uses same arguments
839       std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
840       for (; aCIt != myConstraints.end(); aCIt++) {
841         if (aConstraint.type != aCIt->type)
842           continue;
843         if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
844             aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
845             aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
846           myDuplicatedConstraint = false;
847           break;
848         }
849       }
850     }
851   }
852   return true;
853 }
854
855 const Slvs_Constraint& SolveSpaceSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
856 {
857   int aPos = Search(theConstraintID, myConstraints);
858   if (aPos >= 0 && aPos < (int)myConstraints.size())
859     return myConstraints[aPos];
860
861   // Constraint is not found, return empty object
862   static Slvs_Constraint aDummy;
863   aDummy.h = 0;
864   return aDummy;
865 }
866
867
868 void SolveSpaceSolver_Storage::initializeSolver(SolverPtr theSolver)
869 {
870   std::shared_ptr<SolveSpaceSolver_Solver> aSolver =
871       std::dynamic_pointer_cast<SolveSpaceSolver_Solver>(theSolver);
872   if (!aSolver)
873     return;
874
875   if (myExistArc)
876     processArcs();
877
878   if (myConstraints.empty()) {
879     // Adjust all arc to place their points correctly
880     std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
881     for (; anEntIt != myEntities.end(); ++anEntIt)
882       if (anEntIt->type == SLVS_E_ARC_OF_CIRCLE)
883         adjustArc(*anEntIt);
884   }
885
886   aSolver->setParameters(myParameters.data(), (int)myParameters.size());
887   aSolver->setEntities(myEntities.data(), (int)myEntities.size());
888   aSolver->setConstraints(myConstraints.data(), (int)myConstraints.size());
889 }
890
891
892 bool SolveSpaceSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
893 {
894   std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
895       std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
896
897   // verify whether the constraint has duplicated
898   bool hasSameID = false;
899   SameConstraintMap::iterator anEqIt = myEqualConstraints.begin();
900   for (; anEqIt != myEqualConstraints.end(); ++anEqIt) {
901     std::set<ConstraintWrapperPtr>::const_iterator aFound = anEqIt->find(aConstraint);
902     if (aFound != anEqIt->end()) {
903       // verify there is a constraint with same ID
904       std::set<ConstraintWrapperPtr>::const_iterator anIt = anEqIt->begin();
905       ConstraintID anID = (*aFound)->id();
906       for (++anIt; anIt != anEqIt->end() && !hasSameID; ++anIt)
907         if ((*anIt)->id() == anID && aFound != anIt)
908           hasSameID = true;
909       // erase constraint
910       anEqIt->erase(aConstraint);
911       break;
912     }
913   }
914   if (anEqIt != myEqualConstraints.end() && hasSameID)
915     return true;
916
917   bool isFullyRemoved = removeConstraint((Slvs_hConstraint)aConstraint->id());
918   // remove point-point coincidence
919   if (aConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT)
920     isFullyRemoved = removeCoincidence(theConstraint) && isFullyRemoved;
921   return SketchSolver_Storage::remove(theConstraint) && isFullyRemoved;
922 }
923
924 bool SolveSpaceSolver_Storage::remove(EntityWrapperPtr theEntity)
925 {
926   if (!theEntity)
927     return false;
928
929   // Additional check for entity to be used in point-point coincidence
930   bool isCoincide = false;
931   if (theEntity->type() == ENTITY_POINT) {
932     CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
933     std::set<EntityWrapperPtr>::const_iterator aCIt;
934     for (; anIt != myCoincidentPoints.end(); ++anIt) {
935       if (anIt->first == theEntity)
936         break;
937       for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt)
938         if (*aCIt == theEntity)
939           break;
940       if (aCIt != anIt->second.end())
941         break;
942     }
943     if (anIt != myCoincidentPoints.end()) {
944       if (anIt->first != theEntity && isUsed(anIt->first->baseAttribute()))
945         isCoincide = true;
946       for (aCIt = anIt->second.begin(); !isCoincide && aCIt != anIt->second.end(); ++aCIt)
947         if (*aCIt != theEntity && isUsed((*aCIt)->baseAttribute()))
948           isCoincide = true;
949     }
950   }
951
952   std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity = 
953         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
954   bool isFullyRemoved = isCoincide ? true : removeEntity((Slvs_hEntity)anEntity->id());
955   return (SketchSolver_Storage::remove(theEntity) || isCoincide) && isFullyRemoved;
956 }
957
958 bool SolveSpaceSolver_Storage::remove(ParameterWrapperPtr theParameter)
959 {
960   return removeParameter((Slvs_hParam)theParameter->id());
961 }
962
963
964 void SolveSpaceSolver_Storage::refresh(bool theFixedOnly) const
965 {
966   //blockEvents(true);
967
968   std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
969   std::list<ParameterWrapperPtr> aParams;
970   std::list<ParameterWrapperPtr>::const_iterator aParIt;
971   for (; anIt != myAttributeMap.end(); ++anIt) {
972     if (!anIt->second)
973       continue;
974     // the external feature always should keep the up to date values, so, 
975     // refresh from the solver is never needed
976     if (anIt->first.get()) {
977       std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
978         std::dynamic_pointer_cast<SketchPlugin_Feature>(anIt->first->owner());
979       if (aSketchFeature.get() && aSketchFeature->isExternal())
980         continue;
981       // not need to refresh here sketch's origin and normal vector
982       CompositeFeaturePtr aSketch =
983           std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anIt->first->owner());
984       if (aSketch)
985         continue;
986     }
987
988     // update parameter wrappers and obtain values of attributes
989     aParams = anIt->second->parameters();
990     double aCoords[3];
991     bool isUpd[3] = {false};
992     int i = 0;
993     for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
994       std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aWrapper = 
995           std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(*aParIt);
996       if (!theFixedOnly || aWrapper->group() == GID_OUTOFGROUP || aWrapper->isParametric()) {
997         aWrapper->changeParameter().val = getParameter((Slvs_hParam)aWrapper->id()).val;
998         aCoords[i] = aWrapper->value();
999         isUpd[i] = true;
1000       }
1001     }
1002     if (!isUpd[0] && !isUpd[1] && !isUpd[2])
1003       continue; // nothing is updated
1004
1005     std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1006         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
1007     if (aPoint2D) {
1008       if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
1009           (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance)) {
1010         if (!isUpd[0]) aCoords[0] = aPoint2D->x();
1011         if (!isUpd[1]) aCoords[1] = aPoint2D->y();
1012         aPoint2D->setValue(aCoords[0], aCoords[1]);
1013         // Find points coincident with this one (probably not in GID_OUTOFGROUP)
1014         std::map<AttributePtr, EntityWrapperPtr>::const_iterator aLocIt;
1015         if (theFixedOnly) 
1016           aLocIt = myAttributeMap.begin();
1017         else {
1018           aLocIt = anIt;
1019           ++aLocIt;
1020         }
1021         for (; aLocIt != myAttributeMap.end(); ++aLocIt) {
1022           if (!aLocIt->second)
1023             continue;
1024           std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
1025             std::dynamic_pointer_cast<SketchPlugin_Feature>(aLocIt->first->owner());
1026           if (aSketchFeature && aSketchFeature->isExternal())
1027             continue;
1028           if (anIt->second->id() == aLocIt->second->id()) {
1029             aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aLocIt->first);
1030             aPoint2D->setValue(aCoords[0], aCoords[1]);
1031           }
1032         }
1033       }
1034       continue;
1035     }
1036     AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
1037     if (aScalar) {
1038       if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
1039         aScalar->setValue(aCoords[0]);
1040       continue;
1041     }
1042     std::shared_ptr<GeomDataAPI_Point> aPoint =
1043         std::dynamic_pointer_cast<GeomDataAPI_Point>(anIt->first);
1044     if (aPoint) {
1045       if ((isUpd[0] && fabs(aPoint->x() - aCoords[0]) > tolerance) ||
1046           (isUpd[1] && fabs(aPoint->y() - aCoords[1]) > tolerance) ||
1047           (isUpd[2] && fabs(aPoint->z() - aCoords[2]) > tolerance))
1048         if (!isUpd[0]) aCoords[0] = aPoint->x();
1049         if (!isUpd[1]) aCoords[1] = aPoint->y();
1050         if (!isUpd[2]) aCoords[2] = aPoint->z();
1051         aPoint->setValue(aCoords[0], aCoords[1], aCoords[2]);
1052       continue;
1053     }
1054   }
1055
1056   //blockEvents(false);
1057 }
1058
1059 void SolveSpaceSolver_Storage::verifyFixed()
1060 {
1061   std::map<AttributePtr, EntityWrapperPtr>::iterator anAttrIt = myAttributeMap.begin();
1062   for (; anAttrIt != myAttributeMap.end(); ++anAttrIt) {
1063     if (!anAttrIt->second)
1064       continue;
1065     if (anAttrIt->second->group() == GID_OUTOFGROUP) {
1066       Slvs_Entity anEnt = getEntity((Slvs_hEntity)anAttrIt->second->id());
1067       if (anEnt.group != (Slvs_hEntity)GID_OUTOFGROUP)
1068         anEnt.group = (Slvs_hEntity)GID_OUTOFGROUP;
1069       updateEntity(anEnt);
1070     }
1071
1072     const std::list<ParameterWrapperPtr>& aParameters = anAttrIt->second->parameters();
1073     std::list<ParameterWrapperPtr>::const_iterator aParIt = aParameters.begin();
1074     for (; aParIt != aParameters.end(); ++aParIt)
1075       if (anAttrIt->second->group() == GID_OUTOFGROUP || (*aParIt)->group() == GID_OUTOFGROUP) {
1076         Slvs_Param aParam = getParameter((Slvs_hParam)(*aParIt)->id());
1077         if (aParam.group != (Slvs_hParam)GID_OUTOFGROUP) {
1078           aParam.group = (Slvs_hParam)GID_OUTOFGROUP;
1079           updateParameter(aParam);
1080         }
1081       }
1082   }
1083 }
1084
1085
1086 void SolveSpaceSolver_Storage::adjustArc(const Slvs_Entity& theArc)
1087 {
1088   double anArcPoints[3][2];
1089   double aDist[3] = {0.0};
1090   bool isFixed[3] = {false};
1091   for (int i = 0; i < 3; ++i) {
1092     Slvs_Entity aPoint = getEntity(theArc.point[i]);
1093     isFixed[i] = (aPoint.group != (Slvs_hGroup)myGroupID);
1094     anArcPoints[i][0] = getParameter(aPoint.param[0]).val;
1095     anArcPoints[i][1] = getParameter(aPoint.param[1]).val;
1096     if (i > 0) {
1097       anArcPoints[i][0] -= anArcPoints[0][0];
1098       anArcPoints[i][1] -= anArcPoints[0][1];
1099       aDist[i] = sqrt(anArcPoints[i][0] * anArcPoints[i][0] + 
1100                       anArcPoints[i][1] * anArcPoints[i][1]);
1101     }
1102   }
1103
1104   if (fabs(aDist[1] - aDist[2]) < tolerance)
1105     return;
1106
1107   int anInd = 2;
1108   while (anInd > 0 && isFixed[anInd])
1109     --anInd;
1110   if (anInd < 1)
1111     return; // adjust only start or end point of the arc
1112
1113   anArcPoints[anInd][0] /= aDist[anInd];
1114   anArcPoints[anInd][1] /= aDist[anInd];
1115
1116   Slvs_Entity aPoint = getEntity(theArc.point[anInd]);
1117   for (int i = 0; i < 2; ++i) {
1118     Slvs_Param aParam = getParameter(aPoint.param[i]);
1119     aParam.val = anArcPoints[0][i] + aDist[3-anInd] * anArcPoints[anInd][i];
1120     updateParameter(aParam);
1121   }
1122 }
1123
1124
1125
1126
1127
1128
1129
1130 // ========================================================
1131 // =========      Auxiliary functions       ===============
1132 // ========================================================
1133
1134 template<typename T>
1135 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1136 {
1137   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1138   int aVecSize = (int)theEntities.size();
1139   if (theEntities.empty())
1140     return 1;
1141   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1142     aResIndex--;
1143   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1144     aResIndex++;
1145   if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1146     aResIndex = aVecSize;
1147   return aResIndex;
1148 }
1149
1150 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1151 {
1152   return fabs(theParam1.val - theParam2.val) > tolerance;
1153 }
1154
1155 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1156 {
1157   int i = 0;
1158   for (; theEntity1.param[i] != 0 && i < 4; i++)
1159     if (theEntity1.param[i] != theEntity2.param[i])
1160       return true;
1161   i = 0;
1162   for (; theEntity1.point[i] != 0 && i < 4; i++)
1163     if (theEntity1.point[i] != theEntity2.point[i])
1164       return true;
1165   return false;
1166 }
1167
1168 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1169 {
1170   return theConstraint1.ptA != theConstraint2.ptA ||
1171          theConstraint1.ptB != theConstraint2.ptB ||
1172          theConstraint1.entityA != theConstraint2.entityA ||
1173          theConstraint1.entityB != theConstraint2.entityB ||
1174          theConstraint1.entityC != theConstraint2.entityC ||
1175          theConstraint1.entityD != theConstraint2.entityD ||
1176          fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;
1177 }