Salome HOME
Merge branch 'master' into Dev_1.1.0
[modules/shaper.git] / src / PartSet / PartSet_Validators.cpp
index f92793f71974c2e9f34d9cf8274ec30226d99a28..0e2ca6f20592abd11060e753f01e1273f1cf504a 100644 (file)
 #include <BRep_Tool.hxx>
 #include <GeomAdaptor_Curve.hxx>
 #include <GeomAbs_CurveType.hxx>
+#include <GeomValidators_Tools.h>
 #include <ModuleBase_ISelection.h>
+#include <ModuleBase_WidgetShapeSelector.h>
 
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeReference.h>
+#include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_Object.h>
 
 #include <SketchPlugin_Sketch.h>
@@ -110,86 +113,132 @@ bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection)
   return (aCount > 0) && (aCount < 2);
 }
 
-bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature, 
-                                                const std::list<std::string>& theArguments,
-                                                const ObjectPtr& theObject,
-                                                const GeomShapePtr& theShape) const
+bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute, 
+                                                const std::list<std::string>& theArguments) const
 {
-  // Check RefAttr attributes
-  std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
-    theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
-  if (anAttrs.size() > 0) {
-    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
-    for(; anAttr != anAttrs.end(); anAttr++) {
-      if (*anAttr) {
-        std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
-          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
-        // check the object is already presented
-        if (aRef->isObject() && aRef->object() == theObject)
-          return false;
-      }
+  FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
+
+  // 1. check whether the object of the attribute is not among the feature attributes
+  // find the attribute's object
+  ObjectPtr anObject =  GeomValidators_Tools::getObject(theAttribute);
+
+  // check whether the object is not among other feature attributes
+  if (anObject.get() != NULL) {
+    // Check RefAttr attributes
+    std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = aFeature->data()->attributes("");
+    //if (anAttrs.size() > 0) {
+    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anIt = anAttrs.begin();
+    for(; anIt != anAttrs.end(); anIt++) {
+      AttributePtr anAttr = *anIt;
+      // the function parameter attribute should be skipped
+      if (anAttr.get() == NULL || anAttr->id() == theAttribute->id())
+        continue;
+      ObjectPtr aCurObject =  GeomValidators_Tools::getObject(anAttr);
+      if (aCurObject  && aCurObject == anObject)
+        return false;
     }
   }
-  // Check selection attributes
-  anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
-  if (anAttrs.size() > 0) {
-    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
-    for(; anAttr != anAttrs.end(); anAttr++) {
-      if (*anAttr) {
-        std::shared_ptr<ModelAPI_AttributeSelection> aRef = 
-          std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
-        // check the object is already presented
-        if (aRef->isInitialized() && aRef->context() == theObject) {
-          if (theShape.get() != NULL) {
-            if (aRef->value()->isEqual(theShape))
-              return false;
-          } else
+  else {
+    // 2. collect object referenced by theAttribute and ...
+    if (featureHasReferences(theAttribute)) {
+      // 3. check whether the attribute value is not among other feature attributes
+      std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
+        aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
+      std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
+      for(; anAttr != anAttrs.end(); anAttr++) {
+        if (*anAttr) {
+          std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
+            std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
+          // check the object is already presented
+          if (!aRef->isObject() && aRef->attr() == theAttribute)
             return false;
         }
+// TODO(nds) v1.0.2 master
+//bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
+//                                                const std::list<std::string>& theArguments,
+//                                                const ObjectPtr& theObject,
+//                                                const GeomShapePtr& theShape) const
+//{
+//  // Check RefAttr attributes
+//  std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
+//    theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
+//  if (anAttrs.size() > 0) {
+//    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
+//    for(; anAttr != anAttrs.end(); anAttr++) {
+//      if (*anAttr) {
+//        std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
+//          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
+//        // check the object is already presented
+//        if (aRef->isObject() && aRef->object() == theObject)
+//          return false;
+//      }
+//    }
+//  }
+//  // Check selection attributes
+//  anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
+//  if (anAttrs.size() > 0) {
+//    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
+//    for(; anAttr != anAttrs.end(); anAttr++) {
+//      if (*anAttr) {
+//        std::shared_ptr<ModelAPI_AttributeSelection> aRef =
+//          std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
+//        // check the object is already presented
+//        if (aRef->isInitialized() && aRef->context() == theObject) {
+//          if (theShape.get() != NULL) {
+//            if (aRef->value()->isEqual(theShape))
+//              return false;
+//          } else
+//            return false;
+//        }
+//      }
+//    }
+//  }
+//  // Check selection attributes
+//  anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::typeId());
+//  if (anAttrs.size() > 0) {
+//    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
+//    for(; anAttr != anAttrs.end(); anAttr++) {
+//      if (*anAttr) {
+//        std::shared_ptr<ModelAPI_AttributeReference> aRef =
+//          std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
+//        // check the object is already presented
+//        if (aRef->isInitialized() && aRef->value() == theObject)
+//          return false;
+// ======= end of todo
       }
-    }
-  }
-  // Check selection attributes
-  anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::typeId());
-  if (anAttrs.size() > 0) {
-    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
-    for(; anAttr != anAttrs.end(); anAttr++) {
-      if (*anAttr) {
-        std::shared_ptr<ModelAPI_AttributeReference> aRef = 
-          std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
-        // check the object is already presented
-        if (aRef->isInitialized() && aRef->value() == theObject)
-          return false;
-      }
+      return true;
     }
   }
   return true;
 }
 
-bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature, 
-                                                const std::list<std::string>& theArguments,
-                                                const AttributePtr& theAttribute) const
-{
-  if (PartSet_DifferentObjectsValidator::isValid(theAttribute, theArguments)) {
-    std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
-      theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
-    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
-    for(; anAttr != anAttrs.end(); anAttr++) {
-      if (*anAttr) {
-        std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
-          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
-        // check the object is already presented
-        if (!aRef->isObject() && aRef->attr() == theAttribute)
-          return false;
-      }
-    }
-    return true;
-  }
-  return false;
-}
-
-bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute, 
-                                                const std::list<std::string>& theArguments) const
+bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
+// TODO(nds) v1.0.2 master
+//bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
+//                                                const std::list<std::string>& theArguments,
+//                                                const AttributePtr& theAttribute) const
+//{
+//  if (PartSet_DifferentObjectsValidator::isValid(theAttribute, theArguments)) {
+//    std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
+//      theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
+//    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
+//    for(; anAttr != anAttrs.end(); anAttr++) {
+//      if (*anAttr) {
+//        std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
+//          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
+//        // check the object is already presented
+//        if (!aRef->isObject() && aRef->attr() == theAttribute)
+//          return false;
+//      }
+//    }
+//    return true;
+//  }
+//  return false;
+//}
+//
+//bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
+//                                                const std::list<std::string>& theArguments) const
+// ======= end of todo
 {
   std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
   if (theAttribute->owner().get() && theAttribute->owner()->data().get())
@@ -219,8 +268,44 @@ bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute
   return true;
 }
 
-bool PartSet_SketchValidator::isValid(const ObjectPtr theObject) const
+bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
+                                            const std::list<std::string>& theArguments) const
 {
-  FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
-  return aFeature->getKind() == SketchPlugin_Sketch::ID();
+  bool isSketchEntities = true;
+  std::set<std::string> anEntityKinds;
+  std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
+  for (; anIt != aLast; anIt++) {
+    anEntityKinds.insert(*anIt);
+  }
+
+  std::string anAttributeType = theAttribute->attributeType();
+  if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
+    AttributeSelectionListPtr aSelectionListAttr = 
+                      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+    // it filters only selection list attributes
+    std::string aType = aSelectionListAttr->selectionType().c_str();
+    // all context objects should be sketch entities
+    int aSize = aSelectionListAttr->size();
+    for (int i = 0; i < aSelectionListAttr->size() && isSketchEntities; i++) {
+      AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
+      ObjectPtr anObject = aSelectAttr->context();
+      FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
+      isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
+    }
+  }
+  if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
+    std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
+                     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+    isSketchEntities = false;
+    if (aRef->isObject()) {
+      ObjectPtr anObject = aRef->object();
+      if (anObject.get() != NULL) {
+        FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
+        if (aFeature.get() != NULL)
+          isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
+      }
+    }
+  }
+
+  return isSketchEntities;
 }