Salome HOME
Constriction type for all sketch entities
[modules/shaper.git] / src / PartSet / PartSet_Validators.cpp
index 69c488639f9e7287e05406cb7b1ba60b7f7e06ae..92100792ede4fba1b1c3a64555721c790578786b 100644 (file)
@@ -16,6 +16,7 @@
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeReference.h>
+#include <ModelAPI_Object.h>
 
 #include <SketchPlugin_Sketch.h>
 
@@ -163,14 +164,52 @@ bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
                                                 const std::list<std::string>& theArguments,
                                                 const AttributePtr& theAttribute) const
 {
-  // not implemented
-  return true;
+  if (PartSet_DifferentObjectsValidator::isValid(theAttribute, theArguments)) {
+    std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
+      theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
+    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
 {
-  // not implemented
+  std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
+  if (theAttribute->owner().get() && theAttribute->owner()->data().get())
+    theAttribute->owner()->data()->referencesToObjects(allRefs);
+  // collect object referenced by theAttribute
+  std::list<ObjectPtr>* anAttrObjs = 0;
+  std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
+  for(; aRefIter != allRefs.end(); aRefIter++) {
+    if (theAttribute->id() == aRefIter->first)
+      anAttrObjs = &(aRefIter->second);
+  }
+  if (!anAttrObjs || anAttrObjs->empty())
+    return true; // theAttribute does not references to anything
+  // check with all others
+  for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
+    if (theAttribute->id() == aRefIter->first)
+      continue; // do not check with myself
+    std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
+    for(; aReferenced != aRefIter->second.end(); aReferenced++) {
+      std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
+      for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
+        if (*aReferenced == *aReferencedByMe) // found same objects!
+          return false;
+      }
+    }
+  }
   return true;
 }