Salome HOME
Adjust unit tests for Boolean Fill
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Dumper.cpp
index 90b6655b7a03d76d15fc6873ca82e47b6ee17bfd..20e8ff20f36c221f489705c00c7b8ca20b1eb388 100644 (file)
@@ -96,6 +96,9 @@ void ModelHighAPI_Dumper::clear(bool bufferOnly)
     myFeatureCount.clear();
     while (!myEntitiesStack.empty())
       myEntitiesStack.pop();
+
+    myPostponed.clear();
+    myDumpPostponedInProgress = false;
   }
 }
 
@@ -139,7 +142,6 @@ const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
       aName = aFolder->data()->name();
       aKind = ModelAPI_Folder::ID();
       isSaveNotDumped = false;
-      myNotDumpedFolders.insert(aFolder);
     }
   }
 
@@ -212,7 +214,8 @@ void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
   const std::list<ResultPtr>& aResults = theFeature->results();
   std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
   for (int i = 0; aResIt != aResults.end(); ++aResIt, ++i) {
-    std::string aDefaultName = ModelAPI_Tools::getDefaultName(*aResIt, i);
+    std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aResIt, i);
+    std::string aDefaultName = aName.first;
     std::string aResName = (*aResIt)->data()->name();
 
     bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
@@ -227,7 +230,8 @@ void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
       for (int j = 0; j < aNbSubs; ++j) {
         ResultPtr aSub = aCompSolid->subResult(j);
         std::string aSubName = aSub->data()->name();
-        aDefaultName = ModelAPI_Tools::getDefaultName(aSub, j);
+        aName = ModelAPI_Tools::getDefaultName(aSub, j);
+        aDefaultName = aName.first;
 
         bool isUserDefinedSubName = isUserDefined || aDefaultName != aSubName;
         myNames[aSub] = EntityName(aSubName,
@@ -265,7 +269,7 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theD
     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
     if (aCompFeat) // iteratively process composite features
       isOk = process(aCompFeat) && isOk;
-    else if (!isDumped(*anObjIt)) {
+    else if (!isDumped(EntityPtr(*anObjIt))) {
       // dump folder
       FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
       if (aFolder)
@@ -277,6 +281,8 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theD
       }
     }
   }
+  // dump folders if any
+  dumpPostponed(true);
   return isOk;
 }
 
@@ -286,7 +292,7 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeatur
   // increase composite features stack
   ++gCompositeStackDepth;
   // dump composite itself
-  if (!isDumped(theComposite) || isForce)
+  if (!isDumped(EntityPtr(theComposite)) || isForce)
     dumpFeature(FeaturePtr(theComposite), isForce);
 
   // sub-part is processed independently, because it provides separate document
@@ -323,6 +329,8 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeatur
   // decrease composite features stack
   --gCompositeStackDepth;
 
+  // dump folders if any
+  dumpPostponed(true);
   return isOk;
 }
 
@@ -336,7 +344,7 @@ bool ModelHighAPI_Dumper::processSubs(
   int aNbSubs = theComposite->numberOfSubs();
   for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
     FeaturePtr aFeature = theComposite->subFeature(anIndex);
-    if (isDumped(aFeature))
+    if (isDumped(EntityPtr(aFeature)))
       continue;
 
     isSubDumped = true;
@@ -361,9 +369,48 @@ bool ModelHighAPI_Dumper::processSubs(
   // dump "setName" for composite feature
   if (isDumpSetName)
     dumpEntitySetName();
+  // dump folders if any
+  dumpPostponed(true);
   return isOk;
 }
 
+void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
+{
+  // keep the name
+  name(theEntity, false);
+  myPostponed.push_back(theEntity);
+}
+
+void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
+{
+  if (myDumpPostponedInProgress)
+    return;
+
+  myDumpPostponedInProgress = true;
+  // make a copy of postponed entities, because the list will be updated
+  // if some features are not able to be dumped
+  std::list<EntityPtr> aPostponedCopy = myPostponed;
+  myPostponed.clear();
+
+  // iterate over postponed entities and try to dump them
+  std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
+  for (; anIt != aPostponedCopy.end(); ++anIt) {
+    FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
+    if (aFolder) {
+      if (theDumpFolders)
+        dumpFolder(aFolder);
+      else
+        myPostponed.push_back(*anIt);
+    }
+    else {
+      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
+      if (aFeature)
+        dumpFeature(aFeature);
+    }
+  }
+  myDumpPostponedInProgress = false;
+}
+
 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
                                                      const FeaturePtr& theSubFeature)
 {
@@ -498,7 +545,30 @@ bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
 {
   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
-  return aFound != myNames.end() || myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
+  return (aFound != myNames.end() && aFound->second.myIsDumped) ||
+         myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
+}
+
+bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
+{
+  FeaturePtr aFeature;
+  if (theRefAttr->isObject())
+    aFeature = ModelAPI_Feature::feature(theRefAttr->object());
+  else
+    aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
+  return aFeature && isDumped(EntityPtr(aFeature));
+}
+
+bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
+{
+  std::list<ObjectPtr> aRefs = theRefList->list();
+  std::list<ObjectPtr>::iterator anIt = aRefs.begin();
+  for (; anIt != aRefs.end(); ++anIt) {
+    FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
+    if (aFeature && !isDumped(EntityPtr(aFeature)))
+      return false;
+  }
+  return true;
 }
 
 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
@@ -688,7 +758,6 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
 {
   myDumpBuffer << name(theFolder);
-  myNotDumpedFolders.erase(theFolder);
   return *this;
 }
 
@@ -978,7 +1047,7 @@ ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
           isCopy = aCopyAttr.get() && aCopyAttr->value();
         }
       }
-    } while (isCopy);
+    } while (isCopy && !theDumper.myEntitiesStack.empty());
   }
 
   // store all not-dumped entities first
@@ -995,18 +1064,19 @@ ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
     else {
       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
       theDumper.dumpFeature(aFeature, true);
-      // dump all referred features for the "Copy"
+      // dump the Projection feature which produces this "Copy" entity
       AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
       if (aCopyAttr.get() && aCopyAttr->value())
       {
         const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
         std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
         for (; aRefIt != aRefs.end(); ++aRefIt)
-        {
-          FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
-          if (anOwner && !theDumper.isDumped(anOwner))
-            theDumper.dumpFeature(anOwner, true);
-        }
+          if ((*aRefIt)->id() == "ProjectedFeature")
+          { // process projection only
+            FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
+            if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
+              theDumper.dumpFeature(anOwner, true);
+          }
       }
     }
   }
@@ -1018,12 +1088,8 @@ ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
   // then store currently dumped string
   theDumper.myFullDump << aBufCopy;
 
-  // now, store all not dumped folders
-  std::set<FolderPtr>::const_iterator aFolderIt = theDumper.myNotDumpedFolders.begin();
-  while (aFolderIt != theDumper.myNotDumpedFolders.end()) {
-    FolderPtr aFolder = *aFolderIt++;
-    theDumper.dumpFolder(aFolder);
-  }
+  // now, store all postponed features
+  theDumper.dumpPostponed();
 
   return theDumper;
 }