Salome HOME
updated copyright message
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_Group.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 "CollectionPlugin_Group.h"
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeIntArray.h>
28 #include <ModelAPI_ResultGroup.h>
29 #include <ModelAPI_Tools.h>
30 #include <sstream>
31
32 CollectionPlugin_Group::CollectionPlugin_Group()
33 {
34 }
35
36 void CollectionPlugin_Group::initAttributes()
37 {
38   AttributeSelectionListPtr aList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(
39     data()->addAttribute(LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
40   aList->setWholeResultAllowed(true); // allow to select the whole result
41 }
42
43 void CollectionPlugin_Group::execute()
44 {
45   if (results().empty() || firstResult()->isDisabled()) { // just create result if not exists
46     ResultPtr aGroup = document()->createGroup(data());
47     setResult(aGroup);
48   }
49 }
50
51 // returns name with suffix, not existing in the existing set
52 static std::wstring findName(
53   const std::wstring theOrigin, int& theSuffix, std::set<std::wstring>& theExisting)
54 {
55   std::wstring aRes;
56   do {
57     std::wostringstream aName;
58     aName<<theOrigin<<"_"<<theSuffix;
59     aRes = aName.str();
60     theSuffix++;
61   } while(theExisting.count(aRes));
62   theExisting.insert(aRes);
63   return aRes;
64
65 }
66
67 static int getDepth(AttributeSelectionPtr& theAttr) {
68   if (theAttr->contextFeature().get())
69     return 0;
70   ResultBodyPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theAttr->context());
71   if (aRes.get()) {
72     int aDepth = 0;
73     for(aRes = ModelAPI_Tools::bodyOwner(aRes); aRes.get(); aRes = ModelAPI_Tools::bodyOwner(aRes))
74       aDepth++;
75     return aDepth;
76   }
77   return 0;
78 }
79
80 bool CollectionPlugin_Group::customAction(const std::string& theActionId)
81 {
82   if (theActionId == "split") {
83     DocumentPtr aDoc = document();
84     // collect all existing names of features to give unique names
85     std::set<std::wstring> aFeatNames, aResNames;
86     std::list<FeaturePtr> allFeat = aDoc->allFeatures();
87     std::list<FeaturePtr>::iterator allFeatIter = allFeat.begin();
88     for(; allFeatIter != allFeat.end(); allFeatIter++) {
89       FeaturePtr aFeat = *allFeatIter;
90       if (aFeat->data().get() && aFeat->data()->isValid()) {
91         aFeatNames.insert(aFeat->name());
92         if (aFeat->getKind() == ID() && aFeat->data().get() && aFeat->data()->isValid()) {
93            std::list<ResultPtr>::const_iterator aRess = aFeat->results().cbegin();
94            for(; aRess != aFeat->results().cend(); aRess++) {
95              ResultPtr aRes = *aRess;
96              if (aRes->data().get() && aRes->data()->isValid()) {
97                aResNames.insert(aRes->data()->name());
98              }
99            }
100         }
101       }
102     }
103     // searching for the depth on the results, minimal for all contextes:
104     // groups will be collected at this depth; the root is 0
105     int aMinDepth = -1;
106     std::map<ObjectPtr, int> aStoredDepth;
107     AttributeSelectionListPtr aList = selectionList(LIST_ID());
108     for (int a = aList->size() - 1; a >= 0; a--) {
109       AttributeSelectionPtr anAttr = aList->value(a);
110       if (!anAttr->isInvalid() && anAttr->contextObject().get()) {
111         int aDepth = getDepth(anAttr);
112         aStoredDepth[anAttr->contextObject()] = aDepth;
113         if (aMinDepth == -1 || aDepth < aMinDepth)
114           aMinDepth = aDepth;
115       }
116     }
117     // get common fathers at the minimal depth and set index of group to them
118     std::map<ObjectPtr, ObjectPtr> aFathers;
119     std::map<ObjectPtr, int> aFatherGroup;
120     bool aSplitAnyway = false;
121     for(int aCurrentDepth = 0; aCurrentDepth <= aMinDepth; aCurrentDepth++) {
122       aFathers.clear();
123       aFatherGroup.clear();
124       for (int a = aList->size() - 1; a >= 0; a--) {
125         // start from zero (this group index), then from the end - as in the next iteration
126         AttributeSelectionPtr anAttr = aList->value(a);
127         if (!anAttr->isInvalid() && anAttr->contextObject().get()) {
128           ObjectPtr anObj = anAttr->contextObject();
129           int aDepth = aStoredDepth[anObj];
130           if (!aSplitAnyway && aDepth > aCurrentDepth) {
131             ResultBodyPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObj);
132             while (aDepth > aCurrentDepth) {
133               aRes = ModelAPI_Tools::bodyOwner(aRes);
134               aDepth--;
135             }
136             anObj = aRes;
137           }
138           aFathers[anAttr->contextObject()] = anObj;
139           if (aFatherGroup.find(anObj) == aFatherGroup.end()) {
140             int aSize = (int)aFatherGroup.size();
141             aFatherGroup[anObj] = aSize; // the first is zero
142           }
143         }
144       }
145       if (aFatherGroup.size() < (size_t)aList->size() && aFatherGroup.size() != 1)  // already good
146         break;
147       if (aFatherGroup.size() == (size_t)aList->size()) // no sence to iterate further
148         break;
149       if (aFatherGroup.size() == 1 && aList->size() > 1 && aCurrentDepth == aMinDepth)
150         aSplitAnyway = true;  // split anyway, better that just move
151     }
152
153     std::set<int> aRemoved;
154     bool aStay = false; // to indicate that the good attribute found stays in the list
155     // added in the order: 3 2 1 orig=0, so, keep the results to give names later
156     std::list<FeaturePtr> aResults;
157     for(int aNext = aList->size() - 1; aNext >= 0; aNext--) {
158       AttributeSelectionPtr anOldAttr = aList->value(aNext);
159       if (anOldAttr->isInvalid() || !anOldAttr->contextObject().get()) {// remove invalids
160         aRemoved.insert(aNext);
161         continue;
162       }
163       if (!aStay) {
164         aStay = true;
165         continue;
166       }
167       int aFGroup = aFatherGroup[aFathers[anOldAttr->contextObject()]];
168       if (!aSplitAnyway && aFGroup == 0) // to stay in this first group
169         continue;
170
171       aRemoved.insert(aNext);
172       FeaturePtr aNew;
173       if (aSplitAnyway || aFGroup > (int)aResults.size()) {
174         aNew = aDoc->addFeature(ID(), false);
175         aResults.push_front(aNew); // to keep the order
176       } else { // appending in already created new result
177         std::list<FeaturePtr>::reverse_iterator aResIter = aResults.rbegin();
178         for (; aFGroup > 1; aResIter++)
179           aFGroup--;
180         aNew = *aResIter;
181       }
182
183       AttributeSelectionListPtr aNewList = aNew->selectionList(LIST_ID());
184       aNewList->setSelectionType(aList->selectionType());
185       aNewList->append(anOldAttr->contextObject(), anOldAttr->value());
186     }
187     aResults.push_back(std::dynamic_pointer_cast<ModelAPI_Feature>(data()->owner()));
188     // remove all selections except the first
189     aList->remove(aRemoved);
190     // set names
191     if (aResults.size() > 1) { // rename if there are new groups appeared only
192       std::list<FeaturePtr>::iterator aResIter = aResults.begin();
193       for(int aSuffix = 1; aResIter != aResults.end(); aResIter++) {
194         FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aResIter);
195         aFeat->execute();
196         aFeat->data()->setName(findName(name(), aSuffix, aFeatNames));
197         if (!aFeat->results().empty() && !results().empty()) {
198           int aResSuf = aSuffix - 1;
199           std::wstring aResName = findName(firstResult()->data()->name(), aResSuf, aResNames);
200           aFeat->firstResult()->data()->setName(aResName);
201           ModelAPI_Tools::copyVisualizationAttrs(firstResult(), aFeat->firstResult());
202         }
203       }
204       // remove also filters if split performed
205       FiltersFeaturePtr aFilters = aList->filters();
206       if (aFilters.get()) {
207         std::list<std::string> aFiltersList = aFilters->filters();
208         std::list<std::string>::iterator aFilterName = aFiltersList.begin();
209         for(; aFilterName != aFiltersList.end(); aFilterName++) {
210           aFilters->removeFilter(*aFilterName);
211         }
212       }
213     }
214   }
215   return true;
216 }