Salome HOME
59dadceb5849031f8c4920fe06c0054b12d89412
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_ImportResult.cpp
1 // Copyright (C) 2017-2020  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 "FeaturesPlugin_ImportResult.h"
21
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>
27
28 void FeaturesPlugin_ImportResult::initAttributes()
29 {
30   data()->addAttribute(OBJECTS(), ModelAPI_AttributeSelectionList::typeId());
31 }
32
33 void FeaturesPlugin_ImportResult::execute()
34 {
35   AttributeSelectionListPtr aList = selectionList(OBJECTS());
36   int aResultIndex = 0;
37   for (int aSelIndex = 0; aSelIndex < aList->size(); aSelIndex++) {
38     AttributeSelectionPtr aSel = aList->value(aSelIndex);
39     ResultPtr aContext = aSel->context();
40     if (!aContext.get())
41       continue;
42     GeomShapePtr aShape = aContext->shape();
43     if (!aShape.get() || aShape->isNull())
44       continue;
45     std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
46     aResultBody->store(aShape);
47     aResultBody->loadFirstLevel(aShape, "ImportResult");
48     setResult(aResultBody, aResultIndex++);
49   }
50   removeResults(aResultIndex);
51 }
52
53 bool FeaturesPlugin_ValidatorImportResults::isValid(const AttributePtr& theAttribute,
54   const std::list<std::string>&, Events_InfoMessage& theError) const
55 {
56   AttributeSelectionListPtr aList =
57     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
58   if (aList->size() == 0) {
59     // LCOV_EXCL_START
60     theError = "Please select sources.";
61     return false;
62     // LCOV_EXCL_STOP
63   }
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;
77       }
78     }
79   }
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()) {
85       // LCOV_EXCL_START
86       theError = "Empty context.";
87       return false;
88       // LCOV_EXCL_STOP
89     }
90     GeomShapePtr aShape = aSel->value();
91     if (aShape.get() && !aShape->isNull() && !aShape->isSame(aContext->shape())) {
92       // LCOV_EXCL_START
93       theError = "Import results does not support selection of sub-shapes";
94       return false;
95       // LCOV_EXCL_STOP
96     }
97     ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContext);
98     if (!aBody.get()) {
99       // LCOV_EXCL_START
100       theError = "Only results may be selected.";
101       return false;
102       // LCOV_EXCL_STOP
103     }
104     if (!aBody->shape()) {
105       // LCOV_EXCL_START
106       theError = "Result is empty.";
107       return false;
108       // LCOV_EXCL_STOP
109     }
110     int aBodyIndex = aDocIndices[aBody->document()];
111     if (aBodyIndex >= aThisIndex) {
112       theError = "The selected result must be located in earlier part.";
113       return false;
114     }
115   }
116   return true;
117 }