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