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