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