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 <ModelAPI_Session.h>
13 #include <GeomDataAPI_Point2D.h>
14
15 bool SketchPlugin_DistanceAttrValidator::isValid(const FeaturePtr& theFeature,
16                                                  const std::list<std::string>& theArguments,
17                                                  const ObjectPtr& theObject) const
18 {
19   std::string aParamA = theArguments.front();
20   SessionPtr aMgr = ModelAPI_Session::get();
21   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
22
23   // If the object is not a line then it is accepted
24   const ModelAPI_ResultValidator* aLineValidator =
25       dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultLine"));
26   if (!aLineValidator->isValid(theObject))
27     return true;
28
29   // If it is a line then we have to check that first attribute id not a line
30   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(theFeature->data(), aParamA);
31   if (aPoint)
32     return true;
33   return false;
34 }
35
36 bool SketchPlugin_DistanceAttrValidator::isValid(
37   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
38 {
39   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
40     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
41   if (anAttr) {
42     const ObjectPtr& anObj = theAttribute->owner();
43     const FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
44     return isValid(aFeature, theArguments, anAttr->object());
45   }
46   return true; // it may be not reference attribute, in this case, it is OK
47 }
48
49 bool SketchPlugin_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
50                                                  const std::list<std::string>& theArguments,
51                                                  const ObjectPtr& theObject) const
52 {
53   std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttrs = 
54     theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
55   std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
56   for(; anAttr != anAttrs.end(); anAttr++) {
57     if (*anAttr) {
58       boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
59         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
60       // check the object is already presented
61       if (aRef->isObject() && aRef->object() == theObject)
62         return false;
63     }
64   }
65   return true;
66 }
67
68 bool SketchPlugin_DifferentObjectsValidator::isValid(
69   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
70 {
71   boost::shared_ptr<ModelAPI_AttributeRefAttr> anOrigAttr = 
72     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
73   if (anOrigAttr && anOrigAttr->isObject()) {
74     const ObjectPtr& anObj = theAttribute->owner();
75     const FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
76
77     std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttrs = 
78       aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
79     std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
80     for(; anAttr != anAttrs.end(); anAttr++) {
81       if (*anAttr && *anAttr != theAttribute) {
82         boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
83           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
84         // check the object is already presented
85         if (aRef->isObject() && aRef->object() == anOrigAttr->object())
86           return false;
87       }
88     }
89   }
90   return true;
91 }