Salome HOME
Initial implementation of restoreByName selector method
[modules/shaper.git] / src / GeomValidators / GeomValidators_ShapeType.cpp
index 4991b92255b5a93d81c8b9ad393c1a656381eed3..afb7dd0c93eb2c6a5bb6eefa2965bd750a5b698b 100755 (executable)
@@ -1,5 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #include "GeomValidators_ShapeType.h"
 #include "GeomValidators_Tools.h"
@@ -8,6 +25,7 @@
 #include <GeomDataAPI_Point2D.h>
 
 #include <ModelAPI_Result.h>
+#include <ModelAPI_Feature.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeSelectionList.h>
@@ -22,7 +40,8 @@
 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
 
 static EdgeTypes MyShapeTypes;
-GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
+GeomValidators_ShapeType::TypeOfShape
+  GeomValidators_ShapeType::shapeType(const std::string& theType)
 {
   if (MyShapeTypes.size() == 0) {
     MyShapeTypes["empty"]     = Empty;
@@ -51,8 +70,8 @@ std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape&
   std::string aValue = "";
 
   if (MyShapeTypes.size() != 0) {
-    std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator anIt = MyShapeTypes.begin(),
-                                                                                 aLast = MyShapeTypes.end();
+    std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
+      anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
     for (; anIt != aLast; anIt++) {
       if (anIt->second == theType)
         aValue = anIt->first;
@@ -89,8 +108,10 @@ bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
     for (; anIt != aLast; anIt++) {
       if (!aTypes.empty())
         aTypes += ", ";
+      aTypes += *anIt;
     }
-    theError = "It does not contain element with acceptable shape type. The type should be one of the next: %1";
+    theError = "It does not contain element with acceptable shape type. "
+               "The type should be one of the next: %1";
     theError.arg(aTypes);
   }
 
@@ -105,12 +126,17 @@ bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute
 
   std::string anAttributeType = theAttribute->attributeType();
   if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
-    AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+    AttributeSelectionPtr anAttr =
+      std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
     GeomShapePtr aShape = anAttr->value();
     if (aShape.get())
       aValid = isValidShape(aShape, theShapeType, theError);
-    else
-      aValid = isValidObject(anAttr->context(), theShapeType, theError);
+    else {
+      if (anAttr->context().get())
+        aValid = isValidObject(anAttr->context(), theShapeType, theError);
+      else
+        aValid = isValidObject(anAttr->contextFeature(), theShapeType, theError);
+    }
   }
   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
@@ -157,6 +183,8 @@ bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute
     theError = "The attribute with the %1 type is not processed";
     theError.arg(anAttributeType);
   }
+  if (aValid)
+    theError = "";
   return aValid;
 }
 
@@ -173,19 +201,29 @@ bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
   }
   else {
     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
-    if( theShapeType==Plane )
-    {
-      ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
-      FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
-      const std::string& aKind = aFeature->getKind();
-      return aResult.get() != NULL && aKind == "Plane";
-    }
-    if (!aResult.get()) {
-      aValid = false;
-      theError = "The result is empty";
-    }
-    else {
-      aValid = isValidShape(aResult->shape(), theShapeType, theError);
+    if (aResult.get()) {
+      if (theShapeType == Plane)
+      {
+        ResultConstructionPtr aResultConstruction =
+          std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
+        FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
+        const std::string& aKind = aFeature->getKind();
+        return aResult.get() != NULL && aKind == "Plane";
+      }
+      if (!aResult.get()) {
+        aValid = false;
+        theError = "The result is empty";
+      } else {
+        aValid = isValidShape(aResult->shape(), theShapeType, theError);
+      }
+    } else {
+      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
+      if (aFeature.get() && (theShapeType == CompSolid))
+        return aValid;
+      else {
+        aValid = false;
+        theError = "The feature has to produce a compsolid";
+      }
     }
   }
   return aValid;
@@ -210,7 +248,7 @@ bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
       aValid = theShape->isEdge();
       break;
     case Line:
-      aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
+      aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
       break;
     case Circle:
       aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();