1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: FeaturesPlugin_Validators.cpp
4 // Created: 22 March 2016
5 // Author: Dmitry Bobylev
7 #include "FeaturesPlugin_Validators.h"
9 #include <ModelAPI_Attribute.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_ResultConstruction.h>
14 //=================================================================================================
15 bool FeaturesPlugin_PipeLocationsValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
16 const std::list<std::string>& theArguments,
17 std::string& theError) const
19 static const std::string aCreationMethodID = "creation_method";
20 static const std::string aBaseObjectsID = "base_objects";
21 static const std::string aLocationsID = "locations_objects";
23 if(theFeature->getKind() != "Pipe") {
24 theError = "Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
28 AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
29 if(!aCreationMethodAttr.get()) {
30 theError = "Could not get \"" + aCreationMethodID + "\" attribute.";
34 if(aCreationMethodAttr->value() != "locations") {
38 AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
39 if(!aBaseObjectsSelectionList.get()) {
40 theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
44 AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
45 if(!aLocationsSelectionList.get()) {
46 theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
50 if(aLocationsSelectionList->size() > 0 && aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
51 theError = "Number of locations should be the same as base objects.";
58 //=================================================================================================
59 bool FeaturesPlugin_PipeLocationsValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
64 //=================================================================================================
65 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
66 const std::list<std::string>& theArguments,
67 std::string& theError) const
69 // Checking attribute.
70 if(!isValidAttribute(theAttribute, theError)) {
71 if(theError.empty()) {
72 theError = "Attribute contains shape with unacceptable type.";
80 //=================================================================================================
81 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
82 std::string& theError) const
84 if(!theAttribute.get()) {
85 theError = "Empty attribute.";
89 std::string anAttributeType = theAttribute->attributeType();
90 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
91 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
92 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
93 // If at least one attribute is invalid, the result is false.
94 if(!isValidAttribute(aListAttr->value(anIndex), theError)) {
98 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
100 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
101 ResultPtr aContext = anAttr->context();
102 if(!aContext.get()) {
103 theError = "Attribute have empty context.";
107 GeomShapePtr aShape = anAttr->value();
108 GeomShapePtr aContextShape = aContext->shape();
110 aShape = aContextShape;
113 theError = "Empty shape selected";
117 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
118 if(aConstruction.get()) {
119 // Construciotn selected. Check that is is not infinite.
120 if(aConstruction->isInfinite()) {
121 theError = "Infinite constructions is not allowed as base.";
125 if(aShape->isEqual(aContextShape)) {
126 // Whole construction selected. Check that it have faces.
127 if(aConstruction->facesNum() > 0) {
131 // Shape on construction selected. Check that it is a face or wire.
132 if(aShape->shapeType() == GeomAPI_Shape::WIRE || aShape->shapeType() == GeomAPI_Shape::FACE) {
138 if(!aShape->isEqual(aContextShape)) {
139 // Local selection on body does not allowed.
140 theError = "Selected shape is in the local selection. Only global selection is allowed.";
144 // Check that object is a shape with allowed type.
145 aShape = aContext->shape();
146 GeomAPI_Shape::ShapeType aShapeType = aShape->shapeType();
147 if(aShapeType != GeomAPI_Shape::VERTEX &&
148 aShapeType != GeomAPI_Shape::EDGE &&
149 aShapeType != GeomAPI_Shape::WIRE &&
150 aShapeType != GeomAPI_Shape::FACE) {
151 theError = "Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, \
152 whole sketch(if it has at least one face), and following objects: vertex, edge, wire, face.";
157 theError = "Following attribute does not supported: " + anAttributeType + ".";