From bd1bcf821858607eabc12f6f191f9b216d325a25 Mon Sep 17 00:00:00 2001 From: azv Date: Tue, 9 Aug 2016 14:26:03 +0300 Subject: [PATCH] Dump Python in the High Level Parameterized Geometry API (issue #1648) Workaround to create sketch entities, used in constraint, before the constraint itself. --- src/ExchangePlugin/ExchangePlugin_Dump.cpp | 1 + src/ModelHighAPI/ModelHighAPI_Dumper.cpp | 54 ++++++++++++++++++---- src/ModelHighAPI/ModelHighAPI_Dumper.h | 17 +++++-- src/PythonAPI/model/dump/DumpAssistant.py | 1 + 4 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/ExchangePlugin/ExchangePlugin_Dump.cpp b/src/ExchangePlugin/ExchangePlugin_Dump.cpp index d89f6d98a..168e9dc19 100644 --- a/src/ExchangePlugin/ExchangePlugin_Dump.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Dump.cpp @@ -46,6 +46,7 @@ void ExchangePlugin_Dump::dump(const std::string& theFileName) Config_ModuleReader::loadScript("model.dump"); ModelHighAPI_Dumper* aDumper = ModelHighAPI_Dumper::getInstance(); + aDumper->clear(); DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument(); if (!aDumper || !aDumper->process(aDoc, theFileName)) setError("An error occured while dumping to " + theFileName); diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp index e1ed55d36..ee7894f50 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp @@ -54,10 +54,26 @@ ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance() return mySelf; } -void ModelHighAPI_Dumper::clear() +void ModelHighAPI_Dumper::clear(bool bufferOnly) { myDumpBuffer = std::ostringstream(); myDumpBuffer << std::setprecision(16); + + clearNotDumped(); + + if (!bufferOnly) { + myFullDump = std::ostringstream(); + myFullDump << std::setprecision(16); + + myNames.clear(); + myModules.clear(); + myLastEntityWithName = EntityPtr(); + } +} + +void ModelHighAPI_Dumper::clearNotDumped() +{ + myNotDumpedEntities.clear(); } const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity) @@ -85,6 +101,7 @@ const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity) } myNames[theEntity] = std::pair(aName, isNameDefined); + myNotDumpedEntities.insert(theEntity); return myNames[theEntity].first; } @@ -118,14 +135,11 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr& theD bool ModelHighAPI_Dumper::process(const std::shared_ptr& theDoc) { bool isOk = true; - CompositeFeaturePtr aLastComposite; // dump all features std::list aFeatures = theDoc->allFeatures(); std::list::const_iterator aFeatIt = aFeatures.begin(); for (; aFeatIt != aFeatures.end(); ++aFeatIt) { - // dump feature if and only if it is not a sub-feature of last composite feature - // (all subs of composite are dumped in special method) - if (!aLastComposite || !aLastComposite->isSub(*aFeatIt)) + if (!isDumped(*aFeatIt)) dumpFeature(*aFeatIt); // iteratively process composite features @@ -144,10 +158,8 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr& theD myNames[aSubDoc] = myNames[*aFeatIt]; isOk = process(aSubDoc) && isOk; - } else { + } else isOk = process(aCompFeat) && isOk; - aLastComposite = aCompFeat; - } } return isOk; } @@ -158,7 +170,8 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptrnumberOfSubs(); for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) { FeaturePtr aFeature = theComposite->subFeature(anIndex); - dumpFeature(aFeature, true); + if (!isDumped(aFeature)) + dumpFeature(aFeature, true); } // dump command to update model myDumpBuffer << "model.do()" << std::endl; @@ -195,7 +208,7 @@ bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName) aFile << "model.begin()" << std::endl; // dump collected data - aFile << myDumpBuffer.str(); + aFile << myFullDump.str(); // standard footer aFile << "model.end()" << std::endl; @@ -228,6 +241,12 @@ void ModelHighAPI_Dumper::dumpEntitySetName() myLastEntityWithName = EntityPtr(); } +bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const +{ + EntityNameMap::const_iterator aFound = myNames.find(theEntity); + return aFound != myNames.end(); +} + ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar) { myDumpBuffer << theChar; @@ -355,6 +374,7 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const EntityPtr& theEntity) myDumpBuffer << name(theEntity); if (myNames[theEntity].second) myLastEntityWithName = theEntity; + myNotDumpedEntities.erase(theEntity); return *this; } @@ -397,5 +417,19 @@ ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper, { theDumper.myDumpBuffer << theEndl; theDumper.dumpEntitySetName(); + + // store all not-dumped entities first + std::set aNotDumped = theDumper.myNotDumpedEntities; + std::string aBufCopy = theDumper.myDumpBuffer.str(); + 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); + } + + // then store currently dumped string + theDumper.myFullDump << aBufCopy; + return theDumper; } diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.h b/src/ModelHighAPI/ModelHighAPI_Dumper.h index c653e5067..200262fdb 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.h +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.h @@ -155,13 +155,16 @@ public: MODELHIGHAPI_EXPORT ModelHighAPI_Dumper& operator<<(const std::shared_ptr& theAttrSelect); + /// Clear dump buffer + MODELHIGHAPI_EXPORT + void clear(bool bufferOnly = false); + /// clear list of not dumped entities + MODELHIGHAPI_EXPORT void clearNotDumped(); + protected: /// Dump "setName" command if last entity had user-defined name MODELHIGHAPI_EXPORT void dumpEntitySetName(); - /// Clear dump buffer - void clear(); - private: ModelHighAPI_Dumper(const ModelHighAPI_Dumper&); const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&); @@ -172,6 +175,9 @@ private: /// Iterate all features in composite feature and dump them into intermediate buffer bool process(const std::shared_ptr& theComposite); + /// Check the entity is already dumped + bool isDumped(const EntityPtr& theEntity) const; + private: typedef std::map > EntityNameMap; typedef std::map > ModulesMap; @@ -179,9 +185,14 @@ private: static ModelHighAPI_Dumper* mySelf; std::ostringstream myDumpBuffer; ///< intermediate buffer to store dumping data + std::ostringstream myFullDump; ///< full buffer of dumped data + ModulesMap myModules; ///< modules and entities to be imported EntityNameMap myNames; ///< names of the entities EntityPtr myLastEntityWithName; ///< not null, if last dumped entity had user defined name + +protected: + std::set myNotDumpedEntities; ///< list of entities, used by other features but not dumped yet }; #endif diff --git a/src/PythonAPI/model/dump/DumpAssistant.py b/src/PythonAPI/model/dump/DumpAssistant.py index 7aa341b98..dc4c87384 100644 --- a/src/PythonAPI/model/dump/DumpAssistant.py +++ b/src/PythonAPI/model/dump/DumpAssistant.py @@ -44,6 +44,7 @@ class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper): self.myFeatures[aFeatureKind](theFeature).dump(self) else: self.name(theFeature) + self.clearNotDumped() else: # Probably the feature is a constraint, try to dump it with SketchAPI_Constraint. # In case of theFeature is not a constraint, it will not be dumped. -- 2.39.2