Salome HOME
Issue #422 - Application hangs up when apply Fillet constraint on preselected segments
[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           aOtherFea->getKind() != SketchPlugin_Arc::ID())
100         return false;
101     } else
102       return false;
103
104     // check that both have coincidence
105     FeaturePtr aConstrFeature;
106     std::set<FeaturePtr> aCoinList;
107     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
108     std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
109     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
110       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
111       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
112       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
113         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
114         AttributePtr aAR = aRAttr->attr();
115         if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
116           aCoinList.insert(aConstrFeature);
117       }
118     }
119     // if there is no coincidence then it is not valid
120     if (aCoinList.size() == 0)
121       return false;
122
123     // find that coincedence is the same
124     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
125     std::set<FeaturePtr>::const_iterator aCoinsIt;
126     for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
127       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
128       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
129       aCoinsIt = aCoinList.find(aConstrFeature);
130       if (aCoinsIt != aCoinList.end()) {
131         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
132         AttributePtr aAR = aRAttr->attr();
133         if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
134           return true;
135       }
136     }
137   }
138   return false;
139 }
140
141 bool SketchPlugin_NotFixedValidator::isValid(
142     const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
143 {
144   std::shared_ptr<SketchPlugin_Feature> aFeature =
145       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
146   if (!aFeature)
147     return true;
148
149   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
150   if (!aRefAttr)
151     return false;
152
153   SketchPlugin_Sketch* aSketch = aFeature->sketch();
154   int aNbFeatures = aSketch->numberOfSubs();
155   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
156     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
157     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
158       continue;
159     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
160         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
161     if (aRefAttr->isObject()) {
162       if (aRefAttr->object() == aRAttr->object())
163         return false;
164     } else if (aRefAttr->attr() == aRAttr->attr())
165       return false;
166   }
167   return true;
168 }
169
170 bool SketchPlugin_EqualAttrValidator::isValid(
171   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
172 {
173   std::string aParamA = theArguments.front();
174   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
175   AttributeRefAttrPtr aRefAttr[2];
176   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
177   if (!aRefAttr)
178     return false;
179   aRefAttr[1] = aFeature->data()->refattr(aParamA);
180
181   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
182     return false;
183
184   int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
185   std::list<std::string> anArguments;
186   for (int i = 0; i < 2; i++) {
187     ObjectPtr anObject = aRefAttr[i]->object();
188     aFeature = ModelAPI_Feature::feature(anObject);
189     if (!aFeature)
190       return false;
191
192     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
193       aType[i] = 1;
194       continue;
195     }
196     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
197       aType[i] = 2;
198       continue;
199     }
200     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
201       aType[i] = 3;
202       continue;
203     }
204     // wrong type of attribute
205     return false;
206   }
207
208   if ((aType[0] == 1 && aType[1] == 2) ||
209       (aType[0] == 2 && aType[1] == 1))
210     return false;
211   return true;
212 }
213