Salome HOME
Copyright update 2020
[modules/shaper.git] / src / GeomValidators / GeomValidators_ShapeType.cpp
1 // Copyright (C) 2014-2020  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomValidators_ShapeType.h"
21 #include "GeomValidators_Tools.h"
22
23 #include <GeomAPI_Curve.h>
24 #include <GeomAPI_ShapeIterator.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 <algorithm>
37 #include <string>
38 #include <map>
39
40
41 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
42
43 static EdgeTypes MyShapeTypes;
44 GeomValidators_ShapeType::TypeOfShape
45   GeomValidators_ShapeType::shapeType(const std::string& theType)
46 {
47   if (MyShapeTypes.size() == 0) {
48     MyShapeTypes["empty"]     = Empty;
49     MyShapeTypes["vertex"]    = Vertex;
50     MyShapeTypes["vertices"]  = Vertex;
51     MyShapeTypes["edge"]      = Edge;
52     MyShapeTypes["edges"]     = Edge;
53     MyShapeTypes["line"]      = Line;
54     MyShapeTypes["circle"]    = Circle;
55     MyShapeTypes["wire"]      = Wire;
56     MyShapeTypes["face"]      = Face;
57     MyShapeTypes["faces"]     = Face;
58     MyShapeTypes["plane"]     = Plane;
59     MyShapeTypes["shell"]     = Shell;
60     MyShapeTypes["solid"]     = Solid;
61     MyShapeTypes["solids"]    = Solid;
62     MyShapeTypes["compsolid"] = CompSolid;
63     MyShapeTypes["compound"]  = Compound;
64   }
65   std::string aType = std::string(theType.c_str());
66   std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
67   if (MyShapeTypes.find(aType) != MyShapeTypes.end())
68     return MyShapeTypes[aType];
69
70 // LCOV_EXCL_START
71   Events_InfoMessage("Shape type defined in XML is not implemented!").send();
72   return AnyShape;
73 // LCOV_EXCL_STOP
74 }
75
76 // LCOV_EXCL_START
77 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
78 {
79   std::string aValue = "";
80
81   if (MyShapeTypes.size() != 0) {
82     std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
83       anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
84     for (; anIt != aLast; anIt++) {
85       if (anIt->second == theType)
86         aValue = anIt->first;
87         break;
88     }
89   }
90   return aValue;
91 }
92 // LCOV_EXCL_STOP
93
94 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
95                                        const std::list<std::string>& theArguments,
96                                        Events_InfoMessage& theError) const
97 {
98   bool aValid = false;
99
100   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
101   // returns true if the attribute satisfies at least one of given arguments
102   for (; anIt != aLast; anIt++) {
103     TypeOfShape aShapeType = shapeType(*anIt);
104     // if arguments contain any shape type value, the validator returns true
105     if (aShapeType == AnyShape) {
106       aValid = true;
107       break;
108     }
109     if (isValidAttribute(theAttribute, aShapeType, theError)) {
110       aValid = true;
111       break;
112     }
113   }
114   if (!aValid && theError.empty()) {
115     std::string aTypes;
116     std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
117     // returns true if the attribute satisfies at least one of given arguments
118     for (; anIt != aLast; anIt++) {
119       if (!aTypes.empty())
120         aTypes += ", ";
121       aTypes += *anIt;
122     }
123     theError = "It does not contain element with acceptable shape type. "
124                "The type should be one of the next: %1";
125     theError.arg(aTypes);
126   }
127
128   return aValid;
129 }
130
131 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
132                                                 const TypeOfShape theShapeType,
133                                                 Events_InfoMessage& theError) const
134 {
135   bool aValid = true;
136
137   std::string anAttributeType = theAttribute->attributeType();
138   if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
139     AttributeSelectionPtr anAttr =
140       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
141     GeomShapePtr aShape = anAttr->value();
142     if (aShape.get())
143       aValid = isValidShape(aShape, theShapeType, anAttr->isGeometricalSelection(), theError);
144     else {
145       if (anAttr->context().get())
146         aValid = isValidObject(anAttr->context(),
147                                theShapeType,
148                                anAttr->isGeometricalSelection(),
149                                theError);
150       else
151         aValid = isValidObject(anAttr->contextFeature(),
152                                theShapeType,
153                                anAttr->isGeometricalSelection(),
154                                theError);
155     }
156   }
157   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
158     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
159     if (anAttr->isObject()) {
160       aValid = isValidObject(anAttr->object(),
161                              theShapeType,
162                              false,
163                              theError);
164     }
165     else if (theShapeType == Vertex) {
166       AttributePtr aRefAttr = anAttr->attr();
167       if (!aRefAttr.get()){
168         aValid = false;
169         theError = "It has reference to an empty attribute";
170       }
171       else {
172         std::string anAttributeType = aRefAttr->attributeType();
173         aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
174         if (!aValid) {
175 // LCOV_EXCL_START
176           theError = "Shape type is \"%1\", it should be \"%2\"";
177           theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
178 // LCOV_EXCL_STOP
179         }
180       }
181     }
182   }
183   else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
184     AttributeReferencePtr anAttr =
185                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
186     aValid = isValidObject(anAttr->value(), theShapeType, false, theError);
187   }
188   else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
189     AttributeSelectionListPtr aListAttr =
190                           std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
191     // the Empty value means that the attribute selection list is valid if it is empty
192     if (aListAttr->size() == 0 && theShapeType == Empty) {
193       return true;
194     }
195     aValid = false; // the list should have elements if the shape type is not Empty
196     for (int i = 0; i < aListAttr->size(); i++) {
197       aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
198       if (!aValid) // if at least one attribute is invalid, the result is false
199         break;
200     }
201   }
202   else {
203 // LCOV_EXCL_START
204     aValid = false;
205     theError = "The attribute with the %1 type is not processed";
206     theError.arg(anAttributeType);
207 // LCOV_EXCL_STOP
208   }
209   if (aValid)
210     theError = "";
211   return aValid;
212 }
213
214 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
215                                              const TypeOfShape theShapeType,
216                                              const bool theIsGeometricalSelection,
217                                              Events_InfoMessage& theError) const
218 {
219   bool aValid = true;
220   if (!theObject.get()) {
221     if(theShapeType != Empty) {
222       aValid = false;
223       theError = "The object is empty";
224     }
225   }
226   else {
227     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
228     if (aResult.get()) {
229       if (theShapeType == Plane)
230       {
231         ResultConstructionPtr aResultConstruction =
232           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
233         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
234         const std::string& aKind = aFeature->getKind();
235         return aResult.get() != NULL && aKind == "Plane";
236       }
237       if (!aResult.get()) {
238         aValid = false;
239         theError = "The result is empty";
240       } else {
241         aValid = isValidShape(aResult->shape(), theShapeType, theIsGeometricalSelection, theError);
242       }
243     } else {
244       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
245       if (aFeature.get() && (theShapeType == CompSolid))
246         return aValid;
247       else {
248         aValid = false;
249         theError = "The feature has to produce a compsolid";
250       }
251     }
252   }
253   return aValid;
254 }
255
256 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
257                                             const TypeOfShape theShapeType,
258                                             const bool theIsGeometricalSelection,
259                                             Events_InfoMessage& theError) const
260 {
261   bool aValid = true;
262
263   if (!theShape.get()) {
264     aValid = false;
265     theError = "The shape is empty";
266   }
267   else {
268     switch (theShapeType) {
269     case Vertex:
270       aValid = theShape->isVertex();
271       break;
272     case Edge:
273       aValid = theShape->isEdge();
274       break;
275     case Line: {
276       if (theIsGeometricalSelection && theShape->isCompound()) {
277         aValid = true;
278         for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
279           if (!anIt.current()->isEdge() || !GeomAPI_Curve(anIt.current()).isLine()) {
280             aValid = false;
281             break;
282           }
283         }
284       }
285       else {
286         aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
287       }
288       break;
289     }
290     case Circle:
291       aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
292       break;
293     case Wire:
294       aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
295       break;
296     case Face:
297       aValid = theShape->isFace();
298       break;
299     case Shell:
300       aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
301       break;
302     case Plane:
303       aValid = theShape->isPlanar();
304       break;
305     case Solid:
306       aValid = theShape->isSolid() || theShape->isCompSolid() ||
307                theShape->isCompoundOfSolids();
308       break;
309     case CompSolid:
310       aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
311       break;
312     case Compound:
313       aValid = theShape->isCompound();
314       break;
315     default:
316       aValid = false;
317       break;
318     }
319   }
320   return aValid;
321 }