Salome HOME
Issue #1367: Fill feature.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_ValidatorTransform.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "FeaturesPlugin_ValidatorTransform.h"
4
5 #include "ModelAPI_AttributeSelectionList.h"
6 #include "ModelAPI_ResultPart.h"
7 #include "ModelAPI_ResultBody.h"
8 #include "ModelAPI_ResultCompSolid.h"
9 #include "ModelAPI_Session.h"
10
11 bool FeaturesPlugin_ValidatorTransform::isValid(const AttributePtr& theAttribute,
12                                                 const std::list<std::string>& theArguments,
13                                                 std::string& theError) const
14 {
15   bool aValid = true;
16   std::string anAttributeType = theAttribute->attributeType();
17   if (anAttributeType != ModelAPI_AttributeSelectionList::typeId()) {
18     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
19     return false;
20   }
21
22   std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
23                            std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
24
25   DocumentPtr aDocument = theAttribute->owner()->document();
26   SessionPtr aMgr = ModelAPI_Session::get();
27   bool isPartSetDocument = aDocument == aMgr->moduleDocument();
28
29   std::string anErrorGroupName;
30   for(int i = 0; i < aCurSelList->size() && aValid; i++) {
31     std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
32     ResultPtr aResult = aSelAttribute->context();
33     if (isPartSetDocument) // PartSet document: Result Part is valid
34       aValid = aResult->groupName() == ModelAPI_ResultPart::group();
35     else { // Part document: Result CompSolid is valid
36       aValid = aResult->groupName() == ModelAPI_ResultBody::group();
37       if (aValid) {
38         ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
39         aValid = aComp.get() != NULL;
40       }
41     }
42     if (!aValid)
43       anErrorGroupName = aResult->groupName();
44   }
45   if (!aValid) {
46     std::string aResultGroupName = isPartSetDocument ? ModelAPI_ResultPart::group()
47                                                      : ModelAPI_ResultBody::group();
48     theError = "Objects from the " + aResultGroupName  +
49                 " group can be selected in the " + aDocument->kind() +
50                 "document, but an objects from the " + anErrorGroupName +
51                 " group is selected.";
52   }
53   return aValid;
54 }