1 // Copyright (C) 2017-2022 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 "FeaturesPlugin_ImportResult.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_ResultGroup.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_ResultPart.h>
28 #include <ModelAPI_Tools.h>
29 #include <Events_InfoMessage.h>
30 #include <GeomAPI_ShapeExplorer.h>
32 void FeaturesPlugin_ImportResult::initAttributes()
34 data()->addAttribute(OBJECTS(), ModelAPI_AttributeSelectionList::typeId());
36 AttributePtr aFeaturesAttribute =
37 data()->addAttribute(FEATURES_ID(),
38 ModelAPI_AttributeRefList::typeId());
39 aFeaturesAttribute->setIsArgument(false);
41 ModelAPI_Session::get()->validators()->registerNotObligatory(
42 getKind(), FEATURES_ID());
45 void FeaturesPlugin_ImportResult::execute()
47 // Process groups/fields
48 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
49 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
51 // Remove previous groups/fields stored in RefList
52 //std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
53 //std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
54 const std::list<ObjectPtr>& anGroupList = aRefListOfGroups->list();
55 std::list<ObjectPtr>::const_iterator anGroupIt = anGroupList.begin();
56 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
57 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
59 document()->removeFeature(aFeature);
62 aRefListOfGroups->clear();
64 std::set<ResultPtr> aGlobalResultsCashed; // cash to speed up searching in all results selected
65 std::list<ResultGroupPtr> aGroups;
67 std::list<ResultPtr> aResults;
69 AttributeSelectionListPtr aList = selectionList(OBJECTS());
71 for (int aSelIndex = 0; aSelIndex < aList->size(); aSelIndex++)
73 AttributeSelectionPtr aSel = aList->value(aSelIndex);
75 ResultPtr aContext = aSel->context();
77 aResults.push_back (aContext);
80 FeaturePtr aFeature = aSel->contextFeature();
83 const std::list<ResultPtr>& aResList = aFeature->results();
84 aResults.assign (aResList.begin(), aResList.end());
89 std::list<ResultPtr>::iterator aResIter = aResults.begin();
90 for(; aResIter != aResults.end(); aResIter++)
92 GeomShapePtr aShape = (*aResIter)->shape();
93 if (!aShape.get() || aShape->isNull())
96 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
97 aResultBody->store(aShape);
98 aResultBody->loadFirstLevel(aShape, "ImportResult");
99 setResult(aResultBody, aResultIndex++);
101 std::set<ResultPtr> allResultsCashed; // cash to speed up searching in all results selected
102 std::list<ResultPtr> aLocalResults;
103 aLocalResults.push_back (*aResIter);
105 std::list<DocumentPtr> aDocuments; /// documents of Parts
106 aDocuments.push_back ((*aResIter)->document());
107 std::list<DocumentPtr>::iterator aDoc = aDocuments.begin();
108 for(; aDoc != aDocuments.end(); aDoc++)
111 int aGroupCount = (*aDoc)->size(ModelAPI_ResultGroup::group());
112 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex)
114 ResultGroupPtr aResultGroup =
115 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
116 (*aDoc)->object(ModelAPI_ResultGroup::group(), aGroupIndex));
118 if (!aResultGroup.get() || !aResultGroup->shape().get())
121 FeaturePtr aGroupFeature = (*aDoc)->feature(aResultGroup);
123 AttributeSelectionListPtr aSelectionList =
124 aGroupFeature->selectionList("group_list");
126 if (!ModelAPI_Tools::isInResults(aSelectionList,
128 allResultsCashed))// skip group not used in result
131 aGlobalResultsCashed.insert (allResultsCashed.begin(), allResultsCashed.end());
133 //Check: may be this group already exists in the list
134 bool anIsFound = false;
135 std::list<ResultGroupPtr>::iterator anIter = aGroups.begin();
136 for (; anIter != aGroups.end(); anIter++)
138 if (*anIter == aResultGroup)
145 aGroups.push_back (aResultGroup);
150 std::list<ResultGroupPtr>::iterator anIter = aGroups.begin();
151 for (; anIter != aGroups.end(); anIter++)
153 DocumentPtr aDoc = (*anIter)->document();
155 FeaturePtr aGroupFeature = aDoc->feature(*anIter);
157 AttributeSelectionListPtr aSelectionList =
158 aGroupFeature->selectionList("group_list");
160 std::shared_ptr<ModelAPI_Feature> aNewGroupFeature = addFeature("Group");
161 aNewGroupFeature->data()->setName(aGroupFeature->name());
163 AttributeSelectionListPtr aNewSelectionList = aNewGroupFeature->selectionList("group_list");
164 aNewSelectionList->setSelectionType (aSelectionList->selectionType());
165 GeomAPI_Shape::ShapeType aTypeOfShape =
166 GeomAPI_Shape::shapeTypeByStr(aSelectionList->selectionType());
168 for (int aLocalSelIndex = 0; aLocalSelIndex < aSelectionList->size(); aLocalSelIndex++) {
170 AttributeSelectionPtr aLocalSel = aSelectionList->value(aLocalSelIndex);
171 ResultPtr aLocalContext = aLocalSel->context();
172 ResultGroupPtr aLocalGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup> (aLocalContext);
173 if (aLocalGroup.get())
175 GeomShapePtr aLocalShape = aGroupFeature->firstResult()->shape();
176 GeomAPI_ShapeExplorer anExplo (aLocalShape, aTypeOfShape);
177 for (; anExplo.more(); anExplo.next())
179 GeomShapePtr anExploredShape = anExplo.current();
180 std::set<ResultPtr>::iterator aResultIter = aGlobalResultsCashed.begin();
181 for (; aResultIter != aGlobalResultsCashed.end(); aResultIter++)
183 GeomShapePtr aCashedShape = (*aResultIter)->shape();
184 if (aCashedShape->isSubShape(anExploredShape))
185 aNewSelectionList->append((*aResultIter), anExploredShape);
192 GeomShapePtr aLocalShape = aLocalSel->value();
194 if (aLocalContext.get() && aGlobalResultsCashed.count(aLocalContext))
195 aNewSelectionList->append(aLocalContext, aLocalShape);
200 removeResults(aResultIndex);
203 bool FeaturesPlugin_ValidatorImportResults::isValid(const AttributePtr& theAttribute,
204 const std::list<std::string>&, Events_InfoMessage& theError) const
206 AttributeSelectionListPtr aList =
207 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
208 if (aList->size() == 0) {
210 theError = "Please select sources.";
214 // store documents in the Part-fesatures in order to check
215 // the selection is located in the previous documents
216 std::map<DocumentPtr, int> aDocIndices;
217 DocumentPtr aRoot = ModelAPI_Session::get()->moduleDocument();
218 int aNum = aRoot->size(ModelAPI_Feature::group());
219 for (int a = 0; a < aNum; a++) {
220 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
221 aRoot->object(ModelAPI_Feature::group(), a));
222 if (aFeat.get() && aFeat->data() && aFeat->data()->isValid() && aFeat->getKind() == "Part" &&
223 aFeat->results().size()) {
224 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeat->firstResult());
225 if (aPart.get() && aPart->partDoc()) {
226 aDocIndices[aPart->partDoc()] = a;
230 int aThisIndex = aDocIndices[aList->owner()->document()];
231 for (int aSelIndex = 0; aSelIndex < aList->size(); aSelIndex++) {
232 AttributeSelectionPtr aSel = aList->value(aSelIndex);
233 ResultPtr aContext = aSel->context();
234 if (!aContext.get()) {
236 theError = "Empty context.";
240 GeomShapePtr aShape = aSel->value();
241 if (aShape.get() && !aShape->isNull() && !aShape->isSame(aContext->shape())) {
243 theError = "Import results does not support selection of sub-shapes";
247 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContext);
250 theError = "Only results may be selected.";
254 if (!aBody->shape()) {
256 theError = "Result is empty.";
260 int aBodyIndex = aDocIndices[aBody->document()];
261 if (aBodyIndex >= aThisIndex) {
262 theError = "The selected result must be located in earlier part.";
269 //============================================================================
270 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_ImportResult::addFeature(
273 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
275 data()->reflist(FEATURES_ID())->append(aNew);
276 // set as current also after it becomes sub to set correctly enabled for other subs
277 //document()->setCurrentFeature(aNew, false);
281 //=================================================================================================
282 int FeaturesPlugin_ImportResult::numberOfSubs(bool /*forTree*/) const
284 return data()->reflist(FEATURES_ID())->size(true);
287 //=================================================================================================
288 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_ImportResult::subFeature(const int theIndex,
291 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
292 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
296 //=================================================================================================
297 int FeaturesPlugin_ImportResult::subFeatureId(const int theIndex) const
299 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
300 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
301 std::list<ObjectPtr> aFeatures = aRefList->list();
302 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
303 int aResultIndex = 1; // number of the counted (created) features, started from 1
304 int aFeatureIndex = -1; // number of the not-empty features in the list
305 for (; anIt != aFeatures.end(); anIt++) {
308 if (aFeatureIndex == theIndex)
315 //=================================================================================================
316 bool FeaturesPlugin_ImportResult::isSub(ObjectPtr theObject) const
318 // check is this feature of result
319 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
321 return data()->reflist(FEATURES_ID())->isInList(aFeature);
325 //=================================================================================================
326 void FeaturesPlugin_ImportResult::removeFeature(
327 std::shared_ptr<ModelAPI_Feature> theFeature)
329 if (!data()->isValid())
331 AttributeRefListPtr aList = reflist(FEATURES_ID());
332 aList->remove(theFeature);