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