1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ExchangePlugin_ImportPart.h>
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_ResultPart.h>
24 #include <ModelAPI_Session.h>
26 #include <PartSetPlugin_Part.h>
31 // Update names of imported features/results concurent with existing objects.
32 static void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theImported);
34 ExchangePlugin_ImportPart::ExchangePlugin_ImportPart()
38 void ExchangePlugin_ImportPart::initAttributes()
40 data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
43 void ExchangePlugin_ImportPart::execute()
45 AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
46 std::string aFilename = aFilePathAttr->value();
47 if (aFilename.empty()) {
48 setError("File name is empty.");
52 // load the file into the active document
53 SessionPtr aSession = ModelAPI_Session::get();
54 DocumentPtr aDoc = document();
55 bool isPartSet = aDoc == aSession->moduleDocument();
56 std::list<FeaturePtr> anImportedFeatures;
57 bool isOk = aDoc->import(aFilename.c_str(), anImportedFeatures, isPartSet);
58 if (!isOk && isPartSet) {
59 // there are features not appropriate for PartSet,
60 // create new part and load there
61 FeaturePtr aPartFeature = aDoc->addFeature(PartSetPlugin_Part::ID());
62 ResultPartPtr aPartResult;
64 aPartFeature->execute();
65 aPartResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
68 aDoc = aPartResult->partDoc();
69 isOk = aDoc->import(aFilename.c_str(), anImportedFeatures);
73 correntNonUniqueNames(aDoc, anImportedFeatures);
75 setError("Cannot import the document.");
79 // ================================ Auxiliary functions ===================================
81 bool splitName(std::string& theName, int& theIndex)
83 size_t aLastUndercore = theName.find_last_of('_');
84 bool isOk = aLastUndercore != std::string::npos;
87 std::string anIndexStr = theName.substr(aLastUndercore + 1);
88 theIndex = std::strtol(anIndexStr.c_str(), &isNumber, 10);
91 theName.erase(aLastUndercore);
96 void addIndexedName(const std::string& theName,
97 std::map<std::string, std::set<int> >& theIndexedNames)
99 std::string aName = theName;
101 bool isIndexed = splitName(aName, anIndex);
102 std::set<int>& anIndices = theIndexedNames[aName];
104 anIndices.insert(anIndex);
107 // Collect names of features and results in the document before the import.
108 // The name of indexed feature/result will be split to the name and the index. For example ,
109 // 'Point_1', 'Point_2' will be placed at the same key with the set of corrsponding indices:
110 // 'Point_1', 'Point_2' => {'Point', [1, 2]}.
111 // Thus, the new point should have index 3 and therefore the name 'Point_3'.
112 static void collectOldNames(DocumentPtr theDocument, std::list<FeaturePtr>& theAvoided,
113 std::map<std::string, std::set<int> >& theIndexedNames)
115 std::list<FeaturePtr> anAllFeatures = theDocument->allFeatures();
116 std::list<FeaturePtr>::iterator aFIt = anAllFeatures.begin();
117 std::list<FeaturePtr>::iterator anAvoidIt = theAvoided.begin();
118 for (; aFIt != anAllFeatures.end(); ++aFIt) {
119 if (anAvoidIt != theAvoided.end() && *aFIt == *anAvoidIt) {
125 // store name of feature
126 addIndexedName((*aFIt)->data()->name(), theIndexedNames);
127 // store names of results
128 const std::list<ResultPtr>& aResults = (*aFIt)->results();
129 for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
130 aRIt != aResults.end(); ++aRIt)
131 addIndexedName((*aRIt)->data()->name(), theIndexedNames);
135 static std::string uniqueName(const std::string& theName,
136 std::map<std::string, std::set<int> >& theExistingNames)
138 std::string aName = theName;
140 splitName(aName, anIndex);
142 std::map<std::string, std::set<int> >::iterator aFound = theExistingNames.find(aName);
143 bool isUnique = false;
144 if (aFound == theExistingNames.end())
147 // search the appropriate index
148 std::set<int>::iterator aFoundIndex = aFound->second.find(anIndex);
149 for (; aFoundIndex != aFound->second.end(); ++aFoundIndex, ++anIndex)
150 if (anIndex != *aFoundIndex)
152 // compose the new name
153 std::ostringstream aNewName;
154 aNewName << aName << "_" << anIndex;
155 aName = aNewName.str();
157 aFound->second.insert(anIndex);
163 addIndexedName(theName, theExistingNames);
168 void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theImported)
170 std::map<std::string, std::set<int> > aNames;
171 collectOldNames(theDocument, theImported, aNames);
173 for (std::list<FeaturePtr>::iterator anIt = theImported.begin();
174 anIt != theImported.end(); ++anIt) {
175 // update name of feature
176 std::string aNewName = uniqueName((*anIt)->data()->name(), aNames);
177 (*anIt)->data()->setName(aNewName);
178 // update names of results
179 const std::list<ResultPtr>& aResults = (*anIt)->results();
180 for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
181 aRIt != aResults.end(); ++aRIt) {
182 aNewName = uniqueName((*aRIt)->data()->name(), aNames);
183 (*aRIt)->data()->setName(aNewName);