Salome HOME
#1721 Selecting arcs in mirror constraint is too long and removes other edges
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Validators.cpp
1 /*
2  * Parameters_VariableValidator.cpp
3  *
4  *  Created on: Apr 9, 2015
5  *      Author: sbh
6  */
7
8 #include <ParametersPlugin_Validators.h>
9
10 #include <ParametersPlugin_Parameter.h>
11
12 #include <Events_InfoMessage.h>
13
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_ResultParameter.h>
17 #include <ModelAPI_Tools.h>
18 #include <ModelAPI_Expression.h>
19
20 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
21 {
22 }
23
24 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
25 {
26 }
27
28 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
29                                                  const std::list<std::string>& theArguments,
30                                                  Events_InfoMessage& theError) const
31 {
32   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
33   if (!aStrAttr->isInitialized()) {
34     theError = "Attribute \"%1\" is not initialized.";
35     theError.arg(aStrAttr->id());
36     return false;
37   }
38   bool isEmptyExpr = aStrAttr->value().empty();
39   if (isEmptyExpr) {
40     theError = "Attribute \"%1\" value is empty.";
41     theError.arg(aStrAttr->id());
42     return false;
43   }
44   if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
45     theError = "Incorrect variable name.";
46     return false;
47   } 
48   if (!isUnique(theAttribute, aStrAttr->value())) {
49     theError = "Variable name is not unique.";
50     return false;
51   }
52   return true;
53 }
54
55 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
56                                                   const std::string& theString) const
57 {
58   DocumentPtr aDocument = theAttribute->owner()->document();
59   for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
60        anIndex < aSize; ++anIndex) {
61     ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
62     if (aParamObj->data()->name() != theString)
63       continue;
64     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
65     if (!aParam.get())
66       continue;
67     FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
68     if (aFeature == theAttribute->owner())
69       continue;
70     return false;
71   }
72   return true;
73 }
74
75 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
76 {
77
78 }
79
80 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
81 {
82
83 }
84
85 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
86                                                    const std::list<std::string>& theArguments,
87                                                    Events_InfoMessage& theError) const
88 {
89   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
90   ResultParameterPtr aParam =
91       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
92
93   AttributeStringPtr aStrAttr =
94       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
95   if (!aStrAttr->isInitialized()) {
96     theError = "Attribute \"%1\" is not initialized.";
97     theError.arg(aStrAttr->id());
98     return false;
99   }
100   bool isEmptyExpr = aStrAttr->value().empty();
101   if (isEmptyExpr) {
102     theError = "Expression is empty.";
103     return false;
104   }
105
106   if (!aParam.get()) {
107     theError = "Result is empty.";
108     return false;
109   }
110
111   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
112   return theError.empty();
113 }