Salome HOME
Python Dump: implement dumping names of CompSolid parts (issue #1930)
authorazv <azv@opencascade.com>
Fri, 30 Dec 2016 13:57:41 +0000 (16:57 +0300)
committerazv <azv@opencascade.com>
Fri, 30 Dec 2016 13:58:20 +0000 (16:58 +0300)
src/ModelHighAPI/ModelHighAPI_Dumper.cpp
src/ModelHighAPI/ModelHighAPI_Dumper.h
src/ModelHighAPI/ModelHighAPI_Selection.cpp
src/ModelHighAPI/ModelHighAPI_Selection.h

index 819d200c8b986b249ef270c18325eec642c4cfde..c32d2ed7f9ed25204b690d5c5b86f683131d9bd2 100644 (file)
@@ -35,6 +35,7 @@
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Result.h>
 #include <ModelAPI_ResultBody.h>
+#include <ModelAPI_ResultCompSolid.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_ResultPart.h>
 
@@ -203,6 +204,30 @@ void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
 
     myNames[*aResIt] = EntityName(aResName,
         (isUserDefined ? aResName : std::string()), !isUserDefined);
+
+    // check names of sub-results for CompSolid
+    ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
+    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;
+          }
+        }
+
+        myNames[aSub] = EntityName(aSubName,
+            (isUserDefined ? aSubName : std::string()), !isUserDefined);
+      }
+    }
   }
 }
 
@@ -436,6 +461,7 @@ void ModelHighAPI_Dumper::dumpEntitySetName()
     }
   }
 
+  myNames[aLastDumped.myEntity].myIsDumped = true;
   myEntitiesStack.pop();
 }
 
@@ -624,22 +650,35 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
 
 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
 {
-  bool isFound = myNames.find(theEntity) != myNames.end();
   myDumpBuffer << name(theEntity);
 
-  if (!isFound) {
+  if (!myNames[theEntity].myIsDumped) {
     bool isUserDefinedName = !myNames[theEntity].myIsDefault;
     // store results if they have user-defined names or colors
     std::list<ResultPtr> aResultsWithNameOrColor;
     const std::list<ResultPtr>& aResults = theEntity->results();
     std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
-    for (; aResIt != aResults.end(); ++aResIt)
+    for (; aResIt != aResults.end(); ++aResIt) {
       if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
           !isDefaultDeflection(*aResIt))
         aResultsWithNameOrColor.push_back(*aResIt);
+
+      ResultCompSolidPtr aCompSolid =
+          std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
+      if (aCompSolid) {
+        int aNbSubs = aCompSolid->numberOfSubs();
+        for (int i = 0; i < aNbSubs; ++i) {
+          ResultPtr aCurRes = aCompSolid->subResult(i);
+          if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
+              !isDefaultDeflection(aCurRes))
+            aResultsWithNameOrColor.push_back(aCurRes);
+        }
+      }
+    }
     // store just dumped entity to stack
-    myEntitiesStack.push(
-        LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
+    if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
+      myEntitiesStack.push(
+          LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
   }
 
   // remove entity from the list of not dumped items
@@ -651,12 +690,24 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
 {
   FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
   int anIndex = 0;
+  int aSubIndex = -1;
   std::list<ResultPtr> aResults = aFeature->results();
   for(std::list<ResultPtr>::const_iterator
       anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
     if(theResult->isSame(*anIt)) {
       break;
     }
+
+    ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
+    if (aCompSolid) {
+      int aNbSubs = aCompSolid->numberOfSubs();
+      for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
+        if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
+          break;
+      if (aSubIndex < aNbSubs)
+        break;
+      aSubIndex = -1;
+    }
   }
 
   myDumpBuffer << name(aFeature);
@@ -665,6 +716,9 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
   } else {
     myDumpBuffer << ".results()[" << anIndex << "]";
   }
+  if (aSubIndex >= 0) {
+    myDumpBuffer << ".subResult(" << aSubIndex << ")";
+  }
   return *this;
 }
 
index ee432d271dad58dee3cf395d979d814d9324e456..362d9b534afcfa83078d01892a5d17f478552ab0 100644 (file)
@@ -264,11 +264,12 @@ private:
     std::string myCurrentName; ///< default name of current feature
     std::string myUserName;    ///< user-defined name
     bool        myIsDefault;   ///< \c true if the name is default
+    bool        myIsDumped;    ///< shows that the names of the feature are already stored
 
     EntityName() {}
 
     EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
-      : myCurrentName(theCurName), myUserName(theUserName), myIsDefault(theDefault)
+      : myCurrentName(theCurName), myUserName(theUserName), myIsDefault(theDefault), myIsDumped(false)
     {}
   };
 
index fb0697635dd2b855552aa3ebd974c3a54e6a6524..e63c40ec8b3b1c5d1577dc4045d1d2c67286983a 100644 (file)
@@ -12,6 +12,7 @@
 #include <ModelAPI_AttributeIntArray.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_ResultCompSolid.h>
 //--------------------------------------------------------------------------------------
 
 //--------------------------------------------------------------------------------------
@@ -130,3 +131,17 @@ void ModelHighAPI_Selection::setDeflection(double theValue)
 
   aDeflectionAttr->setValue(theValue);
 }
+
+ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex)
+{
+  if (myVariantType != VT_ResultSubShapePair)
+    return ModelHighAPI_Selection();
+
+  ResultCompSolidPtr aCompSolid =
+      std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
+  if (!aCompSolid)
+    return ModelHighAPI_Selection();
+
+  ResultBodyPtr aResult = aCompSolid->subResult(theIndex);
+  return ModelHighAPI_Selection(aResult, aResult->shape());
+}
index db78cb69ee7c3911d1b2ca6cd3bc0e19a7397c46..7d898698397a0e4b75e0cf2fbe11ebe585867873 100644 (file)
@@ -91,6 +91,10 @@ public:
   MODELHIGHAPI_EXPORT
   void setDeflection(double theValue);
 
+  /// Return sub-result for ResultCompSolid
+  MODELHIGHAPI_EXPORT
+  ModelHighAPI_Selection subResult(int theIndex);
+
 private:
   VariantType myVariantType;
   ResultSubShapePair myResultSubShapePair;