]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportPart.cpp
Salome HOME
Fix incorrect naming of features and results when import (same name of feature and...
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportPart.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ExchangePlugin_ImportPart.h>
21
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_ResultPart.h>
24 #include <ModelAPI_Session.h>
25
26 #include <PartSetPlugin_Part.h>
27
28 #include <map>
29 #include <sstream>
30
31 // Update names of imported features/results concurent with existing objects.
32 static void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theImported);
33
34 ExchangePlugin_ImportPart::ExchangePlugin_ImportPart()
35 {
36 }
37
38 void ExchangePlugin_ImportPart::initAttributes()
39 {
40   data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
41 }
42
43 void ExchangePlugin_ImportPart::execute()
44 {
45   AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
46   std::string aFilename = aFilePathAttr->value();
47   if (aFilename.empty()) {
48     setError("File name is empty.");
49     return;
50   }
51
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;
63     if (aPartFeature) {
64       aPartFeature->execute();
65       aPartResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
66     }
67     if (aPartResult) {
68       aDoc = aPartResult->partDoc();
69       isOk = aDoc->import(aFilename.c_str(), anImportedFeatures);
70     }
71   }
72   if (isOk)
73     correntNonUniqueNames(aDoc, anImportedFeatures);
74   else
75     setError("Cannot import the document.");
76 }
77
78
79 // ================================     Auxiliary functions     ===================================
80
81 typedef std::map<std::string, std::map<std::string, std::set<int> > > ObjectNameMap;
82
83 bool splitName(std::string& theName, int& theIndex)
84 {
85   size_t aLastUndercore = theName.find_last_of('_');
86   bool isOk = aLastUndercore != std::string::npos;
87   if (isOk) {
88     char* isNumber;
89     std::string anIndexStr = theName.substr(aLastUndercore + 1);
90     theIndex = std::strtol(anIndexStr.c_str(), &isNumber, 10);
91     isOk = isNumber != 0;
92     if (isOk)
93       theName.erase(aLastUndercore);
94   }
95   return isOk;
96 }
97
98 void addIndexedName(const ObjectPtr& theObject, ObjectNameMap& theIndexedNames)
99 {
100   std::string aName = theObject->data()->name();
101   std::string aGroup = theObject->groupName();
102   int anIndex = 0;
103   bool isIndexed = splitName(aName, anIndex);
104   std::set<int>& anIndices = theIndexedNames[aGroup][aName];
105   if (isIndexed)
106     anIndices.insert(anIndex);
107 }
108
109 // Collect names of features and results in the document before the import.
110 // The name of indexed feature/result will be split to the name and the index. For example ,
111 // 'Point_1', 'Point_2' will be placed at the same key with the set of corrsponding indices:
112 // 'Point_1', 'Point_2' => {'Point', [1, 2]}.
113 // Thus, the new point should have index 3 and therefore the name 'Point_3'.
114 static void collectOldNames(DocumentPtr theDocument, std::list<FeaturePtr>& theAvoided,
115                             ObjectNameMap& theIndexedNames)
116 {
117   std::list<FeaturePtr> anAllFeatures = theDocument->allFeatures();
118   std::list<FeaturePtr>::iterator aFIt = anAllFeatures.begin();
119   std::list<FeaturePtr>::iterator anAvoidIt = theAvoided.begin();
120   for (; aFIt != anAllFeatures.end(); ++aFIt) {
121     if (anAvoidIt != theAvoided.end() && *aFIt == *anAvoidIt) {
122       // skip this feature
123       ++anAvoidIt;
124       continue;
125     }
126
127     // store name of feature
128     addIndexedName(*aFIt, theIndexedNames);
129     // store names of results
130     const std::list<ResultPtr>& aResults = (*aFIt)->results();
131     for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
132          aRIt != aResults.end(); ++aRIt)
133       addIndexedName(*aRIt, theIndexedNames);
134   }
135 }
136
137 static std::string uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExistingNames)
138 {
139   std::string aName = theObject->data()->name();
140   std::string aGroup = theObject->groupName();
141   int anIndex = 1;
142   splitName(aName, anIndex);
143
144   ObjectNameMap::iterator aFoundGroup = theExistingNames.find(aGroup);
145   bool isUnique = aFoundGroup == theExistingNames.end();
146
147   std::map<std::string, std::set<int> >::iterator aFound;
148   if (!isUnique) {
149     aFound = aFoundGroup->second.find(aName);
150     isUnique = aFound == aFoundGroup->second.end();
151   }
152
153   if (isUnique) {
154     // name is unique
155     aName = theObject->data()->name();
156     addIndexedName(theObject, theExistingNames);
157   }
158   else {
159     // search the appropriate index
160     std::set<int>::iterator aFoundIndex = aFound->second.find(anIndex);
161     for (; aFoundIndex != aFound->second.end(); ++aFoundIndex, ++anIndex)
162       if (anIndex != *aFoundIndex)
163         break;
164     // compose the new name
165     std::ostringstream aNewName;
166     aNewName << aName << "_" << anIndex;
167     aName = aNewName.str();
168     // add new index
169     aFound->second.insert(anIndex);
170   }
171
172   return aName;
173 }
174
175 void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theImported)
176 {
177   ObjectNameMap aNames;
178   collectOldNames(theDocument, theImported, aNames);
179
180   for (std::list<FeaturePtr>::iterator anIt = theImported.begin();
181        anIt != theImported.end(); ++anIt) {
182     // update name of feature
183     std::string aNewName = uniqueName(*anIt, aNames);
184     (*anIt)->data()->setName(aNewName);
185     // update names of results
186     const std::list<ResultPtr>& aResults = (*anIt)->results();
187     for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
188          aRIt != aResults.end(); ++aRIt) {
189       aNewName = uniqueName(*aRIt, aNames);
190       (*aRIt)->data()->setName(aNewName);
191     }
192   }
193 }