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