Salome HOME
Add error descriptions for some feture and attribute validators
[modules/shaper.git] / src / GeomValidators / GeomValidators_Face.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
4 #include "GeomValidators_Face.h"
5 #include "GeomValidators_Tools.h"
6
7 #include "ModelAPI_AttributeSelection.h"
8
9 #include <GeomAPI_Face.h>
10
11 #include <Events_Error.h>
12
13 #include <QString>
14 #include <QMap>
15
16 typedef std::map<std::string, GeomAbs_SurfaceType> FaceTypes;
17 static FaceTypes MyFaceTypes;
18
19 GeomAbs_SurfaceType GeomValidators_Face::faceType(const std::string& theType)
20 {
21   if (MyFaceTypes.size() == 0) {
22     MyFaceTypes["plane"] = GeomAbs_Plane;
23     MyFaceTypes["cylinder"] = GeomAbs_Cylinder;
24   }
25   std::string aType = std::string(theType.c_str());
26   if (MyFaceTypes.find(aType) != MyFaceTypes.end())
27     return MyFaceTypes[aType];
28   
29   Events_Error::send("Face type defined in XML is not implemented!");
30   return GeomAbs_Plane;
31 }
32
33 bool GeomValidators_Face::isValid(const AttributePtr& theAttribute,
34                                   const std::list<std::string>& theArguments,
35                                   std::string& theError) const
36 {
37   bool aValid = false;
38
39   GeomAbs_SurfaceType aFaceType = GeomAbs_Plane;
40   if (theArguments.size() == 1) {
41     std::string anArgument = theArguments.front();
42     aFaceType = faceType(anArgument);
43   }
44
45   ObjectPtr anObject = GeomValidators_Tools::getObject(theAttribute);
46   if (anObject.get() != NULL) {
47     AttributeSelectionPtr aSelectionAttr =
48         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
49     if (aSelectionAttr.get() == NULL) {
50       theError = "Is not a selection attribute.";
51       return aValid;
52     }
53
54     std::shared_ptr<GeomAPI_Shape> aGeomShape = aSelectionAttr->value();
55     if (!aGeomShape.get()) {
56       // if the shape is empty, apply the validator to the shape of result
57       aGeomShape = aSelectionAttr->context()->shape();
58     }
59     // it is necessary to check whether the shape is face in order to set in selection a value
60     // with any type and check the type in this validator
61     // It is realized to select any object in OB and filter it in this validator (sketch plane)
62     if (aGeomShape->isFace()) {
63       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
64       if (aGeomFace.get() != NULL) {
65         switch(aFaceType) {
66             case GeomAbs_Plane:
67               aValid = aGeomFace->isPlanar();
68               if (!aValid)
69                 theError = "The shape is not a plane."
70               break;
71             case GeomAbs_Cylinder:
72               aValid = aGeomFace->isCylindrical();
73               if (!aValid)
74                 theError = "The shape is not a cylinder."
75               break;
76             default:
77               theError = "The shape is not an available face."
78               break;
79         }
80       }
81     } else
82       theError = "The shape is not a face."
83   }
84   else
85     aValid = true; // an empty face selected is valid.
86   return aValid;
87 }