Salome HOME
Merge branch 'BR_ADVANCED_CURRENT_FEATURE_MANAGEMENT' into Dev_1.5.0
[modules/shaper.git] / src / PartSet / PartSet_Validators.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 6d17784..99468bc
@@ -6,6 +6,8 @@
 
 #include "PartSet_Validators.h"
 
+#include "PartSet_Tools.h"
+
 #include <TopoDS.hxx>
 #include <TopoDS_Edge.hxx>
 #include <BRep_Tool.hxx>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Tools.h>
 
 #include <SketchPlugin_Sketch.h>
+#include <SketchPlugin_ConstraintCoincidence.h>
+#include <SketchPlugin_Arc.h>
+#include <GeomAPI_Edge.h>
 
 #include <list>
 #ifdef _DEBUG
@@ -31,7 +38,9 @@
 
 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
 {
-  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();  
+  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+  ModuleBase_ISelection::filterPreselectionOnEqualPoints(aList);
+
   int aCount = 0;
   foreach (ModuleBase_ViewerPrs aPrs, aList) {
     const TopoDS_Shape& aShape = aPrs.shape();
@@ -45,7 +54,7 @@ int shapesNbPoints(const ModuleBase_ISelection* theSelection)
 
 int shapesNbLines(const ModuleBase_ISelection* theSelection)
 {
-  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
+  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
   int aCount = 0;
   foreach(ModuleBase_ViewerPrs aPrs, aList) {
     const TopoDS_Shape& aShape = aPrs.shape();
@@ -63,33 +72,33 @@ int shapesNbLines(const ModuleBase_ISelection* theSelection)
   return aCount;
 }
 
-bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_DistanceSelection::isValid(const ModuleBase_ISelection* theSelection) const
 {
   int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
   return (aCount > 0) && (aCount < 3);
 }
 
-bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_LengthSelection::isValid(const ModuleBase_ISelection* theSelection) const
 {
   int aCount = shapesNbLines(theSelection);
-  return (aCount > 0) && (aCount < 2);
+  return (aCount == 1);
 }
 
-bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_PerpendicularSelection::isValid(const ModuleBase_ISelection* theSelection) const
 {
   int aCount = shapesNbLines(theSelection);
   return (aCount > 0) && (aCount < 3);
 }
 
-bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_ParallelSelection::isValid(const ModuleBase_ISelection* theSelection) const
 {
   int aCount = shapesNbLines(theSelection);
   return (aCount > 0) && (aCount < 3);
 }
 
-bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_RadiusSelection::isValid(const ModuleBase_ISelection* theSelection) const
 {
-  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
+  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
   ModuleBase_ViewerPrs aPrs;
   int aCount = 0;
   foreach (ModuleBase_ViewerPrs aPrs, aList) {
@@ -105,17 +114,119 @@ bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection)
       }
     }
   }
-  return (aCount > 0) && (aCount < 2);
+  return (aCount == 1);
+}
+
+bool PartSet_RigidSelection::isValid(const ModuleBase_ISelection* theSelection) const
+{
+  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+  return (aList.count() == 1);
+}
+
+
+bool PartSet_CoincidentSelection::isValid(const ModuleBase_ISelection* theSelection) const
+{
+  // Coincident can be applied to points and to lines
+  int aCount = shapesNbPoints(theSelection);
+  aCount += shapesNbLines(theSelection);
+  return (aCount > 0) && (aCount < 3);
 }
 
-bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
+bool PartSet_HVDirSelection::isValid(const ModuleBase_ISelection* theSelection) const
 {
   int aCount = shapesNbLines(theSelection);
-  return (aCount > 0) && (aCount < 2);
+  return (aCount == 1);
+}
+
+bool PartSet_FilletSelection::isValid(const ModuleBase_ISelection* theSelection) const
+{
+  int aCount = shapesNbLines(theSelection);
+  return (aCount > 0) && (aCount < 3);
+}
+
+bool PartSet_TangentSelection::isValid(const ModuleBase_ISelection* theSelection) const
+{
+  QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
+  if ((aList.size() == 0) || (aList.size() > 2))
+    return false;
+
+  ModuleBase_ViewerPrs aPrs = aList.first();
+  const TopoDS_Shape& aShape = aPrs.shape();
+  if (aShape.IsNull())
+    return false;
+
+  if (aShape.ShapeType() != TopAbs_EDGE)
+    return false;
+
+  std::shared_ptr<GeomAPI_Shape> aShapePtr(new GeomAPI_Shape);
+  aShapePtr->setImpl(new TopoDS_Shape(aShape));
+  GeomAPI_Edge aEdge1(aShapePtr);
+
+  if (aEdge1.isLine() || aEdge1.isArc()) {
+    if (aList.size() == 2) {
+      // Check second selection
+      aPrs = aList.last();
+      const TopoDS_Shape& aShape2 = aPrs.shape();
+      if (aShape2.IsNull())
+        return false;
+
+      if (aShape2.ShapeType() != TopAbs_EDGE)
+        return false;
+
+      std::shared_ptr<GeomAPI_Shape> aShapePtr2(new GeomAPI_Shape);
+      aShapePtr2->setImpl(new TopoDS_Shape(aShape2));
+      GeomAPI_Edge aEdge2(aShapePtr2);
+      if (aEdge1.isLine() && aEdge2.isArc())
+        return true;
+      else if (aEdge1.isArc() && aEdge2.isLine())
+        return true;
+      else
+        return false;
+    } else
+      return true;
+  }
+  return false;
+}
+
+bool PartSet_AngleSelection::isValid(const ModuleBase_ISelection* theSelection) const
+{
+  int aCount = shapesNbLines(theSelection);
+  return (aCount > 0) && (aCount < 3);
+}
+
+std::string PartSet_DifferentObjectsValidator::errorMessage(
+                         const PartSet_DifferentObjectsValidator::ErrorType& theType,
+                         const std::string& thEqualObject, const std::string& theFirstAttribute,
+                         const std::string& theSecondAttribute) const
+{
+  std::string anError;
+  switch (theType) {
+    case EqualObjects:
+      anError = "The feature uses one " + thEqualObject + " object in " +
+                theFirstAttribute + " and " + theSecondAttribute + " attributes.";
+      break;
+    case EqualAttributes:
+      anError = "The feature uses reference to one " + thEqualObject + " attribute in " +
+                theFirstAttribute + " and " + theSecondAttribute + " attributes.";
+      break;
+    case EqualShapes:
+      anError = "The feature uses one shape in " +
+                theFirstAttribute + " and " + theSecondAttribute + " attributes.";
+      break;
+    case EmptyShapes:
+      anError = "The feature uses empty shapes in " +
+                theFirstAttribute + " and " + theSecondAttribute + " attributes.";
+      break;
+      break;
+    default:
+      break;
+  }
+  return anError;
 }
 
 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute, 
-                                                const std::list<std::string>& theArguments) const
+                                                const std::list<std::string>& theArguments,
+                                                std::string& theError) const
 {
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
 
@@ -140,18 +251,25 @@ bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute
           if (aRef->isObject() != isObject)
             continue;
           if (isObject) {
-            if (aRef->object() == anObject)
+            if (aRef->object() == anObject) {
+              theError = errorMessage(EqualObjects, anObject.get() ? anObject->data()->name() : "",
+                                      theAttribute->id(), aRef->id());
               return false;
+            }
           }
           else { // the attribute reference
-            if (aRef->attr() == theAttribute)
+            if (aRef->attr() == anAttributeAttr) {
+              theError = errorMessage(EqualAttributes,
+                                      anAttributeAttr.get() ? anAttributeAttr->id() : "",
+                                      theAttribute->id(), aRef->id());
               return false;
+            }
           }
         }
       }
     }
   }
-  else if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
+  else if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
     ResultPtr aContext = anAttr->context();
     GeomShapePtr aShape = anAttr->value();
@@ -167,8 +285,10 @@ bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute
           // check the object is already presented
           if (aRef->context() == aContext) {
             bool aHasShape = aShape.get() != NULL;
-            if (!aHasShape || aRef->value()->isEqual(aShape))
+            if (!aHasShape || aRef->value()->isEqual(aShape)) {
+              theError = errorMessage(EqualShapes, "", theAttribute->id(), aRef->id());
               return false;
+            }
           }
         }
       }
@@ -186,69 +306,145 @@ bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute
           std::shared_ptr<ModelAPI_AttributeReference> aRef =
             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
           // check the object is already presented
-          if (aRef->value() == anObject)
+          if (aRef->value() == anObject) {
+            theError = errorMessage(EqualObjects, anObject.get() ? anObject->data()->name() : "",
+                                    theAttribute->id(), aRef->id());
             return false;
+          }
         }
         return true;
       }
     }
   }
-  return !featureHasReferences(theAttribute);
-}
-
-bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
-{
-  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);
+  else if(anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
+    std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
+            std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+    anAttrs = aFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
+    if(anAttrs.size() > 0) {
+      std::list<std::shared_ptr<ModelAPI_Attribute>>::iterator anAttrItr = anAttrs.begin();
+      for(; anAttrItr != anAttrs.end(); anAttrItr++){
+        if ((*anAttrItr).get() && (*anAttrItr)->id() != theAttribute->id()){
+          std::shared_ptr<ModelAPI_AttributeSelectionList> aRefSelList = 
+            std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*anAttrItr);
+          for(int i = 0; i < aCurSelList->size(); i++) {
+            std::shared_ptr<ModelAPI_AttributeSelection> aCurSel = aCurSelList->value(i);
+            ResultPtr aCurSelContext = aCurSel->context();
+            ResultCompSolidPtr aCurSelCompSolidPtr = ModelAPI_Tools::compSolidOwner(aCurSelContext);
+            std::shared_ptr<GeomAPI_Shape> aCurSelCompSolid;
+            if(aCurSelCompSolidPtr.get()) {
+              aCurSelCompSolid = aCurSelCompSolidPtr->shape();
+            }
+            for(int j = 0; j < aRefSelList->size(); j++) {
+              std::shared_ptr<ModelAPI_AttributeSelection> aRefSel = aRefSelList->value(j);
+              ResultPtr aRefSelContext = aRefSel->context();
+              ResultCompSolidPtr aRefSelCompSolidPtr = ModelAPI_Tools::compSolidOwner(aRefSelContext);
+              std::shared_ptr<GeomAPI_Shape> aRefSelCompSolid;
+              if(aRefSelCompSolidPtr.get()) {
+                aRefSelCompSolid = aRefSelCompSolidPtr->shape();
+              }
+              if ((aCurSelCompSolid.get() && aCurSelCompSolid->isEqual(aRefSel->value()))
+                || (aRefSelCompSolid.get() && aRefSelCompSolid->isEqual(aCurSel->value()))) {
+                  theError = errorMessage(EqualShapes, "", theAttribute->id(),
+                                          aRefSel->id());
+                  return false;
+              }
+              if(aCurSelContext == aRefSelContext) {
+                if (aCurSel->value().get() == NULL || aRefSel->value().get() == NULL) {
+                  theError = errorMessage(EmptyShapes, "", theAttribute->id(),
+                                          aRefSel->id());
+                  return false;
+                }
+                if (aCurSel->value()->isEqual(aRefSel->value())) {
+                  theError = errorMessage(EqualShapes, "", theAttribute->id(),
+                                          aRefSel->id());
+                  return false;
+                }
+              }
+            }
+          }
+        }
+      }
+    }
   }
-  if (!anAttrObjs || anAttrObjs->empty())
-    return false; // 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 true;
+  else if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
+    std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
+      std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
+    anAttrs = aFeature->data()->attributes(ModelAPI_AttributeRefList::typeId());
+    if (anAttrs.size() > 0) {
+      std::list<std::shared_ptr<ModelAPI_Attribute>>::iterator anAttrItr = anAttrs.begin();
+      for (; anAttrItr != anAttrs.end(); anAttrItr++){
+        if ((*anAttrItr).get() && (*anAttrItr)->id() != theAttribute->id()){
+          std::shared_ptr<ModelAPI_AttributeRefList> aRefSelList =
+            std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anAttrItr);
+          for (int i = 0; i < aCurSelList->size(); i++) {
+            ObjectPtr aCurSelObject = aCurSelList->object(i);
+            for (int j = 0; j < aRefSelList->size(); j++) {
+              if (aCurSelObject == aRefSelList->object(j)) {
+                theError = errorMessage(EqualObjects,
+                              aCurSelObject.get() ? aCurSelObject->data()->name() : "",
+                              theAttribute->id(), aCurSelList->id());
+                return false;
+              }
+            }
+          }
+        }
       }
     }
   }
-  return false;
+  return true;
 }
 
 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
-                                            const std::list<std::string>& theArguments) const
+                                            const std::list<std::string>& theArguments,
+                                            std::string& theError) const
 {
   bool isSketchEntities = true;
   std::set<std::string> anEntityKinds;
+  std::string anEntityKindsStr;
   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
   for (; anIt != aLast; anIt++) {
     anEntityKinds.insert(*anIt);
+    if (!anEntityKindsStr.empty())
+      anEntityKindsStr += ", ";
+    anEntityKindsStr += *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++) {
+    for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize && isSketchEntities; i++) {
       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
       ObjectPtr anObject = aSelectAttr->context();
-      FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
-      isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
+      // a context of the selection attribute is a feature result. It can be a case when the result
+      // of the feature is null, e.g. the feature is modified and has not been executed yet.
+      // The validator returns an invalid result here. The case is an extrusion built on a sketch
+      // feature. A new sketch element creation leads to an empty result.
+      if (!anObject.get())
+        isSketchEntities = false;
+      else {
+        FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
+        isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
+      }
+    }
+  }
+  if (anAttributeType == ModelAPI_AttributeRefList::typeId()) {
+    AttributeRefListPtr aRefListAttr =
+      std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
+    // all context objects should be sketch entities
+    for (int i = 0, aSize = aRefListAttr->size(); i < aSize && isSketchEntities; i++) {
+      ObjectPtr anObject = aRefListAttr->object(i);
+      // a context of the selection attribute is a feature result. It can be a case when the result
+      // of the feature is null, e.g. the feature is modified and has not been executed yet.
+      // The validator returns an invalid result here. The case is an extrusion built on a sketch
+      // feature. A new sketch element creation leads to an empty result.
+      if (!anObject.get())
+        isSketchEntities = false;
+      else {
+        FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
+        isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
+      }
     }
   }
   if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
@@ -264,15 +460,22 @@ bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
       }
     }
   }
+  if (!isSketchEntities) {
+    theError = "It refers to feature, which kind is not in the list: " + anEntityKindsStr;
+  }
 
   return isSketchEntities;
 }
 
-
-
-bool PartSet_SameTypeAttrValidator::isValid(
-  const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
+bool PartSet_CoincidentAttr::isValid(const AttributePtr& theAttribute, 
+                                     const std::list<std::string>& theArguments,
+                                     std::string& theError) const
 {
+  if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
+    theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
+    return false;
+  }
+
   // there is a check whether the feature contains a point and a linear edge or two point values
   std::string aParamA = theArguments.front();
   SessionPtr aMgr = ModelAPI_Session::get();
@@ -280,19 +483,50 @@ bool PartSet_SameTypeAttrValidator::isValid(
 
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
-  if (!aRefAttr)
-    return false;
+  QList<FeaturePtr> aCoinsideLines;
 
   bool isObject = aRefAttr->isObject();
   ObjectPtr anObject = aRefAttr->object();
   if (isObject && anObject) {
     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
-
     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
     ObjectPtr aOtherObject = aOtherAttr->object();
+    // if the other attribute is not filled still, the result is true
+    if (!aOtherObject.get())
+      return true;
     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
-    return aRefFea->getKind() == aOtherFea->getKind();
+
+    // check that both have coincidence
+    FeaturePtr aConstrFeature;
+    std::set<FeaturePtr> aCoinList;
+    const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
+    std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
+    for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
+      std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
+      aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
+      if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
+        AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
+        AttributePtr aAR = aRAttr->attr();
+        if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
+          aCoinList.insert(aConstrFeature);
+          PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines,
+                                          SketchPlugin_ConstraintCoincidence::ENTITY_A());
+          PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines,
+                                          SketchPlugin_ConstraintCoincidence::ENTITY_B());
+      }
+    }
+    // if there is no coincidence then it is not valid
+    if (aCoinList.size() > 0) {
+      QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(), aLast = aCoinsideLines.end();
+      bool aValid = false;
+      for (; anIt != aLast && !aValid; anIt++) {
+        aValid = *anIt == aOtherFea;
+      }
+      if (aValid)
+        return true;
+    }
   }
+  theError = "There is no a common coincident point.";
   return false;
 }