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_AttributeInteger.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_AttributeStringArray.h>
25 #include <ModelAPI_ResultPart.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Tools.h>
29 #include <PartSetPlugin_Part.h>
34 static const std::string THE_NEW_PART_STR("New Part");
35 static const std::string THE_PART_SET_STR("PartSet");
37 // Update names of imported features/results concurent with existing objects.
38 static void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theImported);
39 // Find the document according to its name or create the new one.
40 static DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::string& thePartName);
42 ExchangePlugin_ImportPart::ExchangePlugin_ImportPart()
46 void ExchangePlugin_ImportPart::initAttributes()
48 data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
49 data()->addAttribute(TARGET_PART_ID(), ModelAPI_AttributeInteger::typeId());
50 data()->addAttribute(TARGET_PARTS_LIST_ID(), ModelAPI_AttributeStringArray::typeId());
54 void ExchangePlugin_ImportPart::execute()
56 AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
57 std::string aFilename = aFilePathAttr->value();
58 if (aFilename.empty()) {
59 setError("File name is empty.");
63 // get the document where to import
64 AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
65 AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID());
66 SessionPtr aSession = ModelAPI_Session::get();
68 findDocument(aSession->moduleDocument(), aPartsAttr->value(aTargetAttr->value()));
70 // load the file into the document
71 std::list<FeaturePtr> anImportedFeatures;
72 if (aDoc && aDoc->importPart(aFilename.c_str(), anImportedFeatures))
73 correntNonUniqueNames(aDoc, anImportedFeatures);
75 setError("Cannot import the document.");
78 void ExchangePlugin_ImportPart::attributeChanged(const std::string& theID)
80 if (theID == FILE_PATH_ID()) {
81 AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
82 if (aFilePathAttr->value().empty())
85 AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
86 AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID());
88 // update the list of target parts
89 SessionPtr aSession = ModelAPI_Session::get();
90 DocumentPtr aDoc = document();
91 bool isPartSet = aDoc == aSession->moduleDocument();
93 std::list<std::string> anAcceptedValues;
94 anAcceptedValues.push_back(THE_NEW_PART_STR);
96 std::list<FeaturePtr> anImportedFeatures;
97 if (aDoc->importPart(aFilePathAttr->value().c_str(), anImportedFeatures, isPartSet))
98 anAcceptedValues.push_back(THE_PART_SET_STR);
100 // append names of all parts
101 std::list<FeaturePtr> aSubFeatures = aDoc->allFeatures();
102 for (std::list<FeaturePtr>::iterator aFIt = aSubFeatures.begin();
103 aFIt != aSubFeatures.end(); ++aFIt) {
104 if ((*aFIt)->getKind() == PartSetPlugin_Part::ID())
105 anAcceptedValues.push_back((*aFIt)->name());
108 if (aPartsAttr->size() != anAcceptedValues.size())
109 aTargetAttr->setValue(0);
111 aPartsAttr->setSize((int)anAcceptedValues.size());
112 std::list<std::string>::iterator anIt = anAcceptedValues.begin();
113 for (int anInd = 0; anIt != anAcceptedValues.end(); ++anIt, ++anInd)
114 aPartsAttr->setValue(anInd, *anIt);
117 // keep only the name of the current part
118 if (aPartsAttr->size() == 0) {
119 FeaturePtr aPartFeature = ModelAPI_Tools::findPartFeature(aSession->moduleDocument(), aDoc);
121 aPartsAttr->setSize(1);
122 aPartsAttr->setValue(0, aPartFeature->name());
123 aTargetAttr->setValue(0);
130 // ================================ Auxiliary functions ===================================
132 DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::string& thePartName)
135 if (thePartName == THE_PART_SET_STR)
136 aDoc = thePartSetDoc;
138 FeaturePtr aPartFeature;
139 if (thePartName == THE_NEW_PART_STR) {
141 aPartFeature = thePartSetDoc->addFeature(PartSetPlugin_Part::ID());
143 aPartFeature->execute();
146 // find existing part by its name
147 std::list<FeaturePtr> aSubFeatures = thePartSetDoc->allFeatures();
148 for (std::list<FeaturePtr>::iterator aFIt = aSubFeatures.begin();
149 aFIt != aSubFeatures.end(); ++aFIt) {
150 if ((*aFIt)->getKind() == PartSetPlugin_Part::ID() && (*aFIt)->name() == thePartName) {
151 aPartFeature = *aFIt;
158 ResultPartPtr aPartResult =
159 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
161 aDoc = aPartResult->partDoc();
167 typedef std::map<std::string, std::map<std::string, std::set<int> > > ObjectNameMap;
169 bool splitName(std::string& theName, int& theIndex)
171 size_t aLastUndercore = theName.find_last_of('_');
172 bool isOk = aLastUndercore != std::string::npos;
175 std::string anIndexStr = theName.substr(aLastUndercore + 1);
176 theIndex = std::strtol(anIndexStr.c_str(), &isNumber, 10);
177 isOk = isNumber != 0;
179 theName.erase(aLastUndercore);
184 void addIndexedName(const ObjectPtr& theObject, ObjectNameMap& theIndexedNames)
186 std::string aName = theObject->data()->name();
187 std::string aGroup = theObject->groupName();
189 bool isIndexed = splitName(aName, anIndex);
190 std::set<int>& anIndices = theIndexedNames[aGroup][aName];
192 anIndices.insert(anIndex);
195 // Collect names of features and results in the document before the import.
196 // The name of indexed feature/result will be split to the name and the index. For example ,
197 // 'Point_1', 'Point_2' will be placed at the same key with the set of corrsponding indices:
198 // 'Point_1', 'Point_2' => {'Point', [1, 2]}.
199 // Thus, the new point should have index 3 and therefore the name 'Point_3'.
200 static void collectOldNames(DocumentPtr theDocument, std::list<FeaturePtr>& theAvoided,
201 ObjectNameMap& theIndexedNames)
203 std::list<FeaturePtr> anAllFeatures = theDocument->allFeatures();
204 std::list<FeaturePtr>::iterator aFIt = anAllFeatures.begin();
205 std::list<FeaturePtr>::iterator anAvoidIt = theAvoided.begin();
206 for (; aFIt != anAllFeatures.end(); ++aFIt) {
207 if (anAvoidIt != theAvoided.end() && *aFIt == *anAvoidIt) {
213 // store name of feature
214 addIndexedName(*aFIt, theIndexedNames);
215 // store names of results
216 const std::list<ResultPtr>& aResults = (*aFIt)->results();
217 for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
218 aRIt != aResults.end(); ++aRIt)
219 addIndexedName(*aRIt, theIndexedNames);
223 static std::string uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExistingNames)
225 std::string aName = theObject->data()->name();
226 std::string aGroup = theObject->groupName();
228 splitName(aName, anIndex);
230 ObjectNameMap::iterator aFoundGroup = theExistingNames.find(aGroup);
231 bool isUnique = aFoundGroup == theExistingNames.end();
233 std::map<std::string, std::set<int> >::iterator aFound;
235 aFound = aFoundGroup->second.find(aName);
236 isUnique = aFound == aFoundGroup->second.end();
241 aName = theObject->data()->name();
242 addIndexedName(theObject, theExistingNames);
245 // search the appropriate index
246 std::set<int>::iterator aFoundIndex = aFound->second.find(anIndex);
247 for (; aFoundIndex != aFound->second.end(); ++aFoundIndex, ++anIndex)
248 if (anIndex != *aFoundIndex)
250 // compose the new name
251 std::ostringstream aNewName;
252 aNewName << aName << "_" << anIndex;
253 aName = aNewName.str();
255 aFound->second.insert(anIndex);
261 void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theImported)
263 ObjectNameMap aNames;
264 collectOldNames(theDocument, theImported, aNames);
266 for (std::list<FeaturePtr>::iterator anIt = theImported.begin();
267 anIt != theImported.end(); ++anIt) {
268 // update name of feature
269 std::string aNewName = uniqueName(*anIt, aNames);
270 (*anIt)->data()->setName(aNewName);
271 // update names of results
272 const std::list<ResultPtr>& aResults = (*anIt)->results();
273 for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
274 aRIt != aResults.end(); ++aRIt) {
275 aNewName = uniqueName(*aRIt, aNames);
276 (*aRIt)->data()->setName(aNewName);