1 // Copyright (C) 2014-2020 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 "CollectionPlugin_Group.h"
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>
32 CollectionPlugin_Group::CollectionPlugin_Group()
36 void CollectionPlugin_Group::initAttributes()
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
43 void CollectionPlugin_Group::execute()
45 if (results().empty() || firstResult()->isDisabled()) { // just create result if not exists
46 ResultPtr aGroup = document()->createGroup(data());
51 // returns name with suffix, not existing in the existing set
52 static std::string findName(
53 const std::string theOrigin, int& theSuffix, std::set<std::string>& theExisting)
57 std::ostringstream aName;
58 aName<<theOrigin<<"_"<<theSuffix;
61 } while(theExisting.count(aRes));
62 theExisting.insert(aRes);
67 static int getDepth(AttributeSelectionPtr& theAttr) {
68 if (theAttr->contextFeature().get())
70 ResultBodyPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theAttr->context());
73 for(aRes = ModelAPI_Tools::bodyOwner(aRes); aRes.get(); aRes = ModelAPI_Tools::bodyOwner(aRes))
80 bool CollectionPlugin_Group::customAction(const std::string& theActionId)
82 if (theActionId == "split") {
83 DocumentPtr aDoc = document();
84 // collect all existing names of features to give unique names
85 std::set<std::string> 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());
103 // searching for the depth on the results, minimal for all contextes:
104 // groups will be collected at this depth; the root is 0
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)
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++) {
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);
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
145 if (aFatherGroup.size() < (size_t)aList->size() && aFatherGroup.size() != 1) // already good
147 if (aFatherGroup.size() == (size_t)aList->size()) // no sence to iterate further
149 if (aFatherGroup.size() == 1 && aList->size() > 1 && aCurrentDepth == aMinDepth)
150 aSplitAnyway = true; // split anyway, better that just move
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);
167 int aFGroup = aFatherGroup[aFathers[anOldAttr->contextObject()]];
168 if (!aSplitAnyway && aFGroup == 0) // to stay in this first group
171 aRemoved.insert(aNext);
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++)
183 AttributeSelectionListPtr aNewList = aNew->selectionList(LIST_ID());
184 aNewList->setSelectionType(aList->selectionType());
185 aNewList->append(anOldAttr->contextObject(), anOldAttr->value());
187 aResults.push_back(std::dynamic_pointer_cast<ModelAPI_Feature>(data()->owner()));
188 // remove all selections except the first
189 aList->remove(aRemoved);
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);
196 aFeat->data()->setName(findName(name(), aSuffix, aFeatNames));
197 if (!aFeat->results().empty() && !results().empty()) {
198 int aResSuf = aSuffix - 1;
199 std::string aResName = findName(firstResult()->data()->name(), aResSuf, aResNames);
200 aFeat->firstResult()->data()->setName(aResName);
201 ModelAPI_Tools::copyVisualizationAttrs(firstResult(), aFeat->firstResult());
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);