Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
authorazv <azv@opencascade.com>
Tue, 9 Aug 2016 11:26:03 +0000 (14:26 +0300)
committerazv <azv@opencascade.com>
Tue, 9 Aug 2016 11:26:03 +0000 (14:26 +0300)
Workaround to create sketch entities, used in constraint, before the constraint itself.

src/ExchangePlugin/ExchangePlugin_Dump.cpp
src/ModelHighAPI/ModelHighAPI_Dumper.cpp
src/ModelHighAPI/ModelHighAPI_Dumper.h
src/PythonAPI/model/dump/DumpAssistant.py

index d89f6d98a7dfd8cb1eefcb0e1bf38fc3edfaf259..168e9dc197756f19592aab502c1ae363d94d6b2d 100644 (file)
@@ -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);
index e1ed55d36942c39d43366d8edbed5f73861d87cc..ee7894f50975a0bea84cf67e59c065ed187fd39e 100644 (file)
@@ -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<std::string, bool>(aName, isNameDefined);
+  myNotDumpedEntities.insert(theEntity);
   return myNames[theEntity].first;
 }
 
@@ -118,14 +135,11 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theD
 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
 {
   bool isOk = true;
-  CompositeFeaturePtr aLastComposite;
   // dump all features
   std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
   std::list<FeaturePtr>::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<ModelAPI_Document>& 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_ptr<ModelAPI_CompositeFeatur
   int aNbSubs = theComposite->numberOfSubs();
   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<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
+  std::string aBufCopy = theDumper.myDumpBuffer.str();
+  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);
+  }
+
+  // then store currently dumped string
+  theDumper.myFullDump << aBufCopy;
+
   return theDumper;
 }
index c653e50679cc51841ea97f463836550da4c254ef..200262fdb5c85d7417dd327fd29efd6a2dc2cf7f 100644 (file)
@@ -155,13 +155,16 @@ public:
   MODELHIGHAPI_EXPORT
   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& 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<ModelAPI_CompositeFeature>& theComposite);
 
+  /// Check the entity is already dumped
+  bool isDumped(const EntityPtr& theEntity) const;
+
 private:
   typedef std::map<EntityPtr, std::pair<std::string, bool> > EntityNameMap;
   typedef std::map<std::string, std::set<std::string> >      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<EntityPtr> myNotDumpedEntities;  ///< list of entities, used by other features but not dumped yet
 };
 
 #endif
index 7aa341b985b3ca003b44d6bae68a39baed9ca824..dc4c8738432a3726ed412fedd7ce0d7953a51c9a 100644 (file)
@@ -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.