Salome HOME
358c82c1b4982e73bf8875c5e31b952798c55d16
[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 "SketcherPrs_Tools.h"
14
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Validator.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_Session.h>
20
21 #include <GeomValidators_Edge.h>
22
23 #include <GeomDataAPI_Point2D.h>
24
25
26 bool SketchPlugin_DistanceAttrValidator::isValid(
27   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
28 {
29   // there is a check whether the feature contains a point and a linear edge or two point values
30   std::string aParamA = theArguments.front();
31   SessionPtr aMgr = ModelAPI_Session::get();
32   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
33
34   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
35   if (!aRefAttr)
36     return false;
37
38   bool isObject = aRefAttr->isObject();
39   if (!isObject) {
40     // an attribute is a point. A point value is valid always for the distance
41     return true;
42   } else {
43     // 1. check whether the references object is a linear
44     ObjectPtr anObject = aRefAttr->object();
45
46     const ModelAPI_AttributeValidator* anEdgeValidator = 
47       dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
48     std::list<std::string> anArguments;
49     anArguments.push_back("circle");
50     bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
51     // the circle line is not a valid case
52     if (anEdgeValid)
53       return false;
54       
55     anArguments.clear();
56     anArguments.push_back("line");
57     anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
58     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
59     if (!anEdgeValid)
60       return true;
61
62     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
63     // If it is a line then we have to check that first attribute id not a line
64     std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(aFeature->data(), aParamA);
65     if (aPoint)
66       return true;
67   }
68   return false;
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