1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomValidators_FeatureKind.cpp
4 // Created: 22 March 2015
5 // Author: Natalia Ermolaeva
7 #include "GeomValidators_FeatureKind.h"
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_AttributeRefList.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_Object.h>
14 bool GeomValidators_FeatureKind::isValid(const AttributePtr& theAttribute,
15 const std::list<std::string>& theArguments,
16 std::string& theError) const
18 bool isSketchEntities = true;
19 std::set<std::string> anEntityKinds;
20 std::string anEntityKindsStr;
21 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
22 for (; anIt != aLast; anIt++) {
23 anEntityKinds.insert(*anIt);
24 if (!anEntityKindsStr.empty())
25 anEntityKindsStr += ", ";
26 anEntityKindsStr += *anIt;
29 std::string anAttributeType = theAttribute->attributeType();
30 if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
31 AttributeSelectionListPtr aSelectionListAttr =
32 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
33 // all context objects should be sketch entities
34 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize && isSketchEntities; i++) {
35 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
36 ObjectPtr anObject = aSelectAttr->context();
37 // a context of the selection attribute is a feature result. It can be a case when the result
38 // of the feature is null, e.g. the feature is modified and has not been executed yet.
39 // The validator returns an invalid result here. The case is an extrusion built on a sketch
40 // feature. A new sketch element creation leads to an empty result.
42 isSketchEntities = false;
44 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
45 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
49 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
50 AttributeSelectionPtr aSelectAttr =
51 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
52 ObjectPtr anObject = aSelectAttr->context();
53 // a context of the selection attribute is a feature result. It can be a case when the result
54 // of the feature is null, e.g. the feature is modified and has not been executed yet.
55 // The validator returns an invalid result here. The case is an extrusion built on a sketch
56 // feature. A new sketch element creation leads to an empty result.
58 isSketchEntities = false;
60 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
61 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
64 if (anAttributeType == ModelAPI_AttributeRefList::typeId()) {
65 AttributeRefListPtr aRefListAttr =
66 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
67 // all context objects should be sketch entities
68 for (int i = 0, aSize = aRefListAttr->size(); i < aSize && isSketchEntities; i++) {
69 ObjectPtr anObject = aRefListAttr->object(i);
70 // a context of the selection attribute is a feature result. It can be a case when the result
71 // of the feature is null, e.g. the feature is modified and has not been executed yet.
72 // The validator returns an invalid result here. The case is an extrusion built on a sketch
73 // feature. A new sketch element creation leads to an empty result.
75 isSketchEntities = false;
77 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
78 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
82 if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
83 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
84 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
85 isSketchEntities = false;
86 if (aRef->isObject()) {
87 ObjectPtr anObject = aRef->object();
88 if (anObject.get() != NULL) {
89 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
90 if (aFeature.get() != NULL)
91 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
95 if (!isSketchEntities) {
96 theError = "It refers to feature, which kind is not in the list: " + anEntityKindsStr;
99 return isSketchEntities;