]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Tools.cpp
Salome HOME
Merge branch 'master' into cgt/devCEA
[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   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
272   if (aBody.get()) {
273     FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
274     if (aFeatureOwner.get()) {
275       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
276         aFeatureOwner->results().cbegin();
277       for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
278         ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
279         if (aComp && aComp->isSub(aBody))
280           return aComp;
281       }
282     }
283   }
284   return ResultCompSolidPtr(); // not found
285 }
286
287 bool hasSubResults(const ResultPtr& theResult)
288 {
289   ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
290   return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
291 }
292
293 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
294 {
295   if (!theFeature.get()) // safety: for empty feature no results
296     return;
297   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
298   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
299   for (; aRIter != aResults.cend(); aRIter++) {
300     theResults.push_back(*aRIter);
301     // iterate sub-bodies of compsolid
302     ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
303     if (aComp.get()) {
304       int aNumSub = aComp->numberOfSubs();
305       for(int a = 0; a < aNumSub; a++) {
306         theResults.push_back(aComp->subResult(a));
307       }
308     }
309   }
310 }
311
312 //******************************************************************
313 bool allDocumentsActivated(std::string& theNotActivatedNames)
314 {
315   theNotActivatedNames = "";
316   bool anAllPartActivated = true;
317
318   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
319   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
320   for (int i = 0; i < aSize; i++) {
321     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
322     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
323     if (!aPart->isActivated()) {
324       anAllPartActivated = false;
325       if (!theNotActivatedNames.empty())
326         theNotActivatedNames += ", ";
327       theNotActivatedNames += aObject->data()->name().c_str();
328     }
329   }
330   return anAllPartActivated;
331 }
332
333 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
334                                  const bool theFlushRedisplay,
335                                  const bool theUseComposite,
336                                  const bool theUseRecursion)
337 {
338 #ifdef DEBUG_REMOVE_FEATURES
339   printListInfo(theFeatures, "selection: ");
340 #endif
341
342   std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
343   ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
344 #ifdef DEBUG_REMOVE_FEATURES
345   printMapInfo(aReferences, "allDependencies: ");
346 #endif
347
348   std::set<FeaturePtr> aFeaturesRefsTo;
349   ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
350 #ifdef DEBUG_REMOVE_FEATURES
351   printListInfo(aFeaturesRefsTo, "references: ");
352 #endif
353
354   std::set<FeaturePtr> aFeatures = theFeatures;
355   if (!aFeaturesRefsTo.empty())
356     aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
357 #ifdef DEBUG_REMOVE_FEATURES
358   printListInfo(aFeatures, "removeFeatures: ");
359 #endif
360
361   return ModelAPI_Tools::removeFeatures(aFeatures, false);
362 }
363
364 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
365                     const bool theFlushRedisplay)
366 {
367   bool isDone = false;
368   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
369                                        aLast = theFeatures.end();
370   for (; anIt != aLast; anIt++) {
371     FeaturePtr aFeature = *anIt;
372     if (aFeature.get()) {
373       DocumentPtr aDoc = aFeature->document();
374       // flush REDISPLAY signal after remove feature
375       aDoc->removeFeature(aFeature);
376       isDone = true;
377     }
378   }
379   if (isDone && theFlushRedisplay) {
380     // the redisplay signal should be flushed in order to erase
381     // the feature presentation in the viewer
382     // if should be done after removeFeature() of document
383     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
384   }
385   return true;
386 }
387
388 // Fills the references list by all references of the feature from the references map.
389 // This is a recusive method to find references by next found feature in the map of references.
390 // \param theFeature a feature to find references
391 // \param theReferencesMap a map of references
392 // \param theReferences an out container of references
393 void addRefsToFeature(const FeaturePtr& theFeature,
394                       const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
395                       std::map<FeaturePtr, std::set<FeaturePtr> >& theProcessedReferences,
396                       int theRecLevel,
397                       std::set<FeaturePtr>& theReferences)
398 {
399   if (theRecLevel > RECURSE_TOP_LEVEL)
400     return;
401   theRecLevel++;
402
403   // if the feature is already processed, get the ready references from the map
404   if (theProcessedReferences.find(theFeature) != theProcessedReferences.end()) {
405     std::set<FeaturePtr> aReferences = theProcessedReferences.at(theFeature);
406     theReferences.insert(aReferences.begin(), aReferences.end());
407     return;
408   }
409
410   if (theReferencesMap.find(theFeature) == theReferencesMap.end())
411     return; // this feature is not in the selection list, so exists without references to it
412   std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
413
414   std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
415                                        aLast = aMainReferences.end();
416 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
417   std::string aSpacing;
418   for (int i = 0; i < theRecLevel; i++)
419     aSpacing.append(" ");
420 #endif
421
422   for (; anIt != aLast; anIt++) {
423     FeaturePtr aRefFeature = *anIt;
424 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
425   std::cout << aSpacing << " Ref: " << getFeatureInfo(aRefFeature) << std::endl;
426 #endif
427     if (theReferences.find(aRefFeature) == theReferences.end())
428       theReferences.insert(aRefFeature);
429     addRefsToFeature(aRefFeature, theReferencesMap, theProcessedReferences,
430                      theRecLevel, theReferences);
431   }
432 }
433
434 // For each feature from the feature list it searches references to the feature and append them
435 // to the references map. This is a recusive method.
436 // \param theFeature a feature to find references
437 // \param theReferencesMap a map of references
438 // \param theReferences an out container of references
439 void findReferences(const std::set<FeaturePtr>& theFeatures,
440                     std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
441                     const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
442 {
443   if (theRecLevel > RECURSE_TOP_LEVEL)
444     return;
445   theRecLevel++;
446   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
447                                         aLast = theFeatures.end();
448   for (; anIt != aLast; anIt++) {
449     FeaturePtr aFeature = *anIt;
450     if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
451       DocumentPtr aSelFeatureDoc = aFeature->document();
452       std::set<FeaturePtr> aSelRefFeatures;
453       aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
454       if (theUseComposite) { // do not filter selection
455         theReferences[aFeature] = aSelRefFeatures;
456       }
457       else { // filter references to skip composition features of the current feature
458         std::set<FeaturePtr> aFilteredFeatures;
459         std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
460                                              aLast = aSelRefFeatures.end();
461         for (; anIt != aLast; anIt++) {
462           FeaturePtr aCFeature = *anIt;
463           CompositeFeaturePtr aComposite =
464             std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
465           if (aComposite.get() && aComposite->isSub(aFeature))
466             continue; /// composite of the current feature should be skipped
467           aFilteredFeatures.insert(aCFeature);
468         }
469         theReferences[aFeature] = aFilteredFeatures;
470       }
471       if (theUseRecursion) {
472 #ifdef DEBUG_CYCLING_1550
473         findReferences(aSelRefFeatures, theReferences, theUseComposite,
474                        theUseRecursion, theRecLevel);
475 #else
476         findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
477                        theRecLevel);
478 #endif
479       }
480     }
481   }
482 }
483
484 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
485                        std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
486                        const bool theUseComposite,
487                        const bool theUseRecursion)
488 {
489   // For dependencies, find main_list:
490   // sk_1(ext_1, vertex_1)
491   // ext_1(bool_1, sk_3)
492   // vertex_1()
493   // sk_2(ext_2)
494   // ext_2(bool_2)
495   // sk_3()
496   // Information: bool_1 is not selected, ext_2(bool_2) exists
497   // find all referenced features
498   std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
499   int aRecLevel = 0;
500   findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
501
502 #ifdef DEBUG_REMOVE_FEATURES
503   printMapInfo(aMainList, "firstDependencies");
504 #endif
505   // find all dependencies for each object:
506   // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
507   // ext_1(bool_1, sk_3)
508   // vertex_1()
509   // sk_2(ext_2) + (bool_1)
510   // ext_2(bool_1)
511   // sk_3()
512   std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
513                                                               aMainLast = aMainList.end();
514   for (; aMainIt != aMainLast; aMainIt++) {
515     FeaturePtr aMainListFeature = aMainIt->first;
516
517     //std::string aName = aMainListFeature->name();
518     std::set<FeaturePtr> aMainRefList = aMainIt->second;
519
520 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
521     char aBuf[50];
522     int n = sprintf(aBuf, "%d", aMainRefList.size());
523     std::string aSize(aBuf);
524     std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
525               << ", references size = " << aSize << std::endl;
526 #endif
527     std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
528                                          aLast = aMainRefList.end();
529     std::set<FeaturePtr> aResultRefList;
530     aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
531     for (; anIt != aLast; anIt++) {
532       FeaturePtr aFeature = *anIt;
533       int aRecLevel = 0;
534 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
535       std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
536 #endif
537       aRecLevel++;
538       addRefsToFeature(aFeature, aMainList, theReferences,
539                        aRecLevel, aResultRefList/*aMainRefList*/);
540     }
541     theReferences[aMainListFeature] = aResultRefList;
542   }
543 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
544     std::cout << std::endl;
545 #endif
546
547 #ifdef DEBUG_REMOVE_FEATURES
548   printMapInfo(theReferences, "allDependencies");
549 #endif
550 }
551
552 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
553                         const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
554                         std::set<FeaturePtr>& theFeaturesRefsTo)
555 {
556   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
557                                        aLast = theFeatures.end();
558   for (; anIt != aLast; anIt++) {
559     FeaturePtr aFeature = *anIt;
560     if (theReferences.find(aFeature) == theReferences.end())
561       continue;
562     std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
563     std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
564     for (; aRefIt != aRefLast; aRefIt++) {
565       FeaturePtr aRefFeature = *aRefIt;
566       CompositeFeaturePtr aComposite =
567         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
568       if (aComposite.get() && aComposite->isSub(aFeature))
569         continue; /// composite of the current feature should not be removed
570
571       if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
572           theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
573         theFeaturesRefsTo.insert(aRefFeature);
574     }
575   }
576 }
577
578 void getConcealedResults(const FeaturePtr& theFeature,
579                          std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
580 {
581   SessionPtr aSession = ModelAPI_Session::get();
582
583   std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
584   theFeature->data()->referencesToObjects(aRefs);
585   std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
586                                                   anIt = aRefs.begin(), aLast = aRefs.end();
587   std::set<ResultPtr> alreadyThere; // to avoid duplications
588   for (; anIt != aLast; anIt++) {
589     if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
590       continue; // use only concealed attributes
591     std::list<ObjectPtr> anObjects = (*anIt).second;
592     std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
593     for (; anOIt != anOLast; anOIt++) {
594       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
595       if (aResult && aResult->isConcealed()) {
596         if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
597           alreadyThere.insert(aResult);
598         else continue;
599         theResults.push_back(aResult);
600       }
601     }
602   }
603 }
604
605 } // namespace ModelAPI_Tools