Salome HOME
Update unit-tests for SketchPlugin (part III)
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ExternalValidator.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        Model_ResultValidators.cpp
4 // Created:     27 Feb 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include "SketchPlugin_ExternalValidator.h"
8 #include "SketchPlugin_Feature.h"
9
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Result.h>
12 #include <ModelAPI_Tools.h>
13 #include <ModelAPI_AttributeRefAttr.h>
14
15 bool SketchPlugin_ExternalValidator::isValid(const AttributePtr& theAttribute,
16                                              const std::list<std::string>& theArguments,
17                                              std::string& theError) const
18 {
19   if (theArguments.size() != 1)
20     return true;
21
22   // ask whether the feature of the attribute is external
23   bool isAttributeExternal = isExternalAttribute(theAttribute);
24
25   // ask whether the feature of the attribute by parameter identifier is external
26   std::string aFrontArgument = theArguments.front();
27   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
28   bool isParameterExternal = isExternalAttribute(aFeature->attribute(aFrontArgument));
29
30   // it is not possible that both features, attribute and attribute in parameter, are external
31   if (isAttributeExternal && isParameterExternal) {
32     theError = "Both features, attribute and attribute in parameter, are external.";
33     return false;
34   }
35   return true;
36 }
37
38 bool SketchPlugin_ExternalValidator::isExternalAttribute(const AttributePtr& theAttribute) const
39 {
40   bool isExternal = false;
41   AttributeRefAttrPtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
42
43   if (anAttribute.get() != NULL) {
44     FeaturePtr anArgumentFeature = ModelAPI_Feature::feature(anAttribute->object());
45     if (anArgumentFeature.get() != NULL) {
46       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
47                                     std::dynamic_pointer_cast<SketchPlugin_Feature>(anArgumentFeature);
48       if (aSketchFeature.get() != NULL) {
49         isExternal = aSketchFeature->isExternal();
50       }
51     }
52   }
53   return isExternal;
54 }