#include <Model_ResultParameter.h>
#include <ModelAPI_Validator.h>
#include <ModelAPI_CompositeFeature.h>
-#include <ModelAPI_Folder.h>
#include <ModelAPI_Tools.h>
#include <Events_Loop.h>
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())
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();
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
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);
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();
}
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());
#include <Model.h>
#include <ModelAPI_Document.h>
#include <ModelAPI_Feature.h>
+#include <ModelAPI_Folder.h>
#include <ModelAPI_Result.h>
#include <ModelAPI_ResultParameter.h>
//! 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
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()
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())