]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Validators.cpp
Salome HOME
Issue #1343: Architecture changes. Composite features now derived from extrusion...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Validators.cpp
4 // Created:     22 March 2016
5 // Author:      Dmitry Bobylev
6
7 #include "FeaturesPlugin_Validators.h"
8
9 #include <ModelAPI_Attribute.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_ResultConstruction.h>
13
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
18 {
19   static const std::string aCreationMethodID = "creation_method";
20   static const std::string aBaseObjectsID = "base_objects";
21   static const std::string aLocationsID = "locations_objects";
22
23   if(theFeature->getKind() != "Pipe") {
24     theError = "Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
25     return false;
26   }
27
28   AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
29   if(!aCreationMethodAttr.get()) {
30     theError = "Could not get \"" + aCreationMethodID + "\" attribute.";
31     return false;
32   }
33
34   if(aCreationMethodAttr->value() != "locations") {
35     return true;
36   }
37
38   AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
39   if(!aBaseObjectsSelectionList.get()) {
40     theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
41     return false;
42   }
43
44   AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
45   if(!aLocationsSelectionList.get()) {
46     theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
47     return false;
48   }
49
50   if(aLocationsSelectionList->size() > 0 && aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
51     theError = "Number of locations should be the same as base objects.";
52     return false;
53   }
54
55   return true;
56 }
57
58 //=================================================================================================
59 bool FeaturesPlugin_PipeLocationsValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
60 {
61   return false;
62 }
63
64 //=================================================================================================
65 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
66                                                         const std::list<std::string>& theArguments,
67                                                         std::string& theError) const
68 {
69   // Checking attribute.
70   if(!isValidAttribute(theAttribute, theError)) {
71     if(theError.empty()) {
72       theError = "Attribute contains shape with unacceptable type.";
73     }
74     return false;
75   }
76
77   return true;
78 }
79
80 //=================================================================================================
81 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
82                                                                  std::string& theError) const
83 {
84   if(!theAttribute.get()) {
85     theError = "Empty attribute.";
86     return false;
87   }
88
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)) {
95         return false;
96       }
97     }
98   } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
99     // Getting context.
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.";
104       return false;
105     }
106
107     GeomShapePtr aShape = anAttr->value();
108     GeomShapePtr aContextShape = aContext->shape();
109     if(!aShape.get()) {
110       aShape = aContextShape;
111     }
112     if(!aShape.get()) {
113       theError = "Empty shape selected";
114       return false;
115     }
116
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.";
122         return false;
123       }
124
125       if(aShape->isEqual(aContextShape)) {
126         // Whole construction selected. Check that it have faces.
127         if(aConstruction->facesNum() > 0) {
128           return true;
129         }
130       } else {
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) {
133           return true;
134         }
135       }
136     }
137
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.";
141       return false;
142     }
143
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.";
153       return false;
154     }
155
156   } else {
157     theError = "Following attribute does not supported: " + anAttributeType + ".";
158     return false;
159   }
160
161   return true;
162 }