From 146cb80b58bc450adca2610b0c39622a987c7b3e Mon Sep 17 00:00:00 2001 From: mpv Date: Fri, 3 Nov 2017 14:48:39 +0300 Subject: [PATCH] Make the same-name results distributed in time recognized correctly in loading of python scripts. --- src/Model/Model_Data.cpp | 2 +- src/Model/Model_Document.cpp | 60 ++++++++++++++++++----------- src/Model/Model_Document.h | 6 ++- src/Model/Model_SelectionNaming.cpp | 5 +-- 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index 867614ca3..e9e55e990 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -134,7 +134,7 @@ void Model_Data::setName(const std::string& theName) ModelAPI_ObjectRenamedMessage::send(myObject, anOldName, theName, this); if (isModified && myObject && myObject->document()) { std::dynamic_pointer_cast(myObject->document())-> - changeNamingName(anOldName, theName); + changeNamingName(anOldName, theName, myLab); } #ifdef DEBUG_NAMES myObject->myName = theName; diff --git a/src/Model/Model_Document.cpp b/src/Model/Model_Document.cpp index 03959c2ff..26329090f 100755 --- a/src/Model/Model_Document.cpp +++ b/src/Model/Model_Document.cpp @@ -1273,23 +1273,35 @@ Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2) void Model_Document::addNamingName(const TDF_Label theLabel, std::string theName) { - myNamingNames[theName] = theLabel; + myNamingNames[theName].push_back(theLabel); } -void Model_Document::changeNamingName(const std::string theOldName, const std::string theNewName) +void Model_Document::changeNamingName(const std::string theOldName, + const std::string theNewName, + const TDF_Label& theLabel) { - std::map::iterator aFind = myNamingNames.find(theOldName); + std::map >::iterator aFind = myNamingNames.find(theOldName); if (aFind != myNamingNames.end()) { - myNamingNames[theNewName] = aFind->second; - myNamingNames.erase(theOldName); + std::list::iterator aLabIter = aFind->second.begin(); + for(; aLabIter != aFind->second.end(); aLabIter++) { + if (theLabel.IsEqual(*aLabIter)) { // found the label + myNamingNames[theNewName].push_back(theLabel); + if (aFind->second.size() == 1) { // only one element, so, just change the name + myNamingNames.erase(theOldName); + } else { // remove from the list + aFind->second.erase(aLabIter); + } + return; + } + } } } TDF_Label Model_Document::findNamingName(std::string theName) { - std::map::iterator aFind = myNamingNames.find(theName); + std::map >::iterator aFind = myNamingNames.find(theName); if (aFind != myNamingNames.end()) { - return aFind->second; + return *(aFind->second.rbegin()); } // not found exact name, try to find by sub-components std::string::size_type aSlash = theName.rfind('/'); @@ -1298,24 +1310,28 @@ TDF_Label Model_Document::findNamingName(std::string theName) aFind = myNamingNames.find(anObjName); if (aFind != myNamingNames.end()) { TCollection_ExtendedString aSubName(theName.substr(aSlash + 1).c_str()); - // searching sub-labels with this name - TDF_ChildIDIterator aNamesIter(aFind->second, TDataStd_Name::GetID(), Standard_True); - for(; aNamesIter.More(); aNamesIter.Next()) { - Handle(TDataStd_Name) aName = Handle(TDataStd_Name)::DownCast(aNamesIter.Value()); - if (aName->Get() == aSubName) - return aName->Label(); - } - // If not found child label with the exact sub-name, then try to find compound with - // such sub-name without suffix. - Standard_Integer aSuffixPos = aSubName.SearchFromEnd('_'); - if (aSuffixPos != -1 && aSuffixPos != aSubName.Length()) { - TCollection_ExtendedString anIndexStr = aSubName.Split(aSuffixPos); - aSubName.Remove(aSuffixPos); - aNamesIter.Initialize(aFind->second, TDataStd_Name::GetID(), Standard_True); + // iterate all possible same-named labels starting from the last one (the recent) + std::list::reverse_iterator aLabIter = aFind->second.rbegin(); + for(; aLabIter != aFind->second.rend(); aLabIter++) { + // searching sub-labels with this name + TDF_ChildIDIterator aNamesIter(*aLabIter, TDataStd_Name::GetID(), Standard_True); for(; aNamesIter.More(); aNamesIter.Next()) { Handle(TDataStd_Name) aName = Handle(TDataStd_Name)::DownCast(aNamesIter.Value()); - if (aName->Get() == aSubName) { + if (aName->Get() == aSubName) return aName->Label(); + } + // If not found child label with the exact sub-name, then try to find compound with + // such sub-name without suffix. + Standard_Integer aSuffixPos = aSubName.SearchFromEnd('_'); + if (aSuffixPos != -1 && aSuffixPos != aSubName.Length()) { + TCollection_ExtendedString anIndexStr = aSubName.Split(aSuffixPos); + aSubName.Remove(aSuffixPos); + aNamesIter.Initialize(*aLabIter, TDataStd_Name::GetID(), Standard_True); + for(; aNamesIter.More(); aNamesIter.Next()) { + Handle(TDataStd_Name) aName = Handle(TDataStd_Name)::DownCast(aNamesIter.Value()); + if (aName->Get() == aSubName) { + return aName->Label(); + } } } } diff --git a/src/Model/Model_Document.h b/src/Model/Model_Document.h index 67e48301e..6114e759b 100644 --- a/src/Model/Model_Document.h +++ b/src/Model/Model_Document.h @@ -206,7 +206,8 @@ class Model_Document : public ModelAPI_Document //! Registers the name of the shape for the topological naming needs void addNamingName(const TDF_Label theLabel, std::string theName); //! Updates the name of some object - void changeNamingName(std::string theOldName, const std::string theNewName); + void changeNamingName(std::string theOldName, const std::string theNewName, + const TDF_Label& theLabel); //! Returns the label, keeper of the name for the topological naming needs TDF_Label findNamingName(std::string theName); //! Returns the result by name of the result (names of results must be unique, used for naming @@ -351,7 +352,8 @@ class Model_Document : public ModelAPI_Document std::list myRedos; //! Optimization for finding the shape-label by topological naming names - std::map myNamingNames; + //! The name -> list of labels where this name is appeared (the last created name is last here) + std::map > myNamingNames; //! If it is true, features are not executed on update (on abort, undo, redo) bool myExecuteFeatures; diff --git a/src/Model/Model_SelectionNaming.cpp b/src/Model/Model_SelectionNaming.cpp index 89c044cb4..11473dcaa 100644 --- a/src/Model/Model_SelectionNaming.cpp +++ b/src/Model/Model_SelectionNaming.cpp @@ -83,7 +83,7 @@ std::string Model_SelectionNaming::getShapeName( // indexes are added to sub-shapes not primitives // (primitives must not be located at the same label) if(!aName.empty() && aNS->Evolution() != TNaming_PRIMITIVE && isNeedContextName) { - const TDF_Label& aLabel = aNS->Label();//theDoc->findNamingName(aName); + const TDF_Label& aLabel = aNS->Label(); static const std::string aPostFix("_"); TNaming_Iterator anItL(aNS); for(int i = 1; anItL.More(); anItL.Next(), i++) { @@ -472,9 +472,6 @@ const TopoDS_Shape findFaceByName( const std::string& theSubShapeName, std::shared_ptr theDoc) { TopoDS_Shape aFace; - //std::string::size_type n, nb = theSubShapeName.rfind('/'); - //if (nb == std::string::npos) nb = 0; - //std::string aSubString = theSubShapeName.substr(nb + 1); std::string aSubString = theSubShapeName; TDF_Label aLabel = theDoc->findNamingName(aSubString); -- 2.39.2