1 // Copyright (C) 2014-2019 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 bool CollectionPlugin_Group::customAction(const std::string& theActionId)
69 if (theActionId == "split") {
70 DocumentPtr aDoc = document();
71 // collect all existing names of features to give unique names
72 std::set<std::string> aFeatNames, aResNames;
73 std::list<FeaturePtr> allFeat = aDoc->allFeatures();
74 std::list<FeaturePtr>::iterator allFeatIter = allFeat.begin();
75 for(; allFeatIter != allFeat.end(); allFeatIter++) {
76 FeaturePtr aFeat = *allFeatIter;
77 if (aFeat->data().get() && aFeat->data()->isValid()) {
78 aFeatNames.insert(aFeat->name());
79 if (aFeat->getKind() == ID() && aFeat->data().get() && aFeat->data()->isValid()) {
80 std::list<ResultPtr>::const_iterator aRess = aFeat->results().cbegin();
81 for(; aRess != aFeat->results().cend(); aRess++) {
82 ResultPtr aRes = *aRess;
83 if (aRes->data().get() && aRes->data()->isValid()) {
84 aResNames.insert(aRes->data()->name());
91 AttributeSelectionListPtr aList = selectionList(LIST_ID());
92 std::set<int> aRemoved;
93 bool aStay = false; // to indicate that the good attribute found stays in the list
94 int anIndex = 1; // index of the name assigned to group-feature and result
95 // added in the order: 3 2 1 orig=0, so, keep the results to give names later
96 std::list<ObjectPtr> aResults;
97 for(int aNext = aList->size() - 1; aNext >= 0; aNext--) {
98 AttributeSelectionPtr anOldAttr = aList->value(aNext);
99 if (anOldAttr->isInvalid() || !anOldAttr->context().get()) {// remove invalids
100 aRemoved.insert(aNext);
107 aRemoved.insert(aNext);
108 FeaturePtr aNew = aDoc->addFeature(ID(), false);
109 AttributeSelectionListPtr aNewList = aNew->selectionList(LIST_ID());
110 aNewList->setSelectionType(aList->selectionType());
111 aNewList->append(anOldAttr->contextObject(), anOldAttr->value());
113 aResults.push_front(aNew); // to keep the order
115 aResults.push_back(data()->owner());
116 // remove all selections except the first
117 aList->remove(aRemoved);
119 if (aResults.size() > 1) { // rename if there are new groups appeared only
120 std::list<ObjectPtr>::iterator aResIter = aResults.begin();
121 for(int aSuffix = 1; aResIter != aResults.end(); aResIter++) {
122 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aResIter);
123 aFeat->data()->setName(findName(name(), aSuffix, aFeatNames));
124 if (!aFeat->results().empty() && !results().empty()) {
125 int aResSuf = aSuffix - 1;
126 std::string aResName = findName(firstResult()->data()->name(), aResSuf, aResNames);
127 aFeat->firstResult()->data()->setName(aResName);
128 ModelAPI_Tools::copyVisualizationAttrs(firstResult(), aFeat->firstResult());
131 // remove also filters if split performed
132 FiltersFeaturePtr aFilters = aList->filters();
133 if (aFilters.get()) {
134 std::list<std::string> aFiltersList = aFilters->filters();
135 std::list<std::string>::iterator aFilterName = aFiltersList.begin();
136 for(; aFilterName != aFiltersList.end(); aFilterName++) {
137 aFilters->removeFilter(*aFilterName);