Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomValidators / GeomValidators_FeatureKind.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "GeomValidators_FeatureKind.h"
21
22 #include <Events_InfoMessage.h>
23
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_AttributeRefAttr.h>
27 #include <ModelAPI_AttributeReference.h>
28 #include <ModelAPI_Object.h>
29
30 #define DEBUG_EXTRUSION_INVALID_SKETCH
31 #ifdef DEBUG_EXTRUSION_INVALID_SKETCH
32   #include <ModelAPI_CompositeFeature.h>
33 #endif
34
35 bool GeomValidators_FeatureKind::isValid(const AttributePtr& theAttribute,
36                                       const std::list<std::string>& theArguments,
37                                       Events_InfoMessage& theError) const
38 {
39   bool isSketchEntities = true;
40   std::set<std::string> anEntityKinds;
41   std::string anEntityKindsStr;
42   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
43   for (; anIt != aLast; anIt++) {
44     anEntityKinds.insert(*anIt);
45     if (!anEntityKindsStr.empty())
46       anEntityKindsStr += ", ";
47     anEntityKindsStr += *anIt;
48   }
49
50   std::string anAttributeType = theAttribute->attributeType();
51   if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
52     AttributeSelectionListPtr aSelectionListAttr =
53                       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
54     // all context objects should be sketch entities
55     for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize && isSketchEntities; i++) {
56       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
57       ObjectPtr anObject = aSelectAttr->context();
58       // a context of the selection attribute is a feature result. It can be a case when the result
59       // of the feature is null, e.g. the feature is modified and has not been executed yet.
60       // The validator returns an invalid result here. The case is an extrusion built on a sketch
61       // feature. A new sketch element creation leads to an empty result.
62       if (!anObject.get())
63         isSketchEntities = false;
64       else {
65         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
66         isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
67 #ifdef DEBUG_EXTRUSION_INVALID_SKETCH
68         CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
69         if (aComp.get() && aComp->numberOfSubs() == 1)
70           return false;
71 #endif
72       }
73     }
74   }
75   if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
76     AttributeSelectionPtr aSelectAttr =
77                       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
78     ObjectPtr anObject = aSelectAttr->context();
79     // a context of the selection attribute is a feature result. It can be a case when the result
80     // of the feature is null, e.g. the feature is modified and has not been executed yet.
81     // The validator returns an invalid result here. The case is an extrusion built on a sketch
82     // feature. A new sketch element creation leads to an empty result.
83     if (!anObject.get())
84       isSketchEntities = false;
85     else {
86       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
87       isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
88     }
89   }
90   if (anAttributeType == ModelAPI_AttributeRefList::typeId()) {
91     AttributeRefListPtr aRefListAttr =
92       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
93     // all context objects should be sketch entities
94     for (int i = 0, aSize = aRefListAttr->size(); i < aSize && isSketchEntities; i++) {
95       ObjectPtr anObject = aRefListAttr->object(i);
96       // a context of the selection attribute is a feature result. It can be a case when the result
97       // of the feature is null, e.g. the feature is modified and has not been executed yet.
98       // The validator returns an invalid result here. The case is an extrusion built on a sketch
99       // feature. A new sketch element creation leads to an empty result.
100       if (!anObject.get())
101         isSketchEntities = false;
102       else {
103         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
104         isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
105       }
106     }
107   }
108   if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
109     std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
110                      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
111     isSketchEntities = false;
112     if (aRef->isObject()) {
113       ObjectPtr anObject = aRef->object();
114       if (anObject.get() != NULL) {
115         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
116         if (aFeature.get() != NULL)
117           isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
118       }
119     }
120   }
121   if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
122     AttributeReferencePtr aRefAttr =
123                       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
124     ObjectPtr anObject = aRefAttr->value();
125     // a context of the selection attribute is a feature result. It can be a case when the result
126     // of the feature is null, e.g. the feature is modified and has not been executed yet.
127     // The validator returns an invalid result here. The case is an extrusion built on a sketch
128     // feature. A new sketch element creation leads to an empty result.
129     if (!anObject.get())
130       isSketchEntities = false;
131     else {
132       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
133       isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
134     }
135   }
136   if (!isSketchEntities) {
137     theError = "It refers to feature, which kind is not in the list: " + anEntityKindsStr;
138   }
139
140   return isSketchEntities;
141 }