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