From f0cec241aae9ca16d86e166f45cb5c4987d2c792 Mon Sep 17 00:00:00 2001 From: azv Date: Mon, 22 Aug 2016 11:41:20 +0300 Subject: [PATCH] Dump Python in the High Level Parameterized Geometry API (issue #1648) Dump user-defined colors of results --- src/ModelHighAPI/ModelHighAPI_Dumper.cpp | 47 ++++++++++++++++----- src/ModelHighAPI/ModelHighAPI_Dumper.h | 11 +++-- src/ModelHighAPI/ModelHighAPI_Selection.cpp | 19 +++++++-- src/ModelHighAPI/ModelHighAPI_Selection.h | 8 +++- 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp index 0c28e02ec..52b9fab36 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -267,7 +268,7 @@ bool ModelHighAPI_Dumper::processSubs(const std::shared_ptr::const_iterator aResIt = aLastDumped.myResultsWithName.begin(); - std::list::const_iterator aResEnd = aLastDumped.myResultsWithName.end(); + std::list::const_iterator aResIt = aLastDumped.myResults.begin(); + std::list::const_iterator aResEnd = aLastDumped.myResults.end(); for (; aResIt != aResEnd; ++aResIt) { + // set result name std::pair anEntityNames = myNames[*aResIt]; if (!anEntityNames.second.empty()) { *this << *aResIt; - myDumpBuffer << ".result().data().setName(\"" << anEntityNames.second << "\")" << std::endl; + myDumpBuffer << ".setName(\"" << anEntityNames.second << "\")" << std::endl; anEntityNames.second.clear(); // don't dump "setName" for the entity twice } + // set result color + if (!isDefaultColor(*aResIt)) { + AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID()); + if (aColor && aColor->isInitialized()) { + *this << *aResIt; + myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1) + << ", " << aColor->value(2) << ")" << std::endl; + } + } } myEntitiesStack.pop(); @@ -361,6 +372,22 @@ bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const return aFound != myNames.end(); } +bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const +{ + AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID()); + if (!aColor || !aColor->isInitialized()) + return true; + + std::string aSection, aName, aDefault; + theResult->colorConfigInfo(aSection, aName, aDefault); + + // dump current color + std::ostringstream aColorInfo; + aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2); + + return aDefault == aColorInfo.str(); +} + ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar) { myDumpBuffer << theChar; @@ -493,16 +520,16 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity { myDumpBuffer << name(theEntity); - bool isUserDefindName = !myNames[theEntity].second.empty(); - // store results if they have user-defined names - std::list aResultsWithUserName; + bool isUserDefinedName = !myNames[theEntity].second.empty(); + // 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) - if (!myNames[*aResIt].second.empty()) - aResultsWithUserName.push_back(*aResIt); + if (!myNames[*aResIt].second.empty() || !isDefaultColor(*aResIt)) + aResultsWithNameOrColor.push_back(*aResIt); // store just dumped entity to stack - myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefindName, aResultsWithUserName)); + myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor)); // remove entity from the list of not dumped items myNotDumpedEntities.erase(theEntity); diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.h b/src/ModelHighAPI/ModelHighAPI_Dumper.h index ed2834f46..181c3800e 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.h +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.h @@ -232,18 +232,21 @@ private: /// Stores names of results for the given feature void saveResultNames(const FeaturePtr& theFeature); + /// Check the result feature has default color + bool isDefaultColor(const ResultPtr& theResult) const; + private: typedef std::map > EntityNameMap; typedef std::map > ModulesMap; typedef std::map > NbFeaturesMap; struct LastDumpedEntity { - EntityPtr myEntity; // last dumped entity - bool myUserName; // the entity hase user-defined name - std::list myResultsWithName; // results of this entity, which has user-defined names + EntityPtr myEntity; ///< last dumped entity + bool myUserName; ///< the entity hase user-defined name + std::list myResults; ///< results of this entity, which has user-defined names or specific colors LastDumpedEntity(EntityPtr theEntity, bool theUserName, const std::list& theResults) - : myEntity(theEntity), myUserName(theUserName), myResultsWithName(theResults) + : myEntity(theEntity), myUserName(theUserName), myResults(theResults) {} }; typedef std::stack DumpStack; diff --git a/src/ModelHighAPI/ModelHighAPI_Selection.cpp b/src/ModelHighAPI/ModelHighAPI_Selection.cpp index 265d0635f..536e22710 100644 --- a/src/ModelHighAPI/ModelHighAPI_Selection.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Selection.cpp @@ -7,6 +7,7 @@ //-------------------------------------------------------------------------------------- #include "ModelHighAPI_Selection.h" +#include #include #include //-------------------------------------------------------------------------------------- @@ -92,9 +93,21 @@ std::string ModelHighAPI_Selection::shapeType() const } //================================================================================================== -std::shared_ptr ModelHighAPI_Selection::result() const +void ModelHighAPI_Selection::setName(const std::string& theName) { if (myVariantType == VT_ResultSubShapePair) - return myResultSubShapePair.first; - return ResultPtr(); + myResultSubShapePair.first->data()->setName(theName); +} + +void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue) +{ + if (myVariantType != VT_ResultSubShapePair) + return; + + AttributeIntArrayPtr aColor = + myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID()); + aColor->setSize(3); + aColor->setValue(0, theRed); + aColor->setValue(1, theGreen); + aColor->setValue(2, theBlue); } diff --git a/src/ModelHighAPI/ModelHighAPI_Selection.h b/src/ModelHighAPI/ModelHighAPI_Selection.h index fcea8f5a5..b138b8f00 100644 --- a/src/ModelHighAPI/ModelHighAPI_Selection.h +++ b/src/ModelHighAPI/ModelHighAPI_Selection.h @@ -76,9 +76,13 @@ public: MODELHIGHAPI_EXPORT virtual std::string shapeType() const; - /// \return Result if exists. + /// Shortcut for result()->data()->setName() MODELHIGHAPI_EXPORT - virtual std::shared_ptr result() const; + void setName(const std::string& theName); + + /// Change result's color + MODELHIGHAPI_EXPORT + void setColor(int theRed, int theGreen, int theBlue); private: VariantType myVariantType; -- 2.39.2