]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportPart.cpp
Salome HOME
4a40de68c25028fc739ef0f9bcb9545040890b40
[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 bool splitName(std::string& theName, int& theIndex)
82 {
83   size_t aLastUndercore = theName.find_last_of('_');
84   bool isOk = aLastUndercore != std::string::npos;
85   if (isOk) {
86     char* isNumber;
87     std::string anIndexStr = theName.substr(aLastUndercore + 1);
88     theIndex = std::strtol(anIndexStr.c_str(), &isNumber, 10);
89     isOk = isNumber != 0;
90     if (isOk)
91       theName.erase(aLastUndercore);
92   }
93   return isOk;
94 }
95
96 void addIndexedName(const std::string& theName, std::map<std::string, std::set<int> >& theIndexedNames)
97 {
98   std::string aName = theName;
99   int anIndex = 0;
100   bool isIndexed = splitName(aName, anIndex);
101   std::set<int>& anIndices = theIndexedNames[aName];
102   if (isIndexed)
103     anIndices.insert(anIndex);
104 }
105
106 // Collect names of features and results in the document before the import.
107 // The name of indexed feature/result will be split to the name and the index. For example ,
108 // 'Point_1', 'Point_2' will be placed at the same key with the set of corrsponding indices:
109 // 'Point_1', 'Point_2' => {'Point', [1, 2]}.
110 // Thus, the new point should have index 3 and therefore the name 'Point_3'.
111 static void collectOldNames(DocumentPtr theDocument, std::list<FeaturePtr>& theAvoided,
112                             std::map<std::string, std::set<int> >& theIndexedNames)
113 {
114   std::list<FeaturePtr> anAllFeatures = theDocument->allFeatures();
115   std::list<FeaturePtr>::iterator aFIt = anAllFeatures.begin();
116   std::list<FeaturePtr>::iterator anAvoidIt = theAvoided.begin();
117   for (; aFIt != anAllFeatures.end(); ++aFIt) {
118     if (anAvoidIt != theAvoided.end() && *aFIt == *anAvoidIt) {
119       // skip this feature
120       ++anAvoidIt;
121       continue;
122     }
123
124     // store name of feature
125     addIndexedName((*aFIt)->data()->name(), theIndexedNames);
126     // store names of results
127     const std::list<ResultPtr>& aResults = (*aFIt)->results();
128     for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
129          aRIt != aResults.end(); ++aRIt)
130       addIndexedName((*aRIt)->data()->name(), theIndexedNames);
131   }
132 }
133
134 static std::string uniqueName(const std::string& theName,
135                               std::map<std::string, std::set<int> >& theExistingNames)
136 {
137   std::string aName = theName;
138   int anIndex = 1;
139   splitName(aName, anIndex);
140
141   std::map<std::string, std::set<int> >::iterator aFound = theExistingNames.find(aName);
142   bool isUnique = false;
143   if (aFound == theExistingNames.end())
144     isUnique = true;
145   else {
146     // search the appropriate index
147     std::set<int>::iterator aFoundIndex = aFound->second.find(anIndex);
148     for (; aFoundIndex != aFound->second.end(); ++aFoundIndex, ++anIndex)
149       if (anIndex != *aFoundIndex)
150         break;
151     // compose the new name
152     std::ostringstream aNewName;
153     aNewName << aName << "_" << anIndex;
154     aName = aNewName.str();
155     // add new index
156     aFound->second.insert(anIndex);
157   }
158
159   if (isUnique) {
160     // name is unique
161     aName = theName;
162     addIndexedName(theName, theExistingNames);
163   }
164   return aName;
165 }
166
167 void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theImported)
168 {
169   std::map<std::string, std::set<int> > aNames;
170   collectOldNames(theDocument, theImported, aNames);
171
172   for (std::list<FeaturePtr>::iterator anIt = theImported.begin();
173        anIt != theImported.end(); ++anIt) {
174     // update name of feature
175     std::string aNewName = uniqueName((*anIt)->data()->name(), aNames);
176     (*anIt)->data()->setName(aNewName);
177     // update names of results
178     const std::list<ResultPtr>& aResults = (*anIt)->results();
179     for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
180          aRIt != aResults.end(); ++aRIt) {
181       aNewName = uniqueName((*aRIt)->data()->name(), aNames);
182       (*aRIt)->data()->setName(aNewName);
183     }
184   }
185 }