]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
authorazv <azv@opencascade.com>
Mon, 22 Aug 2016 08:41:20 +0000 (11:41 +0300)
committerazv <azv@opencascade.com>
Mon, 22 Aug 2016 08:41:20 +0000 (11:41 +0300)
Dump user-defined colors of results

src/ModelHighAPI/ModelHighAPI_Dumper.cpp
src/ModelHighAPI/ModelHighAPI_Dumper.h
src/ModelHighAPI/ModelHighAPI_Selection.cpp
src/ModelHighAPI/ModelHighAPI_Selection.h

index 0c28e02ec16fe9fce6f3b8c1a810cb82a978b89d..52b9fab366d46cf51972c15c6706d6db34cdc688 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <ModelAPI_AttributeBoolean.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeIntArray.h>
 #include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeRefAttrList.h>
@@ -267,7 +268,7 @@ bool ModelHighAPI_Dumper::processSubs(const std::shared_ptr<ModelAPI_CompositeFe
   bool isDumpSetName = !myEntitiesStack.empty() &&
       myEntitiesStack.top().myEntity == EntityPtr(theComposite);
   bool isForceModelDo = isDumpSetName &&
-      (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResultsWithName.empty());
+      (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
   // It is necessary for the sketch to create its result when complete (command "model.do()").
   // This option is set by flat theDumpModelDo.
   // However, nested sketches are rebuilt by parent feature, so, they do not need
@@ -341,15 +342,25 @@ void ModelHighAPI_Dumper::dumpEntitySetName()
     anEntityNames.second.clear(); // don't dump "setName" for the entity twice
   }
   // dump "setName" for results
-  std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResultsWithName.begin();
-  std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResultsWithName.end();
+  std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
+  std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
   for (; aResIt != aResEnd; ++aResIt) {
+    // set result name
     std::pair<std::string, std::string> 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<ResultPtr> aResultsWithUserName;
+  bool isUserDefinedName = !myNames[theEntity].second.empty();
+  // 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)
-    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);
index ed2834f4684f317e6ce8fb87eb11b8ec469d05ef..181c3800ef5557c492cb95fcfa9c7c52b0ffe59f 100644 (file)
@@ -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<EntityPtr, std::pair<std::string, std::string> > EntityNameMap;
   typedef std::map<std::string, std::set<std::string> >             ModulesMap;
   typedef std::map<DocumentPtr, std::map<std::string, int> >        NbFeaturesMap;
 
   struct LastDumpedEntity {
-    EntityPtr            myEntity; // last dumped entity
-    bool                 myUserName; // the entity hase user-defined name
-    std::list<ResultPtr> 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<ResultPtr> myResults;  ///< results of this entity, which has user-defined names or specific colors
 
     LastDumpedEntity(EntityPtr theEntity, bool theUserName, const std::list<ResultPtr>& theResults)
-      : myEntity(theEntity), myUserName(theUserName), myResultsWithName(theResults)
+      : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
     {}
   };
   typedef std::stack<LastDumpedEntity>                              DumpStack;
index 265d0635ffca2e69aa27b9fa92fb468bbd061a2f..536e22710957bd1e3740e992a7434f96a7dcf0be 100644 (file)
@@ -7,6 +7,7 @@
 //--------------------------------------------------------------------------------------
 #include "ModelHighAPI_Selection.h"
 
+#include <ModelAPI_AttributeIntArray.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeSelectionList.h>
 //--------------------------------------------------------------------------------------
@@ -92,9 +93,21 @@ std::string ModelHighAPI_Selection::shapeType() const
 }
 
 //==================================================================================================
-std::shared_ptr<ModelAPI_Result> 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);
 }
index fcea8f5a5ea80863de017130dad00858e78f0841..b138b8f009962002f990485681593e80e997f573 100644 (file)
@@ -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<ModelAPI_Result> 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;