]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Tools.cpp
Salome HOME
Fix for the ability to duplicate part which was translated.
[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,
163                        const std::string& theName)
164 {
165   for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
166     ObjectPtr anObject = theDocument->object(theGroup, anIndex);
167     if (anObject->data()->name() == theName)
168       return anObject;
169   }
170   // not found
171   return ObjectPtr();
172 }
173
174 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
175                   const std::string& theName, double& outValue, ResultParameterPtr& theParam)
176 {
177   ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
178   theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
179   if (!theParam.get())
180     return false;
181   // avoid usage of parameters created later than the initial parameter
182   if (theSearcher.get() && theDocument->isLater(theDocument->feature(theParam), theSearcher))
183     return false;
184   AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
185   outValue = aValueAttribute->value();
186   return true;
187 }
188
189 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue,
190                   ResultParameterPtr& theParam, const DocumentPtr& theDocument)
191 {
192   SessionPtr aSession = ModelAPI_Session::get();
193   std::list<DocumentPtr> aDocList;
194   DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
195   if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
196     return true;
197   DocumentPtr aRootDocument = aSession->moduleDocument();
198   if (aDocument != aRootDocument) {
199     // any parameters in PartSet is okindependently on the Part position (issu #1504)
200     if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
201       return true;
202   }
203   return false;
204 }
205
206 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
207 {
208   // to optimize and avoid of crash on partset document close
209   // (don't touch the sub-document structure)
210   if (theMain != theSub) {
211     for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
212       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
213           theMain->object(ModelAPI_ResultPart::group(), a));
214       if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
215         return aPart;
216       }
217     }
218   }
219   return ResultPtr();
220 }
221
222 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
223 {
224   // to optimize and avoid of crash on partset document close
225   // (don't touch the sub-document structure)
226   if (theMain != theSub) {
227     // iteration from top to bottom to avoid finding the movement documents before the original
228     int aSize = theMain->size(ModelAPI_Feature::group());
229     for (int a = 0; a < aSize; a++) {
230       FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
231           theMain->object(ModelAPI_Feature::group(), a));
232       if (aPartFeat.get()) {
233         const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
234         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
235         for(; aRes != aResList.end(); aRes++) {
236           ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
237           if (aPart.get()) {
238             if (aPart->isActivated() && aPart->partDoc() == theSub)
239               return aPartFeat;
240           } else break; // if the first is not Part, others are also not
241         }
242       }
243     }
244   }
245   return FeaturePtr();
246 }
247
248 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
249 {
250   if (theFeature.get() && theFeature->data()->isValid()) {
251     const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
252     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
253     for(; aRefIter != aRefs.end(); aRefIter++) {
254       CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
255         ((*aRefIter)->owner());
256       if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
257         return aComp;
258     }
259   }
260   return CompositeFeaturePtr(); // not found
261 }
262
263 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
264 {
265   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
266   if (aBody.get()) {
267     FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
268     if (aFeatureOwner.get()) {
269       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
270         aFeatureOwner->results().cbegin();
271       for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
272         ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
273         if (aComp && aComp->isSub(aBody))
274           return aComp;
275       }
276     }
277   }
278   return ResultCompSolidPtr(); // not found
279 }
280
281 bool hasSubResults(const ResultPtr& theResult)
282 {
283   ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
284   return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
285 }
286
287 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
288 {
289   if (!theFeature.get()) // safety: for empty feature no results
290     return;
291   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
292   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
293   for (; aRIter != aResults.cend(); aRIter++) {
294     theResults.push_back(*aRIter);
295     // iterate sub-bodies of compsolid
296     ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
297     if (aComp.get()) {
298       int aNumSub = aComp->numberOfSubs();
299       for(int a = 0; a < aNumSub; a++) {
300         theResults.push_back(aComp->subResult(a));
301       }
302     }
303   }
304 }
305
306 //******************************************************************
307 bool allDocumentsActivated(std::string& theNotActivatedNames)
308 {
309   theNotActivatedNames = "";
310   bool anAllPartActivated = true;
311
312   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
313   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
314   for (int i = 0; i < aSize; i++) {
315     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
316     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
317     if (!aPart->isActivated()) {
318       anAllPartActivated = false;
319       if (!theNotActivatedNames.empty())
320         theNotActivatedNames += ", ";
321       theNotActivatedNames += aObject->data()->name().c_str();
322     }
323   }
324   return anAllPartActivated;
325 }
326
327 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
328                                  const bool theFlushRedisplay,
329                                  const bool theUseComposite,
330                                  const bool theUseRecursion)
331 {
332 #ifdef DEBUG_REMOVE_FEATURES
333   printListInfo(theFeatures, "selection: ");
334 #endif
335
336   std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
337   ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
338 #ifdef DEBUG_REMOVE_FEATURES
339   printMapInfo(aReferences, "allDependencies: ");
340 #endif
341
342   std::set<FeaturePtr> aFeaturesRefsTo;
343   ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
344 #ifdef DEBUG_REMOVE_FEATURES
345   printListInfo(aFeaturesRefsTo, "references: ");
346 #endif
347
348   std::set<FeaturePtr> aFeatures = theFeatures;
349   if (!aFeaturesRefsTo.empty())
350     aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
351 #ifdef DEBUG_REMOVE_FEATURES
352   printListInfo(aFeatures, "removeFeatures: ");
353 #endif
354
355   return ModelAPI_Tools::removeFeatures(aFeatures, false);
356 }
357
358 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
359                     const bool theFlushRedisplay)
360 {
361   bool isDone = false;
362   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
363                                        aLast = theFeatures.end();
364   for (; anIt != aLast; anIt++) {
365     FeaturePtr aFeature = *anIt;
366     if (aFeature.get()) {
367       DocumentPtr aDoc = aFeature->document();
368       // flush REDISPLAY signal after remove feature
369       aDoc->removeFeature(aFeature);
370       isDone = true;
371     }
372   }
373   if (isDone && theFlushRedisplay) {
374     // the redisplay signal should be flushed in order to erase
375     // the feature presentation in the viewer
376     // if should be done after removeFeature() of document
377     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
378   }
379   return true;
380 }
381
382 // Fills the references list by all references of the feature from the references map.
383 // This is a recusive method to find references by next found feature in the map of references.
384 // \param theFeature a feature to find references
385 // \param theReferencesMap a map of references
386 // \param theReferences an out container of references
387 void addRefsToFeature(const FeaturePtr& theFeature,
388                       const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
389                       std::map<FeaturePtr, std::set<FeaturePtr> >& theProcessedReferences,
390                       int theRecLevel,
391                       std::set<FeaturePtr>& theReferences)
392 {
393   if (theRecLevel > RECURSE_TOP_LEVEL)
394     return;
395   theRecLevel++;
396
397   // if the feature is already processed, get the ready references from the map
398   if (theProcessedReferences.find(theFeature) != theProcessedReferences.end()) {
399     std::set<FeaturePtr> aReferences = theProcessedReferences.at(theFeature);
400     theReferences.insert(aReferences.begin(), aReferences.end());
401     return;
402   }
403
404   if (theReferencesMap.find(theFeature) == theReferencesMap.end())
405     return; // this feature is not in the selection list, so exists without references to it
406   std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
407
408   std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
409                                        aLast = aMainReferences.end();
410 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
411   std::string aSpacing;
412   for (int i = 0; i < theRecLevel; i++)
413     aSpacing.append(" ");
414 #endif
415
416   for (; anIt != aLast; anIt++) {
417     FeaturePtr aRefFeature = *anIt;
418 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
419   std::cout << aSpacing << " Ref: " << getFeatureInfo(aRefFeature) << std::endl;
420 #endif
421     if (theReferences.find(aRefFeature) == theReferences.end())
422       theReferences.insert(aRefFeature);
423     addRefsToFeature(aRefFeature, theReferencesMap, theProcessedReferences,
424                      theRecLevel, theReferences);
425   }
426 }
427
428 // For each feature from the feature list it searches references to the feature and append them
429 // to the references map. This is a recusive method.
430 // \param theFeature a feature to find references
431 // \param theReferencesMap a map of references
432 // \param theReferences an out container of references
433 void findReferences(const std::set<FeaturePtr>& theFeatures,
434                     std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
435                     const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
436 {
437   if (theRecLevel > RECURSE_TOP_LEVEL)
438     return;
439   theRecLevel++;
440   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
441                                         aLast = theFeatures.end();
442   for (; anIt != aLast; anIt++) {
443     FeaturePtr aFeature = *anIt;
444     if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
445       DocumentPtr aSelFeatureDoc = aFeature->document();
446       std::set<FeaturePtr> aSelRefFeatures;
447       aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
448       if (theUseComposite) { // do not filter selection
449         theReferences[aFeature] = aSelRefFeatures;
450       }
451       else { // filter references to skip composition features of the current feature
452         std::set<FeaturePtr> aFilteredFeatures;
453         std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
454                                              aLast = aSelRefFeatures.end();
455         for (; anIt != aLast; anIt++) {
456           FeaturePtr aCFeature = *anIt;
457           CompositeFeaturePtr aComposite =
458             std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
459           if (aComposite.get() && aComposite->isSub(aFeature))
460             continue; /// composite of the current feature should be skipped
461           aFilteredFeatures.insert(aCFeature);
462         }
463         theReferences[aFeature] = aFilteredFeatures;
464       }
465       if (theUseRecursion) {
466 #ifdef DEBUG_CYCLING_1550
467         findReferences(aSelRefFeatures, theReferences, theUseComposite,
468                        theUseRecursion, theRecLevel);
469 #else
470         findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
471                        theRecLevel);
472 #endif
473       }
474     }
475   }
476 }
477
478 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
479                        std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
480                        const bool theUseComposite,
481                        const bool theUseRecursion)
482 {
483   // For dependencies, find main_list:
484   // sk_1(ext_1, vertex_1)
485   // ext_1(bool_1, sk_3)
486   // vertex_1()
487   // sk_2(ext_2)
488   // ext_2(bool_2)
489   // sk_3()
490   // Information: bool_1 is not selected, ext_2(bool_2) exists
491   // find all referenced features
492   std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
493   int aRecLevel = 0;
494   findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
495
496 #ifdef DEBUG_REMOVE_FEATURES
497   printMapInfo(aMainList, "firstDependencies");
498 #endif
499   // find all dependencies for each object:
500   // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
501   // ext_1(bool_1, sk_3)
502   // vertex_1()
503   // sk_2(ext_2) + (bool_1)
504   // ext_2(bool_1)
505   // sk_3()
506   std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
507                                                               aMainLast = aMainList.end();
508   for (; aMainIt != aMainLast; aMainIt++) {
509     FeaturePtr aMainListFeature = aMainIt->first;
510
511     //std::string aName = aMainListFeature->name();
512     std::set<FeaturePtr> aMainRefList = aMainIt->second;
513
514 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
515     char aBuf[50];
516     int n = sprintf(aBuf, "%d", aMainRefList.size());
517     std::string aSize(aBuf);
518     std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
519               << ", references size = " << aSize << std::endl;
520 #endif
521     std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
522                                          aLast = aMainRefList.end();
523     std::set<FeaturePtr> aResultRefList;
524     aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
525     for (; anIt != aLast; anIt++) {
526       FeaturePtr aFeature = *anIt;
527       int aRecLevel = 0;
528 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
529       std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
530 #endif
531       aRecLevel++;
532       addRefsToFeature(aFeature, aMainList, theReferences,
533                        aRecLevel, aResultRefList/*aMainRefList*/);
534     }
535     theReferences[aMainListFeature] = aResultRefList;
536   }
537 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
538     std::cout << std::endl;
539 #endif
540
541 #ifdef DEBUG_REMOVE_FEATURES
542   printMapInfo(theReferences, "allDependencies");
543 #endif
544 }
545
546 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
547                         const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
548                         std::set<FeaturePtr>& theFeaturesRefsTo)
549 {
550   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
551                                        aLast = theFeatures.end();
552   for (; anIt != aLast; anIt++) {
553     FeaturePtr aFeature = *anIt;
554     if (theReferences.find(aFeature) == theReferences.end())
555       continue;
556     std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
557     std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
558     for (; aRefIt != aRefLast; aRefIt++) {
559       FeaturePtr aRefFeature = *aRefIt;
560       CompositeFeaturePtr aComposite =
561         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
562       if (aComposite.get() && aComposite->isSub(aFeature))
563         continue; /// composite of the current feature should not be removed
564
565       if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
566           theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
567         theFeaturesRefsTo.insert(aRefFeature);
568     }
569   }
570 }
571
572 void getConcealedResults(const FeaturePtr& theFeature,
573                          std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
574 {
575   SessionPtr aSession = ModelAPI_Session::get();
576
577   std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
578   theFeature->data()->referencesToObjects(aRefs);
579   std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
580                                                   anIt = aRefs.begin(), aLast = aRefs.end();
581   std::set<ResultPtr> alreadyThere; // to avoid duplications
582   for (; anIt != aLast; anIt++) {
583     if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
584       continue; // use only concealed attributes
585     std::list<ObjectPtr> anObjects = (*anIt).second;
586     std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
587     for (; anOIt != anOLast; anOIt++) {
588       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
589       if (aResult && aResult->isConcealed()) {
590         if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
591           alreadyThere.insert(aResult);
592         else continue;
593         theResults.push_back(aResult);
594       }
595     }
596   }
597 }
598
599 } // namespace ModelAPI_Tools