Salome HOME
Issue #653 - Double and triple click edges
authorspo <sergey.pokhodenko@opencascade.com>
Wed, 8 Jul 2015 14:03:05 +0000 (17:03 +0300)
committerspo <sergey.pokhodenko@opencascade.com>
Wed, 8 Jul 2015 14:04:05 +0000 (17:04 +0300)
src/GeomValidators/CMakeLists.txt
src/GeomValidators/GeomValidators_Different.cpp [new file with mode: 0644]
src/GeomValidators/GeomValidators_Different.h [new file with mode: 0644]
src/PartSet/PartSet_Module.cpp
src/SketchPlugin/plugin-Sketch.xml

index 07004c22a5449c406343afea46af03dfe570f538..ced6fea05a7d4820c01746227c35445591766e87 100644 (file)
@@ -11,6 +11,7 @@ SET(PROJECT_HEADERS
     GeomValidators_ShapeType.h
     GeomValidators_Tools.h
     GeomValidators_ZeroOffset.h
+    GeomValidators_Different.h
 )
 
 SET(PROJECT_SOURCES
@@ -21,6 +22,7 @@ SET(PROJECT_SOURCES
     GeomValidators_ShapeType.cpp
     GeomValidators_Tools.cpp
     GeomValidators_ZeroOffset.cpp
+    GeomValidators_Different.cpp
 )
 
 SET(PROJECT_LIBRARIES
diff --git a/src/GeomValidators/GeomValidators_Different.cpp b/src/GeomValidators/GeomValidators_Different.cpp
new file mode 100644 (file)
index 0000000..65b834d
--- /dev/null
@@ -0,0 +1,83 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
+// File:        GeomValidators_Different.cpp
+// Created:     08 July 2015
+// Author:      Sergey POKHODENKO
+
+#include <GeomValidators_Different.h>
+
+#include <GeomDataAPI_Point2D.h>
+
+#include <algorithm>
+#include <map>
+#include <list>
+
+//=================================================================================================
+/* Help
+To extend GeomValidators_Different validator with new attribute types:
+1. Modify function isEqualAttributes
+2. Create new implementation of isEqual() for the new type
+*/
+//=================================================================================================
+
+bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
+{
+  return theLeft->x() == theRight->x() &&
+         theLeft->y() == theRight->y();
+}
+
+bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
+{
+  if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() && 
+      theRight->attributeType() == GeomDataAPI_Point2D::typeId())
+    return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
+                   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
+  return false;
+}
+
+class IsEqual {
+  AttributePtr myAttribute;
+public:
+  IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
+  bool operator()(const AttributePtr& theAttribute) {
+    return isEqualAttributes(myAttribute, theAttribute);
+  }
+};
+
+bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
+                                       const std::list<std::string>& theArguments) const
+{
+  std::map<std::string, std::list<AttributePtr> > anAttributesMap;
+  // For all attributes referred by theArguments 
+  // sort it using attributeType() and store into anAttributesMap 
+  std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
+  for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
+    AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
+    anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
+  }
+
+  // Search differences inside each attribute list
+  std::map<std::string, std::list<AttributePtr> >::const_iterator anAttributesMapIt = anAttributesMap.begin();
+  for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
+    const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
+    // for the list of attributes check that all elements are unique
+    std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
+    if (anAttributeIt != anAttributes.end()) {
+      std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
+      while (aNextIt != anAttributes.end()) {
+        // if equal attribute is found then all attributes are not different
+        if (std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt)) != anAttributes.end()) 
+          return false;
+        ++anAttributeIt;
+        ++aNextIt; 
+      }
+    }
+  }
+
+  return true;
+}
+
+bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)
+{
+  return true;
+}
diff --git a/src/GeomValidators/GeomValidators_Different.h b/src/GeomValidators/GeomValidators_Different.h
new file mode 100644 (file)
index 0000000..14380dd
--- /dev/null
@@ -0,0 +1,32 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
+// File:        GeomValidators_Different.h
+// Created:     08 July 2015
+// Author:      Sergey POKHODENKO
+
+#ifndef GeomValidators_Different_H
+#define GeomValidators_Different_H
+
+#include <GeomValidators.h>
+#include <ModelAPI_Feature.h>
+#include <ModelAPI_FeatureValidator.h>
+
+/** \class GeomValidators_Different
+ *  \ingroup Validators
+ *  \brief Validates that attributes are not the same.
+ */
+class GeomValidators_Different : public ModelAPI_FeatureValidator
+{
+public:
+  /** \brief Returns true if feature and/or attributes are valid.
+   *  \param[in] theFeature the validated feature.
+   *  \param[in] theArguments the arguments in the configuration file for this validator.
+   *  \returns true if feature is valid.
+   */
+  GEOMVALIDATORS_EXPORT virtual bool isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
+                                             const std::list<std::string>& theArguments) const;
+
+  GEOMVALIDATORS_EXPORT virtual bool isNotObligatory(std::string theFeature, std::string theAttribute);
+};
+
+#endif
index e518c7df33a602a001d1e9ce1823bcac0634b3f6..cce9ffc82ed3bef301a32acc003fa38283d6e01d 100644 (file)
@@ -36,6 +36,7 @@
 #include <GeomValidators_ConstructionComposite.h>
 #include <GeomValidators_ZeroOffset.h>
 #include <GeomValidators_BooleanArguments.h>
+#include <GeomValidators_Different.h>
 
 
 #include <ModelAPI_Object.h>
@@ -208,6 +209,9 @@ void PartSet_Module::registerValidators()
 
   aFactory->registerValidator("PartSet_SameTypeAttr",
                               new PartSet_SameTypeAttrValidator);
+
+  aFactory->registerValidator("GeomValidators_Different",
+                              new GeomValidators_Different);
 }
 
 void PartSet_Module::registerFilters()
index 9cdbb2b0e88e4c21f8677a7162993aadfcca2220..626ec3089ea0219e799e66fa394f1cc564d47ce2 100644 (file)
@@ -23,6 +23,7 @@
         <sketch-2dpoint_selector id="StartPoint" title="Start point" tooltip="Start point coordinates" previous_feature_param="EndPoint"/>
         <sketch-2dpoint_selector id="EndPoint" title="End point" tooltip="End point coordinates"/>
         <boolvalue id="Auxiliary" label="Auxiliary" default="false" tooltip="Construction element" obligatory="0"/>
+        <validator id="GeomValidators_Different" parameters="StartPoint,EndPoint"/>
       </feature>
       <feature id="SketchCircle" title="Circle" tooltip="Create circle" icon=":icons/circle.png">
         <sketch-2dpoint_selector id="CircleCenter" title="Center" tooltip="Center coordinates"/>
@@ -36,6 +37,7 @@
         <sketch-2dpoint_selector id="ArcStartPoint" title="Start point" tooltip="Start point"/>
         <sketch-2dpoint_selector id="ArcEndPoint" title="End point" tooltip="End point"/>
         <boolvalue id="Auxiliary" label="Auxiliary" default="false" tooltip="Construction element" obligatory="0"/>
+        <validator id="GeomValidators_Different" parameters="ArcCenter,ArcStartPoint,ArcEndPoint"/>
       </feature>
     </group>