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