Salome HOME
Merge branch 'master' of salome:modules/shaper
[modules/shaper.git] / src / ModelAPI / ModelAPI_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Tools.cpp
4 // Created:     06 Aug 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ModelAPI_Tools.h"
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Document.h>
10 #include <ModelAPI_Object.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_ResultParameter.h>
13 #include <ModelAPI_ResultPart.h>
14 #include <ModelAPI_AttributeDocRef.h>
15 #include <ModelAPI_Validator.h>
16 #include <list>
17 #include <map>
18 #include <iostream>
19
20 #include <Events_Loop.h>
21 #include <ModelAPI_Events.h>
22
23 #define RECURSE_TOP_LEVEL 50
24
25 //#define DEBUG_REMOVE_FEATURES
26 //#define DEBUG_REMOVE_FEATURES_RECURSE
27 //#define DEBUG_CYCLING_1550
28
29 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
30 #include <sstream>
31 std::string getFeatureInfo(FeaturePtr theFeature)
32 {
33   if (!theFeature.get())
34     return "";
35   //std::ostringstream aPtrStr;
36   //aPtrStr << "[" << theFeature.get() << "] ";
37   std::string aFeatureInfo = /*aPtrStr.str() + */theFeature->name();
38   CompositeFeaturePtr aComposite = ModelAPI_Tools::compositeOwner(theFeature);
39   if (aComposite.get()) {
40       aFeatureInfo = aFeatureInfo + "[in " + aComposite->name() + "]";
41   }
42   return aFeatureInfo;
43 }
44 #endif
45
46 #ifdef DEBUG_REMOVE_FEATURES
47 void printMapInfo(const std::map<FeaturePtr, std::set<FeaturePtr> >& theMainList,
48                   const std::string& thePrefix)
49 {
50   std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = theMainList.begin(),
51                                                               aMainLast = theMainList.end();
52   std::string anInfo;
53   for (; aMainIt != aMainLast; aMainIt++) {
54     FeaturePtr aMainListFeature = aMainIt->first;
55     std::set<FeaturePtr> aMainRefList = aMainIt->second;
56     std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(), aLast = aMainRefList.end();
57     std::string aRefsInfo;
58     for (; anIt != aLast; anIt++) {
59       aRefsInfo += (*anIt)->name().c_str();
60       if (anIt != aLast)
61         aRefsInfo += ", ";
62     }
63     if (!aRefsInfo.empty()) {
64       anInfo = anInfo + aMainListFeature->name().c_str() + ": " + aRefsInfo + "\n";
65     }
66   }
67   std::cout << thePrefix.c_str() << " [feature: references to]: \n" << anInfo.c_str() << std::endl;
68 }
69
70 void printListInfo(const std::set<FeaturePtr>& theMainList,
71                   const std::string& thePrefix)
72 {
73   std::set<FeaturePtr>::const_iterator aMainIt = theMainList.begin(),
74                                        aMainLast = theMainList.end();
75   std::string anInfo;
76   for (; aMainIt != aMainLast; aMainIt++) {
77     FeaturePtr aRefFeature = *aMainIt;
78     anInfo += aRefFeature->name().c_str();
79     if (aMainIt != aMainLast)
80       anInfo += ", ";
81   }
82   std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
83 }
84 #endif
85
86 namespace ModelAPI_Tools {
87
88 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
89 {
90   return theResult->shape();
91 }
92
93 void shapesOfType(const FeaturePtr& theFeature,
94                   const GeomAPI_Shape::ShapeType& theType,
95                   std::set<GeomShapePtr>& theShapes)
96 {
97   std::list<ResultPtr> aResults = theFeature->results();
98   std::list<ResultPtr>::const_iterator aRIter = aResults.cbegin();
99   for (; aRIter != aResults.cend(); aRIter++) {
100     ResultPtr aResult = *aRIter;
101     GeomShapePtr aShape = aResult->shape();
102     if (aShape.get() && aShape->shapeType() == theType)
103       theShapes.insert(aShape);
104   }
105 }
106
107 const char* toString(ModelAPI_ExecState theExecState) 
108 {
109 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
110   switch (theExecState) {
111   TO_STRING(ModelAPI_StateDone)
112   TO_STRING(ModelAPI_StateMustBeUpdated)
113   TO_STRING(ModelAPI_StateExecFailed)
114   TO_STRING(ModelAPI_StateInvalidArgument)
115   TO_STRING(ModelAPI_StateNothing)
116   default: return "Unknown ExecState.";
117   }
118 #undef TO_STRING
119 }
120
121 std::string getFeatureError(const FeaturePtr& theFeature)
122 {
123   std::string anError;
124   if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
125     return anError;
126
127   // to be removed later, this error should be got from the feature
128   if (theFeature->data()->execState() == ModelAPI_StateDone ||
129       theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
130     return anError;
131
132   // set error indication
133   anError = theFeature->error();
134   if (anError.empty()) {
135     bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
136                  || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
137     if (!isDone) {
138       anError = toString(theFeature->data()->execState());
139       // If the feature is Composite and error is StateInvalidArgument,
140       // error text should include error of first invalid sub-feature. Otherwise
141       // it is not clear what is the reason of the invalid argument.
142       if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
143         CompositeFeaturePtr aComposite =
144                     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
145         if (aComposite) {
146           for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
147             FeaturePtr aSubFeature = aComposite->subFeature(i);
148             std::string aSubFeatureError = getFeatureError(aSubFeature);
149             if (!aSubFeatureError.empty()) {
150               anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
151               break;
152             }
153           }
154         }
155       }
156     }
157   }
158
159   return anError;
160 }
161
162 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup, const std::string& theName)
163 {
164   for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
165     ObjectPtr anObject = theDocument->object(theGroup, anIndex);
166     if (anObject->data()->name() == theName)
167       return anObject;
168   }
169   // not found
170   return ObjectPtr();
171 }
172
173 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
174                   const std::string& theName, double& outValue, ResultParameterPtr& theParam)
175 {
176   ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
177   theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
178   if (!theParam.get())
179     return false;
180   // avoid usage of parameters created later than the initial parameter
181   if (theSearcher.get() && theDocument->isLater(theDocument->feature(theParam), theSearcher))
182     return false;
183   AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
184   outValue = aValueAttribute->value();
185   return true;
186 }
187
188 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue, ResultParameterPtr& theParam,
189                   const DocumentPtr& theDocument)
190 {
191   SessionPtr aSession = ModelAPI_Session::get();
192   std::list<DocumentPtr> aDocList;
193   DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
194   if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
195     return true;
196   DocumentPtr aRootDocument = aSession->moduleDocument();
197   if (aDocument != aRootDocument) {
198     // any parameters in PartSet is okindependently on the Part position (issu #1504)
199     if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
200       return true;
201   }
202   return false;
203 }
204
205 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
206 {
207   if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
208     for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
209       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
210           theMain->object(ModelAPI_ResultPart::group(), a));
211       if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
212         return aPart;
213       }
214     }
215   }
216   return ResultPtr();
217 }
218
219 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
220 {
221   if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
222     for (int a = theMain->size(ModelAPI_Feature::group()) - 1; a >= 0; a--) {
223       FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
224           theMain->object(ModelAPI_Feature::group(), a));
225       if (aPartFeat.get()) {
226         const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
227         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
228         for(; aRes != aResList.end(); aRes++) {
229           ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
230           if (aPart.get()) {
231             if (aPart->isActivated() && aPart->partDoc() == theSub)
232               return aPartFeat;
233           } else break; // if the first is not Part, others are also not
234         }
235       }
236     }
237   }
238   return FeaturePtr();
239 }
240
241 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
242 {
243   if (theFeature.get() && theFeature->data()->isValid()) {
244     const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
245     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
246     for(; aRefIter != aRefs.end(); aRefIter++) {
247       CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
248         ((*aRefIter)->owner());
249       if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
250         return aComp;
251     }
252   }
253   return CompositeFeaturePtr(); // not found
254 }
255
256 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
257 {
258   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
259   if (aBody.get()) {
260     FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
261     if (aFeatureOwner.get()) {
262       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = 
263         aFeatureOwner->results().cbegin();
264       for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
265         ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
266         if (aComp && aComp->isSub(aBody))
267           return aComp;
268       }
269     }
270   }
271   return ResultCompSolidPtr(); // not found
272 }
273
274 bool hasSubResults(const ResultPtr& theResult)
275 {
276   ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
277   return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
278 }
279
280 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
281 {
282   if (!theFeature.get()) // safety: for empty feature no results
283     return;
284   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
285   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
286   for (; aRIter != aResults.cend(); aRIter++) {
287     theResults.push_back(*aRIter);
288     // iterate sub-bodies of compsolid
289     ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
290     if (aComp.get()) {
291       int aNumSub = aComp->numberOfSubs();
292       for(int a = 0; a < aNumSub; a++) {
293         theResults.push_back(aComp->subResult(a));
294       }
295     }
296   }
297 }
298
299 //******************************************************************
300 bool allDocumentsActivated(std::string& theNotActivatedNames)
301 {
302   theNotActivatedNames = "";
303   bool anAllPartActivated = true;
304
305   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
306   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
307   for (int i = 0; i < aSize; i++) {
308     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
309     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
310     if (!aPart->isActivated()) {
311       anAllPartActivated = false;
312       if (!theNotActivatedNames.empty())
313         theNotActivatedNames += ", ";
314       theNotActivatedNames += aObject->data()->name().c_str();
315     }
316   }
317   return anAllPartActivated;
318 }
319
320 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
321                                  const bool theFlushRedisplay,
322                                  const bool theUseComposite,
323                                  const bool theUseRecursion)
324 {
325 #ifdef DEBUG_REMOVE_FEATURES
326   printListInfo(theFeatures, "selection: ");
327 #endif
328
329   std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
330   ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
331 #ifdef DEBUG_REMOVE_FEATURES
332   printMapInfo(aReferences, "allDependencies: ");
333 #endif
334
335   std::set<FeaturePtr> aFeaturesRefsTo;
336   ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
337 #ifdef DEBUG_REMOVE_FEATURES
338   printListInfo(aFeaturesRefsTo, "references: ");
339 #endif
340
341   std::set<FeaturePtr> aFeatures = theFeatures;
342   if (!aFeaturesRefsTo.empty())
343     aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
344 #ifdef DEBUG_REMOVE_FEATURES
345   printListInfo(aFeatures, "removeFeatures: ");
346 #endif
347
348   return ModelAPI_Tools::removeFeatures(aFeatures, false);
349 }
350
351 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
352                     const bool theFlushRedisplay)
353 {
354   bool isDone = false;
355   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
356                                        aLast = theFeatures.end();
357   for (; anIt != aLast; anIt++) {
358     FeaturePtr aFeature = *anIt;
359     if (aFeature.get()) {
360       DocumentPtr aDoc = aFeature->document();
361       // flush REDISPLAY signal after remove feature
362       aDoc->removeFeature(aFeature);
363       isDone = true;
364     }
365   }
366   if (isDone && theFlushRedisplay) {
367     // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
368     // if should be done after removeFeature() of document
369     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
370   }
371   return true;
372 }
373
374 // Fills the references list by all references of the feature from the references map.
375 // This is a recusive method to find references by next found feature in the map of references.
376 // \param theFeature a feature to find references
377 // \param theReferencesMap a map of references
378 // \param theReferences an out container of references
379 void addRefsToFeature(const FeaturePtr& theFeature,
380                       const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
381                       std::map<FeaturePtr, std::set<FeaturePtr> >& theProcessedReferences,
382                       int theRecLevel,
383                       std::set<FeaturePtr>& theReferences)
384 {
385   if (theRecLevel > RECURSE_TOP_LEVEL)
386     return;
387   theRecLevel++;
388
389   // if the feature is already processed, get the ready references from the map
390   if (theProcessedReferences.find(theFeature) != theProcessedReferences.end()) {
391     std::set<FeaturePtr> aReferences = theProcessedReferences.at(theFeature);
392     theReferences.insert(aReferences.begin(), aReferences.end());
393     return;
394   }
395
396   if (theReferencesMap.find(theFeature) == theReferencesMap.end())
397     return; // this feature is not in the selection list, so exists without references to it
398   std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
399
400   std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
401                                        aLast = aMainReferences.end();
402 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
403   std::string aSpacing;
404   for (int i = 0; i < theRecLevel; i++)
405     aSpacing.append(" ");
406 #endif
407
408   for (; anIt != aLast; anIt++) {
409     FeaturePtr aRefFeature = *anIt;
410 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
411   std::cout << aSpacing << " Ref: " << getFeatureInfo(aRefFeature) << std::endl;
412 #endif
413     if (theReferences.find(aRefFeature) == theReferences.end())
414       theReferences.insert(aRefFeature);
415     addRefsToFeature(aRefFeature, theReferencesMap, theProcessedReferences, theRecLevel, theReferences);
416   }
417 }
418
419 // For each feature from the feature list it searches references to the feature and append them
420 // to the references map. This is a recusive method.
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 findReferences(const std::set<FeaturePtr>& theFeatures,
425                     std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
426                     const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
427 {
428   if (theRecLevel > RECURSE_TOP_LEVEL)
429     return;
430   theRecLevel++;
431   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
432                                         aLast = theFeatures.end();
433   for (; anIt != aLast; anIt++) {
434     FeaturePtr aFeature = *anIt;
435     if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
436       DocumentPtr aSelFeatureDoc = aFeature->document();
437       std::set<FeaturePtr> aSelRefFeatures;
438       aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
439       if (theUseComposite) { // do not filter selection
440         theReferences[aFeature] = aSelRefFeatures;
441       }
442       else { // filter references to skip composition features of the current feature
443         std::set<FeaturePtr> aFilteredFeatures;
444         std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
445                                              aLast = aSelRefFeatures.end();
446         for (; anIt != aLast; anIt++) {
447           FeaturePtr aCFeature = *anIt;
448           CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
449           if (aComposite.get() && aComposite->isSub(aFeature))
450             continue; /// composite of the current feature should be skipped
451           aFilteredFeatures.insert(aCFeature);
452         }
453         theReferences[aFeature] = aFilteredFeatures;
454       }
455       if (theUseRecursion) {
456 #ifdef DEBUG_CYCLING_1550
457         findReferences(aSelRefFeatures, theReferences, theUseComposite, theUseRecursion, theRecLevel);
458 #else
459         findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
460                        theRecLevel);
461 #endif
462       }
463     }
464   }
465 }
466
467 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
468                        std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
469                        const bool theUseComposite,
470                        const bool theUseRecursion)
471 {
472   // For dependencies, find main_list:
473   // sk_1(ext_1, vertex_1)
474   // ext_1(bool_1, sk_3)
475   // vertex_1()
476   // sk_2(ext_2)
477   // ext_2(bool_2)
478   // sk_3()
479   // Information: bool_1 is not selected, ext_2(bool_2) exists
480   // find all referenced features
481   std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
482   int aRecLevel = 0;
483   findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
484
485 #ifdef DEBUG_REMOVE_FEATURES
486   printMapInfo(aMainList, "firstDependencies");
487 #endif
488   // find all dependencies for each object:
489   // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
490   // ext_1(bool_1, sk_3)
491   // vertex_1()
492   // sk_2(ext_2) + (bool_1)
493   // ext_2(bool_1)
494   // sk_3()
495   std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
496                                                               aMainLast = aMainList.end();
497   for (; aMainIt != aMainLast; aMainIt++) {
498     FeaturePtr aMainListFeature = aMainIt->first;
499
500     //std::string aName = aMainListFeature->name();
501     std::set<FeaturePtr> aMainRefList = aMainIt->second;
502
503 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
504     char aBuf[50];
505     int n = sprintf(aBuf, "%d", aMainRefList.size());
506     std::string aSize(aBuf);
507     std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
508               << ", references size = " << aSize << std::endl;
509 #endif
510     std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
511                                          aLast = aMainRefList.end();
512     std::set<FeaturePtr> aResultRefList;
513     aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
514     for (; anIt != aLast; anIt++) {
515       FeaturePtr aFeature = *anIt;
516       int aRecLevel = 0;
517 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
518       std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
519 #endif
520       aRecLevel++;
521       addRefsToFeature(aFeature, aMainList, theReferences, aRecLevel, aResultRefList/*aMainRefList*/);
522     }
523     theReferences[aMainListFeature] = aResultRefList;
524   }
525 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
526     std::cout << std::endl;
527 #endif
528
529 #ifdef DEBUG_REMOVE_FEATURES
530   printMapInfo(theReferences, "allDependencies");
531 #endif
532 }
533
534 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
535                         const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
536                         std::set<FeaturePtr>& theFeaturesRefsTo)
537 {
538   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
539                                        aLast = theFeatures.end();
540   for (; anIt != aLast; anIt++) {
541     FeaturePtr aFeature = *anIt;
542     if (theReferences.find(aFeature) == theReferences.end())
543       continue;
544     std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
545     std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
546     for (; aRefIt != aRefLast; aRefIt++) {
547       FeaturePtr aRefFeature = *aRefIt;
548       CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
549       if (aComposite.get() && aComposite->isSub(aFeature))
550         continue; /// composite of the current feature should not be removed
551
552       if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
553           theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
554         theFeaturesRefsTo.insert(aRefFeature);
555     }
556   }
557 }
558
559 void getConcealedResults(const FeaturePtr& theFeature,
560                          std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
561 {
562   SessionPtr aSession = ModelAPI_Session::get();
563
564   std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
565   theFeature->data()->referencesToObjects(aRefs);
566   std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
567                                                   anIt = aRefs.begin(), aLast = aRefs.end();
568   std::set<ResultPtr> alreadyThere; // to avoid duplications
569   for (; anIt != aLast; anIt++) {
570     if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
571       continue; // use only concealed attributes
572     std::list<ObjectPtr> anObjects = (*anIt).second;
573     std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
574     for (; anOIt != anOLast; anOIt++) {
575       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
576       if (aResult && aResult->isConcealed()) {
577         if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
578           alreadyThere.insert(aResult);
579         else continue;
580         theResults.push_back(aResult);
581       }
582     }
583   }
584 }
585
586 } // namespace ModelAPI_Tools