]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Set unique name to folder (Task 2.3. Ability to put consecutive Features in a folder)
authorazv <azv@opencascade.com>
Thu, 23 Nov 2017 06:57:36 +0000 (09:57 +0300)
committerazv <azv@opencascade.com>
Thu, 23 Nov 2017 06:57:36 +0000 (09:57 +0300)
src/Model/Model_Objects.cpp
src/Model/Model_Objects.h
src/ModelAPI/ModelAPI.i
src/ModelAPI/ModelAPI_Folder.h
src/ModelAPI/Test/TestFolder_Create.py

index 6010d6e5e5977bd18b5dee18ab82afa14d0367e1..1eb9349d564c4485455c2a1a2c81b5ba30a458ce 100644 (file)
@@ -32,7 +32,6 @@
 #include <Model_ResultParameter.h>
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_CompositeFeature.h>
-#include <ModelAPI_Folder.h>
 #include <ModelAPI_Tools.h>
 
 #include <Events_Loop.h>
@@ -651,6 +650,13 @@ TDF_Label Model_Objects::featuresLabel() const
   return myMain.FindChild(TAG_OBJECTS);
 }
 
+static std::string composeName(const std::string& theFeatureKind, const int theIndex)
+{
+  std::stringstream aNameStream;
+  aNameStream << theFeatureKind << "_" << theIndex;
+  return aNameStream.str();
+}
+
 void Model_Objects::setUniqueName(FeaturePtr theFeature)
 {
   if (!theFeature->data()->name().empty())
@@ -664,9 +670,7 @@ void Model_Objects::setUniqueName(FeaturePtr theFeature)
       aNumObjects++;
   }
   // generate candidate name
-  std::stringstream aNameStream;
-  aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
-  aName = aNameStream.str();
+  aName = composeName(theFeature->getKind(), aNumObjects + 1);
   // check this is unique, if not, increase index by 1
   for (aFIter.Initialize(myFeatures); aFIter.More();) {
     FeaturePtr aFeature = aFIter.Value();
@@ -681,9 +685,7 @@ void Model_Objects::setUniqueName(FeaturePtr theFeature)
 
     if (isSameName) {
       aNumObjects++;
-      std::stringstream aNameStream;
-      aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
-      aName = aNameStream.str();
+      aName = composeName(theFeature->getKind(), aNumObjects + 1);
       // reinitialize iterator to make sure a new name is unique
       aFIter.Initialize(myFeatures);
     } else
@@ -692,6 +694,28 @@ void Model_Objects::setUniqueName(FeaturePtr theFeature)
   theFeature->data()->setName(aName);
 }
 
+void Model_Objects::setUniqueName(FolderPtr theFolder)
+{
+  if (!theFolder->name().empty())
+    return; // name is already defined
+
+  int aNbFolders = myFolders.Size();
+  std::string aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
+
+  // check the uniqueness of the name
+  NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator anIt(myFolders);
+  while (anIt.More()) {
+    if (anIt.Value()->data()->name() == aName) {
+      aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
+      // reinitialize iterator to make sure a new name is unique
+      anIt.Initialize(myFolders);
+    } else
+      anIt.Next();
+  }
+
+  theFolder->data()->setName(aName);
+}
+
 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
 {
   std::shared_ptr<Model_Data> aData(new Model_Data);
@@ -702,6 +726,10 @@ void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTa
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
   if (aFeature.get()) {
     setUniqueName(aFeature);  // must be before "initAttributes" because duplicate part uses name
+  } else { // is it a folder?
+    FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theObj);
+    if (aFolder)
+      setUniqueName(aFolder);
   }
   theObj->initAttributes();
 }
@@ -1201,7 +1229,7 @@ std::shared_ptr<ModelAPI_Folder> Model_Objects::createFolder(
   AddToRefArray(aFeaturesLab, aFolderLab, aPrevFeatureLab);
 
   // keep the feature ID to restore document later correctly
-  TDataStd_Comment::Set(aFolderLab, aFolder->getKind().c_str());
+  TDataStd_Comment::Set(aFolderLab, ModelAPI_Folder::ID().c_str());
   myFolders.Bind(aFolderLab, aFolder);
   // must be before the event sending: for OB the feature is already added
   updateHistory(ModelAPI_Folder::group());
index 15b8ed61888ca79a297e8f66bf34dadaf2d833c4..532b161ad1cf34a2b1299ff6587880423aeeb66b 100644 (file)
@@ -24,6 +24,7 @@
 #include <Model.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Feature.h>
+#include <ModelAPI_Folder.h>
 #include <ModelAPI_Result.h>
 #include <ModelAPI_ResultParameter.h>
 
@@ -168,6 +169,9 @@ class Model_Objects
   //! feature type + "_" + index
   void setUniqueName(FeaturePtr theFeature);
 
+  //! Initializes the foldet with an unique name ("Folder_" + index)
+  void setUniqueName(FolderPtr theFolder);
+
   //! Synchronizes myFeatures list with the updated document
   //! \param theUpdated list of labels that are marked as modified, so featrues must be also
   //! \param theUpdateReferences causes the update of back-references
index 8a42a919d5a70963d53bf451bd3b334f7475a5e8..6e03cd6ed8ca25f2b87e91bf6f2b20661073d52e 100644 (file)
 %include "ModelAPI_ResultParameter.h"
 %include "ModelAPI_Tools.h"
 %include "ModelAPI_ResultCompSolid.h"
-#include "ModelAPI_Folder.h"
+%include "ModelAPI_Folder.h"
 
 // std::list -> []
 %template(StringList) std::list<std::string>;
index 702931ffee677887de7945b8f29d172ac8e4b2db..1752a90eafcc45f28b3389ce3f14857cf82b115b 100644 (file)
@@ -42,13 +42,6 @@ public:
     return MY_FOLDER_ID;
   }
 
-  /// Returns the unique kind of a feature
-  virtual const std::string& getKind()
-  {
-    static const std::string& MY_KIND = ModelAPI_Folder::ID();
-    return MY_KIND;
-  }
-
   /// Returns the group identifier of all features
   inline static std::string group()
   {
index 6041aa505c61e84f6d23db1b33893ff7d7d2af4a..a46b964d5868e9aca24131e38a828c06e7f2fa1b 100644 (file)
@@ -57,7 +57,8 @@ aFolder1 = aPartSetDoc.addFolder(aPoint1)
 aSession.finishOperation()
 
 assert(aPartSetDoc.size("Folders") == 1)
-assert(aFolder1.name() == "Folder_1")
+FOLDER_NAME_EXPECTED = "Folder_1"
+assert(aFolder1.name() == FOLDER_NAME_EXPECTED), "Actual name '{}', expected '{}'".format(aFolder1.name(), FOLDER_NAME_EXPECTED)
 
 ## Folder at the end of features list
 #aSession.startOperation()
@@ -94,7 +95,8 @@ aFolder2 = aPartDoc.addFolder(aPoint2)
 aSession.finishOperation()
 
 assert(aPartDoc.size("Folders") == 1)
-assert(aFolder2.data().name() == "Folder_1")
+FOLDER_NAME_EXPECTED = "Folder_1"
+assert(aFolder1.name() == FOLDER_NAME_EXPECTED), "Actual name '{}', expected '{}'".format(aFolder1.name(), FOLDER_NAME_EXPECTED)
 
 from salome.shaper import model
 assert(model.checkPythonDump())