Salome HOME
Use Feature as a value for selection attribute
[modules/shaper.git] / src / GeomValidators / GeomValidators_ShapeType.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomValidators_ShapeType.h"
22 #include "GeomValidators_Tools.h"
23
24 #include <GeomAPI_Curve.h>
25 #include <GeomDataAPI_Point2D.h>
26
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeSelectionList.h>
32 #include <ModelAPI_AttributeReference.h>
33
34 #include <Events_InfoMessage.h>
35
36 #include <string>
37 #include <map>
38
39
40 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
41
42 static EdgeTypes MyShapeTypes;
43 GeomValidators_ShapeType::TypeOfShape
44   GeomValidators_ShapeType::shapeType(const std::string& theType)
45 {
46   if (MyShapeTypes.size() == 0) {
47     MyShapeTypes["empty"]     = Empty;
48     MyShapeTypes["vertex"]    = Vertex;
49     MyShapeTypes["edge"]      = Edge;
50     MyShapeTypes["line"]      = Line;
51     MyShapeTypes["circle"]    = Circle;
52     MyShapeTypes["wire"]      = Wire;
53     MyShapeTypes["face"]      = Face;
54     MyShapeTypes["plane"]     = Plane;
55     MyShapeTypes["shell"]     = Shell;
56     MyShapeTypes["solid"]     = Solid;
57     MyShapeTypes["compsolid"] = CompSolid;
58     MyShapeTypes["compound"]  = Compound;
59   }
60   std::string aType = std::string(theType.c_str());
61   if (MyShapeTypes.find(aType) != MyShapeTypes.end())
62     return MyShapeTypes[aType];
63
64   Events_InfoMessage("Shape type defined in XML is not implemented!").send();
65   return AnyShape;
66 }
67
68 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
69 {
70   std::string aValue = "";
71
72   if (MyShapeTypes.size() != 0) {
73     std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
74       anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
75     for (; anIt != aLast; anIt++) {
76       if (anIt->second == theType)
77         aValue = anIt->first;
78         break;
79     }
80   }
81   return aValue;
82 }
83
84 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
85                                        const std::list<std::string>& theArguments,
86                                        Events_InfoMessage& theError) const
87 {
88   bool aValid = false;
89
90   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
91   // returns true if the attribute satisfies at least one of given arguments
92   for (; anIt != aLast; anIt++) {
93     TypeOfShape aShapeType = shapeType(*anIt);
94     // if arguments contain any shape type value, the validator returns true
95     if (aShapeType == AnyShape) {
96       aValid = true;
97       break;
98     }
99     if (isValidAttribute(theAttribute, aShapeType, theError)) {
100       aValid = true;
101       break;
102     }
103   }
104   if (!aValid && theError.empty()) {
105     std::string aTypes;
106     std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
107     // returns true if the attribute satisfies at least one of given arguments
108     for (; anIt != aLast; anIt++) {
109       if (!aTypes.empty())
110         aTypes += ", ";
111       aTypes += *anIt;
112     }
113     theError = "It does not contain element with acceptable shape type. "
114                "The type should be one of the next: %1";
115     theError.arg(aTypes);
116   }
117
118   return aValid;
119 }
120
121 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
122                                                 const TypeOfShape theShapeType,
123                                                 Events_InfoMessage& theError) const
124 {
125   bool aValid = true;
126
127   std::string anAttributeType = theAttribute->attributeType();
128   if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
129     AttributeSelectionPtr anAttr =
130       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
131     GeomShapePtr aShape = anAttr->value();
132     if (aShape.get())
133       aValid = isValidShape(aShape, theShapeType, theError);
134     else {
135       if (anAttr->context().get())
136         aValid = isValidObject(anAttr->context(), theShapeType, theError);
137       else
138         aValid = isValidObject(anAttr->contextFeature(), theShapeType, theError);
139     }
140   }
141   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
142     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
143     if (anAttr->isObject()) {
144       aValid = isValidObject(anAttr->object(), theShapeType, theError);
145     }
146     else if (theShapeType == Vertex) {
147       AttributePtr aRefAttr = anAttr->attr();
148       if (!aRefAttr.get()){
149         aValid = false;
150         theError = "It has reference to an empty attribute";
151       }
152       else {
153         std::string anAttributeType = aRefAttr->attributeType();
154         aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
155         if (!aValid) {
156           theError = "Shape type is \"%1\", it should be \"%2\"";
157           theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
158         }
159       }
160     }
161   }
162   else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
163     AttributeReferencePtr anAttr =
164                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
165     aValid = isValidObject(anAttr->value(), theShapeType, theError);
166   }
167   else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
168     AttributeSelectionListPtr aListAttr =
169                           std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
170     // the Empty value means that the attribute selection list is valid if it is empty
171     if (aListAttr->size() == 0 && theShapeType == Empty) {
172       return true;
173     }
174     aValid = false; // the list should have elements if the shape type is not Empty
175     for (int i = 0; i < aListAttr->size(); i++) {
176       aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
177       if (!aValid) // if at least one attribute is invalid, the result is false
178         break;
179     }
180   }
181   else {
182     aValid = false;
183     theError = "The attribute with the %1 type is not processed";
184     theError.arg(anAttributeType);
185   }
186   if (aValid)
187     theError = "";
188   return aValid;
189 }
190
191 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
192                                              const TypeOfShape theShapeType,
193                                              Events_InfoMessage& theError) const
194 {
195   bool aValid = true;
196   if (!theObject.get()) {
197     if(theShapeType != Empty) {
198       aValid = false;
199       theError = "The object is empty";
200     }
201   }
202   else {
203     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
204     if (aResult.get()) {
205       if (theShapeType == Plane)
206       {
207         ResultConstructionPtr aResultConstruction =
208           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
209         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
210         const std::string& aKind = aFeature->getKind();
211         return aResult.get() != NULL && aKind == "Plane";
212       }
213       if (!aResult.get()) {
214         aValid = false;
215         theError = "The result is empty";
216       } else {
217         aValid = isValidShape(aResult->shape(), theShapeType, theError);
218       }
219     } else {
220       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
221       if (aFeature.get() && (theShapeType == CompSolid))
222         return aValid;
223       else {
224         aValid = false;
225         theError = "The feature has to produce a compsolid";
226       }
227     }
228   }
229   return aValid;
230 }
231
232 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
233                                             const TypeOfShape theShapeType,
234                                             Events_InfoMessage& theError) const
235 {
236   bool aValid = true;
237
238   if (!theShape.get()) {
239     aValid = false;
240     theError = "The shape is empty";
241   }
242   else {
243     switch (theShapeType) {
244     case Vertex:
245       aValid = theShape->isVertex();
246       break;
247     case Edge:
248       aValid = theShape->isEdge();
249       break;
250     case Line:
251       aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
252       break;
253     case Circle:
254       aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
255       break;
256     case Wire:
257       aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
258       break;
259     case Face:
260       aValid = theShape->isFace();
261       break;
262     case Shell:
263       aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
264       break;
265     case Solid:
266       aValid = theShape->isSolid() || theShape->isCompSolid() ||
267                theShape->isCompoundOfSolids();
268       break;
269     case CompSolid:
270       aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
271       break;
272     case Compound:
273       aValid = theShape->isCompound();
274       break;
275     default:
276       aValid = false;
277       break;
278     }
279   }
280   return aValid;
281 }