Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom 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(
18   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
19 {
20   // there is a check whether the feature contains a point and a linear edge or two point values
21   std::string aParamA = theArguments.front();
22   SessionPtr aMgr = ModelAPI_Session::get();
23   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
24
25   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
26   if (!aRefAttr)
27     return false;
28
29   bool isObject = aRefAttr->isObject();
30   if (!isObject) {
31     // an attribute is a point. A point value is valid always for the distance
32     return true;
33   } else {
34     // 1. check whether the references object is a linear
35     ObjectPtr anObject = aRefAttr->object();
36     const ModelAPI_ResultValidator* anArcValidator =
37         dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultArc"));
38     bool anArcValid = anArcValidator->isValid(anObject);
39     if (anArcValid)
40       return false;
41
42     // If the object is not a line then it is accepted. It can be a point feature selected
43     const ModelAPI_ResultValidator* aLineValidator =
44         dynamic_cast<const ModelAPI_ResultValidator*>(aFactory->validator("SketchPlugin_ResultLine"));
45     bool aLineValid = aLineValidator->isValid(anObject);
46     if (!aLineValid)
47       return true;
48     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
49
50     // If it is a line then we have to check that first attribute id not a line
51     std::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(aFeature->data(), aParamA);
52     if (aPoint)
53       return true;
54   }
55   return false;
56 }
57
58 //bool SketchPlugin_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
59 //                                                 const std::list<std::string>& theArguments,
60 //                                                 const ObjectPtr& theObject) const
61 //{
62 //  std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
63 //    theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
64 //  std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
65 //  for(; anAttr != anAttrs.end(); anAttr++) {
66 //    if (*anAttr) {
67 //      std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
68 //        std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
69 //      // check the object is already presented
70 //      if (aRef->isObject() && aRef->object() == theObject)
71 //        return false;
72 //    }
73 //  }
74 //  return true;
75 //}
76
77 //bool SketchPlugin_DifferentObjectsValidator::isValid(
78 //  const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
79 //{
80 //  std::shared_ptr<ModelAPI_AttributeRefAttr> anOrigAttr = 
81 //    std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
82 //  if (anOrigAttr && anOrigAttr->isObject()) {
83 //    const ObjectPtr& anObj = theAttribute->owner();
84 //    const FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
85 //
86 //    std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
87 //      aFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
88 //    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
89 //    for(; anAttr != anAttrs.end(); anAttr++) {
90 //      if (*anAttr && *anAttr != theAttribute) {
91 //        std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
92 //          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
93 //        // check the object is already presented
94 //        if (aRef->isObject() && aRef->object() == anOrigAttr->object())
95 //          return false;
96 //      }
97 //    }
98 //  }
99 //  return true;
100 //}
101
102 //bool SketchPlugin_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
103 //  const std::list<std::string>& theArguments, const AttributePtr& theAttribute) const
104 //{
105 //  std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
106 //    theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
107 //  std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
108 //  for(; anAttr != anAttrs.end(); anAttr++) {
109 //    if (*anAttr) {
110 //      std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
111 //        std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
112 //      // check the object is already presented
113 //      if (!aRef->isObject() && aRef->attr() == theAttribute)
114 //        return false;
115 //    }
116 //  }
117 //  return true;
118 //}