Salome HOME
Improvement #610: Fuse: tools or objects are not needed.
[modules/shaper.git] / src / GeomValidators / GeomValidators_ShapeType.cpp
index f5ce2fd0b396d52b27544c5ef3657d4028281391..3d68f6a8f95e598efbf63c3c11401d6abfd504dc 100644 (file)
@@ -8,7 +8,9 @@
 #include <GeomDataAPI_Point2D.h>
 
 #include <ModelAPI_Result.h>
-#include "ModelAPI_AttributeRefAttr.h"
+#include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_AttributeReference.h>
 
 #include <Events_Error.h>
 
 
 
 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
-static EdgeTypes MyEdgeTypes;
 
 GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
 {
-  if (MyEdgeTypes.size() == 0) {
-    MyEdgeTypes["vertex"] = Vertex;
-    MyEdgeTypes["line"]   = Line;
-    MyEdgeTypes["circle"] = Circle;
+  static EdgeTypes MyShapeTypes;
+
+  if (MyShapeTypes.size() == 0) {
+    MyShapeTypes["empty"]  = Empty;
+    MyShapeTypes["vertex"] = Vertex;
+    MyShapeTypes["edge"]   = Edge;
+    MyShapeTypes["line"]   = Line;
+    MyShapeTypes["circle"] = Circle;
+    MyShapeTypes["face"]   = Face;
+    MyShapeTypes["solid"]  = Solid;
   }
   std::string aType = std::string(theType.c_str());
-  if (MyEdgeTypes.find(aType) != MyEdgeTypes.end())
-    return MyEdgeTypes[aType];
+  if (MyShapeTypes.find(aType) != MyShapeTypes.end())
+    return MyShapeTypes[aType];
   
-  Events_Error::send("Edge type defined in XML is not implemented!");
+  Events_Error::send("Shape type defined in XML is not implemented!");
   return AnyShape;
 }
 
@@ -55,36 +62,104 @@ bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute,
   if (aShapeType == AnyShape)
     return true;
 
-  ObjectPtr anObject = GeomValidators_Tools::getObject(theAttribute);
-  if (anObject.get() != NULL) {
-    FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
-    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
-    if (aResult.get() != NULL) {
-      GeomShapePtr aShape = aResult->shape();
-      if (aShape.get() != NULL) {
-        switch (aShapeType) {
-          case Line:
-            aValid = aShape->isEdge() && !GeomAPI_Curve(aShape).isCircle();
-          break;
-          case Circle:
-            aValid = aShape->isEdge() && GeomAPI_Curve(aShape).isCircle();
-          break;
-          case Vertex:
-            aValid = aShape->isVertex();
-          break;
-          default: break;
-        }
-      }
+  AttributeSelectionListPtr aListAttr = 
+           std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+  if (aListAttr.get()) {
+    if(aListAttr->size() == 0 && shapeType(theArgument) == Empty) {
+      return true;
+    }
+    for (int i = 0; i < aListAttr->size(); i++) {
+      aValid = isValidAttribute(aListAttr->value(i), aShapeType);
+      if (!aValid) // if at least one attribute is invalid, the result is false
+        break;
     }
   }
   else {
+    aValid = isValidAttribute(theAttribute, aShapeType);
+  }
+  return aValid;
+}
+
+bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
+                                                const TypeOfShape theShapeType) const
+{
+  bool aValid = false;
+
+  std::string anAttributeType = theAttribute->attributeType();
+
+  if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
+    AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+    GeomShapePtr aShape = anAttr->value();
+    if (aShape.get())
+      aValid = isValidShape(aShape, theShapeType);
+    else
+      aValid = isValidObject(anAttr->context(), theShapeType);
+  }
+  else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
     if (anAttr.get() != NULL) {
-      if (aShapeType == Vertex) {
+      if (anAttr->isObject()) {
+        aValid = isValidObject(anAttr->object(), theShapeType);
+      }
+      else if (theShapeType == Vertex) {
         AttributePtr aRefAttr = anAttr->attr();
         aValid = aRefAttr.get() != NULL && aRefAttr->attributeType() == GeomDataAPI_Point2D::typeId();
       }
     }
   }
+  else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
+    AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
+    if (anAttr.get() != NULL)
+      aValid = isValidObject(anAttr->value(), theShapeType);
+  }
+  return aValid;
+}
+
+bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
+                                             const TypeOfShape theShapeType) const
+{
+  bool aValid = false;
+  if (theObject.get() != NULL) {
+    FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+    if (aResult.get() != NULL) {
+      GeomShapePtr aShape = aResult->shape();
+      aValid = isValidShape(aShape, theShapeType);
+    }
+  }
+  return aValid;
+}
+
+bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
+                                            const TypeOfShape theShapeType) const
+{
+  bool aValid = false;
+
+  if (theShape.get() != NULL) {
+    switch (theShapeType) {
+      case Edge:
+        aValid = theShape->isEdge();
+      break;
+      case Line:
+        aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
+      break;
+      case Circle:
+        aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
+      break;
+      case Vertex:
+        aValid = theShape->isVertex();
+      break;
+      case Solid:
+        aValid = theShape->isSolid();
+        break;
+      case Face:
+        aValid = theShape->isFace();
+        break;
+      case Compound:
+        aValid = theShape->isCompound();
+        break;
+      default: break;
+    }
+  }
   return aValid;
 }