]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Dumping of the folders to Python
authorazv <azv@opencascade.com>
Sat, 25 Nov 2017 13:58:05 +0000 (16:58 +0300)
committerazv <azv@opencascade.com>
Sat, 25 Nov 2017 13:58:19 +0000 (16:58 +0300)
src/Model/Model_Document.h
src/ModelAPI/ModelAPI_Document.h
src/ModelAPI/Test/TestFolder_Stability.py
src/ModelHighAPI/ModelHighAPI.i
src/ModelHighAPI/ModelHighAPI_Dumper.cpp
src/ModelHighAPI/ModelHighAPI_Dumper.h
src/ModelHighAPI/ModelHighAPI_Folder.cpp
src/ModelHighAPI/ModelHighAPI_Folder.h
src/PythonAPI/model/dump/DumpAssistant.py

index 946025f73fe34fa23d6644943b226afbc3be1083..d4c178103ded8fd2da022de13657fda43ede292a 100644 (file)
@@ -207,6 +207,8 @@ class Model_Document : public ModelAPI_Document
     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
 
   //! Creates a folder (group of the features in the object browser)
+  //! \param theAddBefore a feature, the folder is added before
+  //!                     (if empty, the folder is added after the last feature)
   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Folder> addFolder(
       std::shared_ptr<ModelAPI_Feature> theAddBefore = std::shared_ptr<ModelAPI_Feature>());
   //! Removes the folder from the document (all features in the folder will be kept).
index 525805b1d575251736573fcc9d3f27cb7da11eb9..dc4160da4041ccbfde7cce899eb446d006c8485f 100644 (file)
@@ -182,8 +182,10 @@ public:
   virtual std::list<std::shared_ptr<ModelAPI_Object> > allObjects() = 0;
 
   //! Creates a folder (group of the features in the object browser)
+  //! \param theAddBefore a feature, the folder is added before
+  //!                     (if empty, the folder is added after the last feature)
   virtual std::shared_ptr<ModelAPI_Folder> addFolder(
-      std::shared_ptr<ModelAPI_Feature> theAddBefore) = 0;
+      std::shared_ptr<ModelAPI_Feature> theAddBefore = std::shared_ptr<ModelAPI_Feature>()) = 0;
   //! Removes the folder from the document (all features in the folder will be kept).
   virtual void removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder) = 0;
   //! Search a folder above the list of features applicable to store them
index 9ceb2ee3e366935017a7d433b0b0aa6299781260..bbd867cd0da62a9ef08608f8fdd6ed8afd19c4a3 100644 (file)
@@ -64,11 +64,35 @@ assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features
 
 #=========================================================================
 # Test 1. Check number of features out of folder
-#         and absense of the crash while getting size of incorrect groupd
+#         and absense of the crash while getting size of incorrect group
 #=========================================================================
 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
 assert(aPartDoc.size("Construction", True) == 1), "Wrong size: {}".format(aPartDoc.size("Construction", True))
 
+#=========================================================================
+# Test 2. Add a feature to the folder and check number of features once again
+#=========================================================================
+toFolder = FeatureList()
+toFolder.append(aPoint1)
+
+aSession.startOperation()
+aFolder2 = aPartDoc.addFolder(aPoint1)
+aSession.finishOperation()
+
+NB_FEATURES_FULL += 1
+NB_FEATURES_OUT  += 1
+
+aSession.startOperation()
+aFolder = aPartDoc.findFolderAbove(toFolder)
+assert(aFolder is not None)
+isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
+aSession.finishOperation()
+assert(isAdded)
+
+NB_FEATURES_OUT -= 1
+assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
+assert(aPartDoc.size("Construction", True) == 1), "Wrong size: {}".format(aPartDoc.size("Construction", True))
+
 
 from salome.shaper import model
 assert(model.checkPythonDump())
index 268c97635a651ce3d6bc44bf858f3d3d9d1c131d..0b49cf05c46f9696caef19d8b3a333fd61b969ad 100644 (file)
@@ -57,6 +57,7 @@
 
 // shared pointers
 %shared_ptr(ModelHighAPI_Interface)
+%shared_ptr(ModelHighAPI_Folder)
 
 // typemaps
 
index 84f4eb55653604cf50e7140e754ac4151dbb8789..90b6655b7a03d76d15fc6873ca82e47b6ee17bfd 100644 (file)
@@ -46,6 +46,7 @@
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Entity.h>
 #include <ModelAPI_Feature.h>
+#include <ModelAPI_Folder.h>
 #include <ModelAPI_Result.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_ResultCompSolid.h>
@@ -124,14 +125,27 @@ const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
     return aFound->second.myCurrentName;
 
   // entity is not found, store it
-  std::string aName;
+  std::string aName, aKind;
   bool isDefaultName = false;
+  bool isSaveNotDumped = theSaveNotDumped;
   std::ostringstream aDefaultName;
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
   if (aFeature) {
     aName = aFeature->name();
-    const std::string& aKind = aFeature->getKind();
-    DocumentPtr aDoc = aFeature->document();
+    aKind = aFeature->getKind();
+  } else {
+    FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
+    if (aFolder) {
+      aName = aFolder->data()->name();
+      aKind = ModelAPI_Folder::ID();
+      isSaveNotDumped = false;
+      myNotDumpedFolders.insert(aFolder);
+    }
+  }
+
+  ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
+  if (anObject) {
+    DocumentPtr aDoc = anObject->document();
     int& aNbFeatures = myFeatureCount[aDoc][aKind];
     aNbFeatures += 1;
 
@@ -164,7 +178,7 @@ const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
   }
 
   myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
-  if (theSaveNotDumped)
+  if (isSaveNotDumped)
     myNotDumpedEntities.insert(theEntity);
 
   // store names of results
@@ -238,18 +252,30 @@ 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;
-  std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
-  std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
+  std::list<ObjectPtr> anObjects = theDoc->allObjects();
+  std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
   // firstly, dump all parameters
-  for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
-    dumpParameter(*aFeatIt);
+  for (; anObjIt != anObjects.end(); ++ anObjIt) {
+    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
+    if (aFeature)
+      dumpParameter(aFeature);
+  }
   // dump all other features
-  for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
-    CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
+  for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
+    CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
     if (aCompFeat) // iteratively process composite features
       isOk = process(aCompFeat) && isOk;
-    else if (!isDumped(*aFeatIt)) // dump common feature
-      dumpFeature(*aFeatIt);
+    else if (!isDumped(*anObjIt)) {
+      // dump folder
+      FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
+      if (aFolder)
+        dumpFolder(aFolder);
+      else {
+        FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
+        if (aFeature) // dump common feature
+          dumpFeature(aFeature);
+      }
+    }
   }
   return isOk;
 }
@@ -659,6 +685,13 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
   return *this;
 }
 
+ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
+{
+  myDumpBuffer << name(theFolder);
+  myNotDumpedFolders.erase(theFolder);
+  return *this;
+}
+
 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
 {
   myDumpBuffer << name(theEntity);
@@ -985,5 +1018,12 @@ ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
   // then store currently dumped string
   theDumper.myFullDump << aBufCopy;
 
+  // now, store all not dumped folders
+  std::set<FolderPtr>::const_iterator aFolderIt = theDumper.myNotDumpedFolders.begin();
+  while (aFolderIt != theDumper.myNotDumpedFolders.end()) {
+    FolderPtr aFolder = *aFolderIt++;
+    theDumper.dumpFolder(aFolder);
+  }
+
   return theDumper;
 }
index c2fe0cd54de1bb6734d24f182989a7007e7ac565..5bba141688bab86cda0cb74df8ce2c783fc4a2cd 100644 (file)
@@ -54,12 +54,14 @@ class ModelAPI_CompositeFeature;
 class ModelAPI_Document;
 class ModelAPI_Entity;
 class ModelAPI_Feature;
+class ModelAPI_Folder;
 class ModelAPI_Object;
 class ModelAPI_Result;
 
 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
 typedef std::shared_ptr<ModelAPI_Entity>   EntityPtr;
 typedef std::shared_ptr<ModelAPI_Feature>  FeaturePtr;
+typedef std::shared_ptr<ModelAPI_Folder>   FolderPtr;
 typedef std::shared_ptr<ModelAPI_Result>   ResultPtr;
 
 /**\class ModelHighAPI_Dumper
@@ -116,6 +118,8 @@ public:
   virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
   /// Dump given feature
   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
+  /// Dump folder
+  virtual void dumpFolder(const FolderPtr& theFolder) = 0;
 
   /// Set a feature that should not be dumped anyway
   MODELHIGHAPI_EXPORT
@@ -203,6 +207,10 @@ public:
   MODELHIGHAPI_EXPORT
   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
 
+  /// Dump folder
+  MODELHIGHAPI_EXPORT
+  ModelHighAPI_Dumper& operator<<(const FolderPtr& theFolder);
+
   /// Dump result
   MODELHIGHAPI_EXPORT
   ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
@@ -330,10 +338,13 @@ private:
   std::set<FeaturePtr> myFeaturesToSkip;
 
 protected:
-   /// list of entities, used by other features but not dumped yet
+  /// list of entities, used by other features but not dumped yet
   std::set<EntityPtr> myNotDumpedEntities;
+  /// list of folders which do not dumped yet
+  std::set<FolderPtr> myNotDumpedFolders;
 
   friend class SketchAPI_Sketch;
+  friend class ModelHighAPI_Folder;
 };
 
 #endif
index 7cfa3215b1570c8234700928e8a74b604ad193c7..cbb986198c057c4f069d7a69a842f970cb33de1b 100644 (file)
 //
 
 #include "ModelHighAPI_Folder.h"
-#include <ModelHighAPI_Selection.h>
+#include <ModelHighAPI_Dumper.h>
+#include <ModelHighAPI_Reference.h>
 
+#include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_Document.h>
-#include <ModelAPI_Folder.h>
 
 //--------------------------------------------------------------------------------------
 
 ModelHighAPI_Folder::ModelHighAPI_Folder(const std::shared_ptr<ModelAPI_Folder> & theFolder)
+  : ModelHighAPI_Interface(FeaturePtr()),
+    myFolder(theFolder)
 {
+  initialize();
 }
 
-
 ModelHighAPI_Folder::~ModelHighAPI_Folder()
 {
 }
 
+bool ModelHighAPI_Folder::initialize()
+{
+  if (!myFolder) {
+    throwException(ID() + " exception: The folder is NULL.");
+    return false;
+  }
+
+  SET_ATTRIBUTE(firstFeature, ModelAPI_AttributeReference, ModelAPI_Folder::FIRST_FEATURE_ID());
+  SET_ATTRIBUTE(lastFeature,  ModelAPI_AttributeReference, ModelAPI_Folder::LAST_FEATURE_ID());
+
+  return true;
+}
+
+void ModelHighAPI_Folder::dump(ModelHighAPI_Dumper& theDumper) const
+{
+  const std::string& aDocName = theDumper.name(myFolder->document());
+
+  AttributeReferencePtr aStartRef = myFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
+  AttributeReferencePtr aEndRef   = myFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
+
+  // Dump folder if it is empty or when its features have been already dumped.
+  // Otherwise, just store the name of the folder.
+  if (!aEndRef->value())
+    theDumper << myFolder << " = model.addFolder(" << aDocName << ")" << std::endl;
+  else if (theDumper.isDumped(aEndRef->value()))
+    theDumper << myFolder << " = model.addFolder(" << aDocName << ", "
+              << aStartRef << ", " << aEndRef << ")" << std::endl;
+  else
+    theDumper.name(myFolder);
+}
+
 //--------------------------------------------------------------------------------------
 
-std::shared_ptr<ModelHighAPI_Folder> addFolder(const std::shared_ptr<ModelAPI_Document>& thePart)
+std::shared_ptr<ModelHighAPI_Folder> addFolder(const std::shared_ptr<ModelAPI_Document>& theDoc)
 {
-  std::shared_ptr<ModelAPI_Folder> aFolder;//// = thePart->addFolder();
+  std::shared_ptr<ModelAPI_Folder> aFolder = theDoc->addFolder();
+  return std::shared_ptr<ModelHighAPI_Folder>(new ModelHighAPI_Folder(aFolder));
+}
+
+std::shared_ptr<ModelHighAPI_Folder> addFolder(const std::shared_ptr<ModelAPI_Document>& theDoc,
+                                               const ModelHighAPI_Reference& theFirstFeature,
+                                               const ModelHighAPI_Reference& theLastFeature)
+{
+  std::shared_ptr<ModelAPI_Folder> aFolder = theDoc->addFolder(theFirstFeature.feature());
+
+  AttributeReferencePtr aFirstFeatAttr = aFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
+  theFirstFeature.fillAttribute(aFirstFeatAttr);
+
+  AttributeReferencePtr aLastFeatAttr = aFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
+  theLastFeature.fillAttribute(aLastFeatAttr);
+
   return std::shared_ptr<ModelHighAPI_Folder>(new ModelHighAPI_Folder(aFolder));
 }
index 2c7bc45d9fae096e142e16abb9dd5e86b0161ce4..6713ff0a04ffc1d49bad6d6e09c0834c9b9ac052 100644 (file)
 
 //--------------------------------------------------------------------------------------
 #include <ModelHighAPI_Interface.h>
+#include <ModelHighAPI_Macro.h>
+
+#include <ModelAPI_Folder.h>
 
 #include <memory>
 //--------------------------------------------------------------------------------------
+class ModelAPI_AttributeReference;
 class ModelAPI_Document;
-class ModelAPI_Folder;
-class ModelHighAPI_Selection;
+class ModelHighAPI_Reference;
 //--------------------------------------------------------------------------------------
 /**\class ModelHighAPI_Folder
  * \ingroup CPPHighAPI
  * \brief Class for filling ModelAPI_Folder
  */
-class ModelHighAPI_Folder
+class ModelHighAPI_Folder : public ModelHighAPI_Interface
 {
 public:
   /// Constructor for a folder
   MODELHIGHAPI_EXPORT
-  explicit ModelHighAPI_Folder(const std::shared_ptr<ModelAPI_Folder> & theFolder);
+  explicit ModelHighAPI_Folder(const std::shared_ptr<ModelAPI_Folder>& theFolder);
   /// Destructor
   MODELHIGHAPI_EXPORT virtual ~ModelHighAPI_Folder();
+
+  static std::string ID() { return ModelAPI_Folder::ID(); }
+  virtual std::string getID() { return ID(); }
+
+  /// First feature reference
+  std::shared_ptr<ModelAPI_AttributeReference> firstFeature() const
+  { return myfirstFeature; }
+
+  /// Last feature reference
+  std::shared_ptr<ModelAPI_AttributeReference> lastFeature() const
+  { return mylastFeature; }
+
+  /// Dump wrapped feature
+  MODELHIGHAPI_EXPORT virtual void dump(ModelHighAPI_Dumper& theDumper) const;
+
+protected:
+  bool initialize();
+
+private:
+  std::shared_ptr<ModelAPI_Folder> myFolder;
+
+  std::shared_ptr<ModelAPI_AttributeReference> myfirstFeature;
+  std::shared_ptr<ModelAPI_AttributeReference> mylastFeature;
 };
 
 //--------------------------------------------------------------------------------------
 /**\ingroup CPPHighAPI
- * \brief Create Folder feature
+ * \brief Create empty Folder feature
  */
 MODELHIGHAPI_EXPORT
 std::shared_ptr<ModelHighAPI_Folder> addFolder(const std::shared_ptr<ModelAPI_Document>& theDoc);
+
+/**\ingroup CPPHighAPI
+ * \brief Create Folder feature
+ */
+MODELHIGHAPI_EXPORT
+std::shared_ptr<ModelHighAPI_Folder> addFolder(const std::shared_ptr<ModelAPI_Document>& theDoc,
+                                               const ModelHighAPI_Reference& theFirstFeature,
+                                               const ModelHighAPI_Reference& theLastFeature);
 //--------------------------------------------------------------------------------------
 #endif /* SRC_MODELHIGHAPI_MODELHIGHAPI_FOLDER_H_ */
index f56860b041d968cb95d73b4ab6735ba97c92081a..0cdebfdfefe8b2e29eaf0b65026fc2cd59e74297 100644 (file)
@@ -72,6 +72,11 @@ class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper):
             # In case of theFeature is not a constraint, it will not be dumped.
             self.myFeatures[SketchAPI.SketchAPI_Constraint.ID()](theFeature).dump(self)
 
+    ## Create wrapper for a folder and dump it
+    def dumpFolder(self, theFolder):
+        if theFolder.ID() in self.myFeatures:
+            self.myFeatures[theFolder.ID()](theFolder).dump(self)
+
     ## Dump all parameters
     def dumpParameter(self, theFeature):
         aFeatureKind = theFeature.getKind()