Salome HOME
46bbf6a6629977e6169a4ba634874da87500fd82
[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_ConstraintRigid.h"
14 #include "SketchPlugin_Line.h"
15 #include "SketchPlugin_Point.h"
16 #include "SketchPlugin_Sketch.h"
17 #include "SketchPlugin_Tools.h"
18
19 #include "SketcherPrs_Tools.h"
20
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeRefAttr.h>
25
26 #include <ModelAPI_AttributeRefAttrList.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_AttributeString.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_ResultConstruction.h>
32
33 #include <GeomAPI_Vertex.h>
34 #include <GeomDataAPI_Point2D.h>
35
36 const double tolerance = 1.e-7;
37
38 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute, 
39                                                  const std::list<std::string>& theArguments,
40                                                  std::string& theError) const
41 {
42   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
43     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
44     return false;
45   }
46
47   // there is a check whether the feature contains a point and a linear edge or two point values
48   std::string aParamA = theArguments.front();
49   SessionPtr aMgr = ModelAPI_Session::get();
50   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
51
52   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
53   bool isObject = aRefAttr->isObject();
54   if (!isObject) {
55     // an attribute is a point. A point value is valid always for the distance
56     return true;
57   } else {
58     // 1. check whether the references object is a linear
59     ObjectPtr anObject = aRefAttr->object();
60
61     const ModelAPI_AttributeValidator* aShapeValidator = 
62       dynamic_cast<const ModelAPI_AttributeValidator*>(aFactory->validator("GeomValidators_ShapeType"));
63     std::list<std::string> anArguments;
64     anArguments.push_back("circle");
65     std::string aCircleError;
66     bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
67     // the circle line is not a valid case
68     if (aShapeValid) {
69       theError = "Circle can not be used in distance constraint";
70       return false;
71     }
72       
73     anArguments.clear();
74     anArguments.push_back("line");
75     std::string aLineError;
76     aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
77     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
78     if (aShapeValid) {
79       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
80       // If it is a line then we have to check that first attribute id not a line
81       std::shared_ptr<SketchPlugin_Feature> aSFeature =
82         std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
83       SketchPlugin_Sketch* aSketch = aSFeature->sketch();
84       std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
85       std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
86         aFeature->data(), aParamA, aPlane);
87       if (!aPoint.get()) {
88         theError = "One of parameters of distance constraint should be a point";
89         return false;
90       }
91     }
92   }
93   return true;
94 }
95
96
97 static bool hasCoincidentPoint(FeaturePtr theFeature1, FeaturePtr theFeature2)
98 {
99   FeaturePtr aConstrFeature;
100   std::list<AttributePtr> anAttrList;
101   if (theFeature1->getKind() == SketchPlugin_Circle::ID() ||
102       theFeature2->getKind() == SketchPlugin_Circle::ID())
103     return false;
104   if (theFeature2->getKind() == SketchPlugin_Line::ID()) {
105     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::START_ID()));
106     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::END_ID()));
107   } else if (theFeature2->getKind() == SketchPlugin_Arc::ID()) {
108     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::START_ID()));
109     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::END_ID()));
110   }
111
112   const std::set<AttributePtr>& aRefsList = theFeature1->data()->refsToMe();
113   std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
114   for (; aRefIt != aRefsList.end(); ++aRefIt) {
115     aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
116     if (aConstrFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
117       continue;
118     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
119     AttributePtr anAttr = aRefAttr->attr();
120     if (anAttr->id() == SketchPlugin_Arc::CENTER_ID())
121       continue;
122
123     anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A());
124     if (anAttr == *aRefIt)
125       anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B());
126
127     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
128     anAttr = aRefAttr->attr();
129     for (std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
130          anIt != anAttrList.end(); ++anIt)
131       if (*anIt == anAttr)
132         return true;
133   }
134   return false;
135 }
136
137 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute, 
138                                                 const std::list<std::string>& theArguments,
139                                                 std::string& theError) const
140 {
141   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
142     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
143     return false;
144   }
145
146   // there is a check whether the feature contains a point and a linear edge or two point values
147   std::string aParamA = theArguments.front();
148   SessionPtr aMgr = ModelAPI_Session::get();
149   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
150
151   FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
152   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
153
154   bool isObject = aRefAttr->isObject();
155   ObjectPtr anObject = aRefAttr->object();
156   if (isObject && anObject.get()) {
157     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
158
159     AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
160     ObjectPtr aOtherObject = aOtherAttr->object();
161     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
162     if (!aOtherFea)
163       return true;
164
165     if ((aRefFea->getKind() == SketchPlugin_Arc::ID() ||
166         aOtherFea->getKind() == SketchPlugin_Arc::ID()) &&
167         !hasCoincidentPoint(aRefFea, aOtherFea))
168       return false;
169
170     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
171       if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
172           aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
173         theError = "It refers to a " + SketchPlugin_Line::ID() + ", but " + aParamA + " is neither an "
174           + SketchPlugin_Arc::ID() + " nor " + SketchPlugin_Circle::ID();
175         return false;
176       }
177     }
178     else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
179       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
180         aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
181         theError = "It refers to an " + SketchPlugin_Arc::ID() + ", but " + aParamA + " is not a "
182           + SketchPlugin_Line::ID() + " or an " + SketchPlugin_Arc::ID();
183         return false;
184       }
185     }
186     else if (aRefFea->getKind() == SketchPlugin_Circle::ID()) {
187       if (aOtherFea->getKind() != SketchPlugin_Line::ID()) {
188         theError = "It refers to an " + SketchPlugin_Circle::ID() + ", but " + aParamA + " is not a "
189           + SketchPlugin_Line::ID();
190         return false;
191       }
192     }
193     else {
194       theError = "It refers to " + aRefFea->getKind() + ", but should refer to " + SketchPlugin_Line::ID()
195         + " or " + SketchPlugin_Arc::ID() + " or " + SketchPlugin_Circle::ID();
196       return false;
197     }
198     return true;
199   }
200   else {
201     theError = "It uses an empty object";
202     return false;
203   }
204
205   return true;
206 }
207
208 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
209                                              const std::list<std::string>& theArguments,
210                                              std::string& theError) const
211 {
212   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
213     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
214     return false;
215   }
216
217   std::shared_ptr<SketchPlugin_Feature> aFeature =
218       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
219   if (!aFeature)
220     return true;
221
222   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
223
224   SketchPlugin_Sketch* aSketch = aFeature->sketch();
225   int aNbFeatures = aSketch->numberOfSubs();
226   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
227     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
228     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
229       continue;
230     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
231         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
232     if (aRefAttr->isObject()) {
233       if (aRefAttr->object() == aRAttr->object()) {
234         ObjectPtr anObject = aRefAttr->object();
235         std::string aName = anObject.get() ? anObject->data()->name() : "";
236         theError = "The object " + aName + " has been already fixed.";
237         return false;
238       }
239     }
240     else if (aRefAttr->attr() == aRAttr->attr()) {
241       AttributePtr anAttribute = aRefAttr->attr();
242       std::string aName = anAttribute.get() ? anAttribute->id() : "";
243       theError = "The attribute " + aName + " has been already fixed.";
244       return false;
245     }
246   }
247   return true;
248 }
249
250 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute, 
251                                               const std::list<std::string>& theArguments,
252                                               std::string& theError) const
253 {
254   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
255     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
256     return false;
257   }
258
259   std::string aParamA = theArguments.front();
260   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
261   AttributeRefAttrPtr aRefAttr[2];
262   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
263   aRefAttr[1] = aFeature->data()->refattr(aParamA);
264
265   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
266     theError = "Attributes can not be used in equal constraint";
267     return false;
268   }
269
270   int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
271   std::list<std::string> anArguments;
272   for (int i = 0; i < 2; i++) {
273     ObjectPtr anObject = aRefAttr[i]->object();
274     if (!anObject.get()) {
275       theError = "An empty object is used.";
276       return false;
277     }
278
279     aFeature = ModelAPI_Feature::feature(anObject);
280     if (!aFeature.get()) {
281       theError = "An empty feature is used.";
282       return false;
283     }
284
285     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
286       aType[i] = 1;
287       continue;
288     }
289     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
290       aType[i] = 2;
291       continue;
292     }
293     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
294       aType[i] = 3;
295       continue;
296     }
297     theError = "The " + aFeature->getKind() + " feature kind of attribute is wrong. It should be " +
298                SketchPlugin_Line::ID() + " or " + SketchPlugin_Circle::ID() + " or " + 
299                SketchPlugin_Arc::ID();
300     // wrong type of attribute
301     return false;
302   }
303
304   if ((aType[0] == 1 && aType[1] == 2) ||
305       (aType[0] == 2 && aType[1] == 1)) {
306     theError = "Feature with kinds " + SketchPlugin_Line::ID() + " and " +
307                SketchPlugin_Circle::ID() + "can not be equal.";
308     return false;
309   }
310   return true;
311 }
312
313 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute, 
314                                                const std::list<std::string>& theArguments,
315                                                std::string& theError) const
316 {
317   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
318     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
319     return false;
320   }
321
322   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
323   AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
324
325   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
326       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
327   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
328
329   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
330     ObjectPtr aSelObject = aSelAttr->object(anInd);
331     std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
332     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
333     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
334       if (aSelObject == *aMirIter) {
335         theError = "The object " + aName + " is a result of mirror";
336         return false;
337       }
338   }
339   return true;
340 }
341
342 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute, 
343                                                     const std::list<std::string>& theArguments,
344                                                     std::string& theError) const
345 {
346   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
347     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
348     return false;
349   }
350
351   // there is a check whether the feature contains a point and a linear edge or two point values
352   std::string aParamA = theArguments.front();
353   SessionPtr aMgr = ModelAPI_Session::get();
354   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
355
356   FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
357   AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
358   if (!aRefAttrA) {
359     theError = "The " + aParamA + " attribute " + " should be " + ModelAPI_AttributeRefAttr::typeId();
360     return false;
361   }
362
363   AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
364
365   // first attribute is a point, it may coincide with any object
366   if (!aRefAttrA->isObject())
367     return true;
368   else {
369     ObjectPtr anObject = aRefAttrA->object();
370     if (!anObject.get()) {
371       theError = aParamA + " attribute has an empty object";
372       return false;
373     }
374     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
375     if (!aFeature.get()) {
376       theError = aParamA + " attribute has an empty feature";
377       return false;
378     }
379
380     if (aFeature->getKind() == SketchPlugin_Point::ID())
381       return true;
382   }
383
384   // second attribute is a point, it may coincide with any object
385   if (!aRefAttrB->isObject())
386     return true;
387   else {
388     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
389     if (!aFeature) {
390       theError = theAttribute->id() + " attribute has an empty object";
391       return false;
392     }
393     if (aFeature->getKind() == SketchPlugin_Point::ID())
394       return true;
395   }
396   theError = "There is no an attribute filled by a point";
397   return false;
398 }
399
400
401 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute, 
402                                          const std::list<std::string>& theArguments,
403                                          std::string& theError) const
404 {
405   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
406     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
407     return false;
408   }
409
410   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
411   AttributeRefListPtr aSelAttr = 
412     std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
413
414   AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
415       aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
416   AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
417       aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
418   std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
419   std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
420
421   std::list<ObjectPtr>::iterator anObjIter;
422   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
423     ObjectPtr aSelObject = aSelAttr->object(anInd);
424     anObjIter = anInitialObjects.begin();
425     for (; anObjIter != anInitialObjects.end(); anObjIter++)
426       if (aSelObject == *anObjIter)
427         break;
428     if (anObjIter != anInitialObjects.end())
429       continue;
430     anObjIter = aCopiedObjects.begin();
431     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
432       if (aSelObject == *anObjIter) {
433         std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
434         theError = "The object " + aName + " is a result of copy";
435         return false;
436       }
437   }
438   return true;
439 }
440
441 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
442                                                 const std::list<std::string>& theArguments,
443                                                 std::string& theError) const
444 {
445   AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
446
447   if (!aAttributeString->value().empty()) {
448     theError = aAttributeString->value();
449     return false;
450   }
451
452   return true;
453 }
454
455 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
456 {
457   return true;
458 }
459
460 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
461                                                  const std::list<std::string>& theArguments,
462                                                  std::string& theError) const
463 {
464   if(!theAttribute.get() || !theAttribute->isInitialized()) {
465     theError = "Error: List of points is not initialized.";
466     return false;
467   }
468
469   FeaturePtr aFilletFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
470   AttributeRefAttrListPtr aPointsRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
471   if(aPointsRefList->size() == 0) {
472     theError = "Error: List of points is empty.";
473     return false;
474   }
475
476   AttributeRefAttrListPtr aBasePointsRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(
477     aFilletFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
478   AttributeRefListPtr aResultEdgesRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
479     aFilletFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
480
481   std::list<std::pair<ObjectPtr, AttributePtr>> aPointsList = aPointsRefList->list();
482   for(std::list<std::pair<ObjectPtr, AttributePtr>>::const_iterator anIt = aPointsList.cbegin(); anIt != aPointsList.cend(); anIt++) {
483     ObjectPtr anObject = (*anIt).first;
484     AttributePtr aPointAttribute = (*anIt).second;
485
486     // If we alredy have some result then:
487     // - if it is the same point all ok, just skip it
488     // - if it is point on the fillet arc then it is not valid
489     if(aBasePointsRefList->size() > 0) {
490       if(aBasePointsRefList->isInList(aPointAttribute)) {
491         continue;
492       }
493
494       // Check that selected point not on the one of the result fillet arc.
495       for(int anIndex = 0; anIndex < aBasePointsRefList->size(); anIndex++) {
496         if(aResultEdgesRefList->size() > 0) {
497           FeaturePtr aResultArc;
498           aResultArc = ModelAPI_Feature::feature(aResultEdgesRefList->object(anIndex * 3 + 2));
499           AttributePtr anArcStart = aResultArc->attribute(SketchPlugin_Arc::START_ID());
500           AttributePtr anArcEnd = aResultArc->attribute(SketchPlugin_Arc::END_ID());
501           std::shared_ptr<GeomAPI_Pnt2d> anArcStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcStart)->pnt();
502           std::shared_ptr<GeomAPI_Pnt2d> anArcEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcEnd)->pnt();
503           std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
504           double aDistSelectedArcStart = aSelectedPnt->distance(anArcStartPnt);
505           double aDistSelectedArcEnd = aSelectedPnt->distance(anArcEndPnt);
506           if(aDistSelectedArcStart < tolerance || aDistSelectedArcEnd < tolerance) {
507             return false;
508           }
509         }
510       }
511     }
512
513     // Obtain constraint coincidence for the fillet point.
514     const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
515     FeaturePtr aConstraintCoincidence;
516     for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
517       std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
518       FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
519       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
520         AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
521           aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
522         AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
523           aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
524         if(anAttrRefA.get() && !anAttrRefA->isObject()) {
525           AttributePtr anAttrA = anAttrRefA->attr();
526           if(aPointAttribute == anAttrA) {
527             aConstraintCoincidence = aConstrFeature;
528             break;
529           }
530         }
531         if(anAttrRefB.get() && !anAttrRefB->isObject()) {
532           AttributePtr anAttrB = anAttrRefB->attr();
533           if(aPointAttribute == anAttrB) {
534             aConstraintCoincidence = aConstrFeature;
535             break;
536           }
537         }
538       }
539     }
540
541     if(!aConstraintCoincidence.get()) {
542       theError = "Error: one of the selected point does not have coicidence.";
543       return false;
544     }
545
546     // Get coincides from constraint.
547     std::set<FeaturePtr> aCoinsides;
548     SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
549                                          SketchPlugin_ConstraintCoincidence::ENTITY_A(),
550                                          aCoinsides);
551     SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
552                                          SketchPlugin_ConstraintCoincidence::ENTITY_B(),
553                                          aCoinsides);
554
555     // Remove points from set of coincides.
556     std::set<FeaturePtr> aNewSetOfCoincides;
557     for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
558       if((*anIt)->getKind() == SketchPlugin_Line::ID() ||
559          (*anIt)->getKind() == SketchPlugin_Arc::ID()) {
560         aNewSetOfCoincides.insert(*anIt);
561       }
562     }
563     aCoinsides = aNewSetOfCoincides;
564
565     // If we still have more than two coincides remove auxilary entities from set of coincides.
566     if(aCoinsides.size() > 2) {
567       aNewSetOfCoincides.clear();
568       for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
569         if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
570           aNewSetOfCoincides.insert(*anIt);
571         }
572       }
573       aCoinsides = aNewSetOfCoincides;
574     }
575
576     if(aCoinsides.size() != 2) {
577       theError = ("Error: One of the selected points does not have two suitable edges for fillet.");
578       return false;
579     }
580
581     // Check that lines not collinear
582     std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
583     FeaturePtr aFirstFeature = *anIt++;
584     FeaturePtr aSecondFeature = *anIt;
585     if(aFirstFeature->getKind() == SketchPlugin_Line::ID() && aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
586       std::string aStartAttr = SketchPlugin_Line::START_ID();
587       std::string anEndAttr = SketchPlugin_Line::END_ID();
588       std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
589       aFirstStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(aStartAttr))->pnt();
590       aFirstEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
591       aSecondStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(aStartAttr))->pnt();
592       aSecondEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(anEndAttr))->pnt();
593       double aCheck1 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondStartPnt->y() - aFirstStartPnt->y()) -
594         (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
595       double aCheck2 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondEndPnt->y() - aFirstStartPnt->y()) -
596         (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
597       if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
598         return false;
599       }
600     }
601   }
602
603   return true;
604 }
605
606 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute, 
607                                                     const std::list<std::string>& theArguments,
608                                                     std::string& theError) const
609 {
610   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
611     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
612     return false;
613   }
614
615   // there is a check whether the feature contains a point and a linear edge or two point values
616   std::string aParamA = theArguments.front();
617   SessionPtr aMgr = ModelAPI_Session::get();
618   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
619
620   FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
621   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
622   AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
623
624   AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
625   int aNbPoints = 0;
626   int aNbLines = 0;
627   for (int i = 0; i < 2; ++i) {
628     if (!aRefAttrs[i]->isObject())
629       ++aNbPoints;
630     else {
631       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
632       if (!aFeature) {
633         if (aNbPoints + aNbLines != 0)
634           return true;
635         else continue;
636       }
637
638       if (aFeature->getKind() == SketchPlugin_Point::ID())
639         ++aNbPoints;
640       else if (aFeature->getKind() == SketchPlugin_Line::ID())
641         ++aNbLines;
642     }
643   }
644
645   if (aNbPoints != 1 || aNbLines != 1) {
646     theError = "Middle point constraint allows points and lines only";
647     return false;
648   }
649   return true;
650 }