Salome HOME
Get rid of compilation warnings. Part I.
[modules/shaper.git] / src / ModelAPI / ModelAPI_Tools.cpp
1 // Copyright (C) 2014-2020  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 "ModelAPI_Tools.h"
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_CompositeFeature.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_ResultParameter.h>
29 #include <ModelAPI_ResultPart.h>
30 #include <ModelAPI_ResultGroup.h>
31 #include <ModelAPI_AttributeDocRef.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_AttributeIntArray.h>
34 #include <ModelAPI_ResultConstruction.h>
35 #include <ModelAPI_AttributeBoolean.h>
36 #include <list>
37 #include <map>
38 #include <iostream>
39 #include <sstream>
40
41 #include <Events_Loop.h>
42 #include <ModelAPI_Events.h>
43
44 #include <GeomAPI_ShapeHierarchy.h>
45 #include <GeomAPI_ShapeIterator.h>
46
47 #define RECURSE_TOP_LEVEL 50
48
49 //#define DEBUG_REMOVE_FEATURES
50 //#define DEBUG_REMOVE_FEATURES_RECURSE
51 //#define DEBUG_CYCLING_1550
52
53 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
54 #include <sstream>
55 std::string getFeatureInfo(FeaturePtr theFeature)
56 {
57   if (!theFeature.get())
58     return "";
59   //std::ostringstream aPtrStr;
60   //aPtrStr << "[" << theFeature.get() << "] ";
61   std::string aFeatureInfo = /*aPtrStr.str() + */theFeature->name();
62   CompositeFeaturePtr aComposite = ModelAPI_Tools::compositeOwner(theFeature);
63   if (aComposite.get()) {
64       aFeatureInfo = aFeatureInfo + "[in " + aComposite->name() + "]";
65   }
66   return aFeatureInfo;
67 }
68 #endif
69
70 #ifdef DEBUG_REMOVE_FEATURES
71 void printMapInfo(const std::map<FeaturePtr, std::set<FeaturePtr> >& theMainList,
72                   const std::string& thePrefix)
73 {
74   std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = theMainList.begin(),
75                                                               aMainLast = theMainList.end();
76   std::string anInfo;
77   for (; aMainIt != aMainLast; aMainIt++) {
78     FeaturePtr aMainListFeature = aMainIt->first;
79     std::set<FeaturePtr> aMainRefList = aMainIt->second;
80     std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(), aLast = aMainRefList.end();
81     std::string aRefsInfo;
82     for (; anIt != aLast; anIt++) {
83       aRefsInfo += (*anIt)->name().c_str();
84       if (anIt != aLast)
85         aRefsInfo += ", ";
86     }
87     if (!aRefsInfo.empty()) {
88       anInfo = anInfo + aMainListFeature->name().c_str() + ": " + aRefsInfo + "\n";
89     }
90   }
91   std::cout << thePrefix.c_str() << " [feature: references to]: \n" << anInfo.c_str() << std::endl;
92 }
93
94 void printListInfo(const std::set<FeaturePtr>& theMainList,
95                   const std::string& thePrefix)
96 {
97   std::set<FeaturePtr>::const_iterator aMainIt = theMainList.begin(),
98                                        aMainLast = theMainList.end();
99   std::string anInfo;
100   for (; aMainIt != aMainLast; aMainIt++) {
101     FeaturePtr aRefFeature = *aMainIt;
102     anInfo += aRefFeature->name().c_str();
103     if (aMainIt != aMainLast)
104       anInfo += ", ";
105   }
106   std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
107 }
108 #endif
109
110 namespace ModelAPI_Tools {
111
112 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
113 {
114   return theResult->shape();
115 }
116
117 // LCOV_EXCL_START
118 const char* toString(ModelAPI_ExecState theExecState)
119 {
120   switch (theExecState) {
121   case ModelAPI_StateDone: return "Done";
122   case ModelAPI_StateMustBeUpdated: return "Must be updated";
123   case ModelAPI_StateExecFailed: return "Execution failed";
124   case ModelAPI_StateInvalidArgument: return "Invalid argument";
125   case ModelAPI_StateNothing: return "Empty state";
126   default: return "Unknown ExecState.";
127   }
128 }
129
130 std::string getFeatureError(const FeaturePtr& theFeature)
131 {
132   std::string anError;
133   if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
134     return anError;
135
136   // to be removed later, this error should be got from the feature
137   if (theFeature->data()->execState() == ModelAPI_StateDone ||
138       theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
139     return anError;
140
141   // set error indication
142   anError = theFeature->error();
143   if (anError.empty()) {
144     bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
145                  || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
146     if (!isDone) {
147       anError = toString(theFeature->data()->execState());
148       // If the feature is Composite and error is StateInvalidArgument,
149       // error text should include error of first invalid sub-feature. Otherwise
150       // it is not clear what is the reason of the invalid argument.
151       if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
152         CompositeFeaturePtr aComposite =
153                     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
154         if (aComposite) {
155           for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
156             FeaturePtr aSubFeature = aComposite->subFeature(i);
157             std::string aSubFeatureError = getFeatureError(aSubFeature);
158             if (!aSubFeatureError.empty()) {
159               anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
160               break;
161             }
162           }
163         }
164       }
165     }
166   }
167
168   return anError;
169 }
170 // LCOV_EXCL_STOP
171
172 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup,
173                        const std::string& theName)
174 {
175   for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
176     ObjectPtr anObject = theDocument->object(theGroup, anIndex);
177     if (anObject->data()->name() == theName)
178       return anObject;
179   }
180   // not found
181   return ObjectPtr();
182 }
183
184 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
185                   const std::string& theName, double& outValue, ResultParameterPtr& theParam)
186 {
187   ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
188   theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
189   if (!theParam.get())
190     return false;
191   // avoid usage of parameters created later than the initial parameter
192
193   if (theSearcher.get()) {
194     FeaturePtr aParamFeat = theDocument->feature(theParam);
195     if (aParamFeat == theSearcher || theDocument->isLater(aParamFeat, theSearcher))
196       return false;
197   }
198   AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
199   outValue = aValueAttribute->value();
200   return true;
201 }
202
203 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue,
204                   ResultParameterPtr& theParam, const DocumentPtr& theDocument)
205 {
206   SessionPtr aSession = ModelAPI_Session::get();
207   std::list<DocumentPtr> aDocList;
208   DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
209   if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
210     return true;
211   DocumentPtr aRootDocument = aSession->moduleDocument();
212   if (aDocument != aRootDocument) {
213     // any parameters in PartSet is okindependently on the Part position (issu #1504)
214     if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
215       return true;
216   }
217   return false;
218 }
219
220 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
221 {
222   // to optimize and avoid of crash on partset document close
223   // (don't touch the sub-document structure)
224   if (theMain != theSub) {
225     for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
226       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
227           theMain->object(ModelAPI_ResultPart::group(), a));
228       if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
229         return aPart;
230       }
231     }
232   }
233   return ResultPtr();
234 }
235
236 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
237 {
238   // to optimize and avoid of crash on partset document close
239   // (don't touch the sub-document structure)
240   if (theMain != theSub) {
241     // iteration from top to bottom to avoid finding the movement documents before the original
242     int aSize = theMain->size(ModelAPI_Feature::group());
243     for (int a = 0; a < aSize; a++) {
244       FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
245           theMain->object(ModelAPI_Feature::group(), a));
246       if (aPartFeat.get()) {
247         const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
248         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
249         for(; aRes != aResList.end(); aRes++) {
250           ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
251           if (aPart.get()) {
252             if (aPart->isActivated() && aPart->partDoc() == theSub)
253               return aPartFeat;
254           } else break; // if the first is not Part, others are also not
255         }
256       }
257     }
258   }
259   return FeaturePtr();
260 }
261
262 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
263 {
264   if (theFeature.get() && theFeature->data() && theFeature->data()->isValid()) {
265     const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
266     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
267     for(; aRefIter != aRefs.end(); aRefIter++) {
268       CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
269         ((*aRefIter)->owner());
270       if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
271         return aComp;
272     }
273   }
274   return CompositeFeaturePtr(); // not found
275 }
276
277 ResultBodyPtr bodyOwner(const ResultPtr& theSub, const bool theRoot)
278 {
279   if (theSub.get()) {
280     ObjectPtr aParent = theSub->document()->parent(theSub);
281     if (aParent.get()) {
282       if (theRoot) { // try to find parent of parent
283         ResultPtr aResultParent = std::dynamic_pointer_cast<ModelAPI_Result>(aParent);
284         ResultBodyPtr aGrandParent = bodyOwner(aResultParent, true);
285         if (aGrandParent.get())
286           aParent = aGrandParent;
287       }
288       return std::dynamic_pointer_cast<ModelAPI_ResultBody>(aParent);
289     }
290   }
291   return ResultBodyPtr(); // not found
292 }
293
294 int bodyIndex(const ResultPtr& theSub)
295 {
296   int anIndex = -1;
297   ResultBodyPtr aParent = bodyOwner(theSub);
298   if (aParent.get()) {
299     ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
300     if (aBody.get() && aParent->isSub(aBody, anIndex))
301       return anIndex;
302   }
303   return anIndex; // not found
304 }
305
306 bool hasSubResults(const ResultPtr& theResult)
307 {
308   ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
309   return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
310 }
311
312 void allSubs(const ResultBodyPtr& theResult, std::list<ResultPtr>& theResults,
313              const bool theLowerOnly) {
314   // iterate sub-bodies of compsolid
315   ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
316   if (aComp.get()) {
317     int aNumSub = aComp->numberOfSubs();
318     for (int a = 0; a < aNumSub; a++) {
319       ResultBodyPtr aSub = aComp->subResult(a);
320       if (!theLowerOnly || aSub->numberOfSubs() == 0)
321         theResults.push_back(aSub);
322       allSubs(aSub, theResults);
323     }
324   }
325 }
326
327 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
328 {
329   if (!theFeature.get()) // safety: for empty feature no results
330     return;
331   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
332   std::list<ResultPtr>::const_iterator aRIter = aResults.begin();
333   for (; aRIter != aResults.cend(); aRIter++) {
334     theResults.push_back(*aRIter);
335     ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRIter);
336     allSubs(aResult, theResults);
337   }
338 }
339
340 //******************************************************************
341 bool allDocumentsActivated(std::string& theNotActivatedNames)
342 {
343   theNotActivatedNames = "";
344   bool anAllPartActivated = true;
345
346   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
347   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
348   for (int i = 0; i < aSize; i++) {
349     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
350     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
351     if (!aPart->isActivated()) {
352       anAllPartActivated = false;
353       if (!theNotActivatedNames.empty())
354         theNotActivatedNames += ", ";
355       theNotActivatedNames += aObject->data()->name().c_str();
356     }
357   }
358   return anAllPartActivated;
359 }
360
361 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
362                                  const bool theFlushRedisplay,
363                                  const bool theUseComposite,
364                                  const bool theUseRecursion)
365 {
366 #ifdef DEBUG_REMOVE_FEATURES
367   printListInfo(theFeatures, "selection: ");
368 #endif
369
370   std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
371   ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
372 #ifdef DEBUG_REMOVE_FEATURES
373   printMapInfo(aReferences, "allDependencies: ");
374 #endif
375
376   std::set<FeaturePtr> aFeaturesRefsTo;
377   ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
378 #ifdef DEBUG_REMOVE_FEATURES
379   printListInfo(aFeaturesRefsTo, "references: ");
380 #endif
381
382   std::set<FeaturePtr> aFeatures = theFeatures;
383   if (!aFeaturesRefsTo.empty())
384     aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
385 #ifdef DEBUG_REMOVE_FEATURES
386   printListInfo(aFeatures, "removeFeatures: ");
387 #endif
388
389   return ModelAPI_Tools::removeFeatures(aFeatures, false);
390 }
391
392 //***********************************************************************
393 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
394                     const bool theFlushRedisplay)
395 {
396   bool isDone = false;
397   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
398                                        aLast = theFeatures.end();
399   for (; anIt != aLast; anIt++) {
400     FeaturePtr aFeature = *anIt;
401     if (aFeature.get()) {
402       DocumentPtr aDoc = aFeature->document();
403       // flush REDISPLAY signal after remove feature
404       aDoc->removeFeature(aFeature);
405       isDone = true;
406     }
407   }
408   if (isDone && theFlushRedisplay) {
409     // the redisplay signal should be flushed in order to erase
410     // the feature presentation in the viewer
411     // if should be done after removeFeature() of document
412     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
413   }
414   return true;
415 }
416
417 //***********************************************************************
418 // Fills the references list by all references of the feature from the references map.
419 // This is a recusive method to find references by next found feature in the map of references.
420 // \param theFeature a feature to find references
421 // \param theReferencesMap a map of references
422 // \param theReferences an out container of references
423 void addRefsToFeature(const FeaturePtr& theFeature,
424                       const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
425                       int theRecLevel,
426                       std::set<FeaturePtr>& theReferences)
427 {
428   if (theRecLevel > RECURSE_TOP_LEVEL)
429     return;
430   theRecLevel++;
431
432   if (theReferencesMap.find(theFeature) == theReferencesMap.end())
433     return; // this feature is not in the selection list, so exists without references to it
434   std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
435
436   std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
437                                        aLast = aMainReferences.end();
438   for (; anIt != aLast; anIt++) {
439     FeaturePtr aRefFeature = *anIt;
440     if (theReferences.find(aRefFeature) == theReferences.end()) {
441       addRefsToFeature(aRefFeature, theReferencesMap, theRecLevel, theReferences);
442       theReferences.insert(aRefFeature);
443     }
444   }
445 }
446
447 // For each feature from the feature list it searches references to the feature and append them
448 // to the references map. This is a recusive method.
449 // \param theFeature a feature to find references
450 // \param theReferencesMap a map of references
451 // \param theReferences an out container of references
452 void findReferences(const std::set<FeaturePtr>& theFeatures,
453                     std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
454                     const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
455 {
456   if (theRecLevel > RECURSE_TOP_LEVEL)
457     return;
458   theRecLevel++;
459   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
460                                         aLast = theFeatures.end();
461   for (; anIt != aLast; anIt++) {
462     FeaturePtr aFeature = *anIt;
463     if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
464       DocumentPtr aSelFeatureDoc = aFeature->document();
465       std::set<FeaturePtr> aSelRefFeatures;
466       aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
467       if (theUseComposite) { // do not filter selection
468         theReferences[aFeature] = aSelRefFeatures;
469       }
470       else { // filter references to skip composition features of the current feature
471         std::set<FeaturePtr> aFilteredFeatures;
472         std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
473                                              aLast = aSelRefFeatures.end();
474         for (; anIt != aLast; anIt++) {
475           FeaturePtr aCFeature = *anIt;
476           CompositeFeaturePtr aComposite =
477             std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
478           if (aComposite.get() && aComposite->isSub(aFeature))
479             continue; /// composite of the current feature should be skipped
480           aFilteredFeatures.insert(aCFeature);
481         }
482         theReferences[aFeature] = aFilteredFeatures;
483       }
484       if (theUseRecursion) {
485 #ifdef DEBUG_CYCLING_1550
486         findReferences(aSelRefFeatures, theReferences, theUseComposite,
487                        theUseRecursion, theRecLevel);
488 #else
489         findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
490                        theRecLevel);
491 #endif
492       }
493     }
494   }
495 }
496
497 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
498                        std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
499                        const bool theUseComposite,
500                        const bool theUseRecursion)
501 {
502   // For dependencies, find main_list:
503   // sk_1(ext_1, vertex_1)
504   // ext_1(bool_1, sk_3)
505   // vertex_1()
506   // sk_2(ext_2)
507   // ext_2(bool_2)
508   // sk_3()
509   // Information: bool_1 is not selected, ext_2(bool_2) exists
510   // find all referenced features
511   std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
512   int aRecLevel = 0;
513   findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
514
515 #ifdef DEBUG_REMOVE_FEATURES
516   printMapInfo(aMainList, "firstDependencies");
517 #endif
518   // find all dependencies for each object:
519   // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
520   // ext_1(bool_1, sk_3)
521   // vertex_1()
522   // sk_2(ext_2) + (bool_1)
523   // ext_2(bool_1)
524   // sk_3()
525   std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
526                                                               aMainLast = aMainList.end();
527   for (; aMainIt != aMainLast; aMainIt++) {
528     FeaturePtr aMainListFeature = aMainIt->first;
529
530     //std::string aName = aMainListFeature->name();
531     std::set<FeaturePtr> aMainRefList = aMainIt->second;
532
533 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
534     char aBuf[50];
535     int n = sprintf(aBuf, "%d", aMainRefList.size());
536     std::string aSize(aBuf);
537     std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
538               << ", references size = " << aSize << std::endl;
539 #endif
540     std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
541                                          aLast = aMainRefList.end();
542     std::set<FeaturePtr> aResultRefList;
543     aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
544     for (; anIt != aLast; anIt++) {
545       FeaturePtr aFeature = *anIt;
546       int aRecLevel = 0;
547 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
548       std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
549 #endif
550       aRecLevel++;
551       addRefsToFeature(aFeature, aMainList,
552                        aRecLevel, aResultRefList/*aMainRefList*/);
553     }
554     theReferences[aMainListFeature] = aResultRefList;
555   }
556 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
557     std::cout << std::endl;
558 #endif
559
560 #ifdef DEBUG_REMOVE_FEATURES
561   printMapInfo(theReferences, "allDependencies");
562 #endif
563 }
564
565 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
566                         const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
567                         std::set<FeaturePtr>& theFeaturesRefsTo)
568 {
569   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
570                                        aLast = theFeatures.end();
571   for (; anIt != aLast; anIt++) {
572     FeaturePtr aFeature = *anIt;
573     if (theReferences.find(aFeature) == theReferences.end())
574       continue;
575     std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
576     std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
577     for (; aRefIt != aRefLast; aRefIt++) {
578       FeaturePtr aRefFeature = *aRefIt;
579       CompositeFeaturePtr aComposite =
580         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
581       if (aComposite.get() && aComposite->isSub(aFeature))
582         continue; /// composite of the current feature should not be removed
583
584       if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
585           theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
586         theFeaturesRefsTo.insert(aRefFeature);
587     }
588   }
589 }
590
591 void getConcealedResults(const FeaturePtr& theFeature,
592                          std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
593 {
594   SessionPtr aSession = ModelAPI_Session::get();
595
596   std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
597   theFeature->data()->referencesToObjects(aRefs);
598   std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
599                                                   anIt = aRefs.begin(), aLast = aRefs.end();
600   std::set<ResultPtr> alreadyThere; // to avoid duplications
601   for (; anIt != aLast; anIt++) {
602     if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
603       continue; // use only concealed attributes
604     std::list<ObjectPtr> anObjects = (*anIt).second;
605     std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
606     for (; anOIt != anOLast; anOIt++) {
607       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
608       if (aResult && aResult->isConcealed()) {
609         if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
610           alreadyThere.insert(aResult);
611         else continue;
612         theResults.push_back(aResult);
613       }
614     }
615   }
616 }
617
618 std::pair<std::string, bool> getDefaultName(const std::shared_ptr<ModelAPI_Result>& theResult,
619                                             const bool theInherited)
620 {
621   typedef std::list< std::pair < std::string, std::list<ObjectPtr> > > ListOfReferences;
622
623   SessionPtr aSession = ModelAPI_Session::get();
624
625   ResultBodyPtr anOwnerRes = bodyOwner(theResult);
626   if (anOwnerRes) {
627     // names of sub-solids in CompSolid should be default (for example,
628     // result of boolean operation 'Boolean_1_1' is a CompSolid which is renamed to 'MyBOOL',
629     // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1_1', 'Boolean_1_1_2' etc.)
630     std::ostringstream aDefaultName;
631     aDefaultName << getDefaultName(anOwnerRes).first;
632     aDefaultName << "_" << (bodyIndex(theResult) + 1);
633     return std::pair<std::string, bool>(aDefaultName.str(), false);
634   }
635
636   FeaturePtr anOwner = ModelAPI_Feature::feature(theResult->data()->owner());
637   DataPtr aData = anOwner->data();
638
639   ListOfReferences aReferences;
640   // find first result with user-defined name
641   ListOfReferences::const_iterator aFoundRef = aReferences.end();
642   if (theInherited) {
643     aData->referencesToObjects(aReferences);
644
645     for (ListOfReferences::const_iterator aRefIt = aReferences.begin();
646          aRefIt != aReferences.end(); ++aRefIt) {
647       bool isConcealed = aSession->validators()->isConcealed(anOwner->getKind(), aRefIt->first);
648       bool isMainArg = isConcealed &&
649                        aSession->validators()->isMainArgument(anOwner->getKind(), aRefIt->first);
650       if (isConcealed) {
651         // check the referred object is a Body
652         // (for example, ExtrusionCut has a sketch as a first attribute which is concealing)
653         bool isBody = aRefIt->second.size() > 1 || (aRefIt->second.size() == 1 &&
654                       aRefIt->second.front().get() &&
655                       aRefIt->second.front()->groupName() == ModelAPI_ResultBody::group());
656         if (isBody && (isMainArg || aFoundRef == aReferences.end() ||
657             aData->isPrecedingAttribute(aRefIt->first, aFoundRef->first)))
658           aFoundRef = aRefIt;
659
660         if (isMainArg)
661           break;
662       }
663     }
664   }
665   // get the result number in the feature
666   int anIndexInOwner = 0;
667   const std::list<ResultPtr>& anOwnerResults = anOwner->results();
668   std::list<ResultPtr>::const_iterator aResIt = anOwnerResults.cbegin();
669   for(; aResIt != anOwnerResults.cend(); aResIt++) {
670     if(*aResIt == theResult)
671       break;
672     anIndexInOwner++;
673   }
674
675   // find an object which is concealed by theResult
676   if (aFoundRef != aReferences.end() && !aFoundRef->second.empty()) {
677     // store number of references for each object
678     std::map<ResultPtr, int> aNbRefToObject;
679     // search the object by result index
680     std::list<ObjectPtr>::const_iterator anObjIt = aFoundRef->second.begin();
681     int aResultIndex = anIndexInOwner;
682     while (--aResultIndex >= 0) {
683       ResultPtr aCurRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
684       ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(aCurRes);
685       if (aParentBody)
686         aCurRes = aParentBody;
687       if (aNbRefToObject.find(aCurRes) == aNbRefToObject.end())
688         aNbRefToObject[aCurRes] = 1;
689       else
690         aNbRefToObject[aCurRes] += 1;
691
692       ++anObjIt;
693       if (anObjIt == aFoundRef->second.end()) {
694         anObjIt = aFoundRef->second.begin();
695         break;
696       }
697     }
698     // check the result is a Body
699     if ((*anObjIt)->groupName() == ModelAPI_ResultBody::group()) {
700       // check the result is part of CompSolid
701       ResultPtr anObjRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
702       ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(anObjRes);
703       if (aParentBody)
704         anObjRes = aParentBody;
705
706       // return name of reference result only if it has been renamed by the user,
707       // in other case compose a default name
708       if (anObjRes->data()->hasUserDefinedName()) {
709         std::stringstream aName;
710         aName << anObjRes->data()->name();
711         std::map<ResultPtr, int>::iterator aFound = aNbRefToObject.find(anObjRes);
712         if (aFound != aNbRefToObject.end()) {
713           // to generate unique name, add suffix if there are several results
714           // referring to the same shape
715           aName << "_" << aFound->second + 1;
716         }
717         return std::pair<std::string, bool>(aName.str(), true);
718       }
719     }
720   }
721
722   // compose default name by the name of the feature and the index of result
723   std::stringstream aDefaultName;
724   aDefaultName << anOwner->name();
725   // if there are several results (issue #899: any number of result),
726   // add unique prefix starting from second
727   if (anIndexInOwner > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
728     aDefaultName << "_" << anIndexInOwner + 1;
729   return std::pair<std::string, bool>(aDefaultName.str(), false);
730 }
731
732 std::set<FeaturePtr> getParents(const FeaturePtr& theFeature)
733 {
734   std::set<FeaturePtr> aParents;
735   for (FeaturePtr aCurFeat = theFeature; aCurFeat; ) {
736     CompositeFeaturePtr aFoundComposite;
737     const std::set<AttributePtr>& aRefs = aCurFeat->data()->refsToMe();
738     for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
739       anIt != aRefs.end(); ++anIt) {
740       FeaturePtr aF = ModelAPI_Feature::feature((*anIt)->owner());
741       aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aF);
742       if (aFoundComposite && aFoundComposite->isSub(aCurFeat))
743         break;
744       else
745         aFoundComposite = CompositeFeaturePtr();
746     }
747
748     if (aFoundComposite) {
749       aParents.insert(aFoundComposite);
750       aCurFeat = aFoundComposite;
751     }
752     else {
753       // add the part containing high-level feature
754       SessionPtr aSession = ModelAPI_Session::get();
755       DocumentPtr aPartSetDoc = aSession->moduleDocument();
756       std::list<FeaturePtr> aPartSetFeatures = aPartSetDoc->allFeatures();
757       for (std::list<FeaturePtr>::const_iterator anIt = aPartSetFeatures.begin();
758         anIt != aPartSetFeatures.end(); ++anIt) {
759         aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
760         if (aFoundComposite && aFoundComposite->isSub(aCurFeat)) {
761           aParents.insert(aFoundComposite);
762           break;
763         }
764       }
765
766       aCurFeat = FeaturePtr();
767     }
768   }
769   return aParents;
770 }
771
772 void fillShapeHierarchy(const GeomShapePtr& theShape,
773                         const ResultPtr& theContext,
774                         GeomAPI_ShapeHierarchy& theHierarchy)
775 {
776   ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
777   if (aResCompSolidPtr.get()) {
778     std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
779     if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
780       theHierarchy.addParent(theShape, aContextShape);
781       fillShapeHierarchy(aContextShape, aResCompSolidPtr, theHierarchy);
782     }
783   }
784 }
785
786
787 void removeResults(const std::list<ResultPtr>& theResults)
788 {
789   // collect all documents where the results must be removed
790   std::map<DocumentPtr, std::list<ResultPtr> > aDocs;
791
792   std::list<ResultPtr>::const_iterator aResIter = theResults.cbegin();
793   for(; aResIter != theResults.cend(); aResIter++) {
794     DocumentPtr aDoc = (*aResIter)->document();
795     if (!aDocs.count(aDoc))
796       aDocs[aDoc] = std::list<ResultPtr>();
797     aDocs[aDoc].push_back(*aResIter);
798   }
799   // create a "remove" feature in each doc
800   std::map<DocumentPtr, std::list<ResultPtr> >::iterator aDoc = aDocs.begin();
801   for(; aDoc != aDocs.end(); aDoc++) {
802     FeaturePtr aRemove = aDoc->first->addFeature("RemoveResults");
803     if (aRemove) {
804       for(aResIter = aDoc->second.cbegin(); aResIter != aDoc->second.cend(); aResIter++)
805         aRemove->selectionList("results")->append(*aResIter, GeomShapePtr());
806     }
807   }
808 }
809
810 // used by GUI only
811 // LCOV_EXCL_START
812
813 //**************************************************************
814 void setDeflection(ResultPtr theResult, const double theDeflection)
815 {
816   if (!theResult.get())
817     return;
818
819   AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
820   if (aDeflectionAttr.get() != NULL) {
821     aDeflectionAttr->setValue(theDeflection);
822   }
823 }
824
825 double getDeflection(const std::shared_ptr<ModelAPI_Result>& theResult)
826 {
827   double aDeflection = -1;
828   // get deflection from the attribute of the result
829   if (theResult.get() != NULL &&
830     theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
831     AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
832     if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
833       double aValue = aDoubleAttr->value();
834       if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
835         aDeflection = aDoubleAttr->value();
836     }
837   }
838   return aDeflection;
839 }
840
841 //******************************************************
842 void setColor(ResultPtr theResult, const std::vector<int>& theColor)
843 {
844   if (!theResult.get())
845     return;
846
847   AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
848   if (aColorAttr.get() != NULL) {
849     if (!aColorAttr->size()) {
850       aColorAttr->setSize(3);
851     }
852     aColorAttr->setValue(0, theColor[0]);
853     aColorAttr->setValue(1, theColor[1]);
854     aColorAttr->setValue(2, theColor[2]);
855   }
856 }
857
858 void getColor(const std::shared_ptr<ModelAPI_Result>& theResult, std::vector<int>& theColor)
859 {
860   theColor.clear();
861   // get color from the attribute of the result
862   if (theResult.get() != NULL &&
863     theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
864     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
865     if (aColorAttr.get() && aColorAttr->size()) {
866       theColor.push_back(aColorAttr->value(0));
867       theColor.push_back(aColorAttr->value(1));
868       theColor.push_back(aColorAttr->value(2));
869     }
870   }
871 }
872
873 //******************************************************
874 void getIsoLines(const std::shared_ptr<ModelAPI_Result>& theResult,
875   bool& isVisible, std::vector<int>& theNbLines)
876 {
877   theNbLines.clear();
878   isVisible = false;
879   if (!theResult.get())
880     return;
881   if (theResult->groupName() == ModelAPI_ResultConstruction::group()) {
882     theNbLines.push_back(0);
883     theNbLines.push_back(0);
884   }
885   else {
886     // get color from the attribute of the result
887     AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
888     if (aAttr.get()) {
889       if (aAttr->size()) {
890         theNbLines.push_back(aAttr->value(0));
891         theNbLines.push_back(aAttr->value(1));
892       }
893     }
894     AttributeBooleanPtr aBoolAttr =
895       theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
896     if (aBoolAttr.get()) {
897       isVisible = aBoolAttr->value();
898     }
899   }
900 }
901
902 //******************************************************
903 void setIsoLines(ResultPtr theResult, const std::vector<int>& theIso)
904 {
905   if (!theResult.get())
906     return;
907
908   AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
909   if (aAttr.get() != NULL) {
910     if (!aAttr->size()) {
911       aAttr->setSize(2);
912     }
913     aAttr->setValue(0, theIso[0]);
914     aAttr->setValue(1, theIso[1]);
915   }
916 }
917
918 //******************************************************
919 void showIsoLines(std::shared_ptr<ModelAPI_Result> theResult, bool theShow)
920 {
921   if (!theResult.get())
922     return;
923
924   AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
925   if (aAttr.get() != NULL) {
926     aAttr->setValue(theShow);
927   }
928 }
929
930 //******************************************************
931 bool isShownIsoLines(std::shared_ptr<ModelAPI_Result> theResult)
932 {
933   if (!theResult.get())
934     return false;
935
936   AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
937   if (aAttr.get() != NULL) {
938     return aAttr->value();
939   }
940   return false;
941 }
942
943 //**************************************************************
944 void setTransparency(ResultPtr theResult, double theTransparency)
945 {
946   if (!theResult.get())
947     return;
948
949   AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
950   if (anAttribute.get() != NULL) {
951     anAttribute->setValue(theTransparency);
952   }
953 }
954
955 double getTransparency(const std::shared_ptr<ModelAPI_Result>& theResult)
956 {
957   double aTransparency = -1;
958   // get transparency from the attribute of the result
959   if (theResult.get() != NULL &&
960     theResult->data()->attribute(ModelAPI_Result::TRANSPARENCY_ID()).get() != NULL) {
961     AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
962     if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
963       aTransparency = aDoubleAttr->value();
964     }
965   }
966   return aTransparency;
967 }
968
969 void copyVisualizationAttrs(
970   std::shared_ptr<ModelAPI_Result> theSource, std::shared_ptr<ModelAPI_Result> theDest)
971 {
972   // color
973   AttributeIntArrayPtr aSourceColor = theSource->data()->intArray(ModelAPI_Result::COLOR_ID());
974   if (aSourceColor.get() && aSourceColor->isInitialized() && aSourceColor->size()) {
975     AttributeIntArrayPtr aDestColor = theDest->data()->intArray(ModelAPI_Result::COLOR_ID());
976     if (aDestColor.get()) {
977       aDestColor->setSize(aSourceColor->size());
978       for(int a = 0; a < aSourceColor->size(); a++)
979         aDestColor->setValue(a, aSourceColor->value(a));
980     }
981   }
982   // Iso-lines
983   AttributeIntArrayPtr aSource = theSource->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
984   if (aSource.get() && aSource->isInitialized() && aSource->size()) {
985     AttributeIntArrayPtr aDest = theDest->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
986     if (aDest.get()) {
987       aDest->setSize(aSource->size());
988       for(int a = 0; a < aSource->size(); a++)
989         aDest->setValue(a, aSource->value(a));
990     }
991   }
992   // deflection
993   AttributeDoublePtr aSourceDefl = theSource->data()->real(ModelAPI_Result::DEFLECTION_ID());
994   if (aSourceDefl.get() && aSourceDefl->isInitialized()) {
995     AttributeDoublePtr aDestDefl = theDest->data()->real(ModelAPI_Result::DEFLECTION_ID());
996     if (aDestDefl.get()) {
997       aDestDefl->setValue(aSourceDefl->value());
998     }
999   }
1000   // transparency
1001   AttributeDoublePtr aSourceTransp = theSource->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1002   if (aSourceTransp.get() && aSourceTransp->isInitialized()) {
1003     AttributeDoublePtr aDestTransp = theDest->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1004     if (aDestTransp.get()) {
1005       aDestTransp->setValue(aSourceTransp->value());
1006     }
1007   }
1008 }
1009
1010 std::list<FeaturePtr> referencedFeatures(
1011   std::shared_ptr<ModelAPI_Result> theTarget, const std::string& theFeatureKind,
1012   const bool theSortResults)
1013 {
1014   std::set<FeaturePtr> aResSet; // collect in the set initially to avoid duplicates
1015   std::list<ResultPtr> allSubRes;
1016   allSubRes.push_back(theTarget);
1017   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theTarget);
1018   if (aBody.get())
1019     allSubs(aBody, allSubRes);
1020   std::list<ResultPtr>::iterator aSub = allSubRes.begin();
1021   for(; aSub != allSubRes.end(); aSub++) {
1022     const std::set<AttributePtr>& aRefs = (*aSub)->data()->refsToMe();
1023     std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1024     for(; aRef != aRefs.cend(); aRef++) {
1025       FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1026       if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1027         aResSet.insert(aFeat);
1028     }
1029   }
1030   // add also feature of the target that may be referenced as a whole
1031   FeaturePtr aTargetFeature = theTarget->document()->feature(theTarget);
1032   const std::set<AttributePtr>& aRefs = aTargetFeature->data()->refsToMe();
1033   std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1034   for(; aRef != aRefs.cend(); aRef++) {
1035     FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1036     if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1037       aResSet.insert(aFeat);
1038   }
1039   // check also Group-operations that may refer to groups - add them for theFeatureKind "Group"
1040   if (theFeatureKind == "Group") {
1041     std::set<FeaturePtr> aGroupOperations;
1042     for(bool aNeedIterate = true; aNeedIterate; ) {
1043       std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1044       for(; aResIter != aResSet.end(); aResIter++) {
1045         std::list<ResultPtr>::const_iterator aGroupRes = (*aResIter)->results().cbegin();
1046         for(; aGroupRes != (*aResIter)->results().cend(); aGroupRes++) {
1047           const std::set<AttributePtr>& aRefs = (*aGroupRes)->data()->refsToMe();
1048           std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1049           for(; aRef != aRefs.cend(); aRef++) {
1050             FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1051             if (aFeat.get() && !aGroupOperations.count(aFeat) && !aFeat->results().empty() &&
1052                 aFeat->firstResult()->groupName() == ModelAPI_ResultGroup::group()) {
1053               // iterate results of this group operation because it may be without theTarget shape
1054               GeomShapePtr aTargetShape = theTarget->shape();
1055               bool anIsIn = false;
1056               std::list<ResultPtr>::const_iterator anOpRes = aFeat->results().cbegin();
1057               for(; anOpRes != aFeat->results().cend() && !anIsIn; anOpRes++) {
1058                 GeomShapePtr anOpShape = (*anOpRes)->shape();
1059                 if (!anOpShape.get() || anOpShape->isNull())
1060                   continue;
1061                 for(GeomAPI_ShapeIterator aSubIt(anOpShape); aSubIt.more(); aSubIt.next()) {
1062                   if (aTargetShape->isSubShape(aSubIt.current(), false)) {
1063                     anIsIn = true;
1064                     break;
1065                   }
1066                 }
1067               }
1068               if (anIsIn)
1069                 aGroupOperations.insert(aFeat);
1070             }
1071           }
1072         }
1073       }
1074       // insert all new group operations into result and if they are, check for next dependencies
1075       aNeedIterate = false;
1076       std::set<FeaturePtr>::iterator aGroupOpIter = aGroupOperations.begin();
1077       for(; aGroupOpIter != aGroupOperations.end(); aGroupOpIter++) {
1078         if (aResSet.find(*aGroupOpIter) == aResSet.end()) {
1079           aResSet.insert(*aGroupOpIter);
1080           aNeedIterate = true;
1081         }
1082       }
1083     }
1084   }
1085
1086   std::list<FeaturePtr> aResList;
1087   std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1088   for(; aResIter != aResSet.end(); aResIter++) {
1089     if (theSortResults) { // sort results by creation-order
1090       std::list<FeaturePtr>::iterator aListIter = aResList.begin();
1091       for(; aListIter != aResList.end(); aListIter++) {
1092         if ((*aResIter)->document()->isLater(*aListIter, *aResIter))
1093           break;
1094       }
1095       if (aListIter == aResList.end()) // goes to the end
1096         aResList.push_back(*aResIter);
1097       else
1098         aResList.insert(aListIter, *aResIter);
1099     } else //just push to the end unsorted
1100       aResList.push_back(*aResIter);
1101   }
1102   return aResList;
1103 }
1104
1105 // LCOV_EXCL_STOP
1106
1107 } // namespace ModelAPI_Tools