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