Salome HOME
Merge branch 'task_5_2_4' into CEA_2019
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Validators.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchPlugin_Validators.h"
21
22 #include "SketchPlugin_Arc.h"
23 #include "SketchPlugin_Circle.h"
24 #include "SketchPlugin_ConstraintCoincidence.h"
25 #include "SketchPlugin_ConstraintDistance.h"
26 #include "SketchPlugin_ConstraintRigid.h"
27 #include "SketchPlugin_ConstraintTangent.h"
28 #include "SketchPlugin_Fillet.h"
29 #include "SketchPlugin_Line.h"
30 #include "SketchPlugin_MacroArc.h"
31 #include "SketchPlugin_MacroCircle.h"
32 #include "SketchPlugin_MultiRotation.h"
33 #include "SketchPlugin_Point.h"
34 #include "SketchPlugin_Sketch.h"
35 #include "SketchPlugin_Trim.h"
36 #include "SketchPlugin_Tools.h"
37
38 #include "SketcherPrs_Tools.h"
39
40 #include <Events_InfoMessage.h>
41
42 #include <ModelAPI_Data.h>
43 #include <ModelAPI_Validator.h>
44 #include <ModelAPI_AttributeDouble.h>
45 #include <ModelAPI_AttributeInteger.h>
46 #include <ModelAPI_AttributeRefAttr.h>
47 #include <ModelAPI_AttributeRefList.h>
48 #include <ModelAPI_AttributeSelectionList.h>
49 #include <ModelAPI_AttributeString.h>
50 #include <ModelAPI_Session.h>
51 #include <ModelAPI_Tools.h>
52 #include <ModelAPI_ResultConstruction.h>
53
54 #include <ModelGeomAlgo_Point2D.h>
55 #include <ModelGeomAlgo_Shape.h>
56
57 #include <GeomAlgoAPI_EdgeBuilder.h>
58 #include <GeomAlgoAPI_ShapeTools.h>
59
60 #include <GeomAPI_Circ.h>
61 #include <GeomAPI_Dir2d.h>
62 #include <GeomAPI_Lin.h>
63 #include <GeomAPI_Edge.h>
64 #include <GeomAPI_Vertex.h>
65 #include <GeomDataAPI_Point2D.h>
66
67 #include <algorithm>
68 #include <cmath>
69
70 const double tolerance = 1.e-7;
71
72 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
73                                                  const std::list<std::string>& theArguments,
74                                                  Events_InfoMessage& theError) const
75 {
76   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
77     theError = "The attribute with the %1 type is not processed";
78     theError.arg(theAttribute->attributeType());
79     return false;
80   }
81
82   // there is a check whether the feature contains a point and a linear edge or two point values
83   std::string aParamA = theArguments.front();
84   SessionPtr aMgr = ModelAPI_Session::get();
85   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
86
87   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
88                                                                       (theAttribute);
89   bool isObject = aRefAttr->isObject();
90   if (!isObject) {
91     // an attribute is a point. A point value is valid always for the distance
92     return true;
93   } else {
94     // 1. check whether the references object is a linear
95     ObjectPtr anObject = aRefAttr->object();
96
97     const ModelAPI_AttributeValidator* aShapeValidator =
98       dynamic_cast<const ModelAPI_AttributeValidator*>(
99       aFactory->validator("GeomValidators_ShapeType"));
100     std::list<std::string> anArguments;
101     anArguments.push_back("circle");
102     Events_InfoMessage aCircleError;
103     bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
104     // the circle line is not a valid case
105     if (aShapeValid) {
106       theError = "Circle can not be used in distance constraint";
107       return false;
108     }
109
110     anArguments.clear();
111     anArguments.push_back("line");
112     Events_InfoMessage aLineError;
113     aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
114     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
115     if (aShapeValid) {
116       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
117       // If it is a line then we have to check that first attribute id not a line
118       std::shared_ptr<SketchPlugin_Feature> aSFeature =
119         std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
120       SketchPlugin_Sketch* aSketch = aSFeature->sketch();
121       std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
122       std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
123         aFeature->data(), aParamA, aPlane);
124       if (!aPoint.get()) {
125         theError = "One of parameters of distance constraint should be a point";
126         return false;
127       }
128     }
129   }
130   return true;
131 }
132
133 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
134                                                 const std::list<std::string>& theArguments,
135                                                 Events_InfoMessage& theError) const
136 {
137   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
138     theError = "The attribute with the %1 type is not processed";
139     theError.arg(theAttribute->attributeType());
140     return false;
141   }
142
143   // there is a check whether the feature contains a point and a linear edge or two point values
144   std::string aParamA = theArguments.front();
145   SessionPtr aMgr = ModelAPI_Session::get();
146   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
147
148   FeaturePtr anAttributeFeature =
149     std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
150   AttributeRefAttrPtr aRefAttr =
151     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
152
153   bool isObject = aRefAttr->isObject();
154   ObjectPtr anObject = aRefAttr->object();
155   if (isObject && anObject.get()) {
156     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
157
158     AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
159     ObjectPtr aOtherObject = aOtherAttr->object();
160     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
161     if (!aOtherFea)
162       return true;
163
164     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
165       if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
166           aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
167         theError = "It refers to a %1, but %2 is neither an %3 nor %4";
168         theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
169             .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
170         return false;
171       }
172     }
173     else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
174              aRefFea->getKind() == SketchPlugin_Circle::ID()) {
175       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
176           aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
177           aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
178         theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
179         theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
180             .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
181             .arg(SketchPlugin_Circle::ID());
182         return false;
183       }
184     }
185     else {
186       theError = "It refers to %1, but should refer to %2 or %3 or %4";
187       theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
188           .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
189       return false;
190     }
191     return true;
192   }
193   else {
194     theError = "It uses an empty object";
195     return false;
196   }
197
198   return true;
199 }
200
201 bool SketchPlugin_PerpendicularAttrValidator::isValid(const AttributePtr& theAttribute,
202                                                       const std::list<std::string>& theArguments,
203                                                       Events_InfoMessage& theError) const
204 {
205   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
206     theError = "The attribute with the %1 type is not processed";
207     theError.arg(theAttribute->attributeType());
208     return false;
209   }
210
211   std::string aParamA = theArguments.front();
212   SessionPtr aMgr = ModelAPI_Session::get();
213   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
214
215   FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
216   AttributeRefAttrPtr aRefAttr =
217       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
218
219   bool isObject = aRefAttr->isObject();
220   ObjectPtr anObject = aRefAttr->object();
221   if (isObject && anObject.get()) {
222     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
223
224     AttributeRefAttrPtr aOtherAttr = anOwner->refattr(aParamA);
225     ObjectPtr aOtherObject = aOtherAttr->object();
226     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
227     if (!aOtherFea)
228       return true;
229
230     // at least one feature should be a line
231     if (aRefFea->getKind() != SketchPlugin_Line::ID() &&
232         aOtherFea->getKind() != SketchPlugin_Line::ID()) {
233       theError = "At least one feature should be a line";
234       return false;
235     }
236   }
237   else {
238     theError = "It uses an empty object";
239     return false;
240   }
241
242   return true;
243 }
244
245 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
246                                              const std::list<std::string>& theArguments,
247                                              Events_InfoMessage& theError) const
248 {
249   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
250     theError = "The attribute with the %1 type is not processed";
251     theError.arg(theAttribute->attributeType());
252     return false;
253   }
254
255   std::shared_ptr<SketchPlugin_Feature> aFeature =
256       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
257   if (!aFeature)
258     return true;
259
260   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
261
262   SketchPlugin_Sketch* aSketch = aFeature->sketch();
263   int aNbFeatures = aSketch->numberOfSubs();
264   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
265     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
266     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
267       continue;
268     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
269         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
270     if (aRefAttr->isObject()) {
271       if (aRefAttr->object() == aRAttr->object()) {
272         ObjectPtr anObject = aRefAttr->object();
273         std::string aName = anObject.get() ? anObject->data()->name() : "";
274         theError = "The object %1 has been already fixed.";
275         theError.arg(aName);
276         return false;
277       }
278     }
279     else if (aRefAttr->attr() == aRAttr->attr()) {
280       AttributePtr anAttribute = aRefAttr->attr();
281       std::string aName = anAttribute.get() ? anAttribute->id() : "";
282       theError = "The attribute %1 has been already fixed.";
283       theError.arg(aName);
284       return false;
285     }
286   }
287   return true;
288 }
289
290 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
291                                               const std::list<std::string>& theArguments,
292                                               Events_InfoMessage& theError) const
293 {
294   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
295     theError = "The attribute with the %1 type is not processed";
296     theError.arg(theAttribute->attributeType());
297     return false;
298   }
299
300   std::string aParamA = theArguments.front();
301   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
302   AttributeRefAttrPtr aRefAttr[2];
303   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
304   aRefAttr[1] = aFeature->data()->refattr(aParamA);
305
306   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
307     theError = "Attributes can not be used in equal constraint";
308     return false;
309   }
310
311   std::string aType[2];
312   std::list<std::string> anArguments;
313   for (int i = 0; i < 2; i++) {
314     ObjectPtr anObject = aRefAttr[i]->object();
315     if (!anObject.get()) {
316       theError = "An empty object is used.";
317       return false;
318     }
319
320     aFeature = ModelAPI_Feature::feature(anObject);
321     if (!aFeature.get()) {
322       theError = "An empty feature is used.";
323       return false;
324     }
325
326     aType[i] = aFeature->getKind();
327     if (aFeature->getKind() != SketchPlugin_Line::ID() &&
328         aFeature->getKind() != SketchPlugin_Circle::ID() &&
329         aFeature->getKind() != SketchPlugin_Arc::ID()) {
330       theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
331       theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
332           .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
333       // wrong type of attribute
334       return false;
335     }
336   }
337
338   if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
339       aType[0] != aType[1]) {
340     theError = "Feature with kinds %1 and %2 can not be equal.";
341     theError.arg(aType[0]).arg(aType[1]);
342     return false;
343   }
344   return true;
345 }
346
347 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
348                                                const std::list<std::string>& theArguments,
349                                                Events_InfoMessage& theError) const
350 {
351   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
352     theError = "The attribute with the %1 type is not processed";
353     theError.arg(theAttribute->attributeType());
354     return false;
355   }
356
357   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
358   AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
359
360   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
361       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
362   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
363
364   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
365     ObjectPtr aSelObject = aSelAttr->object(anInd);
366     std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
367     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
368     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
369       if (aSelObject == *aMirIter) {
370         theError = "The object %1 is a result of mirror";
371         theError.arg(aName);
372         return false;
373       }
374   }
375   return true;
376 }
377
378 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
379                                                     const std::list<std::string>& theArguments,
380                                                     Events_InfoMessage& theError) const
381 {
382   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
383     theError = "The attribute with the %1 type is not processed";
384     theError.arg(theAttribute->attributeType());
385     return false;
386   }
387
388   // there is a check whether the feature contains a point and a linear edge or two point values
389   std::string aParamA = theArguments.front();
390   SessionPtr aMgr = ModelAPI_Session::get();
391   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
392
393   FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
394   AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
395   if (!aRefAttrA) {
396     theError = "The %1 attribute should be %2";
397     theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
398     return false;
399   }
400
401   AttributeRefAttrPtr aRefAttrB =
402     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
403
404   // first attribute is a point, it may coincide with any object
405   if (!aRefAttrA->isObject())
406     return true;
407   else {
408     ObjectPtr anObject = aRefAttrA->object();
409     if (!anObject.get()) {
410       theError = "%1 attribute has an empty object";
411       theError.arg(aParamA);
412       return false;
413     }
414     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
415     if (!aFeature.get()) {
416       theError = "%1 attribute has an empty feature";
417       theError.arg(aParamA);
418       return false;
419     }
420
421     if (aFeature->getKind() == SketchPlugin_Point::ID())
422       return true;
423   }
424
425   // second attribute is a point, it may coincide with any object
426   if (!aRefAttrB->isObject())
427     return true;
428   else {
429     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
430     if (!aFeature) {
431       theError = "%1 attribute has an empty object";
432       theError.arg(theAttribute->id());
433       return false;
434     }
435     if (aFeature->getKind() == SketchPlugin_Point::ID())
436       return true;
437   }
438   theError = "There is no an attribute filled by a point";
439   return false;
440 }
441
442
443 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
444                                          const std::list<std::string>& theArguments,
445                                          Events_InfoMessage& theError) const
446 {
447   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
448     theError = "The attribute with the %1 type is not processed";
449     theError.arg(theAttribute->attributeType());
450     return false;
451   }
452
453   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
454   AttributeRefListPtr aSelAttr =
455     std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
456
457   AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
458       aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
459   AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
460       aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
461   std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
462   std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
463
464   std::list<ObjectPtr>::iterator anObjIter;
465   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
466     ObjectPtr aSelObject = aSelAttr->object(anInd);
467     anObjIter = anInitialObjects.begin();
468     for (; anObjIter != anInitialObjects.end(); anObjIter++)
469       if (aSelObject == *anObjIter)
470         break;
471     if (anObjIter != anInitialObjects.end())
472       continue;
473     anObjIter = aCopiedObjects.begin();
474     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
475       if (aSelObject == *anObjIter) {
476         std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
477         theError = "The object %1 is a result of copy";
478         theError.arg(aName);
479         return false;
480       }
481   }
482   return true;
483 }
484
485 bool SketchPlugin_SolverErrorValidator::isValid(
486   const std::shared_ptr<ModelAPI_Feature>& theFeature,
487   const std::list<std::string>& theArguments,
488   Events_InfoMessage& theError) const
489 {
490   AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
491
492   if (!aAttributeString->value().empty()) {
493     theError = aAttributeString->value();
494     return false;
495   }
496
497   return true;
498 }
499
500 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
501                                                         std::string theAttribute)
502 {
503   return true;
504 }
505
506 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
507                                   const FeaturePtr theFeature)
508 {
509   for(std::set<AttributePtr>::const_iterator
510       anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
511     std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
512     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
513     if (!aFeature)
514       continue;
515     if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
516       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
517         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
518       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
519         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
520       if(anAttrRefA.get()) {
521         ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
522         if(aResA.get()) {
523           DocumentPtr aDoc = aResA->document();
524           if(aDoc.get()) {
525             FeaturePtr aFeatureA = aDoc->feature(aResA);
526             if(aFeatureA.get() && aFeatureA == theFeature) {
527               return true;
528             }
529           }
530         }
531       }
532       if(anAttrRefB.get()) {
533         ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
534         if(aResB.get()) {
535           DocumentPtr aDoc = aResB->document();
536           if(aDoc.get()) {
537             FeaturePtr aFeatureB = aDoc->feature(aResB);
538             if(aFeatureB.get() && aFeatureB == theFeature) {
539               return true;
540             }
541           }
542         }
543       }
544     }
545   }
546   return false;
547 }
548
549 static bool isPointPointCoincidence(const FeaturePtr& theCoincidence)
550 {
551   AttributeRefAttrPtr aRefAttr[2] = {
552       theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_A()),
553       theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_B())
554   };
555
556   bool arePoints = true;
557   for (int i = 0; i < 2 && arePoints; ++i) {
558     if (aRefAttr[i]->isObject()) {
559       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
560       arePoints = aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID();
561     } else
562       arePoints = aRefAttr[i]->attr().get() != NULL;
563   }
564   return arePoints;
565 }
566
567 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
568                                                  const std::list<std::string>& theArguments,
569                                                  Events_InfoMessage& theError) const
570 {
571   AttributeRefAttrPtr aPointRefAttr =
572     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
573   if(!aPointRefAttr.get()) {
574     theError = "Error: Point not selected.";
575     return false;
576   }
577
578   AttributePtr aPointAttribute = aPointRefAttr->attr();
579   if (!aPointAttribute.get()) {
580     theError = "Error: Bad point selected.";
581     return false;
582   }
583   std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
584     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
585
586   // Obtain constraint coincidence for the fillet point.
587   const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
588   FeaturePtr aConstraintCoincidence;
589   for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
590       anIt != aRefsList.cend(); ++anIt) {
591     std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
592     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
593     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
594       if (!isPointPointCoincidence(aConstrFeature))
595         continue;
596
597       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
598         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
599       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
600         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
601
602       AttributePtr anAttrA = anAttrRefA->attr();
603       if(aPointAttribute == anAttrA) {
604         aConstraintCoincidence = aConstrFeature;
605         break;
606       }
607
608       AttributePtr anAttrB = anAttrRefB->attr();
609       if(aPointAttribute == anAttrB) {
610         aConstraintCoincidence = aConstrFeature;
611         break;
612       }
613     }
614   }
615
616   if(!aConstraintCoincidence.get()) {
617     theError = "Error: one of the selected point does not have coicidence.";
618     return false;
619   }
620
621   // Get coincides from constraint.
622   std::set<FeaturePtr> aCoinsides;
623   SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
624                                         SketchPlugin_ConstraintCoincidence::ENTITY_A(),
625                                         aCoinsides,
626                                         true);
627   SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
628                                         SketchPlugin_ConstraintCoincidence::ENTITY_B(),
629                                         aCoinsides,
630                                         true);
631
632   // Remove points and external lines from set of coincides.
633   std::set<FeaturePtr> aNewSetOfCoincides;
634   for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
635       anIt != aCoinsides.end(); ++anIt) {
636     std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
637       std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
638     if(aSketchEntity.get() && (aSketchEntity->isCopy() || aSketchEntity->isExternal())) {
639       continue;
640     }
641     if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
642         (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
643           continue;
644     }
645     if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
646       AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
647       std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
648         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
649       double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
650       if(aDistSelectedArcCenter < tolerance) {
651         continue;
652       }
653     }
654     aNewSetOfCoincides.insert(*anIt);
655   }
656   aCoinsides = aNewSetOfCoincides;
657
658   // If we still have more than two coincides remove auxilary entities from set of coincides.
659   if(aCoinsides.size() > 2) {
660     aNewSetOfCoincides.clear();
661     for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
662         anIt != aCoinsides.end(); ++anIt) {
663       if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
664         aNewSetOfCoincides.insert(*anIt);
665       }
666     }
667     aCoinsides = aNewSetOfCoincides;
668   }
669
670   if(aCoinsides.size() != 2) {
671     theError = "Error: One of the selected points does not have two suitable edges for fillet.";
672     return false;
673   }
674
675   // Check that selected edges don't have tangent constraint.
676   std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
677   FeaturePtr aFirstFeature = *anIt++;
678   FeaturePtr aSecondFeature = *anIt;
679   const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
680   if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
681     theError = "Error: Edges in selected point has tangent constraint.";
682     return false;
683   }
684
685   std::list<ResultPtr> aFirstResults = aFirstFeature->results();
686   for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
687       aResIt != aFirstResults.end(); ++aResIt) {
688     ResultPtr aRes = *aResIt;
689     const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
690     if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
691       theError = "Error: Edges in selected point has tangent constraint.";
692       return false;
693     }
694   }
695
696   // Check the features are not tangent
697   std::shared_ptr<GeomAPI_Shape> aFirstShape = aFirstFeature->lastResult()->shape();
698   std::shared_ptr<GeomAPI_Shape> aSecondShape = aSecondFeature->lastResult()->shape();
699   if (!aFirstShape || !aFirstShape->isEdge() ||
700       !aSecondShape || !aSecondShape->isEdge()) {
701     theError = "Error: At least on of the features is not an edge";
702     return false;
703   }
704
705   std::shared_ptr<GeomAPI_Edge> anEdge1 = std::dynamic_pointer_cast<GeomAPI_Edge>(aFirstShape);
706   std::shared_ptr<GeomAPI_Edge> anEdge2 = std::dynamic_pointer_cast<GeomAPI_Edge>(aSecondShape);
707
708   static const double TOL = 1.e-7;
709   if (anEdge1->isLine() && anEdge2->isLine()) {
710     // Check that lines not collinear
711     std::shared_ptr<GeomAPI_Dir> aDir1 = anEdge1->line()->direction();
712     std::shared_ptr<GeomAPI_Dir> aDir2 = anEdge2->line()->direction();
713     double aCross = aDir1->cross(aDir2)->squareModulus();
714     if (aCross < TOL * TOL)
715       return false;
716   } else if (anEdge1->isArc() && anEdge2->isArc()) {
717     // check the circles are not tangent
718     std::shared_ptr<GeomAPI_Circ> aCirc1 = anEdge1->circle();
719     std::shared_ptr<GeomAPI_Circ> aCirc2 = anEdge2->circle();
720     double aDistCC = aCirc1->center()->distance(aCirc2->center());
721     double aRadSum = aCirc1->radius() + aCirc2->radius();
722     double aRadDiff = fabs(aCirc1->radius() - aCirc2->radius());
723     if (fabs(aDistCC - aRadSum) < TOL || fabs(aDistCC - aRadDiff) < TOL)
724       return false;
725   } else {
726     // check whether line and arc are tangent
727     std::shared_ptr<GeomAPI_Circ> aCirc;
728     std::shared_ptr<GeomAPI_Lin> aLine;
729     if (anEdge1->isLine()) {
730       aLine = anEdge1->line();
731       aCirc = anEdge2->circle();
732     } else {
733       aCirc = anEdge1->circle();
734       aLine = anEdge2->line();
735     }
736
737     double aDistCL = aLine->distance(aCirc->center());
738     if (fabs(aDistCL - aCirc->radius()) < TOL)
739       return false;
740   }
741
742   return true;
743 }
744
745 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
746                                                     const std::list<std::string>& theArguments,
747                                                     Events_InfoMessage& theError) const
748 {
749   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
750     theError = "The attribute with the %1 type is not processed";
751     theError.arg(theAttribute->attributeType());
752     return false;
753   }
754
755   // there is a check whether the feature contains a point and a linear edge or two point values
756   std::string aParamA = theArguments.front();
757   SessionPtr aMgr = ModelAPI_Session::get();
758   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
759
760   FeaturePtr anAttributeFeature =
761     std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
762   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
763   AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
764
765   AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
766   int aNbPoints = 0;
767   int aNbLines = 0;
768   for (int i = 0; i < 2; ++i) {
769     if (!aRefAttrs[i]->isObject())
770       ++aNbPoints;
771     else {
772       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
773       if (!aFeature) {
774         if (aNbPoints + aNbLines != 0)
775           return true;
776         else continue;
777       }
778
779       if (aFeature->getKind() == SketchPlugin_Point::ID())
780         ++aNbPoints;
781       else if (aFeature->getKind() == SketchPlugin_Line::ID() ||
782                aFeature->getKind() == SketchPlugin_Arc::ID())
783         ++aNbLines;
784     }
785   }
786
787   if (aNbPoints != 1 || aNbLines != 1) {
788     theError = "Middle point constraint allows points and lines only";
789     return false;
790   }
791   return true;
792 }
793
794 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
795                                                     const std::list<std::string>& /*theArguments*/,
796                                                     Events_InfoMessage& theError) const
797 {
798   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
799     theError = "The attribute with the %1 type is not processed";
800     theError.arg(theAttribute->attributeType());
801     return false;
802   }
803   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
804   AttributePtr anAttr = aRefAttr->attr();
805   if (!anAttr) {
806     theError = "The attribute %1 should be a point";
807     theError.arg(theAttribute->id());
808     return false;
809   }
810
811   FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
812   const std::string& aFeatureType = anAttrFeature->getKind();
813   if (aFeatureType == SketchPlugin_Arc::ID()) {
814     // selected point should not be a center of arc
815     const std::string& aPntId = anAttr->id();
816     if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
817       theError = "The attribute %1 is not supported";
818       theError.arg(aPntId);
819       return false;
820     }
821   }
822   else if (aFeatureType == SketchPlugin_Line::ID()) {
823     // selected point should be bound point of line
824     const std::string& aPntId = anAttr->id();
825     if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
826       theError = "The attribute %1 is not supported";
827       theError.arg(aPntId);
828       return false;
829     }
830   }
831   else {
832     theError = "Unable to build tangent arc on %1";
833     theError.arg(anAttrFeature->getKind());
834     return false;
835   }
836
837   return true;
838 }
839
840 bool SketchPlugin_ArcTransversalPointValidator::isValid(
841     const AttributePtr& theAttribute,
842     const std::list<std::string>& /*theArguments*/,
843     Events_InfoMessage& theError) const
844 {
845   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
846     theError = "The attribute with the %1 type is not processed";
847     theError.arg(theAttribute->attributeType());
848     return false;
849   }
850   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
851   AttributePtr anAttr = aRefAttr->attr();
852   if (!anAttr) {
853     theError = "The attribute %1 should be a point";
854     theError.arg(theAttribute->id());
855     return false;
856   }
857
858   FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
859   const std::string& aFeatureType = anAttrFeature->getKind();
860   if (aFeatureType == SketchPlugin_Line::ID()) {
861     // selected point should be bound point of line
862     const std::string& aPntId = anAttr->id();
863     if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
864       theError = "The attribute %1 is not supported";
865       theError.arg(aPntId);
866       return false;
867     }
868   }
869   else {
870     theError = "Unable to build transversal arc on %1";
871     theError.arg(anAttrFeature->getKind());
872     return false;
873   }
874
875   return true;
876 }
877
878 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
879                                                  const std::list<std::string>& theArguments,
880                                                  Events_InfoMessage& theError) const
881 {
882   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
883     theError = "The attribute with the %1 type is not processed";
884     theError.arg(theAttribute->attributeType());
885     return false;
886   }
887   AttributeSelectionPtr anExternalAttr =
888       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
889   std::shared_ptr<GeomAPI_Edge> anEdge;
890   if (anExternalAttr && anExternalAttr->value() && anExternalAttr->value()->isEdge()) {
891     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->value()));
892   } else if(anExternalAttr->context() && anExternalAttr->context()->shape() &&
893             anExternalAttr->context()->shape()->isEdge()) {
894     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->context()->shape()));
895   }
896
897   if (!anEdge) {
898     theError = "The attribute %1 should be an edge";
899     theError.arg(theAttribute->id());
900     return false;
901   }
902
903   // find a sketch
904   std::shared_ptr<SketchPlugin_Sketch> aSketch;
905   std::set<AttributePtr> aRefs = anExternalAttr->owner()->data()->refsToMe();
906   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
907   for (; anIt != aRefs.end(); ++anIt) {
908     CompositeFeaturePtr aComp =
909         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
910     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
911       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
912       break;
913     }
914   }
915   if (!aSketch) {
916     theError = "There is no sketch referring to the current feature";
917     return false;
918   }
919
920   // check the edge is intersected with sketch plane
921   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
922
923   std::list<GeomPointPtr> anIntersectionsPoints;
924   anEdge->intersectWithPlane(aPlane, anIntersectionsPoints);
925   if (anIntersectionsPoints.empty()) {
926     theError = "The edge is not intersected with sketch plane";
927     return false;
928   }
929   return true;
930 }
931
932 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
933                                           const std::list<std::string>& theArguments,
934                                           Events_InfoMessage& theError) const
935 {
936   bool aValid = false;
937
938   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
939     theError = "The attribute with the %1 type is not processed";
940     theError.arg(theAttribute->attributeType());
941     return aValid;
942   }
943   AttributeReferencePtr aFeatureAttr =
944             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
945
946   ObjectPtr anAttrObject = aFeatureAttr->value();
947   FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
948   if (!anAttrFeature)
949     return aValid;
950
951   std::string aKind = anAttrFeature->getKind();
952   if (aKind == SketchPlugin_Line::ID() ||
953       aKind == SketchPlugin_Arc::ID() ||
954       aKind == SketchPlugin_Circle::ID()) {
955
956     std::set<ResultPtr> anEdgeShapes;
957     ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
958     if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
959       return aValid;
960
961     // coincidences to the feature
962     std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
963     ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
964                         SketchPlugin_ConstraintCoincidence::ID(),
965                         aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
966
967     GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
968     std::shared_ptr<SketchPlugin_Feature> aSFeature =
969                                  std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
970     SketchPlugin_Sketch* aSketch = aSFeature->sketch();
971
972     std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
973     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
974         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
975     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
976         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
977     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
978         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
979     std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
980
981     typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
982                      std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
983                                std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
984     PointToRefsMap aPointsInfo;
985
986     ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
987                                                 aX->dir(), aDirY, aPointsInfo);
988     int aCoincidentToFeature = (int)aPointsInfo.size();
989     if (aKind == SketchPlugin_Circle::ID())
990       aValid = aCoincidentToFeature >= 2;
991     else
992       aValid = aCoincidentToFeature >= 1;
993   }
994
995   return aValid;
996 }
997
998 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
999                                          const std::list<std::string>& theArguments,
1000                                          Events_InfoMessage& theError) const
1001 {
1002   bool aValid = false;
1003
1004   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1005     theError = "The attribute with the %1 type is not processed";
1006     theError.arg(theAttribute->attributeType());
1007     return aValid;
1008   }
1009   AttributeReferencePtr aBaseObjectAttr =
1010             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1011
1012   std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
1013                  std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
1014
1015   ObjectPtr aBaseObject = aBaseObjectAttr->value();
1016   if (!aBaseObject) {
1017     AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
1018     aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
1019     aBaseObject = aBaseObjectAttr->value();
1020   }
1021
1022   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1023   if (!aBaseFeature)
1024     return aValid;
1025
1026   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1027                                  std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
1028   if (!aSketchFeature.get() || aSketchFeature->isCopy())
1029     return aValid;
1030
1031   std::string aKind = aBaseFeature->getKind();
1032   if (aKind != SketchPlugin_Line::ID() &&
1033       aKind != SketchPlugin_Arc::ID() &&
1034       aKind != SketchPlugin_Circle::ID())
1035     return aValid;
1036
1037   // point on feature
1038   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1039                        aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
1040
1041   SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
1042
1043   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
1044   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
1045                                                               anAttributePnt2d->y());
1046
1047   std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
1048   std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
1049            std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
1050                      std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
1051   SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
1052                                       aCashedShapes, anObjectToPoints);
1053   const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
1054
1055   return aShapes.size() > 1;
1056 }
1057
1058 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
1059                                                const std::list<std::string>& theArguments,
1060                                                Events_InfoMessage& theError) const
1061 {
1062   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
1063     theError = "The attribute with the %1 type is not processed";
1064     theError.arg(theAttribute->attributeType());
1065     return false;
1066   }
1067
1068   AttributeSelectionPtr aFeatureAttr =
1069       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
1070   std::shared_ptr<GeomAPI_Edge> anEdge;
1071   std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
1072   if (aFeatureAttr.get()) {
1073     GeomShapePtr aVal = aFeatureAttr->value();
1074     ResultPtr aRes = aFeatureAttr->context();
1075     if (aVal && aVal->isVertex())
1076       return true; // vertex is always could be projected
1077     if (aVal && aVal->isEdge()) {
1078       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
1079     } else if(aRes && aRes->shape()) {
1080       if (aRes->shape()->isVertex())
1081         return true; // vertex is always could be projected
1082       else if (aRes->shape()->isEdge())
1083         anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
1084     }
1085
1086     // try to convert result to sketch feature
1087     if (aRes) {
1088       aSketchFeature =
1089         std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
1090     }
1091   }
1092   if (!anEdge) {
1093     theError = "The attribute %1 should be an edge or vertex";
1094     theError.arg(theAttribute->id());
1095     return false;
1096   }
1097
1098   // find a sketch
1099   std::shared_ptr<SketchPlugin_Sketch> aSketch;
1100   std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
1101   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
1102   for (; anIt != aRefs.end(); ++anIt) {
1103     CompositeFeaturePtr aComp =
1104         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1105     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1106       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1107       break;
1108     }
1109   }
1110   if (!aSketch) {
1111     theError = "There is no sketch referring to the current feature";
1112     return false;
1113   }
1114   if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
1115     theError = "Unable to project feature from the same sketch";
1116     return false;
1117   }
1118
1119   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1120   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1121   std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1122
1123   if (anEdge->isLine()) {
1124     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1125     std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1126     double aDot = fabs(aNormal->dot(aLineDir));
1127     bool aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
1128     if (!aValid)
1129       theError = "Error: Edge is already in the sketch plane.";
1130     return aValid;
1131   }
1132   else if (anEdge->isCircle() || anEdge->isArc()) {
1133     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1134     std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1135     double aDot = fabs(aNormal->dot(aCircNormal));
1136     bool aValid = fabs(aDot - 1.0) < tolerance * tolerance;
1137     if (!aValid)
1138       theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
1139                                       : "Error: Arc is already in the sketch plane.");
1140     return aValid;
1141   }
1142
1143   theError = "Error: Selected object is not line, circle or arc.";
1144   return false;
1145 }
1146
1147
1148 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1149                                    const std::set<FeaturePtr>& theSet2)
1150 {
1151   std::set<FeaturePtr> aCommon;
1152   if (theSet1.empty() || theSet2.empty())
1153     return aCommon;
1154
1155   std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1156   for (; anIt2 != theSet2.end(); ++anIt2)
1157     if (theSet1.find(*anIt2) != theSet1.end())
1158       aCommon.insert(*anIt2);
1159   return aCommon;
1160 }
1161
1162 bool SketchPlugin_DifferentReferenceValidator::isValid(
1163     const AttributePtr& theAttribute,
1164     const std::list<std::string>& theArguments,
1165     Events_InfoMessage& theError) const
1166 {
1167   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1168
1169   int aNbFeaturesReferred = 0;
1170   int aNbAttributesReferred = 0;
1171   std::set<FeaturePtr> aCommonReferredFeatures;
1172
1173   // find all features referred by attributes listed in theArguments
1174   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1175   for (; anArgIt != theArguments.end(); ++anArgIt) {
1176     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1177     if (!aRefAttr)
1178       continue;
1179
1180     std::set<FeaturePtr> aCoincidentFeatures;
1181     if (aRefAttr->isObject()) {
1182       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1183       if (aFeature) {
1184         aCoincidentFeatures.insert(aFeature);
1185         aNbFeaturesReferred += 1;
1186       }
1187     } else {
1188       AttributePoint2DPtr aPoint =
1189           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1190       if (aPoint) {
1191         aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1192         aNbAttributesReferred += 1;
1193       }
1194     }
1195
1196     if (aCommonReferredFeatures.empty())
1197       aCommonReferredFeatures = aCoincidentFeatures;
1198     else
1199       aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1200
1201     if (aCommonReferredFeatures.empty())
1202       return true;
1203   }
1204
1205   bool isOk = aNbFeaturesReferred < 1;
1206   if (aNbFeaturesReferred == 1) {
1207     if (aCommonReferredFeatures.size() == 1) {
1208       FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1209       isOk = aNbAttributesReferred <= 1 ||
1210              aFeature->getKind() == SketchPlugin_Circle::ID() ||
1211              aFeature->getKind() == SketchPlugin_Arc::ID();
1212     } else
1213       isOk = false;
1214   }
1215
1216   if (!isOk)
1217     theError = "Attributes are referred to the same feature";
1218   return isOk;
1219 }
1220
1221 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1222     const AttributePtr& theAttribute,
1223     const std::list<std::string>& theArguments,
1224     Events_InfoMessage& theError) const
1225 {
1226   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1227   std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1228
1229   // find all points referred by attributes listed in theArguments
1230   bool hasRefsToPoints = false;
1231   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1232   for (; anArgIt != theArguments.end(); ++anArgIt) {
1233     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1234     if (!aRefAttr)
1235       continue;
1236
1237     if (!aRefAttr->isObject()) {
1238       AttributePoint2DPtr aPoint =
1239           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1240       if (aReferredCoincidentPoints.empty())
1241         aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1242       else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1243         return true; // non-coincident point has been found
1244       else
1245         hasRefsToPoints = true;
1246     }
1247   }
1248
1249   if (hasRefsToPoints)
1250     theError = "Attributes are referred to the same point";
1251   return !hasRefsToPoints;
1252 }
1253
1254 bool SketchPlugin_CirclePassedPointValidator::isValid(
1255     const AttributePtr& theAttribute,
1256     const std::list<std::string>&,
1257     Events_InfoMessage& theError) const
1258 {
1259   static const std::string aErrorMessage(
1260       "Passed point refers to the same feature as a center point");
1261
1262   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1263
1264   AttributeRefAttrPtr aCenterRef =
1265       anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1266   AttributeRefAttrPtr aPassedRef =
1267       anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1268
1269   if (!aPassedRef->isObject())
1270     return true;
1271
1272   FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1273   if (!aPassedFeature)
1274     return true;
1275
1276   if (aCenterRef->isObject()) {
1277     if (aCenterRef->object() == aPassedRef->object()) {
1278       theError = aErrorMessage;
1279       return false;
1280     }
1281   } else {
1282     AttributePoint2DPtr aCenterPoint =
1283         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1284     if (aCenterPoint) {
1285       std::set<FeaturePtr> aCoincidentFeatures =
1286           SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1287       // check one of coincident features is a feature referred by passed point
1288       std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1289       for(; anIt != aCoincidentFeatures.end(); ++anIt)
1290         if (*anIt == aPassedFeature) {
1291           theError = aErrorMessage;
1292           return false;
1293         }
1294     }
1295   }
1296   return true;
1297 }
1298
1299 bool SketchPlugin_ThirdPointValidator::isValid(
1300     const AttributePtr& theAttribute,
1301     const std::list<std::string>& theArguments,
1302     Events_InfoMessage& theError) const
1303 {
1304   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1305   return arePointsNotOnLine(anOwner, theError) &&
1306          arePointsNotSeparated(anOwner, theArguments, theError);
1307 }
1308
1309 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1310                                               const std::string& thePointAttrName,
1311                                               const std::string& theRefPointAttrName)
1312 {
1313   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1314       theMacroCircle->attribute(thePointAttrName));
1315   AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1316
1317   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1318   if (aRefAttr) {
1319     if (aRefAttr->isObject()) {
1320       // project a point onto selected feature
1321       std::shared_ptr<SketchPlugin_Feature> aFeature =
1322           std::dynamic_pointer_cast<SketchPlugin_Feature>(
1323           ModelAPI_Feature::feature(aRefAttr->object()));
1324       if (aFeature) {
1325         SketchPlugin_Sketch* aSketch = aFeature->sketch();
1326         std::shared_ptr<GeomAPI_Edge> anEdge =
1327             std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1328         if (anEdge) {
1329           std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1330           if (anEdge->isLine())
1331             aPoint3D = anEdge->line()->project(aPoint3D);
1332           else if (anEdge->isCircle())
1333             aPoint3D = anEdge->circle()->project(aPoint3D);
1334           if(aPoint3D)
1335             aPoint = aSketch->to2D(aPoint3D);
1336         }
1337       }
1338     } else {
1339       AttributePoint2DPtr anOtherPoint =
1340           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1341       if (anOtherPoint)
1342         aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1343     }
1344   }
1345
1346   return aPoint;
1347 }
1348
1349 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1350                                  std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1351 {
1352   if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1353     thePoints[0] = toPoint(theMacroFeature,
1354           SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1355           SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1356     thePoints[1] = toPoint(theMacroFeature,
1357           SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1358           SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1359     thePoints[2] = toPoint(theMacroFeature,
1360           SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1361           SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1362   } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1363     thePoints[0] = toPoint(theMacroFeature,
1364           SketchPlugin_MacroArc::START_POINT_2_ID(),
1365           SketchPlugin_MacroArc::START_POINT_REF_ID());
1366     thePoints[1] = toPoint(theMacroFeature,
1367           SketchPlugin_MacroArc::END_POINT_2_ID(),
1368           SketchPlugin_MacroArc::END_POINT_REF_ID());
1369     thePoints[2] = toPoint(theMacroFeature,
1370           SketchPlugin_MacroArc::PASSED_POINT_ID(),
1371           SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1372   }
1373 }
1374
1375 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1376                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1377                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1378 {
1379   static const double aTolerance = 1.e-7;
1380   if (thePoint1->distance(thePoint2) < aTolerance ||
1381       thePoint1->distance(thePoint3) < aTolerance)
1382     return true;
1383
1384   std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1385                                                             thePoint2->y() - thePoint1->y()));
1386   std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1387                                                             thePoint3->y() - thePoint1->y()));
1388   return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1389 }
1390
1391 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1392                          const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1393                          const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1394 {
1395   static const double aTolerance = 1.e-7;
1396   std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1397   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1398
1399   std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1400   // the first point is on the line
1401   if (aVec1->squareModulus() < aTolerance * aTolerance)
1402     return false;
1403   std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1404   std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1405   // the second point is on the line
1406   if (aVec2->squareModulus() < aTolerance * aTolerance)
1407     return false;
1408   std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1409
1410   return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1411 }
1412
1413 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1414                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint1,
1415                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint2)
1416 {
1417   static const double aTolerance = 1.e-7;
1418   std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1419   double aDistP1C = thePoint1->distance(aCenter);
1420   double aDistP2C = thePoint2->distance(aCenter);
1421   return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1422 }
1423
1424 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1425     const FeaturePtr& theMacroFeature,
1426     Events_InfoMessage& theError) const
1427 {
1428   static const std::string aErrorPointsOnLine(
1429       "Selected points are on the same line");
1430
1431   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1432   threePointsOfFeature(theMacroFeature, aPoints);
1433
1434   if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1435     theError = aErrorPointsOnLine;
1436     return false;
1437   }
1438   return true;
1439 }
1440
1441 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1442     const FeaturePtr& theMacroFeature,
1443     const std::list<std::string>& theArguments,
1444     Events_InfoMessage& theError) const
1445 {
1446   static const std::string aErrorPointsApart(
1447       "Selected entity is lying between first two points");
1448
1449   AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1450   FeaturePtr aRefByThird;
1451   if (aThirdPointRef->isObject())
1452     aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1453   if (!aRefByThird)
1454     return true;
1455
1456   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1457   threePointsOfFeature(theMacroFeature, aPoints);
1458
1459   std::shared_ptr<GeomAPI_Edge> aThirdShape =
1460       std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1461   if (!aThirdShape)
1462     return true;
1463
1464   SketchPlugin_Sketch* aSketch =
1465       std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1466   std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1467   std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1468
1469   bool isOk = true;
1470   if (aThirdShape->isLine())
1471     isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1472   else if (aThirdShape->isCircle() || aThirdShape->isArc())
1473     isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1474
1475   if (!isOk)
1476     theError = aErrorPointsApart;
1477   return isOk;
1478 }
1479
1480 bool SketchPlugin_ArcEndPointValidator::isValid(
1481     const AttributePtr& theAttribute,
1482     const std::list<std::string>& theArguments,
1483     Events_InfoMessage& theError) const
1484 {
1485   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1486   AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1487
1488   if(!anEndPointRef.get()) {
1489     return true;
1490   }
1491
1492   ObjectPtr anObject = anEndPointRef->object();
1493   AttributePtr anAttr = anEndPointRef->attr();
1494   if(!anObject.get() && !anAttr.get()) {
1495     return true;
1496   }
1497
1498   if(anEndPointRef->attr().get()) {
1499     return false;
1500   }
1501
1502   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1503   if(aResult.get()) {
1504     GeomShapePtr aShape = aResult->shape();
1505     if(aShape.get() && aShape->isVertex()) {
1506       return false;
1507     }
1508   }
1509
1510   aFeature = ModelAPI_Feature::feature(anObject);
1511   if(aFeature.get()) {
1512     if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1513       return false;
1514     }
1515   }
1516
1517   return true;
1518 }
1519
1520 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1521 {
1522   if(!theShape.get()) {
1523     return theShape;
1524   }
1525
1526   if(!theShape->isEdge()) {
1527     return theShape;
1528   }
1529
1530   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1531
1532   if(!anEdge.get()) {
1533     return theShape;
1534   }
1535
1536   if(anEdge->isLine()) {
1537     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1538     GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1539     return aShape;
1540   }
1541
1542   if(anEdge->isCircle() || anEdge->isArc()) {
1543     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1544     GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1545     return aShape;
1546   }
1547
1548   return theShape;
1549 }
1550
1551 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1552     const AttributePtr& theAttribute,
1553     const std::list<std::string>& theArguments,
1554     Events_InfoMessage& theError) const
1555 {
1556   std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1557       std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1558   AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1559
1560   if(!anEndPointRef.get()) {
1561     return true;
1562   }
1563
1564   GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1565
1566   if(!anArcShape.get() || anArcShape->isNull()) {
1567     return true;
1568   }
1569
1570   ObjectPtr anObject = anEndPointRef->object();
1571   AttributePtr anAttr = anEndPointRef->attr();
1572   if(!anObject.get() && !anAttr.get()) {
1573     return true;
1574   }
1575
1576   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1577   if(aResult.get()) {
1578     GeomShapePtr aShape = aResult->shape();
1579     if (!aShape->isEdge())
1580       return true;
1581     aShape = toInfiniteEdge(aShape);
1582     if(aShape.get() && !aShape->isNull()) {
1583       if(anArcShape->isIntersect(aShape)) {
1584         return true;
1585       }
1586     }
1587   }
1588
1589   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1590   if(aSelectedFeature.get()) {
1591     std::list<ResultPtr> aResults = aSelectedFeature->results();
1592     for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1593         anIt != aResults.cend();
1594         ++anIt)
1595     {
1596       GeomShapePtr aShape = (*anIt)->shape();
1597       if (!aShape->isEdge())
1598         return true;
1599       aShape = toInfiniteEdge(aShape);
1600       if(aShape.get() && !aShape->isNull()) {
1601         if(anArcShape->isIntersect(aShape)) {
1602           return true;
1603         }
1604       }
1605     }
1606   }
1607
1608   return false;
1609 }
1610
1611 bool SketchPlugin_HasNoConstraint::isValid(const AttributePtr& theAttribute,
1612                                            const std::list<std::string>& theArguments,
1613                                            Events_InfoMessage& theError) const
1614 {
1615   std::set<std::string> aFeatureKinds;
1616   for (std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1617        anArgIt != theArguments.end(); anArgIt++) {
1618     aFeatureKinds.insert(*anArgIt);
1619   }
1620
1621   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1622     theError = "The attribute with the %1 type is not processed";
1623     theError.arg(theAttribute->attributeType());
1624     return false;
1625   }
1626
1627   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
1628                                                                       (theAttribute);
1629   bool isObject = aRefAttr->isObject();
1630   if (!isObject) {
1631     theError = "It uses an empty object";
1632     return false;
1633   }
1634   ObjectPtr anObject = aRefAttr->object();
1635   FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1636   if (!aFeature.get()) {
1637     theError = "The feature of the checked attribute is empty";
1638     return false;
1639   }
1640
1641   FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aRefAttr->owner());
1642
1643   std::set<AttributePtr> aRefsList = anObject->data()->refsToMe();
1644   std::set<AttributePtr>::const_iterator anIt = aRefsList.begin();
1645   for (; anIt != aRefsList.end(); anIt++) {
1646     FeaturePtr aRefFeature = ModelAPI_Feature::feature((*anIt)->owner());
1647     if (aRefFeature.get() && aCurrentFeature != aRefFeature &&
1648         aFeatureKinds.find(aRefFeature->getKind()) != aFeatureKinds.end())
1649       return false; // constraint is found, that means that the check is not valid
1650   }
1651   return true;
1652 }
1653
1654 bool SketchPlugin_ReplicationReferenceValidator::isValid(
1655     const AttributePtr& theAttribute,
1656     const std::list<std::string>& theArguments,
1657     Events_InfoMessage& theError) const
1658 {
1659   AttributeRefAttrPtr aRefAttr =
1660       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1661   if (!aRefAttr)
1662   {
1663     theError = "Incorrect attribute";
1664     return false;
1665   }
1666
1667   ObjectPtr anOwner;
1668   if (aRefAttr->isObject())
1669     anOwner = aRefAttr->object();
1670   else
1671   {
1672     AttributePtr anAttr = aRefAttr->attr();
1673     anOwner = anAttr->owner();
1674   }
1675   FeaturePtr anAttrOwnerFeature = ModelAPI_Feature::feature(anOwner);
1676   if (!anAttrOwnerFeature)
1677     return true;
1678   AttributeBooleanPtr aCopyAttr = anAttrOwnerFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
1679   if (!aCopyAttr || !aCopyAttr->value())
1680     return true; // feature is not a copy, thus valid
1681
1682   // check the copy feature is already referred by the "Multi" feature
1683   FeaturePtr aMultiFeature = ModelAPI_Feature::feature(theAttribute->owner());
1684   AttributeRefListPtr aRefList = aMultiFeature->reflist(theArguments.front());
1685   for (int i = 0; i < aRefList->size(); ++i)
1686   {
1687     FeaturePtr aRefOwner = ModelAPI_Feature::feature(aRefList->object(i));
1688     if (aRefOwner == anAttrOwnerFeature)
1689     {
1690       theError = "Attribute refers to the object generated by this feature";
1691       return false;
1692     }
1693   }
1694
1695   return true;
1696 }
1697
1698 bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
1699                                                   const std::list<std::string>& theArguments,
1700                                                   Events_InfoMessage& theError) const
1701 {
1702   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1703     theError = "The attribute with the %1 type is not processed";
1704     theError.arg(theAttribute->attributeType());
1705     return false;
1706   }
1707
1708   // check the attribute refers to a sketch feature
1709   AttributeRefAttrPtr aRefAttr =
1710       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1711   bool isSketchFeature = aRefAttr->isObject();
1712   if (isSketchFeature) {
1713     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1714     isSketchFeature = aFeature.get() != NULL;
1715     if (isSketchFeature) {
1716       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1717           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1718       isSketchFeature = aSketchFeature.get() != NULL;
1719     }
1720   }
1721
1722   if (isSketchFeature)
1723     return true;
1724
1725   theError = "The object selected is not a sketch feature";
1726   return false;
1727 }
1728
1729 bool SketchPlugin_MultiRotationAngleValidator::isValid(const AttributePtr& theAttribute,
1730                                                        const std::list<std::string>& theArguments,
1731                                                        Events_InfoMessage& theError) const
1732 {
1733   if (theAttribute->attributeType() != ModelAPI_AttributeDouble::typeId()) {
1734     theError = "The attribute with the %1 type is not processed";
1735     theError.arg(theAttribute->attributeType());
1736     return false;
1737   }
1738
1739   AttributeDoublePtr anAngleAttr =
1740     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1741
1742   FeaturePtr aMultiRotation = ModelAPI_Feature::feature(theAttribute->owner());
1743   AttributeStringPtr anAngleType =
1744       aMultiRotation->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
1745   AttributeIntegerPtr aNbCopies =
1746       aMultiRotation->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID());
1747
1748   if (anAngleType->value() != "FullAngle")
1749   {
1750     double aFullAngleValue = anAngleAttr->value() * (aNbCopies->value() - 1);
1751     if (aFullAngleValue < -1.e-7 || aFullAngleValue > 359.9999999)
1752     {
1753       theError = "Rotation single angle should produce full angle less than 360 degree";
1754       return false;
1755     }
1756   }
1757   else
1758   {
1759     double aFullAngleValue = anAngleAttr->value();
1760     if (aFullAngleValue < -1.e-7 || aFullAngleValue > 360.0000001)
1761     {
1762       theError = "Rotation full angle should be in range [0, 360]";
1763       return false;
1764     }
1765   }
1766
1767   return true;
1768 }