Salome HOME
Task 2.1. Management of result names
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Dumper.cpp
index f3b96003e9e1a4a950719095161f755e36260f2b..84f4eb55653604cf50e7140e754ac4151dbb8789 100644 (file)
@@ -1,10 +1,23 @@
-// Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
-// File:        ModelHighAPI_Dumper.cpp
-// Created:     1 August 2016
-// Author:      Artem ZHIDKOV
-
-//--------------------------------------------------------------------------------------
 #include "ModelHighAPI_Dumper.h"
 
 #include <Config_PropManager.h>
@@ -38,6 +51,7 @@
 #include <ModelAPI_ResultCompSolid.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_ResultPart.h>
+#include <ModelAPI_Tools.h>
 
 #include <PartSetPlugin_Part.h>
 
@@ -178,29 +192,16 @@ const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
 {
   // Default name of the feature
-  const std::string& aKind = theFeature->getKind();
-  DocumentPtr aDoc = theFeature->document();
-  int aNbFeatures = myFeatureCount[aDoc][aKind];
-  std::ostringstream aNameStream;
-  aNameStream << aKind << "_" << aNbFeatures;
-  std::string aFeatureName = aNameStream.str();
+  bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
 
   // Save only names of results which is not correspond to default feature name
   const std::list<ResultPtr>& aResults = theFeature->results();
   std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
-  for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) {
-    bool isUserDefined = true;
+  for (int i = 0; aResIt != aResults.end(); ++aResIt, ++i) {
+    std::string aDefaultName = ModelAPI_Tools::getDefaultName(*aResIt, i);
     std::string aResName = (*aResIt)->data()->name();
-    size_t anIndex = aResName.find(aFeatureName);
-    if (anIndex == 0) {
-      std::string aSuffix = aResName.substr(aFeatureName.length());
-      if (aSuffix.empty() && i == 1) // first result may not constain index in the name
-        isUserDefined = false;
-      else {
-        if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i)
-          isUserDefined = false;
-      }
-    }
+
+    bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
 
     myNames[*aResIt] = EntityName(aResName,
         (isUserDefined ? aResName : std::string()), !isUserDefined);
@@ -210,22 +211,13 @@ void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
     if (aCompSolid) {
       int aNbSubs = aCompSolid->numberOfSubs();
       for (int j = 0; j < aNbSubs; ++j) {
-        isUserDefined = true;
         ResultPtr aSub = aCompSolid->subResult(j);
         std::string aSubName = aSub->data()->name();
-        size_t anIndex = aSubName.find(aResName);
-        if (anIndex == 0) {
-          std::string aSuffix = aSubName.substr(aResName.length());
-          if (aSuffix.empty() && aNbSubs == 1) // first result may not constain index in the name
-            isUserDefined = false;
-          else {
-            if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == j + 1)
-              isUserDefined = false;
-          }
-        }
+        aDefaultName = ModelAPI_Tools::getDefaultName(aSub, j);
 
+        bool isUserDefinedSubName = isUserDefined || aDefaultName != aSubName;
         myNames[aSub] = EntityName(aSubName,
-            (isUserDefined ? aSubName : std::string()), !isUserDefined);
+            (isUserDefinedSubName ? aSubName : std::string()), !isUserDefinedSubName);
       }
     }
   }
@@ -295,7 +287,9 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeatur
     // dump document in a separate line
     *this << aDocName << " = " << aPartName << ".document()" << std::endl;
     // dump features in the document
-    return process(aSubDoc);
+    bool aRes = process(aSubDoc);
+    *this << "model.do()" << std::endl;
+    return aRes;
   }
 
   // dump sub-features
@@ -459,6 +453,15 @@ void ModelHighAPI_Dumper::dumpEntitySetName()
         myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
       }
     }
+    // set result transparency
+    if (!isDefaultTransparency(*aResIt)) {
+      AttributeDoublePtr aTransparencyAttr =
+        (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
+      if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
+        *this << *aResIt;
+        myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
+      }
+    }
   }
 
   myNames[aLastDumped.myEntity].myIsDumped = true;
@@ -468,7 +471,8 @@ void ModelHighAPI_Dumper::dumpEntitySetName()
 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
 {
   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
-  return aFound != myNames.end();
+  FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
+  return aFound != myNames.end() || myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
 }
 
 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
@@ -511,15 +515,22 @@ bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
     }
   }
   if (isConstruction)
-    aDefault = Config_PropManager::real("Visualization", "construction_deflection",
-                                        ModelAPI_ResultConstruction::DEFAULT_DEFLECTION());
+    aDefault = Config_PropManager::real("Visualization", "construction_deflection");
   else
-    aDefault = Config_PropManager::real("Visualization", "body_deflection",
-                                        ModelAPI_ResultBody::DEFAULT_DEFLECTION());
+    aDefault = Config_PropManager::real("Visualization", "body_deflection");
 
   return fabs(aCurrent - aDefault) < 1.e-12;
 }
 
+bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
+{
+  AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
+  if(!anAttribute || !anAttribute->isInitialized()) {
+    return true;
+  }
+  return fabs(anAttribute->value()) < 1.e-12;
+}
+
 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
 {
   myDumpBuffer << theChar;
@@ -660,7 +671,7 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity
     std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
     for (; aResIt != aResults.end(); ++aResIt) {
       if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
-          !isDefaultDeflection(*aResIt))
+          !isDefaultDeflection(*aResIt) || !isDefaultTransparency(*aResIt))
         aResultsWithNameOrColor.push_back(*aResIt);
 
       ResultCompSolidPtr aCompSolid =
@@ -670,7 +681,7 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity
         for (int i = 0; i < aNbSubs; ++i) {
           ResultPtr aCurRes = aCompSolid->subResult(i);
           if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
-              !isDefaultDeflection(aCurRes))
+              !isDefaultDeflection(aCurRes) || !isDefaultTransparency(aCurRes))
             aResultsWithNameOrColor.push_back(aCurRes);
         }
       }
@@ -873,7 +884,9 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
     AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
     aShape = anAttribute->value();
     if(!aShape.get()) {
-      aShape = anAttribute->context()->shape();
+      ResultPtr aContext = anAttribute->context();
+      if (aContext.get())
+        aShape = aContext->shape();
     }
 
     if(!aShape.get()) {
@@ -949,6 +962,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"
+      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);
+        }
+      }
     }
   }