Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Validators.cpp
1 // File:        SketchPlugin_Validators.cpp
2 // Created:     01 Aug 2014
3 // Author:      Vitaly SMETANNIKOV
4
5 #include "SketchPlugin_Validators.h"
6 #include "SketchPlugin_ConstraintDistance.h"
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_Validator.h>
9 #include <ModelAPI_ResultValidator.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <GeomDataAPI_Point2D.h>
13
14 bool SketchPlugin_DistanceAttrValidator::isValid(const FeaturePtr& theFeature,
15                                                  const std::list<std::string>& theArguments,
16                                                  const ObjectPtr& theObject) const
17 {
18   std::string aParamA = theArguments.front();
19   SessionPtr aMgr = ModelAPI_Session::get();
20   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
21
22   // If the object is not a line then it is accepted
23   const ModelAPI_ResultValidator* aLineValidator =
24       dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultLineValidator"));
25   if (!aLineValidator->isValid(theObject))
26     return true;
27
28   // If it is a line then we have to check that first attribute id not a line
29   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(theFeature->data(), aParamA);
30   if (aPoint)
31     return true;
32   return false;
33 }
34
35 bool SketchPlugin_DistanceAttrValidator::isValid(
36   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
37 {
38   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
39     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
40   if (anAttr) {
41     const ObjectPtr& anObj = theAttribute->owner();
42     const FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
43     return isValid(aFeature, theArguments, anAttr->object());
44   }
45   return true; // it may be not reference attribute, in this case, it is OK
46 }
47
48 bool SketchPlugin_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
49                                                  const std::list<std::string>& theArguments,
50                                                  const ObjectPtr& theObject) const
51 {
52   std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttrs = 
53     theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
54   std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
55   for(; anAttr != anAttrs.end(); anAttr++) {
56     if (*anAttr) {
57       boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
58         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
59       // check the object is already presented
60       if (aRef->isObject() && aRef->object() == theObject)
61         return false;
62     }
63   }
64   return true;
65 }
66
67 bool SketchPlugin_DifferentObjectsValidator::isValid(
68   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
69 {
70   boost::shared_ptr<ModelAPI_AttributeRefAttr> anOrigAttr = 
71     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
72   if (anOrigAttr && anOrigAttr->isObject()) {
73     const ObjectPtr& anObj = theAttribute->owner();
74     const FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
75
76     std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttrs = 
77       aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
78     std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
79     for(; anAttr != anAttrs.end(); anAttr++) {
80       if (*anAttr && *anAttr != theAttribute) {
81         boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
82           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
83         // check the object is already presented
84         if (aRef->isObject() && aRef->object() == anOrigAttr->object())
85           return false;
86       }
87     }
88   }
89   return true;
90 }