From 71cba2c17885bef016f7a4cb26cad9ae935d9d2b Mon Sep 17 00:00:00 2001 From: azv Date: Fri, 12 Aug 2016 15:05:44 +0300 Subject: [PATCH] Dump Python in the High Level Parameterized Geometry API (issue #1648) * Dumping nested sketches no need "model.do()" command --- src/ModelHighAPI/ModelHighAPI_Dumper.cpp | 99 ++++++++++++++++-------- src/ModelHighAPI/ModelHighAPI_Dumper.h | 7 +- src/SketchAPI/SketchAPI_Sketch.cpp | 4 +- 3 files changed, 73 insertions(+), 37 deletions(-) diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp index 5bd5df4a5..f5c859367 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp @@ -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& theD std::list aFeatures = theDoc->allFeatures(); std::list::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(*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((*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(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& theComposite) +bool ModelHighAPI_Dumper::process(const std::shared_ptr& 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(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(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& 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(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::const_iterator anIt = aNotDumped.begin(); for (; anIt != aNotDumped.end(); ++anIt) { - FeaturePtr aFeature = std::dynamic_pointer_cast(*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(aFeature); + std::dynamic_pointer_cast(*anIt); if (aCompFeat) - theDumper.process(aCompFeat); + theDumper.process(aCompFeat, true); + else { + FeaturePtr aFeature = std::dynamic_pointer_cast(*anIt); + theDumper.dumpFeature(aFeature, true); + } } // avoid multiple empty lines diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.h b/src/ModelHighAPI/ModelHighAPI_Dumper.h index 880de6c8b..481529b32 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.h +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.h @@ -203,9 +203,14 @@ private: /// Iterate all features in document and dump them into intermediate buffer bool process(const std::shared_ptr& theDoc); + /// Dump composite feature and all it sub-features + bool process(const std::shared_ptr& 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& theComposite); + bool processSubs(const std::shared_ptr& theComposite, bool theDumpModelDo = false); /// Check the entity is already dumped bool isDumped(const EntityPtr& theEntity) const; diff --git a/src/SketchAPI/SketchAPI_Sketch.cpp b/src/SketchAPI/SketchAPI_Sketch.cpp index d7e719450..137671cde 100644 --- a/src/SketchAPI/SketchAPI_Sketch.cpp +++ b/src/SketchAPI/SketchAPI_Sketch.cpp @@ -660,7 +660,5 @@ void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const // dump sketch's subfeatures CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast(aBase); - theDumper.process(aCompFeat); - // necessary to be sure that the result of sketch was built - theDumper << "model.do()" << std::endl; + theDumper.processSubs(aCompFeat, true); } -- 2.39.2