]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Validators.cpp
Salome HOME
fe88ebf595e516bc949a0b732905bf6c59cd8031
[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_ConstraintRigid.h"
11 #include "SketchPlugin_Line.h"
12 #include "SketchPlugin_Arc.h"
13 #include "SketchPlugin_Circle.h"
14
15 #include "SketcherPrs_Tools.h"
16
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_Session.h>
22
23 #include <GeomValidators_Edge.h>
24
25 #include <GeomDataAPI_Point2D.h>
26
27
28 bool SketchPlugin_DistanceAttrValidator::isValid(
29   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
30 {
31   // there is a check whether the feature contains a point and a linear edge or two point values
32   std::string aParamA = theArguments.front();
33   SessionPtr aMgr = ModelAPI_Session::get();
34   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
35
36   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
37   if (!aRefAttr)
38     return false;
39
40   bool isObject = aRefAttr->isObject();
41   if (!isObject) {
42     // an attribute is a point. A point value is valid always for the distance
43     return true;
44   } else {
45     // 1. check whether the references object is a linear
46     ObjectPtr anObject = aRefAttr->object();
47
48     const ModelAPI_AttributeValidator* anEdgeValidator = 
49       dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
50     std::list<std::string> anArguments;
51     anArguments.push_back("circle");
52     bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
53     // the circle line is not a valid case
54     if (anEdgeValid)
55       return false;
56       
57     anArguments.clear();
58     anArguments.push_back("line");
59     anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
60     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
61     if (!anEdgeValid)
62       return true;
63
64     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
65     // If it is a line then we have to check that first attribute id not a line
66     std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(aFeature->data(), aParamA);
67     if (aPoint)
68       return true;
69   }
70   return false;
71 }
72 bool SketchPlugin_TangentAttrValidator::isValid(
73   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
74 {
75   // there is a check whether the feature contains a point and a linear edge or two point values
76   std::string aParamA = theArguments.front();
77   SessionPtr aMgr = ModelAPI_Session::get();
78   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
79
80   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
81   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
82   if (!aRefAttr)
83     return false;
84
85   bool isObject = aRefAttr->isObject();
86   ObjectPtr anObject = aRefAttr->object();
87   if (isObject && anObject) {
88     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
89
90     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
91     ObjectPtr aOtherObject = aOtherAttr->object();
92     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
93
94     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
95       if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
96         return false;
97     } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
98       if (aOtherFea->getKind() != SketchPlugin_Line::ID())
99         return false;
100     } else
101       return false;
102
103     // check that both have coincidence
104     FeaturePtr aConstrFeature;
105     std::set<FeaturePtr> aCoinList;
106     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
107     std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
108     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
109       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
110       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
111       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
112         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
113         AttributePtr aAR = aRAttr->attr();
114         if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
115           aCoinList.insert(aConstrFeature);
116       }
117     }
118     // if there is no coincidence then it is not valid
119     if (aCoinList.size() == 0)
120       return false;
121
122     // find that coincedence is the same
123     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
124     std::set<FeaturePtr>::const_iterator aCoinsIt;
125     for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
126       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
127       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
128       aCoinsIt = aCoinList.find(aConstrFeature);
129       if (aCoinsIt != aCoinList.end()) {
130         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
131         AttributePtr aAR = aRAttr->attr();
132         if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
133           return true;
134       }
135     }
136   }
137   return false;
138 }
139
140 bool SketchPlugin_NotFixedValidator::isValid(
141     const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
142 {
143   std::shared_ptr<SketchPlugin_Feature> aFeature =
144       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
145   if (!aFeature)
146     return true;
147
148   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
149   if (!aRefAttr)
150     return false;
151
152   SketchPlugin_Sketch* aSketch = aFeature->sketch();
153   int aNbFeatures = aSketch->numberOfSubs();
154   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
155     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
156     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
157       continue;
158     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
159         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
160     if (aRefAttr->isObject()) {
161       if (aRefAttr->object() == aRAttr->object())
162         return false;
163     } else if (aRefAttr->attr() == aRAttr->attr())
164       return false;
165   }
166   return true;
167 }
168
169 bool SketchPlugin_EqualAttrValidator::isValid(
170   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
171 {
172   std::string aParamA = theArguments.front();
173   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
174   AttributeRefAttrPtr aRefAttr[2];
175   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
176   if (!aRefAttr)
177     return false;
178   aRefAttr[1] = aFeature->data()->refattr(aParamA);
179
180   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
181     return false;
182
183   int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
184   std::list<std::string> anArguments;
185   for (int i = 0; i < 2; i++) {
186     ObjectPtr anObject = aRefAttr[i]->object();
187     aFeature = ModelAPI_Feature::feature(anObject);
188     if (!aFeature)
189       return false;
190
191     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
192       aType[i] = 1;
193       continue;
194     }
195     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
196       aType[i] = 2;
197       continue;
198     }
199     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
200       aType[i] = 3;
201       continue;
202     }
203     // wrong type of attribute
204     return false;
205   }
206
207   if ((aType[0] == 1 && aType[1] == 2) ||
208       (aType[0] == 2 && aType[1] == 1))
209     return false;
210   return true;
211 }
212