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 "SketchPlugin_ConstraintCoincidence.h"
10 #include "SketchPlugin_Line.h"
11 #include "SketchPlugin_Arc.h"
12
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Validator.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_Session.h>
18
19 #include <GeomValidators_Edge.h>
20
21 #include <GeomDataAPI_Point2D.h>
22
23 bool SketchPlugin_DistanceAttrValidator::isValid(
24   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
25 {
26   // there is a check whether the feature contains a point and a linear edge or two point values
27   std::string aParamA = theArguments.front();
28   SessionPtr aMgr = ModelAPI_Session::get();
29   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
30
31   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
32   if (!aRefAttr)
33     return false;
34
35   bool isObject = aRefAttr->isObject();
36   if (!isObject) {
37     // an attribute is a point. A point value is valid always for the distance
38     return true;
39   } else {
40     // 1. check whether the references object is a linear
41     ObjectPtr anObject = aRefAttr->object();
42
43     const ModelAPI_AttributeValidator* anEdgeValidator = 
44       dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
45     std::list<std::string> anArguments;
46     anArguments.push_back("circle");
47     bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
48     // the circle line is not a valid case
49     if (anEdgeValid)
50       return false;
51       
52     anArguments.clear();
53     anArguments.push_back("line");
54     anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
55     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
56     if (!anEdgeValid)
57       return true;
58
59     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
60     // If it is a line then we have to check that first attribute id not a line
61     std::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(aFeature->data(), aParamA);
62     if (aPoint)
63       return true;
64   }
65   return false;
66 }
67 bool SketchPlugin_TangentAttrValidator::isValid(
68   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
69 {
70   // there is a check whether the feature contains a point and a linear edge or two point values
71   std::string aParamA = theArguments.front();
72   SessionPtr aMgr = ModelAPI_Session::get();
73   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
74
75   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
76   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
77   if (!aRefAttr)
78     return false;
79
80   bool isObject = aRefAttr->isObject();
81   ObjectPtr anObject = aRefAttr->object();
82   if (isObject && anObject) {
83     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
84
85     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
86     ObjectPtr aOtherObject = aOtherAttr->object();
87     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
88
89     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
90       if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
91         return false;
92     } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
93       if (aOtherFea->getKind() != SketchPlugin_Line::ID())
94         return false;
95     } else
96       return false;
97
98     // check that both have coincidence
99     FeaturePtr aConstrFeature;
100     std::set<FeaturePtr> aCoinList;
101     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
102     std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
103     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
104       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
105       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
106       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
107         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
108         AttributePtr aAR = aRAttr->attr();
109         if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
110           aCoinList.insert(aConstrFeature);
111       }
112     }
113     // if there is no coincidence then it is not valid
114     if (aCoinList.size() == 0)
115       return false;
116
117     // find that coincedence is the same
118     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
119     std::set<FeaturePtr>::const_iterator aCoinsIt;
120     for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
121       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
122       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
123       aCoinsIt = aCoinList.find(aConstrFeature);
124       if (aCoinsIt != aCoinList.end()) {
125         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
126         AttributePtr aAR = aRAttr->attr();
127         if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
128           return true;
129       }
130     }
131   }
132   return false;
133 }
134
135