Salome HOME
Tangency arc feature can not be opened/saved (issue #1401)
[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
9 #include "SketchPlugin_Arc.h"
10 #include "SketchPlugin_Circle.h"
11 #include "SketchPlugin_ConstraintCoincidence.h"
12 #include "SketchPlugin_ConstraintDistance.h"
13 #include "SketchPlugin_ConstraintFillet.h"
14 #include "SketchPlugin_ConstraintRigid.h"
15 #include "SketchPlugin_ConstraintTangent.h"
16 #include "SketchPlugin_Line.h"
17 #include "SketchPlugin_Point.h"
18 #include "SketchPlugin_Sketch.h"
19 #include "SketchPlugin_Tools.h"
20
21 #include "SketcherPrs_Tools.h"
22
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeRefAttr.h>
27
28 #include <ModelAPI_AttributeRefAttrList.h>
29 #include <ModelAPI_AttributeRefList.h>
30 #include <ModelAPI_AttributeSelectionList.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_ResultConstruction.h>
34
35 #include <GeomAPI_Lin.h>
36 #include <GeomAPI_Edge.h>
37 #include <GeomAPI_Vertex.h>
38 #include <GeomDataAPI_Point2D.h>
39
40 #include <algorithm>
41 #include <cmath>
42
43 const double tolerance = 1.e-7;
44
45 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute, 
46                                                  const std::list<std::string>& theArguments,
47                                                  std::string& theError) const
48 {
49   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
50     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
51     return false;
52   }
53
54   // there is a check whether the feature contains a point and a linear edge or two point values
55   std::string aParamA = theArguments.front();
56   SessionPtr aMgr = ModelAPI_Session::get();
57   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
58
59   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
60   bool isObject = aRefAttr->isObject();
61   if (!isObject) {
62     // an attribute is a point. A point value is valid always for the distance
63     return true;
64   } else {
65     // 1. check whether the references object is a linear
66     ObjectPtr anObject = aRefAttr->object();
67
68     const ModelAPI_AttributeValidator* aShapeValidator = 
69       dynamic_cast<const ModelAPI_AttributeValidator*>(aFactory->validator("GeomValidators_ShapeType"));
70     std::list<std::string> anArguments;
71     anArguments.push_back("circle");
72     std::string aCircleError;
73     bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
74     // the circle line is not a valid case
75     if (aShapeValid) {
76       theError = "Circle can not be used in distance constraint";
77       return false;
78     }
79       
80     anArguments.clear();
81     anArguments.push_back("line");
82     std::string aLineError;
83     aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
84     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
85     if (aShapeValid) {
86       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
87       // If it is a line then we have to check that first attribute id not a line
88       std::shared_ptr<SketchPlugin_Feature> aSFeature =
89         std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
90       SketchPlugin_Sketch* aSketch = aSFeature->sketch();
91       std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
92       std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
93         aFeature->data(), aParamA, aPlane);
94       if (!aPoint.get()) {
95         theError = "One of parameters of distance constraint should be a point";
96         return false;
97       }
98     }
99   }
100   return true;
101 }
102
103
104 static bool hasCoincidentPoint(FeaturePtr theFeature1, FeaturePtr theFeature2)
105 {
106   FeaturePtr aConstrFeature;
107   std::list<AttributePtr> anAttrList;
108   if (theFeature1->getKind() == SketchPlugin_Circle::ID() ||
109       theFeature2->getKind() == SketchPlugin_Circle::ID())
110     return false;
111   if (theFeature2->getKind() == SketchPlugin_Line::ID()) {
112     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::START_ID()));
113     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::END_ID()));
114   } else if (theFeature2->getKind() == SketchPlugin_Arc::ID()) {
115     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::START_ID()));
116     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::END_ID()));
117   }
118
119   const std::set<AttributePtr>& aRefsList = theFeature1->data()->refsToMe();
120   std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
121   for (; aRefIt != aRefsList.end(); ++aRefIt) {
122     aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
123     if (aConstrFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
124       continue;
125     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
126     AttributePtr anAttr = aRefAttr->attr();
127     if (anAttr->id() == SketchPlugin_Arc::CENTER_ID())
128       continue;
129
130     anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A());
131     if (anAttr == *aRefIt)
132       anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B());
133
134     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
135     if (!aRefAttr)
136       continue;
137     anAttr = aRefAttr->attr();
138     for (std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
139          anIt != anAttrList.end(); ++anIt)
140       if (*anIt == anAttr)
141         return true;
142   }
143   return false;
144 }
145
146 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute, 
147                                                 const std::list<std::string>& theArguments,
148                                                 std::string& theError) const
149 {
150   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
151     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
152     return false;
153   }
154
155   // there is a check whether the feature contains a point and a linear edge or two point values
156   std::string aParamA = theArguments.front();
157   SessionPtr aMgr = ModelAPI_Session::get();
158   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
159
160   FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
161   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
162
163   bool isObject = aRefAttr->isObject();
164   ObjectPtr anObject = aRefAttr->object();
165   if (isObject && anObject.get()) {
166     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
167
168     AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
169     ObjectPtr aOtherObject = aOtherAttr->object();
170     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
171     if (!aOtherFea)
172       return true;
173
174     if ((aRefFea->getKind() == SketchPlugin_Arc::ID() ||
175         aOtherFea->getKind() == SketchPlugin_Arc::ID()) &&
176         !hasCoincidentPoint(aRefFea, aOtherFea))
177       return false;
178
179     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
180       if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
181           aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
182         theError = "It refers to a " + SketchPlugin_Line::ID() + ", but " + aParamA + " is neither an "
183           + SketchPlugin_Arc::ID() + " nor " + SketchPlugin_Circle::ID();
184         return false;
185       }
186     }
187     else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
188       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
189         aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
190         theError = "It refers to an " + SketchPlugin_Arc::ID() + ", but " + aParamA + " is not a "
191           + SketchPlugin_Line::ID() + " or an " + SketchPlugin_Arc::ID();
192         return false;
193       }
194     }
195     else if (aRefFea->getKind() == SketchPlugin_Circle::ID()) {
196       if (aOtherFea->getKind() != SketchPlugin_Line::ID()) {
197         theError = "It refers to an " + SketchPlugin_Circle::ID() + ", but " + aParamA + " is not a "
198           + SketchPlugin_Line::ID();
199         return false;
200       }
201     }
202     else {
203       theError = "It refers to " + aRefFea->getKind() + ", but should refer to " + SketchPlugin_Line::ID()
204         + " or " + SketchPlugin_Arc::ID() + " or " + SketchPlugin_Circle::ID();
205       return false;
206     }
207     return true;
208   }
209   else {
210     theError = "It uses an empty object";
211     return false;
212   }
213
214   return true;
215 }
216
217 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
218                                              const std::list<std::string>& theArguments,
219                                              std::string& theError) const
220 {
221   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
222     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
223     return false;
224   }
225
226   std::shared_ptr<SketchPlugin_Feature> aFeature =
227       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
228   if (!aFeature)
229     return true;
230
231   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
232
233   SketchPlugin_Sketch* aSketch = aFeature->sketch();
234   int aNbFeatures = aSketch->numberOfSubs();
235   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
236     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
237     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
238       continue;
239     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
240         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
241     if (aRefAttr->isObject()) {
242       if (aRefAttr->object() == aRAttr->object()) {
243         ObjectPtr anObject = aRefAttr->object();
244         std::string aName = anObject.get() ? anObject->data()->name() : "";
245         theError = "The object " + aName + " has been already fixed.";
246         return false;
247       }
248     }
249     else if (aRefAttr->attr() == aRAttr->attr()) {
250       AttributePtr anAttribute = aRefAttr->attr();
251       std::string aName = anAttribute.get() ? anAttribute->id() : "";
252       theError = "The attribute " + aName + " has been already fixed.";
253       return false;
254     }
255   }
256   return true;
257 }
258
259 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute, 
260                                               const std::list<std::string>& theArguments,
261                                               std::string& theError) const
262 {
263   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
264     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
265     return false;
266   }
267
268   std::string aParamA = theArguments.front();
269   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
270   AttributeRefAttrPtr aRefAttr[2];
271   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
272   aRefAttr[1] = aFeature->data()->refattr(aParamA);
273
274   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
275     theError = "Attributes can not be used in equal constraint";
276     return false;
277   }
278
279   std::string aType[2];
280   std::list<std::string> anArguments;
281   for (int i = 0; i < 2; i++) {
282     ObjectPtr anObject = aRefAttr[i]->object();
283     if (!anObject.get()) {
284       theError = "An empty object is used.";
285       return false;
286     }
287
288     aFeature = ModelAPI_Feature::feature(anObject);
289     if (!aFeature.get()) {
290       theError = "An empty feature is used.";
291       return false;
292     }
293
294     aType[i] = aFeature->getKind();
295     if (aFeature->getKind() != SketchPlugin_Line::ID() &&
296         aFeature->getKind() != SketchPlugin_Circle::ID() &&
297         aFeature->getKind() != SketchPlugin_Arc::ID()) {
298       theError = "The " + aFeature->getKind() + " feature kind of attribute is wrong. It should be " +
299                  SketchPlugin_Line::ID() + " or " + SketchPlugin_Circle::ID() + " or " + 
300                  SketchPlugin_Arc::ID();
301       // wrong type of attribute
302       return false;
303     }
304   }
305
306   if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
307       aType[0] != aType[1]) {
308     theError = "Feature with kinds " + aType[0] + " and " + aType[1] + "can not be equal.";
309     return false;
310   }
311   return true;
312 }
313
314 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute, 
315                                                const std::list<std::string>& theArguments,
316                                                std::string& theError) const
317 {
318   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
319     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
320     return false;
321   }
322
323   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
324   AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
325
326   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
327       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
328   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
329
330   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
331     ObjectPtr aSelObject = aSelAttr->object(anInd);
332     std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
333     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
334     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
335       if (aSelObject == *aMirIter) {
336         theError = "The object " + aName + " is a result of mirror";
337         return false;
338       }
339   }
340   return true;
341 }
342
343 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute, 
344                                                     const std::list<std::string>& theArguments,
345                                                     std::string& theError) const
346 {
347   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
348     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
349     return false;
350   }
351
352   // there is a check whether the feature contains a point and a linear edge or two point values
353   std::string aParamA = theArguments.front();
354   SessionPtr aMgr = ModelAPI_Session::get();
355   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
356
357   FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
358   AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
359   if (!aRefAttrA) {
360     theError = "The " + aParamA + " attribute " + " should be " + ModelAPI_AttributeRefAttr::typeId();
361     return false;
362   }
363
364   AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
365
366   // first attribute is a point, it may coincide with any object
367   if (!aRefAttrA->isObject())
368     return true;
369   else {
370     ObjectPtr anObject = aRefAttrA->object();
371     if (!anObject.get()) {
372       theError = aParamA + " attribute has an empty object";
373       return false;
374     }
375     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
376     if (!aFeature.get()) {
377       theError = aParamA + " attribute has an empty feature";
378       return false;
379     }
380
381     if (aFeature->getKind() == SketchPlugin_Point::ID())
382       return true;
383   }
384
385   // second attribute is a point, it may coincide with any object
386   if (!aRefAttrB->isObject())
387     return true;
388   else {
389     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
390     if (!aFeature) {
391       theError = theAttribute->id() + " attribute has an empty object";
392       return false;
393     }
394     if (aFeature->getKind() == SketchPlugin_Point::ID())
395       return true;
396   }
397   theError = "There is no an attribute filled by a point";
398   return false;
399 }
400
401
402 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute, 
403                                          const std::list<std::string>& theArguments,
404                                          std::string& theError) const
405 {
406   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
407     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
408     return false;
409   }
410
411   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
412   AttributeRefListPtr aSelAttr = 
413     std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
414
415   AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
416       aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
417   AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
418       aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
419   std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
420   std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
421
422   std::list<ObjectPtr>::iterator anObjIter;
423   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
424     ObjectPtr aSelObject = aSelAttr->object(anInd);
425     anObjIter = anInitialObjects.begin();
426     for (; anObjIter != anInitialObjects.end(); anObjIter++)
427       if (aSelObject == *anObjIter)
428         break;
429     if (anObjIter != anInitialObjects.end())
430       continue;
431     anObjIter = aCopiedObjects.begin();
432     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
433       if (aSelObject == *anObjIter) {
434         std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
435         theError = "The object " + aName + " is a result of copy";
436         return false;
437       }
438   }
439   return true;
440 }
441
442 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
443                                                 const std::list<std::string>& theArguments,
444                                                 std::string& theError) const
445 {
446   AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
447
448   if (!aAttributeString->value().empty()) {
449     theError = aAttributeString->value();
450     return false;
451   }
452
453   return true;
454 }
455
456 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
457 {
458   return true;
459 }
460
461 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList, const FeaturePtr theFeature)
462 {
463   for(std::set<AttributePtr>::const_iterator anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
464     std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
465     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
466     if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
467       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
468         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
469       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
470         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
471       if(anAttrRefA.get()) {
472         ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
473         if(aResA.get()) {
474           DocumentPtr aDoc = aResA->document();
475           if(aDoc.get()) {
476             FeaturePtr aFeatureA = aDoc->feature(aResA);
477             if(aFeatureA.get() && aFeatureA == theFeature) {
478               return true;
479             }
480           }
481         }
482       }
483       if(anAttrRefB.get()) {
484         ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
485         if(aResB.get()) {
486           DocumentPtr aDoc = aResB->document();
487           if(aDoc.get()) {
488             FeaturePtr aFeatureB = aDoc->feature(aResB);
489             if(aFeatureB.get() && aFeatureB == theFeature) {
490               return true;
491             }
492           }
493         }
494       }
495     }
496   }
497   return false;
498 }
499
500 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
501                                                  const std::list<std::string>& theArguments,
502                                                  std::string& theError) const
503 {
504   std::shared_ptr<SketchPlugin_ConstraintFillet> aFilletFeature = std::dynamic_pointer_cast<SketchPlugin_ConstraintFillet>(theAttribute->owner());
505   AttributeRefAttrListPtr aPointsRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
506   if(aPointsRefList->size() == 0) {
507     theError = "Error: List of points is empty.";
508     return false;
509   }
510
511   std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures> aPointsFeaturesMap = aFilletFeature->pointsFeaturesMap();
512   std::set<AttributePtr> aSetOfPointsOnResultEdges;
513   for(std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures>::iterator aPointsIter = aPointsFeaturesMap.begin();
514       aPointsIter != aPointsFeaturesMap.end();
515       ++aPointsIter) {
516     const SketchPlugin_ConstraintFillet::FilletFeatures& aFeatures = aPointsIter->second;
517     const std::list<FeaturePtr>& aResultEdges = aFeatures.resultEdges;
518     for(std::list<FeaturePtr>::const_iterator aResultIter = aResultEdges.cbegin();
519         aResultIter != aResultEdges.cend();
520         ++aResultIter) {
521       FeaturePtr aResultFeature = *aResultIter;
522       if(aResultFeature->getKind() == SketchPlugin_Line::ID()) {
523         aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::START_ID()));
524         aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::END_ID()));
525       } else if(aResultFeature->getKind() == SketchPlugin_Arc::ID()) {
526         aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::START_ID()));
527         aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::END_ID()));
528       }
529     }
530   }
531
532   std::list<std::pair<ObjectPtr, AttributePtr>> aPointsList = aPointsRefList->list();
533   for(std::list<std::pair<ObjectPtr, AttributePtr>>::const_iterator aPointsIt = aPointsList.cbegin(); aPointsIt != aPointsList.cend(); aPointsIt++) {
534     ObjectPtr anObject = (*aPointsIt).first;
535     AttributePtr aPointAttribute = (*aPointsIt).second;
536     std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
537
538     // If we alredy have some result then:
539     // - if it is the same point all ok, just skip it
540     // - if it is point on the fillet result edge then it is not valid
541     if(!aPointsFeaturesMap.empty()) {
542       if(aPointsFeaturesMap.find(aPointAttribute) != aPointsFeaturesMap.end()) {
543         continue;
544       }
545
546       // Check that selected point not on the one of the fillet result edge.
547       if(aSetOfPointsOnResultEdges.find(aPointAttribute) != aSetOfPointsOnResultEdges.end()) {
548         return false;
549       }
550     }
551
552     // Obtain constraint coincidence for the fillet point.
553     const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
554     FeaturePtr aConstraintCoincidence;
555     for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
556       std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
557       FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
558       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
559         AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
560           aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
561         AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
562           aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
563         if(anAttrRefA.get() && !anAttrRefA->isObject()) {
564           AttributePtr anAttrA = anAttrRefA->attr();
565           if(aPointAttribute == anAttrA) {
566             aConstraintCoincidence = aConstrFeature;
567             break;
568           }
569         }
570         if(anAttrRefB.get() && !anAttrRefB->isObject()) {
571           AttributePtr anAttrB = anAttrRefB->attr();
572           if(aPointAttribute == anAttrB) {
573             aConstraintCoincidence = aConstrFeature;
574             break;
575           }
576         }
577       }
578     }
579
580     if(!aConstraintCoincidence.get()) {
581       theError = "Error: one of the selected point does not have coicidence.";
582       return false;
583     }
584
585     // Get coincides from constraint.
586     std::set<FeaturePtr> aCoinsides;
587     SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
588                                          SketchPlugin_ConstraintCoincidence::ENTITY_A(),
589                                          aCoinsides);
590     SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
591                                          SketchPlugin_ConstraintCoincidence::ENTITY_B(),
592                                          aCoinsides);
593
594     // Remove points from set of coincides.
595     std::set<FeaturePtr> aNewSetOfCoincides;
596     for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
597       if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
598          (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
599            continue;
600       }
601       if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
602         AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
603         std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
604         double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
605         if(aDistSelectedArcCenter < tolerance) {
606           continue;
607         }
608       }
609       aNewSetOfCoincides.insert(*anIt);
610     }
611     aCoinsides = aNewSetOfCoincides;
612
613     // If we still have more than two coincides remove auxilary entities from set of coincides.
614     if(aCoinsides.size() > 2) {
615       aNewSetOfCoincides.clear();
616       for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
617         if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
618           aNewSetOfCoincides.insert(*anIt);
619         }
620       }
621       aCoinsides = aNewSetOfCoincides;
622     }
623
624     if(aCoinsides.size() != 2) {
625       theError = "Error: One of the selected points does not have two suitable edges for fillet.";
626       return false;
627     }
628
629     // Check that selected edges don't have tangent constraint.
630     std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
631     FeaturePtr aFirstFeature = *anIt++;
632     FeaturePtr aSecondFeature = *anIt;
633     const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
634     if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
635       theError = "Error: Edges in selected point has tangent constraint.";
636       return false;
637     }
638
639     std::list<ResultPtr> aFirstResults = aFirstFeature->results();
640     for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin(); aResIt != aFirstResults.end(); ++aResIt) {
641       ResultPtr aRes = *aResIt;
642       const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
643       if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
644         theError = "Error: Edges in selected point has tangent constraint.";
645         return false;
646       }
647     }
648
649     // Check that lines not collinear
650     if(aFirstFeature->getKind() == SketchPlugin_Line::ID() && aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
651       std::string aStartAttr = SketchPlugin_Line::START_ID();
652       std::string anEndAttr = SketchPlugin_Line::END_ID();
653       std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
654       aFirstStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(aStartAttr))->pnt();
655       aFirstEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
656       aSecondStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(aStartAttr))->pnt();
657       aSecondEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(anEndAttr))->pnt();
658       double aCheck1 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondStartPnt->y() - aFirstStartPnt->y()) -
659         (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
660       double aCheck2 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondEndPnt->y() - aFirstStartPnt->y()) -
661         (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
662       if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
663         return false;
664       }
665     }
666   }
667
668   return true;
669 }
670
671 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute, 
672                                                     const std::list<std::string>& theArguments,
673                                                     std::string& theError) const
674 {
675   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
676     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
677     return false;
678   }
679
680   // there is a check whether the feature contains a point and a linear edge or two point values
681   std::string aParamA = theArguments.front();
682   SessionPtr aMgr = ModelAPI_Session::get();
683   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
684
685   FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
686   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
687   AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
688
689   AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
690   int aNbPoints = 0;
691   int aNbLines = 0;
692   for (int i = 0; i < 2; ++i) {
693     if (!aRefAttrs[i]->isObject())
694       ++aNbPoints;
695     else {
696       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
697       if (!aFeature) {
698         if (aNbPoints + aNbLines != 0)
699           return true;
700         else continue;
701       }
702
703       if (aFeature->getKind() == SketchPlugin_Point::ID())
704         ++aNbPoints;
705       else if (aFeature->getKind() == SketchPlugin_Line::ID())
706         ++aNbLines;
707     }
708   }
709
710   if (aNbPoints != 1 || aNbLines != 1) {
711     theError = "Middle point constraint allows points and lines only";
712     return false;
713   }
714   return true;
715 }
716
717 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
718                                                     const std::list<std::string>& /*theArguments*/,
719                                                     std::string& theError) const
720 {
721   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
722     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
723     return false;
724   }
725   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
726   AttributePtr anAttr = aRefAttr->attr();
727   if (!anAttr) {
728     theError = "The attribute " + theAttribute->id() + " should be a point";
729     return false;
730   }
731
732   FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
733   const std::string& aFeatureType = anAttrFeature->getKind();
734   if (aFeatureType == SketchPlugin_Arc::ID()) {
735     // selected point should not be a center of arc
736     const std::string& aPntId = anAttr->id();
737     if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
738       theError = "The attribute " + aPntId + " is not supported";
739       return false;
740     }
741   }
742   else if (aFeatureType == SketchPlugin_Line::ID()) {
743     // selected point should be bound point of line
744     const std::string& aPntId = anAttr->id();
745     if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
746       theError = "The attribute " + aPntId + " is not supported";
747       return false;
748     }
749   }
750   else {
751     theError = "Unable to build tangent arc on " + anAttrFeature->getKind();
752     return false;
753   }
754
755   // Check the tangent point is equal to arc end
756   FeaturePtr anArc = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefAttr->owner());
757   std::shared_ptr<GeomDataAPI_Point2D> anEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
758       anArc->attribute(SketchPlugin_Arc::END_ID()));
759   if (anEndPoint->isInitialized()) {
760     std::shared_ptr<GeomDataAPI_Point2D> aTangPt =
761         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
762     if (aTangPt->pnt()->distance(anEndPoint->pnt()) < tolerance) {
763       theError = "Unable to build arc on same points";
764       return false;
765     }
766   }
767
768   return true;
769 }
770
771 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
772                                                  const std::list<std::string>& theArguments,
773                                                  std::string& theError) const
774 {
775   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
776     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
777     return false;
778   }
779   AttributeSelectionPtr aLineAttr =
780                               std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
781   std::shared_ptr<GeomAPI_Edge> anEdge;
782   if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
783     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
784   } else if(aLineAttr->context() && aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
785     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
786   }
787
788   if (!anEdge || !anEdge->isLine()) {
789     theError = "The attribute " + theAttribute->id() + " should be a line";
790     return false;
791   }
792
793   std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
794
795   // find a sketch
796   std::shared_ptr<SketchPlugin_Sketch> aSketch;
797   std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
798   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
799   for (; anIt != aRefs.end(); ++anIt) {
800     CompositeFeaturePtr aComp =
801         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
802     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
803       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
804       break;
805     }
806   }
807   if (!aSketch) {
808     theError = "There is no sketch referring to the current feature";
809     return false;
810   }
811
812   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
813   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
814   return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
815 }