--- /dev/null
+// 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;
+}
--- /dev/null
+// 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
#include <GeomValidators_ConstructionComposite.h>
#include <GeomValidators_ZeroOffset.h>
#include <GeomValidators_BooleanArguments.h>
+#include <GeomValidators_Different.h>
#include <ModelAPI_Object.h>
aFactory->registerValidator("PartSet_SameTypeAttr",
new PartSet_SameTypeAttrValidator);
+
+ aFactory->registerValidator("GeomValidators_Different",
+ new GeomValidators_Different);
}
void PartSet_Module::registerFilters()
<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"/>
<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>