Salome HOME
Copyright update 2022
[modules/shaper.git] / src / GeomValidators / GeomValidators_ShapeType.cpp
1 // Copyright (C) 2014-2022  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(),
67                  [](char c) { return static_cast<char>(::tolower(c)); });
68   if (MyShapeTypes.find(aType) != MyShapeTypes.end())
69     return MyShapeTypes[aType];
70
71 // LCOV_EXCL_START
72   //Events_InfoMessage("Shape type defined in XML is not implemented!").send();
73   return AnyShape;
74 // LCOV_EXCL_STOP
75 }
76
77 // LCOV_EXCL_START
78 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
79 {
80   std::string aValue = "";
81
82   if (MyShapeTypes.size() != 0) {
83     std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
84       anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
85     for (; anIt != aLast; anIt++)
86       if (anIt->second == theType) {
87         aValue = anIt->first;
88         break;
89       }
90   }
91   return aValue;
92 }
93 // LCOV_EXCL_STOP
94
95 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
96                                        const std::list<std::string>& theArguments,
97                                        Events_InfoMessage& theError) const
98 {
99   bool aValid = false;
100
101   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
102   // returns true if the attribute satisfies at least one of given arguments
103   for (; anIt != aLast; anIt++) {
104     TypeOfShape aShapeType = shapeType(*anIt);
105     // if arguments contain any shape type value, the validator returns true
106     if (aShapeType == AnyShape) {
107       aValid = true;
108       break;
109     }
110     if (isValidAttribute(theAttribute, aShapeType, theError)) {
111       aValid = true;
112       break;
113     }
114   }
115   if (!aValid && theError.empty()) {
116     std::string aTypes;
117     // returns true if the attribute satisfies at least one of given arguments
118     for (anIt = theArguments.begin(); 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 anAttrType = aRefAttr->attributeType();
173         aValid = anAttrType == GeomDataAPI_Point2D::typeId();
174         if (!aValid) {
175 // LCOV_EXCL_START
176           theError = "Shape type is \"%1\", it should be \"%2\"";
177           theError.arg(anAttrType).arg(getShapeTypeDescription(theShapeType));
178 // LCOV_EXCL_STOP
179         }
180       }
181     }
182     else
183       aValid = false;
184   }
185   else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
186     AttributeReferencePtr anAttr =
187                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
188     aValid = isValidObject(anAttr->value(), theShapeType, false, theError);
189   }
190   else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
191     AttributeSelectionListPtr aListAttr =
192                           std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
193     // the Empty value means that the attribute selection list is valid if it is empty
194     if (aListAttr->size() == 0 && theShapeType == Empty) {
195       return true;
196     }
197     aValid = false; // the list should have elements if the shape type is not Empty
198     for (int i = 0; i < aListAttr->size(); i++) {
199       aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
200       if (!aValid) // if at least one attribute is invalid, the result is false
201         break;
202     }
203   }
204   else {
205 // LCOV_EXCL_START
206     aValid = false;
207     theError = "The attribute with the %1 type is not processed";
208     theError.arg(anAttributeType);
209 // LCOV_EXCL_STOP
210   }
211   if (aValid)
212     theError = "";
213   return aValid;
214 }
215
216 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
217                                              const TypeOfShape theShapeType,
218                                              const bool theIsGeometricalSelection,
219                                              Events_InfoMessage& theError) const
220 {
221   bool aValid = true;
222   if (!theObject.get()) {
223     if(theShapeType != Empty) {
224       aValid = false;
225       theError = "The object is empty";
226     }
227   }
228   else {
229     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
230     if (aResult.get()) {
231       if (theShapeType == Plane)
232       {
233         ResultConstructionPtr aResultConstruction =
234           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
235         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
236         const std::string& aKind = aFeature->getKind();
237         return aResult.get() != NULL && aKind == "Plane";
238       }
239       if (!aResult.get()) {
240         aValid = false;
241         theError = "The result is empty";
242       } else {
243         aValid = isValidShape(aResult->shape(), theShapeType, theIsGeometricalSelection, theError);
244       }
245     } else {
246       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
247       if (aFeature.get() && (theShapeType == CompSolid))
248         return aValid;
249       else {
250         aValid = false;
251         theError = "The feature has to produce a compsolid";
252       }
253     }
254   }
255   return aValid;
256 }
257
258 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
259                                             const TypeOfShape theShapeType,
260                                             const bool theIsGeometricalSelection,
261                                             Events_InfoMessage& theError) const
262 {
263   bool aValid = true;
264
265   if (!theShape.get()) {
266     aValid = false;
267     theError = "The shape is empty";
268   }
269   else {
270     switch (theShapeType) {
271     case Vertex:
272       aValid = theShape->isVertex();
273       break;
274     case Edge:
275       aValid = theShape->isEdge();
276       break;
277     case Line: {
278       if (theIsGeometricalSelection && theShape->isCompound()) {
279         aValid = true;
280         for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
281           if (!anIt.current()->isEdge() || !GeomAPI_Curve(anIt.current()).isLine()) {
282             aValid = false;
283             break;
284           }
285         }
286       }
287       else {
288         aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
289       }
290       break;
291     }
292     case Circle:
293       aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
294       break;
295     case Wire:
296       aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
297       break;
298     case Face:
299       aValid = theShape->isFace();
300       break;
301     case Shell:
302       aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
303       break;
304     case Plane:
305       aValid = theShape->isPlanar();
306       break;
307     case Solid:
308       aValid = theShape->isSolid() || theShape->isCompSolid() ||
309                theShape->isCompoundOfSolids();
310       break;
311     case CompSolid:
312       aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
313       break;
314     case Compound:
315       aValid = theShape->isCompound();
316       break;
317     default:
318       aValid = false;
319       break;
320     }
321   }
322   return aValid;
323 }