Salome HOME
Issue #273: Add copyright string
[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   // If the object is not a line then it is accepted
26   const ModelAPI_ResultValidator* aLineValidator =
27       dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultLine"));
28   if (!aLineValidator->isValid(theObject))
29     return true;
30
31   // If it is a line then we have to check that first attribute id not a line
32   std::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(theFeature->data(), aParamA);
33   if (aPoint)
34     return true;
35   return false;
36 }
37
38 bool SketchPlugin_DistanceAttrValidator::isValid(
39   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
40 {
41   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
42     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
43   if (anAttr) {
44     const ObjectPtr& anObj = theAttribute->owner();
45     const FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
46     return isValid(aFeature, theArguments, anAttr->object());
47   }
48   return true; // it may be not reference attribute, in this case, it is OK
49 }
50
51 bool SketchPlugin_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
52                                                  const std::list<std::string>& theArguments,
53                                                  const ObjectPtr& theObject) const
54 {
55   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
56     theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
57   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
58   for(; anAttr != anAttrs.end(); anAttr++) {
59     if (*anAttr) {
60       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
61         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
62       // check the object is already presented
63       if (aRef->isObject() && aRef->object() == theObject)
64         return false;
65     }
66   }
67   return true;
68 }
69
70 bool SketchPlugin_DifferentObjectsValidator::isValid(
71   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
72 {
73   std::shared_ptr<ModelAPI_AttributeRefAttr> anOrigAttr = 
74     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
75   if (anOrigAttr && anOrigAttr->isObject()) {
76     const ObjectPtr& anObj = theAttribute->owner();
77     const FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
78
79     std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
80       aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
81     std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
82     for(; anAttr != anAttrs.end(); anAttr++) {
83       if (*anAttr && *anAttr != theAttribute) {
84         std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
85           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
86         // check the object is already presented
87         if (aRef->isObject() && aRef->object() == anOrigAttr->object())
88           return false;
89       }
90     }
91   }
92   return true;
93 }
94
95 bool SketchPlugin_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
96   const std::list<std::string>& theArguments, const AttributePtr& theAttribute) const
97 {
98   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
99     theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
100   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
101   for(; anAttr != anAttrs.end(); anAttr++) {
102     if (*anAttr) {
103       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
104         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
105       // check the object is already presented
106       if (!aRef->isObject() && aRef->attr() == theAttribute)
107         return false;
108     }
109   }
110   return true;
111 }