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