1 // Copyright (C) 2017-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 "FeaturesPlugin_ImportResult.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_ResultPart.h>
26 #include <Events_InfoMessage.h>
28 void FeaturesPlugin_ImportResult::initAttributes()
30 data()->addAttribute(OBJECTS(), ModelAPI_AttributeSelectionList::typeId());
33 void FeaturesPlugin_ImportResult::execute()
35 AttributeSelectionListPtr aList = selectionList(OBJECTS());
37 for (int aSelIndex = 0; aSelIndex < aList->size(); aSelIndex++) {
38 AttributeSelectionPtr aSel = aList->value(aSelIndex);
39 ResultPtr aContext = aSel->context();
42 GeomShapePtr aShape = aContext->shape();
43 if (!aShape.get() || aShape->isNull())
45 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
46 aResultBody->store(aShape);
47 aResultBody->loadFirstLevel(aShape, "ImportResult");
48 setResult(aResultBody, aResultIndex++);
50 removeResults(aResultIndex);
53 bool FeaturesPlugin_ValidatorImportResults::isValid(const AttributePtr& theAttribute,
54 const std::list<std::string>&, Events_InfoMessage& theError) const
56 AttributeSelectionListPtr aList =
57 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
58 if (aList->size() == 0) {
60 theError = "Please select sources.";
64 // store documents in the Part-fesatures in order to check
65 // the selection is located in the previous documents
66 std::map<DocumentPtr, int> aDocIndices;
67 DocumentPtr aRoot = ModelAPI_Session::get()->moduleDocument();
68 int aNum = aRoot->size(ModelAPI_Feature::group());
69 for (int a = 0; a < aNum; a++) {
70 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
71 aRoot->object(ModelAPI_Feature::group(), a));
72 if (aFeat.get() && aFeat->data() && aFeat->data()->isValid() && aFeat->getKind() == "Part" &&
73 aFeat->results().size()) {
74 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeat->firstResult());
75 if (aPart.get() && aPart->partDoc()) {
76 aDocIndices[aPart->partDoc()] = a;
80 int aThisIndex = aDocIndices[aList->owner()->document()];
81 for (int aSelIndex = 0; aSelIndex < aList->size(); aSelIndex++) {
82 AttributeSelectionPtr aSel = aList->value(aSelIndex);
83 ResultPtr aContext = aSel->context();
84 if (!aContext.get()) {
86 theError = "Empty context.";
90 GeomShapePtr aShape = aSel->value();
91 if (aShape.get() && !aShape->isNull() && !aShape->isSame(aContext->shape())) {
93 theError = "Import results does not support selection of sub-shapes";
97 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContext);
100 theError = "Only results may be selected.";
104 if (!aBody->shape()) {
106 theError = "Result is empty.";
110 int aBodyIndex = aDocIndices[aBody->document()];
111 if (aBodyIndex >= aThisIndex) {
112 theError = "The selected result must be located in earlier part.";