From: azv Date: Thu, 23 Nov 2017 06:57:36 +0000 (+0300) Subject: Set unique name to folder (Task 2.3. Ability to put consecutive Features in a folder) X-Git-Tag: V_2.10.0RC~123^2~29 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=93d2d70f5dd018318be3e157dabc4a920b1da6ba;p=modules%2Fshaper.git Set unique name to folder (Task 2.3. Ability to put consecutive Features in a folder) --- diff --git a/src/Model/Model_Objects.cpp b/src/Model/Model_Objects.cpp index 6010d6e5e..1eb9349d5 100644 --- a/src/Model/Model_Objects.cpp +++ b/src/Model/Model_Objects.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include @@ -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::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 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(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(theObj); + if (aFolder) + setUniqueName(aFolder); } theObj->initAttributes(); } @@ -1201,7 +1229,7 @@ std::shared_ptr 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()); diff --git a/src/Model/Model_Objects.h b/src/Model/Model_Objects.h index 15b8ed618..532b161ad 100644 --- a/src/Model/Model_Objects.h +++ b/src/Model/Model_Objects.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -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 diff --git a/src/ModelAPI/ModelAPI.i b/src/ModelAPI/ModelAPI.i index 8a42a919d..6e03cd6ed 100644 --- a/src/ModelAPI/ModelAPI.i +++ b/src/ModelAPI/ModelAPI.i @@ -153,7 +153,7 @@ %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; diff --git a/src/ModelAPI/ModelAPI_Folder.h b/src/ModelAPI/ModelAPI_Folder.h index 702931ffe..1752a90ea 100644 --- a/src/ModelAPI/ModelAPI_Folder.h +++ b/src/ModelAPI/ModelAPI_Folder.h @@ -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() { diff --git a/src/ModelAPI/Test/TestFolder_Create.py b/src/ModelAPI/Test/TestFolder_Create.py index 6041aa505..a46b964d5 100644 --- a/src/ModelAPI/Test/TestFolder_Create.py +++ b/src/ModelAPI/Test/TestFolder_Create.py @@ -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())