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