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