]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Tools.cpp
Salome HOME
8d0b8f01829463233bca856e132203c01719e541
[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   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
363   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
364   for (; aRIter != aResults.cend(); aRIter++) {
365     theResults.push_back(*aRIter);
366     // iterate sub-bodies of compsolid
367     ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
368     if (aComp.get()) {
369       int aNumSub = aComp->numberOfSubs();
370       for(int a = 0; a < aNumSub; a++) {
371         theResults.push_back(aComp->subResult(a));
372       }
373     }
374   }
375 }
376
377 //******************************************************************
378 bool allDocumentsActivated(std::string& theNotActivatedNames)
379 {
380   theNotActivatedNames = "";
381   bool anAllPartActivated = true;
382
383   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
384   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
385   for (int i = 0; i < aSize; i++) {
386     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
387     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
388     if (!aPart->isActivated()) {
389       anAllPartActivated = false;
390       if (!theNotActivatedNames.empty())
391         theNotActivatedNames += ", ";
392       theNotActivatedNames += aObject->data()->name().c_str();
393     }
394   }
395   return anAllPartActivated;
396 }
397
398 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
399                                  const bool theFlushRedisplay,
400                                  const bool theUseComposite,
401                                  const bool theUseRecursion)
402 {
403 #ifdef DEBUG_REMOVE_FEATURES
404   printListInfo(theFeatures, "selection: ");
405 #endif
406
407   std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
408   ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
409 #ifdef DEBUG_REMOVE_FEATURES
410   printMapInfo(aReferences, "allDependencies: ");
411 #endif
412
413   std::set<FeaturePtr> aFeaturesRefsTo;
414   ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
415 #ifdef DEBUG_REMOVE_FEATURES
416   printListInfo(aFeaturesRefsTo, "references: ");
417 #endif
418
419   std::set<FeaturePtr> aFeatures = theFeatures;
420   if (!aFeaturesRefsTo.empty())
421     aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
422 #ifdef DEBUG_REMOVE_FEATURES
423   printListInfo(aFeatures, "removeFeatures: ");
424 #endif
425
426   return ModelAPI_Tools::removeFeatures(aFeatures, false);
427 }
428
429 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
430                     const bool theFlushRedisplay)
431 {
432   bool isDone = false;
433   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
434                                        aLast = theFeatures.end();
435   for (; anIt != aLast; anIt++) {
436     FeaturePtr aFeature = *anIt;
437     if (aFeature.get()) {
438       DocumentPtr aDoc = aFeature->document();
439       // flush REDISPLAY signal after remove feature
440       aDoc->removeFeature(aFeature);
441       isDone = true;
442     }
443   }
444   if (isDone && theFlushRedisplay) {
445     // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
446     // if should be done after removeFeature() of document
447     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
448   }
449   return true;
450 }
451
452 // Fills the references list by all references of the feature from the references map.
453 // This is a recusive method to find references by next found feature in the map of references.
454 // \param theFeature a feature to find references
455 // \param theReferencesMap a map of references
456 // \param theReferences an out container of references
457 void addRefsToFeature(const FeaturePtr& theFeature,
458                       const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
459                       std::map<FeaturePtr, std::set<FeaturePtr> >& theProcessedReferences,
460                       int theRecLevel,
461                       std::set<FeaturePtr>& theReferences)
462 {
463   if (theRecLevel > RECURSE_TOP_LEVEL)
464     return;
465   theRecLevel++;
466
467   // if the feature is already processed, get the ready references from the map
468   if (theProcessedReferences.find(theFeature) != theProcessedReferences.end()) {
469     std::set<FeaturePtr> aReferences = theProcessedReferences.at(theFeature);
470     theReferences.insert(aReferences.begin(), aReferences.end());
471     return;
472   }
473
474   if (theReferencesMap.find(theFeature) == theReferencesMap.end())
475     return; // this feature is not in the selection list, so exists without references to it
476   std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
477
478   std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
479                                        aLast = aMainReferences.end();
480 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
481   std::string aSpacing;
482   for (int i = 0; i < theRecLevel; i++)
483     aSpacing.append(" ");
484 #endif
485
486   for (; anIt != aLast; anIt++) {
487     FeaturePtr aRefFeature = *anIt;
488 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
489   std::cout << aSpacing << " Ref: " << getFeatureInfo(aRefFeature) << std::endl;
490 #endif
491     if (theReferences.find(aRefFeature) == theReferences.end())
492       theReferences.insert(aRefFeature);
493     addRefsToFeature(aRefFeature, theReferencesMap, theProcessedReferences, theRecLevel, theReferences);
494   }
495 }
496
497 // For each feature from the feature list it searches references to the feature and append them
498 // to the references map. This is a recusive method.
499 // \param theFeature a feature to find references
500 // \param theReferencesMap a map of references
501 // \param theReferences an out container of references
502 void findReferences(const std::set<FeaturePtr>& theFeatures,
503                     std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
504                     const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
505 {
506   if (theRecLevel > RECURSE_TOP_LEVEL)
507     return;
508   theRecLevel++;
509   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
510                                         aLast = theFeatures.end();
511   for (; anIt != aLast; anIt++) {
512     FeaturePtr aFeature = *anIt;
513     if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
514       DocumentPtr aSelFeatureDoc = aFeature->document();
515       std::set<FeaturePtr> aSelRefFeatures;
516       aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
517       if (theUseComposite) { // do not filter selection
518         theReferences[aFeature] = aSelRefFeatures;
519       }
520       else { // filter references to skip composition features of the current feature
521         std::set<FeaturePtr> aFilteredFeatures;
522         std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
523                                              aLast = aSelRefFeatures.end();
524         for (; anIt != aLast; anIt++) {
525           FeaturePtr aCFeature = *anIt;
526           CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
527           if (aComposite.get() && aComposite->isSub(aFeature))
528             continue; /// composite of the current feature should be skipped
529           aFilteredFeatures.insert(aCFeature);
530         }
531         theReferences[aFeature] = aFilteredFeatures;
532       }
533       if (theUseRecursion) {
534 #ifdef DEBUG_CYCLING_1550
535         findReferences(aSelRefFeatures, theReferences, theUseComposite, theUseRecursion, theRecLevel);
536 #else
537         findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
538                        theRecLevel);
539 #endif
540       }
541     }
542   }
543 }
544
545 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
546                        std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
547                        const bool theUseComposite,
548                        const bool theUseRecursion)
549 {
550   // For dependencies, find main_list:
551   // sk_1(ext_1, vertex_1)
552   // ext_1(bool_1, sk_3)
553   // vertex_1()
554   // sk_2(ext_2)
555   // ext_2(bool_2)
556   // sk_3()
557   // Information: bool_1 is not selected, ext_2(bool_2) exists
558   // find all referenced features
559   std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
560   int aRecLevel = 0;
561   findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
562
563 #ifdef DEBUG_REMOVE_FEATURES
564   printMapInfo(aMainList, "firstDependencies");
565 #endif
566   // find all dependencies for each object:
567   // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
568   // ext_1(bool_1, sk_3)
569   // vertex_1()
570   // sk_2(ext_2) + (bool_1)
571   // ext_2(bool_1)
572   // sk_3()
573   std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
574                                                               aMainLast = aMainList.end();
575   for (; aMainIt != aMainLast; aMainIt++) {
576     FeaturePtr aMainListFeature = aMainIt->first;
577
578     //std::string aName = aMainListFeature->name();
579     std::set<FeaturePtr> aMainRefList = aMainIt->second;
580
581 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
582     char aBuf[50];
583     int n = sprintf(aBuf, "%d", aMainRefList.size());
584     std::string aSize(aBuf);
585     std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
586               << ", references size = " << aSize << std::endl;
587 #endif
588     std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
589                                          aLast = aMainRefList.end();
590     std::set<FeaturePtr> aResultRefList;
591     aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
592     for (; anIt != aLast; anIt++) {
593       FeaturePtr aFeature = *anIt;
594       int aRecLevel = 0;
595 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
596       std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
597 #endif
598       aRecLevel++;
599       addRefsToFeature(aFeature, aMainList, theReferences, aRecLevel, aResultRefList/*aMainRefList*/);
600     }
601     theReferences[aMainListFeature] = aResultRefList;
602   }
603 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
604     std::cout << std::endl;
605 #endif
606
607 #ifdef DEBUG_REMOVE_FEATURES
608   printMapInfo(theReferences, "allDependencies");
609 #endif
610 }
611
612 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
613                         const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
614                         std::set<FeaturePtr>& theFeaturesRefsTo)
615 {
616   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
617                                        aLast = theFeatures.end();
618   for (; anIt != aLast; anIt++) {
619     FeaturePtr aFeature = *anIt;
620     if (theReferences.find(aFeature) == theReferences.end())
621       continue;
622     std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
623     std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
624     for (; aRefIt != aRefLast; aRefIt++) {
625       FeaturePtr aRefFeature = *aRefIt;
626       CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
627       if (aComposite.get() && aComposite->isSub(aFeature))
628         continue; /// composite of the current feature should not be removed
629
630       if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
631           theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
632         theFeaturesRefsTo.insert(aRefFeature);
633     }
634   }
635 }
636
637 } // namespace ModelAPI_Tools