Salome HOME
Merge remote-tracking branch 'origin/master' into gni/documentation
[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                                              const bool theRecursive)
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           (theRecursive && anObjRes->data()->name() != getDefaultName(anObjRes).first)) {
712         std::wstringstream aName;
713         aName << anObjRes->data()->name();
714         std::map<ResultPtr, int>::iterator aFound = aNbRefToObject.find(anObjRes);
715         if (aFound != aNbRefToObject.end()) {
716           // to generate unique name, add suffix if there are several results
717           // referring to the same shape
718           aName << "_" << aFound->second + 1;
719         }
720         return std::pair<std::wstring, bool>(aName.str(), true);
721       }
722     }
723   }
724
725   // compose default name by the name of the feature and the index of result
726   std::wstringstream aDefaultName;
727   aDefaultName << anOwner->name();
728   // if there are several results (issue #899: any number of result),
729   // add unique prefix starting from second
730   if (anIndexInOwner > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
731     aDefaultName << "_" << anIndexInOwner + 1;
732   return std::pair<std::wstring, bool>(aDefaultName.str(), false);
733 }
734
735 std::set<FeaturePtr> getParents(const FeaturePtr& theFeature)
736 {
737   std::set<FeaturePtr> aParents;
738   for (FeaturePtr aCurFeat = theFeature; aCurFeat; ) {
739     CompositeFeaturePtr aFoundComposite;
740     const std::set<AttributePtr>& aRefs = aCurFeat->data()->refsToMe();
741     for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
742       anIt != aRefs.end(); ++anIt) {
743       FeaturePtr aF = ModelAPI_Feature::feature((*anIt)->owner());
744       aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aF);
745       if (aFoundComposite && aFoundComposite->isSub(aCurFeat))
746         break;
747       else
748         aFoundComposite = CompositeFeaturePtr();
749     }
750
751     if (aFoundComposite) {
752       aParents.insert(aFoundComposite);
753       aCurFeat = aFoundComposite;
754     }
755     else {
756       // add the part containing high-level feature
757       SessionPtr aSession = ModelAPI_Session::get();
758       DocumentPtr aPartSetDoc = aSession->moduleDocument();
759       std::list<FeaturePtr> aPartSetFeatures = aPartSetDoc->allFeatures();
760       for (std::list<FeaturePtr>::const_iterator anIt = aPartSetFeatures.begin();
761         anIt != aPartSetFeatures.end(); ++anIt) {
762         aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
763         if (aFoundComposite && aFoundComposite->isSub(aCurFeat)) {
764           aParents.insert(aFoundComposite);
765           break;
766         }
767       }
768
769       aCurFeat = FeaturePtr();
770     }
771   }
772   return aParents;
773 }
774
775 void fillShapeHierarchy(const GeomShapePtr& theShape,
776                         const ResultPtr& theContext,
777                         GeomAPI_ShapeHierarchy& theHierarchy)
778 {
779   ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
780   if (aResCompSolidPtr.get()) {
781     std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
782     if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
783       theHierarchy.addParent(theShape, aContextShape);
784       fillShapeHierarchy(aContextShape, aResCompSolidPtr, theHierarchy);
785     }
786   }
787 }
788
789
790 void removeResults(const std::list<ResultPtr>& theResults)
791 {
792   // collect all documents where the results must be removed
793   std::map<DocumentPtr, std::list<ResultPtr> > aDocs;
794
795   std::list<ResultPtr>::const_iterator aResIter = theResults.cbegin();
796   for(; aResIter != theResults.cend(); aResIter++) {
797     DocumentPtr aDoc = (*aResIter)->document();
798     if (!aDocs.count(aDoc))
799       aDocs[aDoc] = std::list<ResultPtr>();
800     aDocs[aDoc].push_back(*aResIter);
801   }
802   // create a "remove" feature in each doc
803   std::map<DocumentPtr, std::list<ResultPtr> >::iterator aDoc = aDocs.begin();
804   for(; aDoc != aDocs.end(); aDoc++) {
805     FeaturePtr aRemove = aDoc->first->addFeature("RemoveResults");
806     if (aRemove) {
807       for(aResIter = aDoc->second.cbegin(); aResIter != aDoc->second.cend(); aResIter++)
808         aRemove->selectionList("results")->append(*aResIter, GeomShapePtr());
809     }
810   }
811 }
812
813 // used by GUI only
814 // LCOV_EXCL_START
815
816 //**************************************************************
817 void setDeflection(ResultPtr theResult, const double theDeflection)
818 {
819   if (!theResult.get())
820     return;
821
822   AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
823   if (aDeflectionAttr.get() != NULL) {
824     aDeflectionAttr->setValue(theDeflection);
825   }
826 }
827
828 double getDeflection(const std::shared_ptr<ModelAPI_Result>& theResult)
829 {
830   double aDeflection = -1;
831   // get deflection from the attribute of the result
832   if (theResult.get() != NULL &&
833     theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
834     AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
835     if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
836       double aValue = aDoubleAttr->value();
837       if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
838         aDeflection = aDoubleAttr->value();
839     }
840   }
841   return aDeflection;
842 }
843
844 //******************************************************
845 void setColor(ResultPtr theResult, const std::vector<int>& theColor)
846 {
847   if (!theResult.get())
848     return;
849
850   AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
851   if (aColorAttr.get() != NULL) {
852     if (!aColorAttr->size()) {
853       aColorAttr->setSize(3);
854     }
855     aColorAttr->setValue(0, theColor[0]);
856     aColorAttr->setValue(1, theColor[1]);
857     aColorAttr->setValue(2, theColor[2]);
858   }
859 }
860
861 void getColor(const std::shared_ptr<ModelAPI_Result>& theResult, std::vector<int>& theColor)
862 {
863   theColor.clear();
864   // get color from the attribute of the result
865   if (theResult.get() != NULL &&
866     theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
867     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
868     if (aColorAttr.get() && aColorAttr->size()) {
869       theColor.push_back(aColorAttr->value(0));
870       theColor.push_back(aColorAttr->value(1));
871       theColor.push_back(aColorAttr->value(2));
872     }
873   }
874 }
875
876 //******************************************************
877 void getIsoLines(const std::shared_ptr<ModelAPI_Result>& theResult,
878   bool& isVisible, std::vector<int>& theNbLines)
879 {
880   theNbLines.clear();
881   isVisible = false;
882   if (!theResult.get())
883     return;
884   if (theResult->groupName() == ModelAPI_ResultConstruction::group()) {
885     theNbLines.push_back(0);
886     theNbLines.push_back(0);
887   }
888   else {
889     // get color from the attribute of the result
890     AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
891     if (aAttr.get()) {
892       if (aAttr->size()) {
893         theNbLines.push_back(aAttr->value(0));
894         theNbLines.push_back(aAttr->value(1));
895       }
896     }
897     AttributeBooleanPtr aBoolAttr =
898       theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
899     if (aBoolAttr.get()) {
900       isVisible = aBoolAttr->value();
901     }
902   }
903 }
904
905 //******************************************************
906 void setIsoLines(ResultPtr theResult, const std::vector<int>& theIso)
907 {
908   if (!theResult.get())
909     return;
910
911   AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
912   if (aAttr.get() != NULL) {
913     if (!aAttr->size()) {
914       aAttr->setSize(2);
915     }
916     aAttr->setValue(0, theIso[0]);
917     aAttr->setValue(1, theIso[1]);
918   }
919 }
920
921 //******************************************************
922 void showIsoLines(std::shared_ptr<ModelAPI_Result> theResult, bool theShow)
923 {
924   if (!theResult.get())
925     return;
926
927   AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
928   if (aAttr.get() != NULL) {
929     aAttr->setValue(theShow);
930   }
931 }
932
933 //******************************************************
934 bool isShownIsoLines(std::shared_ptr<ModelAPI_Result> theResult)
935 {
936   if (!theResult.get())
937     return false;
938
939   AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
940   if (aAttr.get() != NULL) {
941     return aAttr->value();
942   }
943   return false;
944 }
945
946 //**************************************************************
947 void setTransparency(ResultPtr theResult, double theTransparency)
948 {
949   if (!theResult.get())
950     return;
951
952   AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
953   if (anAttribute.get() != NULL) {
954     anAttribute->setValue(theTransparency);
955   }
956 }
957
958 double getTransparency(const std::shared_ptr<ModelAPI_Result>& theResult)
959 {
960   double aTransparency = -1;
961   // get transparency from the attribute of the result
962   if (theResult.get() != NULL &&
963     theResult->data()->attribute(ModelAPI_Result::TRANSPARENCY_ID()).get() != NULL) {
964     AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
965     if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
966       aTransparency = aDoubleAttr->value();
967     }
968   }
969   return aTransparency;
970 }
971
972 void copyVisualizationAttrs(
973   std::shared_ptr<ModelAPI_Result> theSource, std::shared_ptr<ModelAPI_Result> theDest)
974 {
975   // color
976   AttributeIntArrayPtr aSourceColor = theSource->data()->intArray(ModelAPI_Result::COLOR_ID());
977   if (aSourceColor.get() && aSourceColor->isInitialized() && aSourceColor->size()) {
978     AttributeIntArrayPtr aDestColor = theDest->data()->intArray(ModelAPI_Result::COLOR_ID());
979     if (aDestColor.get()) {
980       aDestColor->setSize(aSourceColor->size());
981       for(int a = 0; a < aSourceColor->size(); a++)
982         aDestColor->setValue(a, aSourceColor->value(a));
983     }
984   }
985   // Iso-lines
986   AttributeIntArrayPtr aSource = theSource->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
987   if (aSource.get() && aSource->isInitialized() && aSource->size()) {
988     AttributeIntArrayPtr aDest = theDest->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
989     if (aDest.get()) {
990       aDest->setSize(aSource->size());
991       for(int a = 0; a < aSource->size(); a++)
992         aDest->setValue(a, aSource->value(a));
993     }
994   }
995   // deflection
996   AttributeDoublePtr aSourceDefl = theSource->data()->real(ModelAPI_Result::DEFLECTION_ID());
997   if (aSourceDefl.get() && aSourceDefl->isInitialized()) {
998     AttributeDoublePtr aDestDefl = theDest->data()->real(ModelAPI_Result::DEFLECTION_ID());
999     if (aDestDefl.get()) {
1000       aDestDefl->setValue(aSourceDefl->value());
1001     }
1002   }
1003   // transparency
1004   AttributeDoublePtr aSourceTransp = theSource->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1005   if (aSourceTransp.get() && aSourceTransp->isInitialized()) {
1006     AttributeDoublePtr aDestTransp = theDest->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1007     if (aDestTransp.get()) {
1008       aDestTransp->setValue(aSourceTransp->value());
1009     }
1010   }
1011 }
1012
1013 std::list<FeaturePtr> referencedFeatures(
1014   std::shared_ptr<ModelAPI_Result> theTarget, const std::string& theFeatureKind,
1015   const bool theSortResults)
1016 {
1017   std::set<FeaturePtr> aResSet; // collect in the set initially to avoid duplicates
1018   std::list<ResultPtr> allSubRes;
1019   allSubRes.push_back(theTarget);
1020   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theTarget);
1021   if (aBody.get())
1022     allSubs(aBody, allSubRes);
1023   std::list<ResultPtr>::iterator aSub = allSubRes.begin();
1024   for(; aSub != allSubRes.end(); aSub++) {
1025     const std::set<AttributePtr>& aRefs = (*aSub)->data()->refsToMe();
1026     std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1027     for(; aRef != aRefs.cend(); aRef++) {
1028       FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1029       if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1030         aResSet.insert(aFeat);
1031     }
1032   }
1033   // add also feature of the target that may be referenced as a whole
1034   FeaturePtr aTargetFeature = theTarget->document()->feature(theTarget);
1035   const std::set<AttributePtr>& aRefs = aTargetFeature->data()->refsToMe();
1036   std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1037   for(; aRef != aRefs.cend(); aRef++) {
1038     FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1039     if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1040       aResSet.insert(aFeat);
1041   }
1042   // check also Group-operations that may refer to groups - add them for theFeatureKind "Group"
1043   if (theFeatureKind == "Group") {
1044     std::set<FeaturePtr> aGroupOperations;
1045     for(bool aNeedIterate = true; aNeedIterate; ) {
1046       std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1047       for(; aResIter != aResSet.end(); aResIter++) {
1048         std::list<ResultPtr>::const_iterator aGroupRes = (*aResIter)->results().cbegin();
1049         for(; aGroupRes != (*aResIter)->results().cend(); aGroupRes++) {
1050           const std::set<AttributePtr>& aGroupRefs = (*aGroupRes)->data()->refsToMe();
1051           std::set<AttributePtr>::const_iterator aRefIt = aGroupRefs.cbegin();
1052           for(; aRefIt != aGroupRefs.cend(); aRefIt++) {
1053             FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
1054             if (aFeat.get() && !aGroupOperations.count(aFeat) && !aFeat->results().empty() &&
1055                 aFeat->firstResult()->groupName() == ModelAPI_ResultGroup::group()) {
1056               // iterate results of this group operation because it may be without theTarget shape
1057               GeomShapePtr aTargetShape = theTarget->shape();
1058               bool anIsIn = false;
1059               std::list<ResultPtr>::const_iterator anOpRes = aFeat->results().cbegin();
1060               for(; anOpRes != aFeat->results().cend() && !anIsIn; anOpRes++) {
1061                 GeomShapePtr anOpShape = (*anOpRes)->shape();
1062                 if (!anOpShape.get() || anOpShape->isNull())
1063                   continue;
1064                 for(GeomAPI_ShapeIterator aSubIt(anOpShape); aSubIt.more(); aSubIt.next()) {
1065                   if (aTargetShape->isSubShape(aSubIt.current(), false)) {
1066                     anIsIn = true;
1067                     break;
1068                   }
1069                 }
1070               }
1071               if (anIsIn)
1072                 aGroupOperations.insert(aFeat);
1073             }
1074           }
1075         }
1076       }
1077       // insert all new group operations into result and if they are, check for next dependencies
1078       aNeedIterate = false;
1079       std::set<FeaturePtr>::iterator aGroupOpIter = aGroupOperations.begin();
1080       for(; aGroupOpIter != aGroupOperations.end(); aGroupOpIter++) {
1081         if (aResSet.find(*aGroupOpIter) == aResSet.end()) {
1082           aResSet.insert(*aGroupOpIter);
1083           aNeedIterate = true;
1084         }
1085       }
1086     }
1087   }
1088
1089   std::list<FeaturePtr> aResList;
1090   std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1091   for(; aResIter != aResSet.end(); aResIter++) {
1092     if (theSortResults) { // sort results by creation-order
1093       std::list<FeaturePtr>::iterator aListIter = aResList.begin();
1094       for(; aListIter != aResList.end(); aListIter++) {
1095         if ((*aResIter)->document()->isLater(*aListIter, *aResIter))
1096           break;
1097       }
1098       if (aListIter == aResList.end()) // goes to the end
1099         aResList.push_back(*aResIter);
1100       else
1101         aResList.insert(aListIter, *aResIter);
1102     } else //just push to the end unsorted
1103       aResList.push_back(*aResIter);
1104   }
1105   return aResList;
1106 }
1107
1108 // LCOV_EXCL_STOP
1109
1110 } // namespace ModelAPI_Tools