From a2450332dcaeea8ab91558a430cccb186cb8f262 Mon Sep 17 00:00:00 2001 From: azv Date: Fri, 30 Dec 2016 16:57:41 +0300 Subject: [PATCH] Python Dump: implement dumping names of CompSolid parts (issue #1930) --- src/ModelHighAPI/ModelHighAPI_Dumper.cpp | 64 +++++++++++++++++++-- src/ModelHighAPI/ModelHighAPI_Dumper.h | 3 +- src/ModelHighAPI/ModelHighAPI_Selection.cpp | 15 +++++ src/ModelHighAPI/ModelHighAPI_Selection.h | 4 ++ 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp index 819d200c8..c32d2ed7f 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -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(*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 aResultsWithNameOrColor; const std::list& aResults = theEntity->results(); std::list::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(*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 aResults = aFeature->results(); for(std::list::const_iterator anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) { if(theResult->isSame(*anIt)) { break; } + + ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast(*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; } diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.h b/src/ModelHighAPI/ModelHighAPI_Dumper.h index ee432d271..362d9b534 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.h +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.h @@ -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) {} }; diff --git a/src/ModelHighAPI/ModelHighAPI_Selection.cpp b/src/ModelHighAPI/ModelHighAPI_Selection.cpp index fb0697635..e63c40ec8 100644 --- a/src/ModelHighAPI/ModelHighAPI_Selection.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Selection.cpp @@ -12,6 +12,7 @@ #include #include #include +#include //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- @@ -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(myResultSubShapePair.first); + if (!aCompSolid) + return ModelHighAPI_Selection(); + + ResultBodyPtr aResult = aCompSolid->subResult(theIndex); + return ModelHighAPI_Selection(aResult, aResult->shape()); +} diff --git a/src/ModelHighAPI/ModelHighAPI_Selection.h b/src/ModelHighAPI/ModelHighAPI_Selection.h index db78cb69e..7d8986983 100644 --- a/src/ModelHighAPI/ModelHighAPI_Selection.h +++ b/src/ModelHighAPI/ModelHighAPI_Selection.h @@ -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; -- 2.39.2