Salome HOME
a613620761710efe89c4f014b9fc02deff3c3bac
[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
68
69
70 bool SketchPlugin_TangentAttrValidator::isValid(
71   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
72 {
73   // there is a check whether the feature contains a point and a linear edge or two point values
74   std::string aParamA = theArguments.front();
75   SessionPtr aMgr = ModelAPI_Session::get();
76   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
77
78   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
79   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
80   if (!aRefAttr)
81     return false;
82
83   bool isObject = aRefAttr->isObject();
84   ObjectPtr anObject = aRefAttr->object();
85   if (isObject && anObject) {
86     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
87
88     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
89     ObjectPtr aOtherObject = aOtherAttr->object();
90     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
91
92     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
93       if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
94         return false;
95     } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
96       if (aOtherFea->getKind() != SketchPlugin_Line::ID())
97         return false;
98     } else
99       return false;
100
101     // check that both have coincidence
102     FeaturePtr aConstrFeature;
103     std::set<FeaturePtr> aCoinList;
104     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
105     std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
106     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
107       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
108       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
109       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
110         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
111         AttributePtr aAR = aRAttr->attr();
112         if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
113           aCoinList.insert(aConstrFeature);
114       }
115     }
116     // if there is no coincidence then it is not valid
117     if (aCoinList.size() == 0)
118       return false;
119
120     // find that coincedence is the same
121     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
122     std::set<FeaturePtr>::const_iterator aCoinsIt;
123     for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
124       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
125       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
126       aCoinsIt = aCoinList.find(aConstrFeature);
127       if (aCoinsIt != aCoinList.end()) {
128         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
129         AttributePtr aAR = aRAttr->attr();
130         if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
131           return true;
132       }
133     }
134   }
135   return false;
136 }
137
138
139