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_AttributeReference.h>
13 #include <ModelAPI_Object.h>
15 #define DEBUG_EXTRUSION_INVALID_SKETCH
16 #ifdef DEBUG_EXTRUSION_INVALID_SKETCH
17 #include <ModelAPI_CompositeFeature.h>
20 bool GeomValidators_FeatureKind::isValid(const AttributePtr& theAttribute,
21 const std::list<std::string>& theArguments,
22 std::string& theError) const
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;
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.
48 isSketchEntities = false;
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)
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.
69 isSketchEntities = false;
71 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
72 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
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.
86 isSketchEntities = false;
88 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
89 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
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();
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.
115 isSketchEntities = false;
117 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
118 isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
121 if (!isSketchEntities) {
122 theError = "It refers to feature, which kind is not in the list: " + anEntityKindsStr;
125 return isSketchEntities;