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