Salome HOME
Revert change for MacroArc attribute names
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Validators.cpp
4 // Created:     01 Aug 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketchPlugin_Validators.h"
8
9 #include "SketchPlugin_Arc.h"
10 #include "SketchPlugin_Circle.h"
11 #include "SketchPlugin_ConstraintCoincidence.h"
12 #include "SketchPlugin_ConstraintDistance.h"
13 #include "SketchPlugin_ConstraintRigid.h"
14 #include "SketchPlugin_ConstraintTangent.h"
15 #include "SketchPlugin_Fillet.h"
16 #include "SketchPlugin_Line.h"
17 #include "SketchPlugin_MacroArc.h"
18 #include "SketchPlugin_MacroCircle.h"
19 #include "SketchPlugin_Point.h"
20 #include "SketchPlugin_Sketch.h"
21 #include "SketchPlugin_Trim.h"
22 #include "SketchPlugin_Tools.h"
23
24 #include "SketcherPrs_Tools.h"
25
26 #include <Events_InfoMessage.h>
27
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32
33 #include <ModelAPI_AttributeRefAttrList.h>
34 #include <ModelAPI_AttributeRefList.h>
35 #include <ModelAPI_AttributeSelectionList.h>
36 #include <ModelAPI_AttributeString.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Tools.h>
39 #include <ModelAPI_ResultConstruction.h>
40
41 #include <ModelGeomAlgo_Point2D.h>
42 #include <ModelGeomAlgo_Shape.h>
43
44 #include <GeomAPI_Circ.h>
45 #include <GeomAPI_Dir2d.h>
46 #include <GeomAPI_Lin.h>
47 #include <GeomAPI_Edge.h>
48 #include <GeomAPI_Vertex.h>
49 #include <GeomDataAPI_Point2D.h>
50
51 #include <algorithm>
52 #include <cmath>
53
54 const double tolerance = 1.e-7;
55
56 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
57                                                  const std::list<std::string>& theArguments,
58                                                  Events_InfoMessage& theError) const
59 {
60   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
61     theError = "The attribute with the %1 type is not processed";
62     theError.arg(theAttribute->attributeType());
63     return false;
64   }
65
66   // there is a check whether the feature contains a point and a linear edge or two point values
67   std::string aParamA = theArguments.front();
68   SessionPtr aMgr = ModelAPI_Session::get();
69   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
70
71   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
72                                                                       (theAttribute);
73   bool isObject = aRefAttr->isObject();
74   if (!isObject) {
75     // an attribute is a point. A point value is valid always for the distance
76     return true;
77   } else {
78     // 1. check whether the references object is a linear
79     ObjectPtr anObject = aRefAttr->object();
80
81     const ModelAPI_AttributeValidator* aShapeValidator =
82       dynamic_cast<const ModelAPI_AttributeValidator*>(
83       aFactory->validator("GeomValidators_ShapeType"));
84     std::list<std::string> anArguments;
85     anArguments.push_back("circle");
86     Events_InfoMessage aCircleError;
87     bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
88     // the circle line is not a valid case
89     if (aShapeValid) {
90       theError = "Circle can not be used in distance constraint";
91       return false;
92     }
93
94     anArguments.clear();
95     anArguments.push_back("line");
96     Events_InfoMessage aLineError;
97     aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
98     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
99     if (aShapeValid) {
100       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
101       // If it is a line then we have to check that first attribute id not a line
102       std::shared_ptr<SketchPlugin_Feature> aSFeature =
103         std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
104       SketchPlugin_Sketch* aSketch = aSFeature->sketch();
105       std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
106       std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
107         aFeature->data(), aParamA, aPlane);
108       if (!aPoint.get()) {
109         theError = "One of parameters of distance constraint should be a point";
110         return false;
111       }
112     }
113   }
114   return true;
115 }
116
117 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
118                                                 const std::list<std::string>& theArguments,
119                                                 Events_InfoMessage& theError) const
120 {
121   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
122     theError = "The attribute with the %1 type is not processed";
123     theError.arg(theAttribute->attributeType());
124     return false;
125   }
126
127   // there is a check whether the feature contains a point and a linear edge or two point values
128   std::string aParamA = theArguments.front();
129   SessionPtr aMgr = ModelAPI_Session::get();
130   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
131
132   FeaturePtr anAttributeFeature =
133     std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
134   AttributeRefAttrPtr aRefAttr =
135     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
136
137   bool isObject = aRefAttr->isObject();
138   ObjectPtr anObject = aRefAttr->object();
139   if (isObject && anObject.get()) {
140     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
141
142     AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
143     ObjectPtr aOtherObject = aOtherAttr->object();
144     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
145     if (!aOtherFea)
146       return true;
147
148     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
149       if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
150           aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
151         theError = "It refers to a %1, but %2 is neither an %3 nor %4";
152         theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
153             .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
154         return false;
155       }
156     }
157     else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
158              aRefFea->getKind() == SketchPlugin_Circle::ID()) {
159       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
160           aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
161           aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
162         theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
163         theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
164             .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
165             .arg(SketchPlugin_Circle::ID());
166         return false;
167       }
168     }
169     else {
170       theError = "It refers to %1, but should refer to %2 or %3 or %4";
171       theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
172           .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
173       return false;
174     }
175     return true;
176   }
177   else {
178     theError = "It uses an empty object";
179     return false;
180   }
181
182   return true;
183 }
184
185 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
186                                              const std::list<std::string>& theArguments,
187                                              Events_InfoMessage& theError) const
188 {
189   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
190     theError = "The attribute with the %1 type is not processed";
191     theError.arg(theAttribute->attributeType());
192     return false;
193   }
194
195   std::shared_ptr<SketchPlugin_Feature> aFeature =
196       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
197   if (!aFeature)
198     return true;
199
200   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
201
202   SketchPlugin_Sketch* aSketch = aFeature->sketch();
203   int aNbFeatures = aSketch->numberOfSubs();
204   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
205     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
206     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
207       continue;
208     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
209         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
210     if (aRefAttr->isObject()) {
211       if (aRefAttr->object() == aRAttr->object()) {
212         ObjectPtr anObject = aRefAttr->object();
213         std::string aName = anObject.get() ? anObject->data()->name() : "";
214         theError = "The object %1 has been already fixed.";
215         theError.arg(aName);
216         return false;
217       }
218     }
219     else if (aRefAttr->attr() == aRAttr->attr()) {
220       AttributePtr anAttribute = aRefAttr->attr();
221       std::string aName = anAttribute.get() ? anAttribute->id() : "";
222       theError = "The attribute %1 has been already fixed.";
223       theError.arg(aName);
224       return false;
225     }
226   }
227   return true;
228 }
229
230 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
231                                               const std::list<std::string>& theArguments,
232                                               Events_InfoMessage& theError) const
233 {
234   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
235     theError = "The attribute with the %1 type is not processed";
236     theError.arg(theAttribute->attributeType());
237     return false;
238   }
239
240   std::string aParamA = theArguments.front();
241   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
242   AttributeRefAttrPtr aRefAttr[2];
243   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
244   aRefAttr[1] = aFeature->data()->refattr(aParamA);
245
246   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
247     theError = "Attributes can not be used in equal constraint";
248     return false;
249   }
250
251   std::string aType[2];
252   std::list<std::string> anArguments;
253   for (int i = 0; i < 2; i++) {
254     ObjectPtr anObject = aRefAttr[i]->object();
255     if (!anObject.get()) {
256       theError = "An empty object is used.";
257       return false;
258     }
259
260     aFeature = ModelAPI_Feature::feature(anObject);
261     if (!aFeature.get()) {
262       theError = "An empty feature is used.";
263       return false;
264     }
265
266     aType[i] = aFeature->getKind();
267     if (aFeature->getKind() != SketchPlugin_Line::ID() &&
268         aFeature->getKind() != SketchPlugin_Circle::ID() &&
269         aFeature->getKind() != SketchPlugin_Arc::ID()) {
270       theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
271       theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
272           .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
273       // wrong type of attribute
274       return false;
275     }
276   }
277
278   if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
279       aType[0] != aType[1]) {
280     theError = "Feature with kinds %1 and %2 can not be equal.";
281     theError.arg(aType[0]).arg(aType[1]);
282     return false;
283   }
284   return true;
285 }
286
287 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
288                                                const std::list<std::string>& theArguments,
289                                                Events_InfoMessage& theError) const
290 {
291   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
292     theError = "The attribute with the %1 type is not processed";
293     theError.arg(theAttribute->attributeType());
294     return false;
295   }
296
297   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
298   AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
299
300   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
301       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
302   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
303
304   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
305     ObjectPtr aSelObject = aSelAttr->object(anInd);
306     std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
307     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
308     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
309       if (aSelObject == *aMirIter) {
310         theError = "The object %1 is a result of mirror";
311         theError.arg(aName);
312         return false;
313       }
314   }
315   return true;
316 }
317
318 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
319                                                     const std::list<std::string>& theArguments,
320                                                     Events_InfoMessage& theError) const
321 {
322   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
323     theError = "The attribute with the %1 type is not processed";
324     theError.arg(theAttribute->attributeType());
325     return false;
326   }
327
328   // there is a check whether the feature contains a point and a linear edge or two point values
329   std::string aParamA = theArguments.front();
330   SessionPtr aMgr = ModelAPI_Session::get();
331   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
332
333   FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
334   AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
335   if (!aRefAttrA) {
336     theError = "The %1 attribute should be %2";
337     theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
338     return false;
339   }
340
341   AttributeRefAttrPtr aRefAttrB =
342     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
343
344   // first attribute is a point, it may coincide with any object
345   if (!aRefAttrA->isObject())
346     return true;
347   else {
348     ObjectPtr anObject = aRefAttrA->object();
349     if (!anObject.get()) {
350       theError = "%1 attribute has an empty object";
351       theError.arg(aParamA);
352       return false;
353     }
354     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
355     if (!aFeature.get()) {
356       theError = "%1 attribute has an empty feature";
357       theError.arg(aParamA);
358       return false;
359     }
360
361     if (aFeature->getKind() == SketchPlugin_Point::ID())
362       return true;
363   }
364
365   // second attribute is a point, it may coincide with any object
366   if (!aRefAttrB->isObject())
367     return true;
368   else {
369     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
370     if (!aFeature) {
371       theError = "%1 attribute has an empty object";
372       theError.arg(theAttribute->id());
373       return false;
374     }
375     if (aFeature->getKind() == SketchPlugin_Point::ID())
376       return true;
377   }
378   theError = "There is no an attribute filled by a point";
379   return false;
380 }
381
382
383 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
384                                          const std::list<std::string>& theArguments,
385                                          Events_InfoMessage& theError) const
386 {
387   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
388     theError = "The attribute with the %1 type is not processed";
389     theError.arg(theAttribute->attributeType());
390     return false;
391   }
392
393   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
394   AttributeRefListPtr aSelAttr =
395     std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
396
397   AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
398       aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
399   AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
400       aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
401   std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
402   std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
403
404   std::list<ObjectPtr>::iterator anObjIter;
405   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
406     ObjectPtr aSelObject = aSelAttr->object(anInd);
407     anObjIter = anInitialObjects.begin();
408     for (; anObjIter != anInitialObjects.end(); anObjIter++)
409       if (aSelObject == *anObjIter)
410         break;
411     if (anObjIter != anInitialObjects.end())
412       continue;
413     anObjIter = aCopiedObjects.begin();
414     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
415       if (aSelObject == *anObjIter) {
416         std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
417         theError = "The object %1 is a result of copy";
418         theError.arg(aName);
419         return false;
420       }
421   }
422   return true;
423 }
424
425 bool SketchPlugin_SolverErrorValidator::isValid(
426   const std::shared_ptr<ModelAPI_Feature>& theFeature,
427   const std::list<std::string>& theArguments,
428   Events_InfoMessage& theError) const
429 {
430   AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
431
432   if (!aAttributeString->value().empty()) {
433     theError = aAttributeString->value();
434     return false;
435   }
436
437   return true;
438 }
439
440 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
441                                                         std::string theAttribute)
442 {
443   return true;
444 }
445
446 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
447                                   const FeaturePtr theFeature)
448 {
449   for(std::set<AttributePtr>::const_iterator
450       anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
451     std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
452     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
453     if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
454       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
455         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
456       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
457         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
458       if(anAttrRefA.get()) {
459         ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
460         if(aResA.get()) {
461           DocumentPtr aDoc = aResA->document();
462           if(aDoc.get()) {
463             FeaturePtr aFeatureA = aDoc->feature(aResA);
464             if(aFeatureA.get() && aFeatureA == theFeature) {
465               return true;
466             }
467           }
468         }
469       }
470       if(anAttrRefB.get()) {
471         ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
472         if(aResB.get()) {
473           DocumentPtr aDoc = aResB->document();
474           if(aDoc.get()) {
475             FeaturePtr aFeatureB = aDoc->feature(aResB);
476             if(aFeatureB.get() && aFeatureB == theFeature) {
477               return true;
478             }
479           }
480         }
481       }
482     }
483   }
484   return false;
485 }
486
487 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
488                                                  const std::list<std::string>& theArguments,
489                                                  Events_InfoMessage& theError) const
490 {
491   AttributeRefAttrPtr aPointRefAttr =
492     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
493   if(!aPointRefAttr.get()) {
494     theError = "Error: Point not selected.";
495     return false;
496   }
497
498   AttributePtr aPointAttribute = aPointRefAttr->attr();
499   if (!aPointAttribute.get()) {
500     theError = "Error: Bad point selected.";
501     return false;
502   }
503   std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
504     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
505
506   // Obtain constraint coincidence for the fillet point.
507   const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
508   FeaturePtr aConstraintCoincidence;
509   for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
510       anIt != aRefsList.cend(); ++anIt) {
511     std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
512     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
513     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
514       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
515         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
516       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
517         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
518       if(anAttrRefA.get() && !anAttrRefA->isObject()) {
519         AttributePtr anAttrA = anAttrRefA->attr();
520         if(aPointAttribute == anAttrA) {
521           aConstraintCoincidence = aConstrFeature;
522           break;
523         }
524       }
525       if(anAttrRefB.get() && !anAttrRefB->isObject()) {
526         AttributePtr anAttrB = anAttrRefB->attr();
527         if(aPointAttribute == anAttrB) {
528           aConstraintCoincidence = aConstrFeature;
529           break;
530         }
531       }
532     }
533   }
534
535   if(!aConstraintCoincidence.get()) {
536     theError = "Error: one of the selected point does not have coicidence.";
537     return false;
538   }
539
540   // Get coincides from constraint.
541   std::set<FeaturePtr> aCoinsides;
542   SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
543                                         SketchPlugin_ConstraintCoincidence::ENTITY_A(),
544                                         aCoinsides);
545   SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
546                                         SketchPlugin_ConstraintCoincidence::ENTITY_B(),
547                                         aCoinsides);
548
549   // Remove points from set of coincides.
550   std::set<FeaturePtr> aNewSetOfCoincides;
551   for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
552       anIt != aCoinsides.end(); ++anIt) {
553     std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
554       std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
555     if(aSketchEntity.get() && aSketchEntity->isCopy()) {
556       continue;
557     }
558     if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
559         (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
560           continue;
561     }
562     if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
563       AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
564       std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
565         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
566       double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
567       if(aDistSelectedArcCenter < tolerance) {
568         continue;
569       }
570     }
571     aNewSetOfCoincides.insert(*anIt);
572   }
573   aCoinsides = aNewSetOfCoincides;
574
575   // If we still have more than two coincides remove auxilary entities from set of coincides.
576   if(aCoinsides.size() > 2) {
577     aNewSetOfCoincides.clear();
578     for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
579         anIt != aCoinsides.end(); ++anIt) {
580       if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
581         aNewSetOfCoincides.insert(*anIt);
582       }
583     }
584     aCoinsides = aNewSetOfCoincides;
585   }
586
587   if(aCoinsides.size() != 2) {
588     theError = "Error: One of the selected points does not have two suitable edges for fillet.";
589     return false;
590   }
591
592   // Check that selected edges don't have tangent constraint.
593   std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
594   FeaturePtr aFirstFeature = *anIt++;
595   FeaturePtr aSecondFeature = *anIt;
596   const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
597   if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
598     theError = "Error: Edges in selected point has tangent constraint.";
599     return false;
600   }
601
602   std::list<ResultPtr> aFirstResults = aFirstFeature->results();
603   for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
604       aResIt != aFirstResults.end(); ++aResIt) {
605     ResultPtr aRes = *aResIt;
606     const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
607     if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
608       theError = "Error: Edges in selected point has tangent constraint.";
609       return false;
610     }
611   }
612
613   // Check that lines not collinear
614   if(aFirstFeature->getKind() == SketchPlugin_Line::ID() &&
615       aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
616     std::string aStartAttr = SketchPlugin_Line::START_ID();
617     std::string anEndAttr = SketchPlugin_Line::END_ID();
618     std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
619     aFirstStartPnt =
620       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
621       aFirstFeature->attribute(aStartAttr))->pnt();
622     aFirstEndPnt =
623       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
624     aSecondStartPnt =
625       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
626       aSecondFeature->attribute(aStartAttr))->pnt();
627     aSecondEndPnt =
628       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
629       aSecondFeature->attribute(anEndAttr))->pnt();
630     double aCheck1 =
631       fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
632       (aSecondStartPnt->y() - aFirstStartPnt->y()) -
633       (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
634     double aCheck2 =
635       fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
636       (aSecondEndPnt->y() - aFirstStartPnt->y()) -
637       (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
638     if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
639       return false;
640     }
641   }
642
643
644   return true;
645 }
646
647 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
648                                                     const std::list<std::string>& theArguments,
649                                                     Events_InfoMessage& theError) const
650 {
651   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
652     theError = "The attribute with the %1 type is not processed";
653     theError.arg(theAttribute->attributeType());
654     return false;
655   }
656
657   // there is a check whether the feature contains a point and a linear edge or two point values
658   std::string aParamA = theArguments.front();
659   SessionPtr aMgr = ModelAPI_Session::get();
660   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
661
662   FeaturePtr anAttributeFeature =
663     std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
664   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
665   AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
666
667   AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
668   int aNbPoints = 0;
669   int aNbLines = 0;
670   for (int i = 0; i < 2; ++i) {
671     if (!aRefAttrs[i]->isObject())
672       ++aNbPoints;
673     else {
674       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
675       if (!aFeature) {
676         if (aNbPoints + aNbLines != 0)
677           return true;
678         else continue;
679       }
680
681       if (aFeature->getKind() == SketchPlugin_Point::ID())
682         ++aNbPoints;
683       else if (aFeature->getKind() == SketchPlugin_Line::ID())
684         ++aNbLines;
685     }
686   }
687
688   if (aNbPoints != 1 || aNbLines != 1) {
689     theError = "Middle point constraint allows points and lines only";
690     return false;
691   }
692   return true;
693 }
694
695 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
696                                                     const std::list<std::string>& /*theArguments*/,
697                                                     Events_InfoMessage& theError) const
698 {
699   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
700     theError = "The attribute with the %1 type is not processed";
701     theError.arg(theAttribute->attributeType());
702     return false;
703   }
704   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
705   AttributePtr anAttr = aRefAttr->attr();
706   if (!anAttr) {
707     theError = "The attribute %1 should be a point";
708     theError.arg(theAttribute->id());
709     return false;
710   }
711
712   FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
713   const std::string& aFeatureType = anAttrFeature->getKind();
714   if (aFeatureType == SketchPlugin_Arc::ID()) {
715     // selected point should not be a center of arc
716     const std::string& aPntId = anAttr->id();
717     if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
718       theError = "The attribute %1 is not supported";
719       theError.arg(aPntId);
720       return false;
721     }
722   }
723   else if (aFeatureType == SketchPlugin_Line::ID()) {
724     // selected point should be bound point of line
725     const std::string& aPntId = anAttr->id();
726     if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
727       theError = "The attribute %1 is not supported";
728       theError.arg(aPntId);
729       return false;
730     }
731   }
732   else {
733     theError = "Unable to build tangent arc on %1";
734     theError.arg(anAttrFeature->getKind());
735     return false;
736   }
737
738   return true;
739 }
740
741 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
742                                                  const std::list<std::string>& theArguments,
743                                                  Events_InfoMessage& theError) const
744 {
745   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
746     theError = "The attribute with the %1 type is not processed";
747     theError.arg(theAttribute->attributeType());
748     return false;
749   }
750   AttributeSelectionPtr aLineAttr =
751                        std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
752   std::shared_ptr<GeomAPI_Edge> anEdge;
753   if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
754     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
755   } else if(aLineAttr->context() &&
756             aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
757     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
758   }
759
760   if (!anEdge || !anEdge->isLine()) {
761     theError = "The attribute %1 should be a line";
762     theError.arg(theAttribute->id());
763     return false;
764   }
765
766   std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
767
768   // find a sketch
769   std::shared_ptr<SketchPlugin_Sketch> aSketch;
770   std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
771   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
772   for (; anIt != aRefs.end(); ++anIt) {
773     CompositeFeaturePtr aComp =
774         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
775     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
776       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
777       break;
778     }
779   }
780   if (!aSketch) {
781     theError = "There is no sketch referring to the current feature";
782     return false;
783   }
784
785   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
786   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
787   return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
788 }
789
790 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
791                                           const std::list<std::string>& theArguments,
792                                           Events_InfoMessage& theError) const
793 {
794   bool aValid = false;
795
796   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
797     theError = "The attribute with the %1 type is not processed";
798     theError.arg(theAttribute->attributeType());
799     return aValid;
800   }
801   AttributeReferencePtr aFeatureAttr =
802             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
803
804   ObjectPtr anAttrObject = aFeatureAttr->value();
805   FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
806   if (!anAttrFeature)
807     return aValid;
808
809   std::string aKind = anAttrFeature->getKind();
810   if (aKind == SketchPlugin_Line::ID() ||
811       aKind == SketchPlugin_Arc::ID() ||
812       aKind == SketchPlugin_Circle::ID()) {
813
814     std::set<ResultPtr> anEdgeShapes;
815     ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
816     if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
817       return aValid;
818
819     // coincidences to the feature
820     std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
821     ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
822                         SketchPlugin_ConstraintCoincidence::ID(),
823                         aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
824
825     GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
826     std::shared_ptr<SketchPlugin_Feature> aSFeature =
827                                  std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
828     SketchPlugin_Sketch* aSketch = aSFeature->sketch();
829
830     std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
831     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
832         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
833     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
834         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
835     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
836         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
837     std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
838
839     typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
840                      std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
841                                std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
842     PointToRefsMap aPointsInfo;
843
844     ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
845                                                 aX->dir(), aDirY, aPointsInfo);
846     int aCoincidentToFeature = (int)aPointsInfo.size();
847     if (aKind == SketchPlugin_Circle::ID())
848       aValid = aCoincidentToFeature >= 2;
849     else
850       aValid = aCoincidentToFeature >= 1;
851   }
852
853   return aValid;
854 }
855
856 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
857                                          const std::list<std::string>& theArguments,
858                                          Events_InfoMessage& theError) const
859 {
860   bool aValid = false;
861
862   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
863     theError = "The attribute with the %1 type is not processed";
864     theError.arg(theAttribute->attributeType());
865     return aValid;
866   }
867   AttributeReferencePtr aBaseObjectAttr =
868             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
869
870   std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
871                  std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
872
873   ObjectPtr aBaseObject = aBaseObjectAttr->value();
874   if (!aBaseObject) {
875     AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
876     aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
877     aBaseObject = aBaseObjectAttr->value();
878
879     //return aValid;
880   }
881
882   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
883   if (!aBaseFeature)
884     return aValid;
885
886   std::string aKind = aBaseFeature->getKind();
887   if (aKind != SketchPlugin_Line::ID() &&
888       aKind != SketchPlugin_Arc::ID() &&
889       aKind != SketchPlugin_Circle::ID())
890     return aValid;
891
892   // point on feature
893   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
894                        aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
895
896   SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
897
898   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
899   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
900                                                               anAttributePnt2d->y());
901
902   std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
903   std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
904            std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
905                      std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
906   SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
907                                       aCashedShapes, anObjectToPoints);
908   const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
909
910   return aShapes.size() > 1;
911 }
912
913 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
914                                                const std::list<std::string>& theArguments,
915                                                Events_InfoMessage& theError) const
916 {
917   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
918     theError = "The attribute with the %1 type is not processed";
919     theError.arg(theAttribute->attributeType());
920     return false;
921   }
922
923   AttributeSelectionPtr aFeatureAttr =
924       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
925   std::shared_ptr<GeomAPI_Edge> anEdge;
926   if (aFeatureAttr.get()) {
927     GeomShapePtr aVal = aFeatureAttr->value();
928     ResultPtr aRes = aFeatureAttr->context();
929     if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
930       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
931     } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
932               aFeatureAttr->context()->shape()->isEdge()) {
933       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
934     }
935   }
936   if (!anEdge) {
937     theError = "The attribute %1 should be an edge";
938     theError.arg(theAttribute->id());
939     return false;
940   }
941
942   // find a sketch
943   std::shared_ptr<SketchPlugin_Sketch> aSketch;
944   std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
945   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
946   for (; anIt != aRefs.end(); ++anIt) {
947     CompositeFeaturePtr aComp =
948         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
949     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
950       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
951       break;
952     }
953   }
954   if (!aSketch) {
955     theError = "There is no sketch referring to the current feature";
956     return false;
957   }
958
959   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
960   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
961   std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
962
963   if (anEdge->isLine()) {
964     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
965     std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
966     std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
967     double aDot = aNormal->dot(aLineDir);
968     double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
969     bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
970            (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
971     if (!aValid)
972       theError = "Error: Edge is already in the sketch plane.";
973     return aValid;
974   }
975   else if (anEdge->isCircle() || anEdge->isArc()) {
976     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
977     std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
978     std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
979     double aDot = fabs(aNormal->dot(aCircNormal));
980     double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
981     bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
982     if (!aValid)
983       theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
984                                       : "Error: Arc is already in the sketch plane.");
985     return aValid;
986   }
987
988   theError = "Error: Selected object is not line, circle or arc.";
989   return false;
990 }
991
992
993 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
994                                    const std::set<FeaturePtr>& theSet2)
995 {
996   std::set<FeaturePtr> aCommon;
997   if (theSet1.empty() || theSet2.empty())
998     return aCommon;
999
1000   std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1001   for (; anIt2 != theSet2.end(); ++anIt2)
1002     if (theSet1.find(*anIt2) != theSet1.end())
1003       aCommon.insert(*anIt2);
1004   return aCommon;
1005 }
1006
1007 bool SketchPlugin_DifferentReferenceValidator::isValid(
1008     const AttributePtr& theAttribute,
1009     const std::list<std::string>& theArguments,
1010     Events_InfoMessage& theError) const
1011 {
1012   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1013
1014   int aNbFeaturesReferred = 0;
1015   int aNbAttributesReferred = 0;
1016   std::set<FeaturePtr> aCommonReferredFeatures;
1017
1018   // find all features referred by attributes listed in theArguments
1019   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1020   for (; anArgIt != theArguments.end(); ++anArgIt) {
1021     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1022     if (!aRefAttr)
1023       continue;
1024
1025     std::set<FeaturePtr> aCoincidentFeatures;
1026     if (aRefAttr->isObject()) {
1027       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1028       if (aFeature) {
1029         aCoincidentFeatures.insert(aFeature);
1030         aNbFeaturesReferred += 1;
1031       }
1032     } else {
1033       AttributePoint2DPtr aPoint =
1034           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1035       if (aPoint) {
1036         aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1037         aNbAttributesReferred += 1;
1038       }
1039     }
1040
1041     if (aCommonReferredFeatures.empty())
1042       aCommonReferredFeatures = aCoincidentFeatures;
1043     else
1044       aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1045
1046     if (aCommonReferredFeatures.empty())
1047       return true;
1048   }
1049
1050   bool isOk = aNbFeaturesReferred < 1;
1051   if (aNbFeaturesReferred == 1) {
1052     if (aCommonReferredFeatures.size() == 1) {
1053       FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1054       isOk = aNbAttributesReferred <= 1 ||
1055              aFeature->getKind() == SketchPlugin_Circle::ID() ||
1056              aFeature->getKind() == SketchPlugin_Arc::ID();
1057     } else
1058       isOk = false;
1059   }
1060
1061   if (!isOk)
1062     theError = "Attributes are referred to the same feature";
1063   return isOk;
1064 }
1065
1066 bool SketchPlugin_CirclePassedPointValidator::isValid(
1067     const AttributePtr& theAttribute,
1068     const std::list<std::string>&,
1069     Events_InfoMessage& theError) const
1070 {
1071   static const std::string aErrorMessage(
1072       "Passed point refers to the same feature as a center point");
1073
1074   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1075
1076   AttributeRefAttrPtr aCenterRef =
1077       anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1078   AttributeRefAttrPtr aPassedRef =
1079       anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1080
1081   if (!aPassedRef->isObject())
1082     return true;
1083
1084   FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1085   if (!aPassedFeature)
1086     return true;
1087
1088   if (aCenterRef->isObject()) {
1089     FeaturePtr aCenterFeature = ModelAPI_Feature::feature(aCenterRef->object());
1090     if (aCenterFeature == aPassedFeature) {
1091       theError = aErrorMessage;
1092       return false;
1093     }
1094   } else {
1095     AttributePoint2DPtr aCenterPoint =
1096         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1097     if (aCenterPoint) {
1098       std::set<FeaturePtr> aCoincidentFeatures =
1099           SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1100       // check one of coincident features is a feature referred by passed point
1101       std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1102       for(; anIt != aCoincidentFeatures.end(); ++anIt)
1103         if (*anIt == aPassedFeature) {
1104           theError = aErrorMessage;
1105           return false;
1106         }
1107     }
1108   }
1109   return true;
1110 }
1111
1112 bool SketchPlugin_ThirdPointValidator::isValid(
1113     const AttributePtr& theAttribute,
1114     const std::list<std::string>& theArguments,
1115     Events_InfoMessage& theError) const
1116 {
1117   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1118   return arePointsNotOnLine(anOwner, theError) &&
1119          arePointsNotSeparated(anOwner, theArguments, theError);
1120 }
1121
1122 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1123                                               const std::string& thePointAttrName,
1124                                               const std::string& theRefPointAttrName)
1125 {
1126   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1127       theMacroCircle->attribute(thePointAttrName));
1128   AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1129
1130   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1131   if (aRefAttr) {
1132     if (aRefAttr->isObject()) {
1133       // project a point onto selected feature
1134       std::shared_ptr<SketchPlugin_Feature> aFeature =
1135           std::dynamic_pointer_cast<SketchPlugin_Feature>(
1136           ModelAPI_Feature::feature(aRefAttr->object()));
1137       if (aFeature) {
1138         SketchPlugin_Sketch* aSketch = aFeature->sketch();
1139         std::shared_ptr<GeomAPI_Edge> anEdge =
1140             std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1141         if (anEdge) {
1142           std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1143           if (anEdge->isLine())
1144             aPoint3D = anEdge->line()->project(aPoint3D);
1145           else if (anEdge->isCircle())
1146             aPoint3D = anEdge->circle()->project(aPoint3D);
1147           aPoint = aSketch->to2D(aPoint3D);
1148         }
1149       }
1150     } else {
1151       AttributePoint2DPtr anOtherPoint =
1152           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1153       if (anOtherPoint)
1154         aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1155     }
1156   }
1157
1158   return aPoint;
1159 }
1160
1161 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1162                                  std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1163 {
1164   if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1165     thePoints[0] = toPoint(theMacroFeature,
1166           SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1167           SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1168     thePoints[1] = toPoint(theMacroFeature,
1169           SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1170           SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1171     thePoints[2] = toPoint(theMacroFeature,
1172           SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1173           SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1174   } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1175     thePoints[0] = toPoint(theMacroFeature,
1176           SketchPlugin_MacroArc::START_POINT_2_ID(),
1177           SketchPlugin_MacroArc::START_POINT_REF_ID());
1178     thePoints[1] = toPoint(theMacroFeature,
1179           SketchPlugin_MacroArc::END_POINT_2_ID(),
1180           SketchPlugin_MacroArc::END_POINT_REF_ID());
1181     thePoints[2] = toPoint(theMacroFeature,
1182           SketchPlugin_MacroArc::PASSED_POINT_ID(),
1183           SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1184   }
1185 }
1186
1187 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1188                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1189                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1190 {
1191   static const double aTolerance = 1.e-7;
1192   if (thePoint1->distance(thePoint2) < aTolerance ||
1193       thePoint1->distance(thePoint3) < aTolerance)
1194     return true;
1195
1196   std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1197                                                             thePoint2->y() - thePoint1->y()));
1198   std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1199                                                             thePoint3->y() - thePoint1->y()));
1200   return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1201 }
1202
1203 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1204                          const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1205                          const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1206 {
1207   static const double aTolerance = 1.e-7;
1208   std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1209   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1210   std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(thePoint1->xyz()->decreased(aLineLoc)));
1211   std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(thePoint2->xyz()->decreased(aLineLoc)));
1212   return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1213 }
1214
1215 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1216                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint1,
1217                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint2)
1218 {
1219   static const double aTolerance = 1.e-7;
1220   std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1221   double aDistP1C = thePoint1->distance(aCenter);
1222   double aDistP2C = thePoint2->distance(aCenter);
1223   return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1224 }
1225
1226 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1227     const FeaturePtr& theMacroFeature,
1228     Events_InfoMessage& theError) const
1229 {
1230   static const std::string aErrorPointsOnLine(
1231       "Selected points are on the same line");
1232
1233   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1234   threePointsOfFeature(theMacroFeature, aPoints);
1235
1236   if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1237     theError = aErrorPointsOnLine;
1238     return false;
1239   }
1240   return true;
1241 }
1242
1243 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1244     const FeaturePtr& theMacroFeature,
1245     const std::list<std::string>& theArguments,
1246     Events_InfoMessage& theError) const
1247 {
1248   static const std::string aErrorPointsApart(
1249       "Selected entity is lying between first two points");
1250
1251   AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1252   FeaturePtr aRefByThird;
1253   if (aThirdPointRef->isObject())
1254     aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1255   if (!aRefByThird)
1256     return true;
1257
1258   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1259   threePointsOfFeature(theMacroFeature, aPoints);
1260
1261   std::shared_ptr<GeomAPI_Edge> aThirdShape =
1262       std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1263   if (!aThirdShape)
1264     return true;
1265
1266   SketchPlugin_Sketch* aSketch =
1267       std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1268   std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1269   std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1270
1271   bool isOk = true;
1272   if (aThirdShape->isLine())
1273     isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1274   else if (aThirdShape->isCircle() || aThirdShape->isArc())
1275     isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1276
1277   if (!isOk)
1278     theError = aErrorPointsApart;
1279   return isOk;
1280 }