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