Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Validators.cpp
4 // Created:     01 Aug 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketchPlugin_Validators.h"
8 #include "SketchPlugin_ConstraintDistance.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_ResultValidator.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_AttributeRefAttr.h>
14 #include <ModelAPI_Session.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 bool SketchPlugin_DistanceAttrValidator::isValid(const FeaturePtr& theFeature,
18                                                  const std::list<std::string>& theArguments,
19                                                  const ObjectPtr& theObject) const
20 {
21   std::string aParamA = theArguments.front();
22   SessionPtr aMgr = ModelAPI_Session::get();
23   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
24
25   const ModelAPI_ResultValidator* anArcValidator =
26       dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultArc"));
27   bool anArcValid = anArcValidator->isValid(theObject);
28   if (anArcValid)
29     return false;
30
31
32   // If the object is not a line then it is accepted
33   const ModelAPI_ResultValidator* aLineValidator =
34       dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultLine"));
35   bool aLineValid = aLineValidator->isValid(theObject);
36   if (!aLineValid)
37     return true;
38
39   // If it is a line then we have to check that first attribute id not a line
40   std::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(theFeature->data(), aParamA);
41   if (aPoint)
42     return true;
43   return false;
44 }
45
46 bool SketchPlugin_DistanceAttrValidator::isValid(
47   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
48 {
49   // any point attribute is acceptable for the distance operation
50   return true;
51 }
52
53 bool SketchPlugin_DistanceAttrValidator::isValid(const FeaturePtr& theFeature,
54                                                  const std::list<std::string>& theArguments,
55                                                  const AttributePtr& theAttribute) const
56 {
57   return isValid(theAttribute, theArguments);
58 }
59
60 bool SketchPlugin_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
61                                                  const std::list<std::string>& theArguments,
62                                                  const ObjectPtr& theObject) const
63 {
64   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
65     theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
66   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
67   for(; anAttr != anAttrs.end(); anAttr++) {
68     if (*anAttr) {
69       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
70         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
71       // check the object is already presented
72       if (aRef->isObject() && aRef->object() == theObject)
73         return false;
74     }
75   }
76   return true;
77 }
78
79 bool SketchPlugin_DifferentObjectsValidator::isValid(
80   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
81 {
82   std::shared_ptr<ModelAPI_AttributeRefAttr> anOrigAttr = 
83     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
84   if (anOrigAttr && anOrigAttr->isObject()) {
85     const ObjectPtr& anObj = theAttribute->owner();
86     const FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
87
88     std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
89       aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
90     std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
91     for(; anAttr != anAttrs.end(); anAttr++) {
92       if (*anAttr && *anAttr != theAttribute) {
93         std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
94           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
95         // check the object is already presented
96         if (aRef->isObject() && aRef->object() == anOrigAttr->object())
97           return false;
98       }
99     }
100   }
101   return true;
102 }
103
104 bool SketchPlugin_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
105   const std::list<std::string>& theArguments, const AttributePtr& theAttribute) const
106 {
107   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
108     theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
109   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
110   for(; anAttr != anAttrs.end(); anAttr++) {
111     if (*anAttr) {
112       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
113         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
114       // check the object is already presented
115       if (!aRef->isObject() && aRef->attr() == theAttribute)
116         return false;
117     }
118   }
119   return true;
120 }