Salome HOME
Validators return InfoMessage instead of string as an error
[modules/shaper.git] / src / GeomValidators / GeomValidators_FeatureKind.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomValidators_FeatureKind.cpp
4 // Created:     22 March 2015
5 // Author:      Natalia Ermolaeva
6
7 #include "GeomValidators_FeatureKind.h"
8
9 #include <Events_InfoMessage.h>
10
11 #include <ModelAPI_AttributeSelectionList.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_AttributeRefAttr.h>
14 #include <ModelAPI_AttributeReference.h>
15 #include <ModelAPI_Object.h>
16
17 #define DEBUG_EXTRUSION_INVALID_SKETCH
18 #ifdef DEBUG_EXTRUSION_INVALID_SKETCH
19   #include <ModelAPI_CompositeFeature.h>
20 #endif
21
22 bool GeomValidators_FeatureKind::isValid(const AttributePtr& theAttribute,
23                                       const std::list<std::string>& theArguments,
24                                       Events_InfoMessage& theError) const
25 {
26   bool isSketchEntities = true;
27   std::set<std::string> anEntityKinds;
28   std::string anEntityKindsStr;
29   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
30   for (; anIt != aLast; anIt++) {
31     anEntityKinds.insert(*anIt);
32     if (!anEntityKindsStr.empty())
33       anEntityKindsStr += ", ";
34     anEntityKindsStr += *anIt;
35   }
36
37   std::string anAttributeType = theAttribute->attributeType();
38   if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
39     AttributeSelectionListPtr aSelectionListAttr = 
40                       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
41     // all context objects should be sketch entities
42     for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize && isSketchEntities; i++) {
43       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
44       ObjectPtr anObject = aSelectAttr->context();
45       // a context of the selection attribute is a feature result. It can be a case when the result
46       // of the feature is null, e.g. the feature is modified and has not been executed yet.
47       // The validator returns an invalid result here. The case is an extrusion built on a sketch
48       // feature. A new sketch element creation leads to an empty result.
49       if (!anObject.get())
50         isSketchEntities = false;
51       else {
52         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
53         isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
54 #ifdef DEBUG_EXTRUSION_INVALID_SKETCH
55         CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
56         if (aComp.get() && aComp->numberOfSubs() == 1)
57           return false;
58 #endif
59       }
60     }
61   }
62   if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
63     AttributeSelectionPtr aSelectAttr = 
64                       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
65     ObjectPtr anObject = aSelectAttr->context();
66     // a context of the selection attribute is a feature result. It can be a case when the result
67     // of the feature is null, e.g. the feature is modified and has not been executed yet.
68     // The validator returns an invalid result here. The case is an extrusion built on a sketch
69     // feature. A new sketch element creation leads to an empty result.
70     if (!anObject.get())
71       isSketchEntities = false;
72     else {
73       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
74       isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
75     }
76   }
77   if (anAttributeType == ModelAPI_AttributeRefList::typeId()) {
78     AttributeRefListPtr aRefListAttr =
79       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
80     // all context objects should be sketch entities
81     for (int i = 0, aSize = aRefListAttr->size(); i < aSize && isSketchEntities; i++) {
82       ObjectPtr anObject = aRefListAttr->object(i);
83       // a context of the selection attribute is a feature result. It can be a case when the result
84       // of the feature is null, e.g. the feature is modified and has not been executed yet.
85       // The validator returns an invalid result here. The case is an extrusion built on a sketch
86       // feature. A new sketch element creation leads to an empty result.
87       if (!anObject.get())
88         isSketchEntities = false;
89       else {
90         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
91         isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
92       }
93     }
94   }
95   if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
96     std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
97                      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
98     isSketchEntities = false;
99     if (aRef->isObject()) {
100       ObjectPtr anObject = aRef->object();
101       if (anObject.get() != NULL) {
102         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
103         if (aFeature.get() != NULL)
104           isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
105       }
106     }
107   }
108   if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
109     AttributeReferencePtr aRefAttr = 
110                       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
111     ObjectPtr anObject = aRefAttr->value();
112     // a context of the selection attribute is a feature result. It can be a case when the result
113     // of the feature is null, e.g. the feature is modified and has not been executed yet.
114     // The validator returns an invalid result here. The case is an extrusion built on a sketch
115     // feature. A new sketch element creation leads to an empty result.
116     if (!anObject.get())
117       isSketchEntities = false;
118     else {
119       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
120       isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
121     }
122   }
123   if (!isSketchEntities) {
124     theError = "It refers to feature, which kind is not in the list: " + anEntityKindsStr;
125   }
126
127   return isSketchEntities;
128 }