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 #include <GeomValidators_ShapeType.h>
16 //=================================================================================================
17 bool FeaturesPlugin_ValidatorPipeLocations::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
18 const std::list<std::string>& theArguments,
19 std::string& theError) const
21 static const std::string aCreationMethodID = "creation_method";
22 static const std::string aBaseObjectsID = "base_objects";
23 static const std::string aLocationsID = "locations_objects";
25 if(theFeature->getKind() != "Pipe") {
26 theError = "Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
30 AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
31 if(!aCreationMethodAttr.get()) {
32 theError = "Could not get \"" + aCreationMethodID + "\" attribute.";
36 if(aCreationMethodAttr->value() != "locations") {
40 AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
41 if(!aBaseObjectsSelectionList.get()) {
42 theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
46 AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
47 if(!aLocationsSelectionList.get()) {
48 theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
52 if(aLocationsSelectionList->size() > 0 && aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
53 theError = "Number of locations should be the same as base objects.";
60 //=================================================================================================
61 bool FeaturesPlugin_ValidatorPipeLocations::isNotObligatory(std::string theFeature, std::string theAttribute)
66 //=================================================================================================
67 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
68 const std::list<std::string>& theArguments,
69 std::string& theError) const
71 if(theArguments.empty()) {
72 theError = "Validator parameters is empty.";
76 // Checking attribute.
77 if(!isValidAttribute(theAttribute, theArguments, theError)) {
78 if(theError.empty()) {
79 theError = "Attribute contains unacceptable shape.";
87 //=================================================================================================
88 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
89 const std::list<std::string>& theArguments,
90 std::string& theError) const
92 if(!theAttribute.get()) {
93 theError = "Empty attribute.";
97 std::string anAttributeType = theAttribute->attributeType();
98 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
99 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
100 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
101 // If at least one attribute is invalid, the result is false.
102 if(!isValidAttribute(aListAttr->value(anIndex), theArguments, theError)) {
106 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
108 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
109 ResultPtr aContext = anAttr->context();
110 if(!aContext.get()) {
111 theError = "Attribute have empty context.";
115 GeomShapePtr aShape = anAttr->value();
116 GeomShapePtr aContextShape = aContext->shape();
118 aShape = aContextShape;
121 theError = "Empty shape selected";
125 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
126 if(aConstruction.get()) {
127 // Construciotn selected. Check that is is not infinite.
128 if(aConstruction->isInfinite()) {
129 theError = "Infinite constructions is not allowed as base.";
133 if(aShape->isEqual(aContextShape)) {
134 // Whole construction selected. Check that it have faces.
135 if(aConstruction->facesNum() > 0) {
139 // Shape on construction selected. Check that it is a face or wire.
140 if(aShape->shapeType() == GeomAPI_Shape::WIRE || aShape->shapeType() == GeomAPI_Shape::FACE) {
146 if(!aShape->isEqual(aContextShape)) {
147 // Local selection on body does not allowed.
148 theError = "Selected shape is in the local selection. Only global selection is allowed.";
152 // Check that object is a shape with allowed type.
153 GeomValidators_ShapeType aShapeTypeValidator;
154 if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
155 theError = "Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, \
156 whole sketch(if it has at least one face), and whole objects with shape types: ";
157 std::list<std::string>::const_iterator anIt = theArguments.cbegin();
159 for(++anIt; anIt != theArguments.cend(); ++anIt) {
160 theError += ", " + *anIt;
166 theError = "Following attribute does not supported: " + anAttributeType + ".";
173 //=================================================================================================
174 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
175 const std::list<std::string>& theArguments,
176 std::string& theError) const
178 FeaturesPlugin_ValidatorBaseForGeneration aBaseValidator;
180 if(aBaseValidator.isValid(theAttribute, theArguments, theError)) {
184 // Check that face selected.
185 GeomValidators_ShapeType aShapeType;
186 std::list<std::string> anArguments;
187 anArguments.push_back("face");
188 if(aShapeType.isValid(theAttribute, anArguments, theError)) {
192 theError = "Selected shape is not suitable for this operation";
197 //=================================================================================================
198 bool FeaturesPlugin_ValidatorCanBeEmpty::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
199 const std::list<std::string>& theArguments,
200 std::string& theError) const
202 if(theArguments.size() != 5 && theArguments.size() != 6) {
203 theError = "Validator should be used with 6 parameters for extrusion and with 5 for revolution.";
207 std::list<std::string>::const_iterator anArgsIt = theArguments.begin(), aLast = theArguments.end();
209 std::string aSelectedMethod;
210 if(theFeature->string(*anArgsIt)) {
211 aSelectedMethod = theFeature->string(*anArgsIt)->value();
214 std::string aCreationMethod = *anArgsIt;
217 AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
220 if(isShapesCanBeEmpty(aCheckAttribute, theError)) {
224 if(aSelectedMethod == aCreationMethod) {
229 for(; anArgsIt != theArguments.cend(); ++anArgsIt) {
230 AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
231 if(!aSelAttr.get()) {
232 theError = "Could not get selection attribute \"" + *anArgsIt + "\".";
236 GeomShapePtr aShape = aSelAttr->value();
238 ResultPtr aContext = aSelAttr->context();
239 if(!aContext.get()) {
240 theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
244 aShape = aContext->shape();
248 theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
256 //=================================================================================================
257 bool FeaturesPlugin_ValidatorCanBeEmpty::isNotObligatory(std::string theFeature, std::string theAttribute)
262 //=================================================================================================
263 bool FeaturesPlugin_ValidatorCanBeEmpty::isShapesCanBeEmpty(const AttributePtr& theAttribute,
264 std::string& theError) const
266 if(!theAttribute.get()) {
270 std::string anAttributeType = theAttribute->attributeType();
271 if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
272 AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
273 for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
274 // If at least one attribute is invalid, the result is false.
275 if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
279 } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
281 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
282 ResultPtr aContext = anAttr->context();
283 if(!aContext.get()) {
287 GeomShapePtr aShape = anAttr->value();
288 GeomShapePtr aContextShape = aContext->shape();
290 aShape = aContextShape;
296 if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
297 aShape->shapeType() == GeomAPI_Shape::EDGE ||
298 !aShape->isPlanar()) {