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