]> 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>
Fri, 12 Aug 2016 12:05:44 +0000 (15:05 +0300)
committerazv <azv@opencascade.com>
Fri, 12 Aug 2016 12:05:44 +0000 (15:05 +0300)
* Dumping nested sketches no need "model.do()" command

src/ModelHighAPI/ModelHighAPI_Dumper.cpp
src/ModelHighAPI/ModelHighAPI_Dumper.h
src/SketchAPI/SketchAPI_Sketch.cpp

index 5bd5df4a5113d24f6717c2c500c2923ee2408f61..f5c8593679225e8b853ab6a9896091e5b641c588 100644 (file)
@@ -39,6 +39,8 @@
 
 #define DUMP_USER_DEFINED_NAMES
 
+static int gCompositeStackDepth = 0;
+
 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
 
 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
@@ -152,47 +154,77 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theD
   std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
   std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
   for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
-    if (!isDumped(*aFeatIt))
-      dumpFeature(*aFeatIt);
-
-    // iteratively process composite features
     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
-    if (!aCompFeat)
-      continue;
-
-    // sub-part is processed independently, because it provides separate document
-    if ((*aFeatIt)->getKind() == PartSetPlugin_Part::ID()) {
-      ResultPartPtr aPartResult =
-          std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->lastResult());
-      if (!aPartResult)
-        continue;
-      DocumentPtr aSubDoc = aPartResult->partDoc();
-      // set name of document
-      const std::string& aPartName = myNames[*aFeatIt].first;
-      std::string aDocName = aPartName + "_doc";
-      myNames[aSubDoc] = std::pair<std::string, bool>(aDocName, false);
-
-      // dump document in a single line
-      *this << aDocName << " = " << aPartName << ".document()" << std::endl;
-
-      isOk = process(aSubDoc) && isOk;
-    } else
+    if (aCompFeat) // iteratively process composite features
       isOk = process(aCompFeat) && isOk;
+    else if (!isDumped(*aFeatIt)) // dump common feature 
+      dumpFeature(*aFeatIt);
   }
   return isOk;
 }
 
-bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite)
+bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce)
+{
+  // increase composite features stack
+  ++gCompositeStackDepth;
+  // dump composite itself
+  if (!isDumped(theComposite) || isForce)
+    dumpFeature(theComposite, isForce);
+
+  // sub-part is processed independently, because it provides separate document
+  if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
+    // decrease composite features stack because we run into separate document
+    --gCompositeStackDepth;
+
+    ResultPartPtr aPartResult =
+        std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
+    if (!aPartResult)
+      return false;
+    DocumentPtr aSubDoc = aPartResult->partDoc();
+    // set name of document
+    const std::string& aPartName = myNames[theComposite].first;
+    std::string aDocName = aPartName + "_doc";
+    myNames[aSubDoc] = std::pair<std::string, bool>(aDocName, false);
+
+    // dump document in a separate line
+    *this << aDocName << " = " << aPartName << ".document()" << std::endl;
+    // dump features in the document
+    return process(aSubDoc);
+  }
+
+  // dump sub-features
+  bool isOk = processSubs(theComposite);
+  // decrease composite features stack
+  --gCompositeStackDepth;
+
+  return isOk;
+}
+
+bool ModelHighAPI_Dumper::processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
+                                      bool theDumpModelDo)
 {
+  bool isOk = true;
   // dump all sub-features;
   int aNbSubs = theComposite->numberOfSubs();
   for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
     FeaturePtr aFeature = theComposite->subFeature(anIndex);
     if (isDumped(aFeature))
       continue;
-    dumpFeature(aFeature, true);
+
+    CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
+    if (aCompFeat) // iteratively process composite features
+      isOk = process(aCompFeat) && isOk;
+    else
+      dumpFeature(aFeature, true);
   }
-  return true;
+
+  // 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
+  // explicit call of "model.do()". This will be controlled by the depth of the stack.
+  if (theDumpModelDo && gCompositeStackDepth <= 1)
+    *this << "model.do()" << std::endl;
+  return isOk;
 }
 
 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
@@ -563,14 +595,15 @@ ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
   theDumper.clear(true);
   std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
   for (; anIt != aNotDumped.end(); ++anIt) {
-    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
-    theDumper.dumpFeature(aFeature, true);
-
-    // if the feature is composite, dump all its subs
+    // if the feature is composite, dump it with all subs
     CompositeFeaturePtr aCompFeat =
-        std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
+        std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
     if (aCompFeat)
-      theDumper.process(aCompFeat);
+      theDumper.process(aCompFeat, true);
+    else {
+      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
+      theDumper.dumpFeature(aFeature, true);
+    }
   }
 
   // avoid multiple empty lines
index 880de6c8b6d9ef62182ea3501d9a1c48678cbbe9..481529b32255b89e3c797d049d407bfb6442e0ae 100644 (file)
@@ -203,9 +203,14 @@ private:
   /// Iterate all features in document and dump them into intermediate buffer
   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
 
+  /// Dump composite feature and all it sub-features
+  bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce = false);
+
   /// Iterate all features in composite feature and dump them into intermediate buffer
+  /// \param theComposite   [in] parent composite feature
+  /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
   MODELHIGHAPI_EXPORT
-  bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite);
+  bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool theDumpModelDo = false);
 
   /// Check the entity is already dumped
   bool isDumped(const EntityPtr& theEntity) const;
index d7e7194508870fb555d775b4b14f2bc15135ef21..137671cde4b89249bfedb563276c0957fe396b9c 100644 (file)
@@ -660,7 +660,5 @@ void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
 
   // dump sketch's subfeatures
   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
-  theDumper.process(aCompFeat);
-  // necessary to be sure that the result of sketch was built
-  theDumper << "model.do()" << std::endl;
+  theDumper.processSubs(aCompFeat, true);
 }