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