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