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