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