Salome HOME
Task #3059 implementation: When “move to the end” of a group, propose to the user...
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_Group.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 "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 <sstream>
30
31 CollectionPlugin_Group::CollectionPlugin_Group()
32 {
33 }
34
35 void CollectionPlugin_Group::initAttributes()
36 {
37   AttributeSelectionListPtr aList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(
38     data()->addAttribute(LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
39   aList->setWholeResultAllowed(true); // allow to select the whole result
40 }
41
42 void CollectionPlugin_Group::execute()
43 {
44   if (results().empty() || firstResult()->isDisabled()) { // just create result if not exists
45     ResultPtr aGroup = document()->createGroup(data());
46     setResult(aGroup);
47   }
48 }
49
50 // returns name with suffix, not existing in the existing set
51 static std::string findName(
52   const std::string theOrigin, int& theSuffix, std::set<std::string>& theExisting)
53 {
54   std::string aRes;
55   do {
56     std::ostringstream aName;
57     aName<<theOrigin<<"_"<<theSuffix;
58     aRes = aName.str();
59     theSuffix++;
60   } while(theExisting.count(aRes));
61   theExisting.insert(aRes);
62   return aRes;
63
64 }
65
66 bool CollectionPlugin_Group::customAction(const std::string& theActionId)
67 {
68   if (theActionId == "split") {
69     DocumentPtr aDoc = document();
70     // collect all existing names of features to give unique names
71     std::set<std::string> aFeatNames, aResNames;
72     std::list<FeaturePtr> allFeat = aDoc->allFeatures();
73     std::list<FeaturePtr>::iterator allFeatIter = allFeat.begin();
74     for(; allFeatIter != allFeat.end(); allFeatIter++) {
75       FeaturePtr aFeat = *allFeatIter;
76       if (aFeat->data().get() && aFeat->data()->isValid()) {
77         aFeatNames.insert(aFeat->name());
78         if (aFeat->getKind() == ID() && aFeat->data().get() && aFeat->data()->isValid()) {
79            std::list<ResultPtr>::const_iterator aRess = aFeat->results().cbegin();
80            for(; aRess != aFeat->results().cend(); aRess++) {
81              ResultPtr aRes = *aRess;
82              if (aRes->data().get() && aRes->data()->isValid()) {
83                aResNames.insert(aRes->data()->name());
84              }
85            }
86         }
87       }
88     }
89
90     AttributeSelectionListPtr aList = selectionList(LIST_ID());
91     std::set<int> aRemoved;
92     bool aStay = false; // to indicate that the good attribute found stays in the list
93     int anIndex = 1; // index of the name assigned to group-feature and result
94     // added in the order: 3 2 1 orig=0, so, keep the results to give names later
95     std::list<ObjectPtr> aResults;
96     for(int aNext = aList->size() - 1; aNext >= 0; aNext--) {
97       AttributeSelectionPtr anOldAttr = aList->value(aNext);
98       if (anOldAttr->isInvalid() || !anOldAttr->context().get()) {// remove invalids
99         aRemoved.insert(aNext);
100         continue;
101       }
102       if (!aStay) {
103         aStay = true;
104         continue;
105       }
106       aRemoved.insert(aNext);
107       FeaturePtr aNew = aDoc->addFeature(ID(), false);
108       AttributeSelectionListPtr aNewList = aNew->selectionList(LIST_ID());
109       aNewList->setSelectionType(aList->selectionType());
110       aNewList->append(anOldAttr->contextObject(), anOldAttr->value());
111       aResults.push_front(aNew); // to keep the order
112     }
113     aResults.push_back(data()->owner());
114     // remove all selections except the first
115     aList->remove(aRemoved);
116     // set names
117     if (aResults.size() > 1) { // rename if there are new groups appeared only
118       std::list<ObjectPtr>::iterator aResIter = aResults.begin();
119       for(int aSuffix = 1; aResIter != aResults.end(); aResIter++) {
120         FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aResIter);
121         aFeat->data()->setName(findName(name(), aSuffix, aFeatNames));
122         if (!aFeat->results().empty() && !results().empty()) {
123           int aResSuf = aSuffix - 1;
124           std::string aResName = findName(firstResult()->data()->name(), aResSuf, aResNames);
125           aFeat->firstResult()->data()->setName(aResName);
126           // set the same color of result as in origin
127           if (firstResult()->data()->attribute(ModelAPI_Result::COLOR_ID()).get()) {
128             AttributeIntArrayPtr aSourceColor =
129               firstResult()->data()->intArray(ModelAPI_Result::COLOR_ID());
130             if (aSourceColor.get() && aSourceColor->size()) {
131               AttributeIntArrayPtr aDestColor =
132                 aFeat->firstResult()->data()->intArray(ModelAPI_Result::COLOR_ID());
133               aDestColor->setSize(aSourceColor->size());
134               for(int a = 0; a < aSourceColor->size(); a++)
135                 aDestColor->setValue(a, aSourceColor->value(a));
136             }
137           }
138         }
139       }
140       // remove also filters if split performed
141       FiltersFeaturePtr aFilters = aList->filters();
142       if (aFilters.get()) {
143         std::list<std::string> aFiltersList = aFilters->filters();
144         std::list<std::string>::iterator aFilterName = aFiltersList.begin();
145         for(; aFilterName != aFiltersList.end(); aFilterName++) {
146           aFilters->removeFilter(*aFilterName);
147         }
148       }
149     }
150   }
151   return true;
152 }