]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Validators.cpp
Salome HOME
Issue #2024: Redesign of circle and arc of circle
[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     //return aValid;
882   }
883
884   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
885   if (!aBaseFeature)
886     return aValid;
887
888   std::string aKind = aBaseFeature->getKind();
889   if (aKind != SketchPlugin_Line::ID() &&
890       aKind != SketchPlugin_Arc::ID() &&
891       aKind != SketchPlugin_Circle::ID())
892     return aValid;
893
894   // point on feature
895   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
896                        aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
897
898   SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
899
900   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
901   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
902                                                               anAttributePnt2d->y());
903
904   std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
905   std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
906            std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
907                      std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
908   SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
909                                       aCashedShapes, anObjectToPoints);
910   const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
911
912   return aShapes.size() > 1;
913 }
914
915 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
916                                                const std::list<std::string>& theArguments,
917                                                Events_InfoMessage& theError) const
918 {
919   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
920     theError = "The attribute with the %1 type is not processed";
921     theError.arg(theAttribute->attributeType());
922     return false;
923   }
924
925   AttributeSelectionPtr aFeatureAttr =
926       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
927   std::shared_ptr<GeomAPI_Edge> anEdge;
928   if (aFeatureAttr.get()) {
929     GeomShapePtr aVal = aFeatureAttr->value();
930     ResultPtr aRes = aFeatureAttr->context();
931     if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
932       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
933     } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
934               aFeatureAttr->context()->shape()->isEdge()) {
935       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
936     }
937   }
938   if (!anEdge) {
939     theError = "The attribute %1 should be an edge";
940     theError.arg(theAttribute->id());
941     return false;
942   }
943
944   // find a sketch
945   std::shared_ptr<SketchPlugin_Sketch> aSketch;
946   std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
947   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
948   for (; anIt != aRefs.end(); ++anIt) {
949     CompositeFeaturePtr aComp =
950         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
951     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
952       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
953       break;
954     }
955   }
956   if (!aSketch) {
957     theError = "There is no sketch referring to the current feature";
958     return false;
959   }
960
961   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
962   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
963   std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
964
965   if (anEdge->isLine()) {
966     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
967     std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
968     std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
969     double aDot = aNormal->dot(aLineDir);
970     double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
971     bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
972            (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
973     if (!aValid)
974       theError = "Error: Edge is already in the sketch plane.";
975     return aValid;
976   }
977   else if (anEdge->isCircle() || anEdge->isArc()) {
978     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
979     std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
980     std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
981     double aDot = fabs(aNormal->dot(aCircNormal));
982     double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
983     bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
984     if (!aValid)
985       theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
986                                       : "Error: Arc is already in the sketch plane.");
987     return aValid;
988   }
989
990   theError = "Error: Selected object is not line, circle or arc.";
991   return false;
992 }
993
994
995 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
996                                    const std::set<FeaturePtr>& theSet2)
997 {
998   std::set<FeaturePtr> aCommon;
999   if (theSet1.empty() || theSet2.empty())
1000     return aCommon;
1001
1002   std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1003   for (; anIt2 != theSet2.end(); ++anIt2)
1004     if (theSet1.find(*anIt2) != theSet1.end())
1005       aCommon.insert(*anIt2);
1006   return aCommon;
1007 }
1008
1009 bool SketchPlugin_DifferentReferenceValidator::isValid(
1010     const AttributePtr& theAttribute,
1011     const std::list<std::string>& theArguments,
1012     Events_InfoMessage& theError) const
1013 {
1014   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1015
1016   int aNbFeaturesReferred = 0;
1017   int aNbAttributesReferred = 0;
1018   std::set<FeaturePtr> aCommonReferredFeatures;
1019
1020   // find all features referred by attributes listed in theArguments
1021   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1022   for (; anArgIt != theArguments.end(); ++anArgIt) {
1023     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1024     if (!aRefAttr)
1025       continue;
1026
1027     std::set<FeaturePtr> aCoincidentFeatures;
1028     if (aRefAttr->isObject()) {
1029       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1030       if (aFeature) {
1031         aCoincidentFeatures.insert(aFeature);
1032         aNbFeaturesReferred += 1;
1033       }
1034     } else {
1035       AttributePoint2DPtr aPoint =
1036           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1037       if (aPoint) {
1038         aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1039         aNbAttributesReferred += 1;
1040       }
1041     }
1042
1043     if (aCommonReferredFeatures.empty())
1044       aCommonReferredFeatures = aCoincidentFeatures;
1045     else
1046       aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1047
1048     if (aCommonReferredFeatures.empty())
1049       return true;
1050   }
1051
1052   bool isOk = aNbFeaturesReferred < 1;
1053   if (aNbFeaturesReferred == 1) {
1054     if (aCommonReferredFeatures.size() == 1) {
1055       FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1056       isOk = aNbAttributesReferred <= 1 ||
1057              aFeature->getKind() == SketchPlugin_Circle::ID() ||
1058              aFeature->getKind() == SketchPlugin_Arc::ID();
1059     } else
1060       isOk = false;
1061   }
1062
1063   if (!isOk)
1064     theError = "Attributes are referred to the same feature";
1065   return isOk;
1066 }
1067
1068 bool SketchPlugin_CirclePassedPointValidator::isValid(
1069     const AttributePtr& theAttribute,
1070     const std::list<std::string>&,
1071     Events_InfoMessage& theError) const
1072 {
1073   static const std::string aErrorMessage(
1074       "Passed point refers to the same feature as a center point");
1075
1076   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1077
1078   AttributeRefAttrPtr aCenterRef =
1079       anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1080   AttributeRefAttrPtr aPassedRef =
1081       anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1082
1083   if (!aPassedRef->isObject())
1084     return true;
1085
1086   FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1087   if (!aPassedFeature)
1088     return true;
1089
1090   if (aCenterRef->isObject()) {
1091     FeaturePtr aCenterFeature = ModelAPI_Feature::feature(aCenterRef->object());
1092     if (aCenterFeature == aPassedFeature) {
1093       theError = aErrorMessage;
1094       return false;
1095     }
1096   } else {
1097     AttributePoint2DPtr aCenterPoint =
1098         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1099     if (aCenterPoint) {
1100       std::set<FeaturePtr> aCoincidentFeatures =
1101           SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1102       // check one of coincident features is a feature referred by passed point
1103       std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1104       for(; anIt != aCoincidentFeatures.end(); ++anIt)
1105         if (*anIt == aPassedFeature) {
1106           theError = aErrorMessage;
1107           return false;
1108         }
1109     }
1110   }
1111   return true;
1112 }
1113
1114 bool SketchPlugin_ThirdPointValidator::isValid(
1115     const AttributePtr& theAttribute,
1116     const std::list<std::string>& theArguments,
1117     Events_InfoMessage& theError) const
1118 {
1119   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1120   return arePointsNotOnLine(anOwner, theError) &&
1121          arePointsNotSeparated(anOwner, theArguments, theError);
1122 }
1123
1124 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1125                                               const std::string& thePointAttrName,
1126                                               const std::string& theRefPointAttrName)
1127 {
1128   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1129       theMacroCircle->attribute(thePointAttrName));
1130   AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1131
1132   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1133   if (aRefAttr) {
1134     if (aRefAttr->isObject()) {
1135       // project a point onto selected feature
1136       std::shared_ptr<SketchPlugin_Feature> aFeature =
1137           std::dynamic_pointer_cast<SketchPlugin_Feature>(
1138           ModelAPI_Feature::feature(aRefAttr->object()));
1139       if (aFeature) {
1140         SketchPlugin_Sketch* aSketch = aFeature->sketch();
1141         std::shared_ptr<GeomAPI_Edge> anEdge =
1142             std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1143         if (anEdge) {
1144           std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1145           if (anEdge->isLine())
1146             aPoint3D = anEdge->line()->project(aPoint3D);
1147           else if (anEdge->isCircle())
1148             aPoint3D = anEdge->circle()->project(aPoint3D);
1149           aPoint = aSketch->to2D(aPoint3D);
1150         }
1151       }
1152     } else {
1153       AttributePoint2DPtr anOtherPoint =
1154           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1155       if (anOtherPoint)
1156         aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1157     }
1158   }
1159
1160   return aPoint;
1161 }
1162
1163 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1164                                  std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1165 {
1166   if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1167     thePoints[0] = toPoint(theMacroFeature,
1168           SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1169           SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1170     thePoints[1] = toPoint(theMacroFeature,
1171           SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1172           SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1173     thePoints[2] = toPoint(theMacroFeature,
1174           SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1175           SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1176   } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1177     thePoints[0] = toPoint(theMacroFeature,
1178           SketchPlugin_MacroArc::START_POINT_2_ID(),
1179           SketchPlugin_MacroArc::START_POINT_REF_ID());
1180     thePoints[1] = toPoint(theMacroFeature,
1181           SketchPlugin_MacroArc::END_POINT_2_ID(),
1182           SketchPlugin_MacroArc::END_POINT_REF_ID());
1183     thePoints[2] = toPoint(theMacroFeature,
1184           SketchPlugin_MacroArc::PASSED_POINT_ID(),
1185           SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1186   }
1187 }
1188
1189 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1190                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1191                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1192 {
1193   static const double aTolerance = 1.e-7;
1194   if (thePoint1->distance(thePoint2) < aTolerance ||
1195       thePoint1->distance(thePoint3) < aTolerance)
1196     return true;
1197
1198   std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1199                                                             thePoint2->y() - thePoint1->y()));
1200   std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1201                                                             thePoint3->y() - thePoint1->y()));
1202   return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1203 }
1204
1205 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1206                          const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1207                          const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1208 {
1209   static const double aTolerance = 1.e-7;
1210   std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1211   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1212   std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(thePoint1->xyz()->decreased(aLineLoc)));
1213   std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(thePoint2->xyz()->decreased(aLineLoc)));
1214   return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1215 }
1216
1217 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1218                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint1,
1219                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint2)
1220 {
1221   static const double aTolerance = 1.e-7;
1222   std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1223   double aDistP1C = thePoint1->distance(aCenter);
1224   double aDistP2C = thePoint2->distance(aCenter);
1225   return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1226 }
1227
1228 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1229     const FeaturePtr& theMacroFeature,
1230     Events_InfoMessage& theError) const
1231 {
1232   static const std::string aErrorPointsOnLine(
1233       "Selected points are on the same line");
1234
1235   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1236   threePointsOfFeature(theMacroFeature, aPoints);
1237
1238   if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1239     theError = aErrorPointsOnLine;
1240     return false;
1241   }
1242   return true;
1243 }
1244
1245 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1246     const FeaturePtr& theMacroFeature,
1247     const std::list<std::string>& theArguments,
1248     Events_InfoMessage& theError) const
1249 {
1250   static const std::string aErrorPointsApart(
1251       "Selected entity is lying between first two points");
1252
1253   AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1254   FeaturePtr aRefByThird;
1255   if (aThirdPointRef->isObject())
1256     aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1257   if (!aRefByThird)
1258     return true;
1259
1260   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1261   threePointsOfFeature(theMacroFeature, aPoints);
1262
1263   std::shared_ptr<GeomAPI_Edge> aThirdShape =
1264       std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1265   if (!aThirdShape)
1266     return true;
1267
1268   SketchPlugin_Sketch* aSketch =
1269       std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1270   std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1271   std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1272
1273   bool isOk = true;
1274   if (aThirdShape->isLine())
1275     isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1276   else if (aThirdShape->isCircle() || aThirdShape->isArc())
1277     isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1278
1279   if (!isOk)
1280     theError = aErrorPointsApart;
1281   return isOk;
1282 }
1283
1284 bool SketchPlugin_ArcEndPointValidator::isValid(
1285     const AttributePtr& theAttribute,
1286     const std::list<std::string>& theArguments,
1287     Events_InfoMessage& theError) const
1288 {
1289   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1290   AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1291
1292   if(!anEndPointRef.get()) {
1293     return true;
1294   }
1295
1296   ObjectPtr anObject = anEndPointRef->object();
1297   AttributePtr anAttr = anEndPointRef->attr();
1298   if(!anObject.get() && !anAttr.get()) {
1299     return true;
1300   }
1301
1302   if(anEndPointRef->attr().get()) {
1303     return false;
1304   }
1305
1306   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1307   if(aResult.get()) {
1308     GeomShapePtr aShape = aResult->shape();
1309     if(aShape.get() && aShape->isVertex()) {
1310       return false;
1311     }
1312   }
1313
1314   aFeature = ModelAPI_Feature::feature(anObject);
1315   if(aFeature.get()) {
1316     if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1317       return false;
1318     }
1319   }
1320
1321   return true;
1322 }
1323
1324 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1325     const AttributePtr& theAttribute,
1326     const std::list<std::string>& theArguments,
1327     Events_InfoMessage& theError) const
1328 {
1329   std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1330       std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1331   AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1332
1333   if(!anEndPointRef.get()) {
1334     return true;
1335   }
1336
1337   GeomShapePtr anArcShape = anArcFeature->getArcShape();
1338
1339   if(!anArcShape.get() || anArcShape->isNull()) {
1340     return true;
1341   }
1342
1343   ObjectPtr anObject = anEndPointRef->object();
1344   AttributePtr anAttr = anEndPointRef->attr();
1345   if(!anObject.get() && !anAttr.get()) {
1346     return true;
1347   }
1348
1349   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1350   if(aResult.get()) {
1351     GeomShapePtr aShape = aResult->shape();
1352     if(aShape.get() && !aShape->isNull()) {
1353       GeomShapePtr anIntersection = anArcShape->intersect(aShape);
1354       if(anIntersection.get() && !anIntersection->isNull()) {
1355         return true;
1356       }
1357     }
1358   }
1359
1360   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1361   if(aSelectedFeature.get()) {
1362     std::list<ResultPtr> aResults = aSelectedFeature->results();
1363     for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1364         anIt != aResults.cend();
1365         ++anIt)
1366     {
1367       GeomShapePtr aShape = (*anIt)->shape();
1368       if(aShape.get() && !aShape->isNull()) {
1369         GeomShapePtr anIntersection = anArcShape->intersect(aShape);
1370         if(anIntersection.get() && !anIntersection->isNull()) {
1371           return true;
1372         }
1373       }
1374     }
1375   }
1376
1377   return false;
1378 }