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