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