]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Validators.cpp
Salome HOME
Issue #2440: Can't validate coincidence between a line and an imported line
[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_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_AttributeRefAttr.h>
46
47 #include <ModelAPI_AttributeRefAttrList.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->getKind() == SketchPlugin_ConstraintTangent::ID()) {
471       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
472         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
473       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
474         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
475       if(anAttrRefA.get()) {
476         ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
477         if(aResA.get()) {
478           DocumentPtr aDoc = aResA->document();
479           if(aDoc.get()) {
480             FeaturePtr aFeatureA = aDoc->feature(aResA);
481             if(aFeatureA.get() && aFeatureA == theFeature) {
482               return true;
483             }
484           }
485         }
486       }
487       if(anAttrRefB.get()) {
488         ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
489         if(aResB.get()) {
490           DocumentPtr aDoc = aResB->document();
491           if(aDoc.get()) {
492             FeaturePtr aFeatureB = aDoc->feature(aResB);
493             if(aFeatureB.get() && aFeatureB == theFeature) {
494               return true;
495             }
496           }
497         }
498       }
499     }
500   }
501   return false;
502 }
503
504 static bool isPointPointCoincidence(const FeaturePtr& theCoincidence)
505 {
506   AttributeRefAttrPtr aRefAttr[2] = {
507       theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_A()),
508       theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_B())
509   };
510
511   bool arePoints = true;
512   for (int i = 0; i < 2 && arePoints; ++i) {
513     if (aRefAttr[i]->isObject()) {
514       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
515       arePoints = aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID();
516     } else
517       arePoints = aRefAttr[i]->attr().get() != NULL;
518   }
519   return arePoints;
520 }
521
522 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
523                                                  const std::list<std::string>& theArguments,
524                                                  Events_InfoMessage& theError) const
525 {
526   AttributeRefAttrPtr aPointRefAttr =
527     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
528   if(!aPointRefAttr.get()) {
529     theError = "Error: Point not selected.";
530     return false;
531   }
532
533   AttributePtr aPointAttribute = aPointRefAttr->attr();
534   if (!aPointAttribute.get()) {
535     theError = "Error: Bad point selected.";
536     return false;
537   }
538   std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
539     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
540
541   // Obtain constraint coincidence for the fillet point.
542   const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
543   FeaturePtr aConstraintCoincidence;
544   for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
545       anIt != aRefsList.cend(); ++anIt) {
546     std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
547     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
548     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
549       if (!isPointPointCoincidence(aConstrFeature))
550         continue;
551
552       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
553         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
554       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
555         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
556
557       AttributePtr anAttrA = anAttrRefA->attr();
558       if(aPointAttribute == anAttrA) {
559         aConstraintCoincidence = aConstrFeature;
560         break;
561       }
562
563       AttributePtr anAttrB = anAttrRefB->attr();
564       if(aPointAttribute == anAttrB) {
565         aConstraintCoincidence = aConstrFeature;
566         break;
567       }
568     }
569   }
570
571   if(!aConstraintCoincidence.get()) {
572     theError = "Error: one of the selected point does not have coicidence.";
573     return false;
574   }
575
576   // Get coincides from constraint.
577   std::set<FeaturePtr> aCoinsides;
578   SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
579                                         SketchPlugin_ConstraintCoincidence::ENTITY_A(),
580                                         aCoinsides,
581                                         true);
582   SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
583                                         SketchPlugin_ConstraintCoincidence::ENTITY_B(),
584                                         aCoinsides,
585                                         true);
586
587   // Remove points and external lines from set of coincides.
588   std::set<FeaturePtr> aNewSetOfCoincides;
589   for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
590       anIt != aCoinsides.end(); ++anIt) {
591     std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
592       std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
593     if(aSketchEntity.get() && (aSketchEntity->isCopy() || aSketchEntity->isExternal())) {
594       continue;
595     }
596     if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
597         (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
598           continue;
599     }
600     if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
601       AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
602       std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
603         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
604       double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
605       if(aDistSelectedArcCenter < tolerance) {
606         continue;
607       }
608     }
609     aNewSetOfCoincides.insert(*anIt);
610   }
611   aCoinsides = aNewSetOfCoincides;
612
613   // If we still have more than two coincides remove auxilary entities from set of coincides.
614   if(aCoinsides.size() > 2) {
615     aNewSetOfCoincides.clear();
616     for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
617         anIt != aCoinsides.end(); ++anIt) {
618       if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
619         aNewSetOfCoincides.insert(*anIt);
620       }
621     }
622     aCoinsides = aNewSetOfCoincides;
623   }
624
625   if(aCoinsides.size() != 2) {
626     theError = "Error: One of the selected points does not have two suitable edges for fillet.";
627     return false;
628   }
629
630   // Check that selected edges don't have tangent constraint.
631   std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
632   FeaturePtr aFirstFeature = *anIt++;
633   FeaturePtr aSecondFeature = *anIt;
634   const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
635   if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
636     theError = "Error: Edges in selected point has tangent constraint.";
637     return false;
638   }
639
640   std::list<ResultPtr> aFirstResults = aFirstFeature->results();
641   for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
642       aResIt != aFirstResults.end(); ++aResIt) {
643     ResultPtr aRes = *aResIt;
644     const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
645     if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
646       theError = "Error: Edges in selected point has tangent constraint.";
647       return false;
648     }
649   }
650
651   // Check the features are not tangent
652   std::shared_ptr<GeomAPI_Shape> aFirstShape = aFirstFeature->lastResult()->shape();
653   std::shared_ptr<GeomAPI_Shape> aSecondShape = aSecondFeature->lastResult()->shape();
654   if (!aFirstShape || !aFirstShape->isEdge() ||
655       !aSecondShape || !aSecondShape->isEdge()) {
656     theError = "Error: At least on of the features is not an edge";
657     return false;
658   }
659
660   std::shared_ptr<GeomAPI_Edge> anEdge1 = std::dynamic_pointer_cast<GeomAPI_Edge>(aFirstShape);
661   std::shared_ptr<GeomAPI_Edge> anEdge2 = std::dynamic_pointer_cast<GeomAPI_Edge>(aSecondShape);
662
663   static const double TOL = 1.e-7;
664   if (anEdge1->isLine() && anEdge2->isLine()) {
665     // Check that lines not collinear
666     std::shared_ptr<GeomAPI_Dir> aDir1 = anEdge1->line()->direction();
667     std::shared_ptr<GeomAPI_Dir> aDir2 = anEdge2->line()->direction();
668     double aCross = aDir1->cross(aDir2)->squareModulus();
669     if (aCross < TOL * TOL)
670       return false;
671   } else if (anEdge1->isArc() && anEdge2->isArc()) {
672     // check the circles are not tangent
673     std::shared_ptr<GeomAPI_Circ> aCirc1 = anEdge1->circle();
674     std::shared_ptr<GeomAPI_Circ> aCirc2 = anEdge2->circle();
675     double aDistCC = aCirc1->center()->distance(aCirc2->center());
676     double aRadSum = aCirc1->radius() + aCirc2->radius();
677     double aRadDiff = fabs(aCirc1->radius() - aCirc2->radius());
678     if (fabs(aDistCC - aRadSum) < TOL || fabs(aDistCC - aRadDiff) < TOL)
679       return false;
680   } else {
681     // check whether line and arc are tangent
682     std::shared_ptr<GeomAPI_Circ> aCirc;
683     std::shared_ptr<GeomAPI_Lin> aLine;
684     if (anEdge1->isLine()) {
685       aLine = anEdge1->line();
686       aCirc = anEdge2->circle();
687     } else {
688       aCirc = anEdge1->circle();
689       aLine = anEdge2->line();
690     }
691
692     double aDistCL = aLine->distance(aCirc->center());
693     if (fabs(aDistCL - aCirc->radius()) < TOL)
694       return false;
695   }
696
697   return true;
698 }
699
700 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
701                                                     const std::list<std::string>& theArguments,
702                                                     Events_InfoMessage& theError) const
703 {
704   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
705     theError = "The attribute with the %1 type is not processed";
706     theError.arg(theAttribute->attributeType());
707     return false;
708   }
709
710   // there is a check whether the feature contains a point and a linear edge or two point values
711   std::string aParamA = theArguments.front();
712   SessionPtr aMgr = ModelAPI_Session::get();
713   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
714
715   FeaturePtr anAttributeFeature =
716     std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
717   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
718   AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
719
720   AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
721   int aNbPoints = 0;
722   int aNbLines = 0;
723   for (int i = 0; i < 2; ++i) {
724     if (!aRefAttrs[i]->isObject())
725       ++aNbPoints;
726     else {
727       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
728       if (!aFeature) {
729         if (aNbPoints + aNbLines != 0)
730           return true;
731         else continue;
732       }
733
734       if (aFeature->getKind() == SketchPlugin_Point::ID())
735         ++aNbPoints;
736       else if (aFeature->getKind() == SketchPlugin_Line::ID())
737         ++aNbLines;
738     }
739   }
740
741   if (aNbPoints != 1 || aNbLines != 1) {
742     theError = "Middle point constraint allows points and lines only";
743     return false;
744   }
745   return true;
746 }
747
748 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
749                                                     const std::list<std::string>& /*theArguments*/,
750                                                     Events_InfoMessage& theError) const
751 {
752   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
753     theError = "The attribute with the %1 type is not processed";
754     theError.arg(theAttribute->attributeType());
755     return false;
756   }
757   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
758   AttributePtr anAttr = aRefAttr->attr();
759   if (!anAttr) {
760     theError = "The attribute %1 should be a point";
761     theError.arg(theAttribute->id());
762     return false;
763   }
764
765   FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
766   const std::string& aFeatureType = anAttrFeature->getKind();
767   if (aFeatureType == SketchPlugin_Arc::ID()) {
768     // selected point should not be a center of arc
769     const std::string& aPntId = anAttr->id();
770     if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
771       theError = "The attribute %1 is not supported";
772       theError.arg(aPntId);
773       return false;
774     }
775   }
776   else if (aFeatureType == SketchPlugin_Line::ID()) {
777     // selected point should be bound point of line
778     const std::string& aPntId = anAttr->id();
779     if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
780       theError = "The attribute %1 is not supported";
781       theError.arg(aPntId);
782       return false;
783     }
784   }
785   else {
786     theError = "Unable to build tangent arc on %1";
787     theError.arg(anAttrFeature->getKind());
788     return false;
789   }
790
791   return true;
792 }
793
794 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
795                                                  const std::list<std::string>& theArguments,
796                                                  Events_InfoMessage& theError) const
797 {
798   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
799     theError = "The attribute with the %1 type is not processed";
800     theError.arg(theAttribute->attributeType());
801     return false;
802   }
803   AttributeSelectionPtr aLineAttr =
804                        std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
805   std::shared_ptr<GeomAPI_Edge> anEdge;
806   if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
807     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
808   } else if(aLineAttr->context() &&
809             aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
810     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
811   }
812
813   if (!anEdge || !anEdge->isLine()) {
814     theError = "The attribute %1 should be a line";
815     theError.arg(theAttribute->id());
816     return false;
817   }
818
819   std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
820
821   // find a sketch
822   std::shared_ptr<SketchPlugin_Sketch> aSketch;
823   std::set<AttributePtr> aRefs = aLineAttr->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   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
839   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
840   return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
841 }
842
843 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
844                                           const std::list<std::string>& theArguments,
845                                           Events_InfoMessage& theError) const
846 {
847   bool aValid = false;
848
849   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
850     theError = "The attribute with the %1 type is not processed";
851     theError.arg(theAttribute->attributeType());
852     return aValid;
853   }
854   AttributeReferencePtr aFeatureAttr =
855             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
856
857   ObjectPtr anAttrObject = aFeatureAttr->value();
858   FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
859   if (!anAttrFeature)
860     return aValid;
861
862   std::string aKind = anAttrFeature->getKind();
863   if (aKind == SketchPlugin_Line::ID() ||
864       aKind == SketchPlugin_Arc::ID() ||
865       aKind == SketchPlugin_Circle::ID()) {
866
867     std::set<ResultPtr> anEdgeShapes;
868     ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
869     if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
870       return aValid;
871
872     // coincidences to the feature
873     std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
874     ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
875                         SketchPlugin_ConstraintCoincidence::ID(),
876                         aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
877
878     GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
879     std::shared_ptr<SketchPlugin_Feature> aSFeature =
880                                  std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
881     SketchPlugin_Sketch* aSketch = aSFeature->sketch();
882
883     std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
884     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
885         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
886     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
887         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
888     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
889         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
890     std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
891
892     typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
893                      std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
894                                std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
895     PointToRefsMap aPointsInfo;
896
897     ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
898                                                 aX->dir(), aDirY, aPointsInfo);
899     int aCoincidentToFeature = (int)aPointsInfo.size();
900     if (aKind == SketchPlugin_Circle::ID())
901       aValid = aCoincidentToFeature >= 2;
902     else
903       aValid = aCoincidentToFeature >= 1;
904   }
905
906   return aValid;
907 }
908
909 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
910                                          const std::list<std::string>& theArguments,
911                                          Events_InfoMessage& theError) const
912 {
913   bool aValid = false;
914
915   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
916     theError = "The attribute with the %1 type is not processed";
917     theError.arg(theAttribute->attributeType());
918     return aValid;
919   }
920   AttributeReferencePtr aBaseObjectAttr =
921             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
922
923   std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
924                  std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
925
926   ObjectPtr aBaseObject = aBaseObjectAttr->value();
927   if (!aBaseObject) {
928     AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
929     aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
930     aBaseObject = aBaseObjectAttr->value();
931   }
932
933   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
934   if (!aBaseFeature)
935     return aValid;
936
937   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
938                                  std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
939   if (!aSketchFeature.get() || aSketchFeature->isCopy())
940     return aValid;
941
942   std::string aKind = aBaseFeature->getKind();
943   if (aKind != SketchPlugin_Line::ID() &&
944       aKind != SketchPlugin_Arc::ID() &&
945       aKind != SketchPlugin_Circle::ID())
946     return aValid;
947
948   // point on feature
949   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
950                        aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
951
952   SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
953
954   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
955   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
956                                                               anAttributePnt2d->y());
957
958   std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
959   std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
960            std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
961                      std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
962   SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
963                                       aCashedShapes, anObjectToPoints);
964   const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
965
966   return aShapes.size() > 1;
967 }
968
969 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
970                                                const std::list<std::string>& theArguments,
971                                                Events_InfoMessage& theError) const
972 {
973   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
974     theError = "The attribute with the %1 type is not processed";
975     theError.arg(theAttribute->attributeType());
976     return false;
977   }
978
979   AttributeSelectionPtr aFeatureAttr =
980       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
981   std::shared_ptr<GeomAPI_Edge> anEdge;
982   std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
983   if (aFeatureAttr.get()) {
984     GeomShapePtr aVal = aFeatureAttr->value();
985     ResultPtr aRes = aFeatureAttr->context();
986     if (aVal && aVal->isVertex())
987       return true; // vertex is always could be projected
988     if (aVal && aVal->isEdge()) {
989       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
990     } else if(aRes && aRes->shape()) {
991       if (aRes->shape()->isVertex())
992         return true; // vertex is always could be projected
993       else if (aRes->shape()->isEdge())
994         anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
995     }
996
997     // try to convert result to sketch feature
998     if (aRes) {
999       aSketchFeature =
1000         std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
1001     }
1002   }
1003   if (!anEdge) {
1004     theError = "The attribute %1 should be an edge or vertex";
1005     theError.arg(theAttribute->id());
1006     return false;
1007   }
1008
1009   // find a sketch
1010   std::shared_ptr<SketchPlugin_Sketch> aSketch;
1011   std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
1012   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
1013   for (; anIt != aRefs.end(); ++anIt) {
1014     CompositeFeaturePtr aComp =
1015         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1016     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1017       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1018       break;
1019     }
1020   }
1021   if (!aSketch) {
1022     theError = "There is no sketch referring to the current feature";
1023     return false;
1024   }
1025   if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
1026     theError = "Unable to project feature from the same sketch";
1027     return false;
1028   }
1029
1030   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1031   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1032   std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1033
1034   if (anEdge->isLine()) {
1035     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1036     std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1037     double aDot = fabs(aNormal->dot(aLineDir));
1038     bool aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
1039     if (!aValid)
1040       theError = "Error: Edge is already in the sketch plane.";
1041     return aValid;
1042   }
1043   else if (anEdge->isCircle() || anEdge->isArc()) {
1044     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1045     std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1046     double aDot = fabs(aNormal->dot(aCircNormal));
1047     bool aValid = fabs(aDot - 1.0) < tolerance * tolerance;
1048     if (!aValid)
1049       theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
1050                                       : "Error: Arc is already in the sketch plane.");
1051     return aValid;
1052   }
1053
1054   theError = "Error: Selected object is not line, circle or arc.";
1055   return false;
1056 }
1057
1058
1059 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1060                                    const std::set<FeaturePtr>& theSet2)
1061 {
1062   std::set<FeaturePtr> aCommon;
1063   if (theSet1.empty() || theSet2.empty())
1064     return aCommon;
1065
1066   std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1067   for (; anIt2 != theSet2.end(); ++anIt2)
1068     if (theSet1.find(*anIt2) != theSet1.end())
1069       aCommon.insert(*anIt2);
1070   return aCommon;
1071 }
1072
1073 bool SketchPlugin_DifferentReferenceValidator::isValid(
1074     const AttributePtr& theAttribute,
1075     const std::list<std::string>& theArguments,
1076     Events_InfoMessage& theError) const
1077 {
1078   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1079
1080   int aNbFeaturesReferred = 0;
1081   int aNbAttributesReferred = 0;
1082   std::set<FeaturePtr> aCommonReferredFeatures;
1083
1084   // find all features referred by attributes listed in theArguments
1085   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1086   for (; anArgIt != theArguments.end(); ++anArgIt) {
1087     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1088     if (!aRefAttr)
1089       continue;
1090
1091     std::set<FeaturePtr> aCoincidentFeatures;
1092     if (aRefAttr->isObject()) {
1093       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1094       if (aFeature) {
1095         aCoincidentFeatures.insert(aFeature);
1096         aNbFeaturesReferred += 1;
1097       }
1098     } else {
1099       AttributePoint2DPtr aPoint =
1100           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1101       if (aPoint) {
1102         aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1103         aNbAttributesReferred += 1;
1104       }
1105     }
1106
1107     if (aCommonReferredFeatures.empty())
1108       aCommonReferredFeatures = aCoincidentFeatures;
1109     else
1110       aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1111
1112     if (aCommonReferredFeatures.empty())
1113       return true;
1114   }
1115
1116   bool isOk = aNbFeaturesReferred < 1;
1117   if (aNbFeaturesReferred == 1) {
1118     if (aCommonReferredFeatures.size() == 1) {
1119       FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1120       isOk = aNbAttributesReferred <= 1 ||
1121              aFeature->getKind() == SketchPlugin_Circle::ID() ||
1122              aFeature->getKind() == SketchPlugin_Arc::ID();
1123     } else
1124       isOk = false;
1125   }
1126
1127   if (!isOk)
1128     theError = "Attributes are referred to the same feature";
1129   return isOk;
1130 }
1131
1132 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1133     const AttributePtr& theAttribute,
1134     const std::list<std::string>& theArguments,
1135     Events_InfoMessage& theError) const
1136 {
1137   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1138   std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1139
1140   // find all points referred by attributes listed in theArguments
1141   bool hasRefsToPoints = false;
1142   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1143   for (; anArgIt != theArguments.end(); ++anArgIt) {
1144     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1145     if (!aRefAttr)
1146       continue;
1147
1148     if (!aRefAttr->isObject()) {
1149       AttributePoint2DPtr aPoint =
1150           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1151       if (aReferredCoincidentPoints.empty())
1152         aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1153       else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1154         return true; // non-coincident point has been found
1155       else
1156         hasRefsToPoints = true;
1157     }
1158   }
1159
1160   if (hasRefsToPoints)
1161     theError = "Attributes are referred to the same point";
1162   return !hasRefsToPoints;
1163 }
1164
1165 bool SketchPlugin_CirclePassedPointValidator::isValid(
1166     const AttributePtr& theAttribute,
1167     const std::list<std::string>&,
1168     Events_InfoMessage& theError) const
1169 {
1170   static const std::string aErrorMessage(
1171       "Passed point refers to the same feature as a center point");
1172
1173   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1174
1175   AttributeRefAttrPtr aCenterRef =
1176       anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1177   AttributeRefAttrPtr aPassedRef =
1178       anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1179
1180   if (!aPassedRef->isObject())
1181     return true;
1182
1183   FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1184   if (!aPassedFeature)
1185     return true;
1186
1187   if (aCenterRef->isObject()) {
1188     if (aCenterRef->object() == aPassedRef->object()) {
1189       theError = aErrorMessage;
1190       return false;
1191     }
1192   } else {
1193     AttributePoint2DPtr aCenterPoint =
1194         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1195     if (aCenterPoint) {
1196       std::set<FeaturePtr> aCoincidentFeatures =
1197           SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1198       // check one of coincident features is a feature referred by passed point
1199       std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1200       for(; anIt != aCoincidentFeatures.end(); ++anIt)
1201         if (*anIt == aPassedFeature) {
1202           theError = aErrorMessage;
1203           return false;
1204         }
1205     }
1206   }
1207   return true;
1208 }
1209
1210 bool SketchPlugin_ThirdPointValidator::isValid(
1211     const AttributePtr& theAttribute,
1212     const std::list<std::string>& theArguments,
1213     Events_InfoMessage& theError) const
1214 {
1215   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1216   return arePointsNotOnLine(anOwner, theError) &&
1217          arePointsNotSeparated(anOwner, theArguments, theError);
1218 }
1219
1220 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1221                                               const std::string& thePointAttrName,
1222                                               const std::string& theRefPointAttrName)
1223 {
1224   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1225       theMacroCircle->attribute(thePointAttrName));
1226   AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1227
1228   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1229   if (aRefAttr) {
1230     if (aRefAttr->isObject()) {
1231       // project a point onto selected feature
1232       std::shared_ptr<SketchPlugin_Feature> aFeature =
1233           std::dynamic_pointer_cast<SketchPlugin_Feature>(
1234           ModelAPI_Feature::feature(aRefAttr->object()));
1235       if (aFeature) {
1236         SketchPlugin_Sketch* aSketch = aFeature->sketch();
1237         std::shared_ptr<GeomAPI_Edge> anEdge =
1238             std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1239         if (anEdge) {
1240           std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1241           if (anEdge->isLine())
1242             aPoint3D = anEdge->line()->project(aPoint3D);
1243           else if (anEdge->isCircle())
1244             aPoint3D = anEdge->circle()->project(aPoint3D);
1245           if(aPoint3D)
1246             aPoint = aSketch->to2D(aPoint3D);
1247         }
1248       }
1249     } else {
1250       AttributePoint2DPtr anOtherPoint =
1251           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1252       if (anOtherPoint)
1253         aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1254     }
1255   }
1256
1257   return aPoint;
1258 }
1259
1260 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1261                                  std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1262 {
1263   if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1264     thePoints[0] = toPoint(theMacroFeature,
1265           SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1266           SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1267     thePoints[1] = toPoint(theMacroFeature,
1268           SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1269           SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1270     thePoints[2] = toPoint(theMacroFeature,
1271           SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1272           SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1273   } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1274     thePoints[0] = toPoint(theMacroFeature,
1275           SketchPlugin_MacroArc::START_POINT_2_ID(),
1276           SketchPlugin_MacroArc::START_POINT_REF_ID());
1277     thePoints[1] = toPoint(theMacroFeature,
1278           SketchPlugin_MacroArc::END_POINT_2_ID(),
1279           SketchPlugin_MacroArc::END_POINT_REF_ID());
1280     thePoints[2] = toPoint(theMacroFeature,
1281           SketchPlugin_MacroArc::PASSED_POINT_ID(),
1282           SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1283   }
1284 }
1285
1286 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1287                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1288                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1289 {
1290   static const double aTolerance = 1.e-7;
1291   if (thePoint1->distance(thePoint2) < aTolerance ||
1292       thePoint1->distance(thePoint3) < aTolerance)
1293     return true;
1294
1295   std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1296                                                             thePoint2->y() - thePoint1->y()));
1297   std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1298                                                             thePoint3->y() - thePoint1->y()));
1299   return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1300 }
1301
1302 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1303                          const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1304                          const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1305 {
1306   static const double aTolerance = 1.e-7;
1307   std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1308   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1309
1310   std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1311   // the first point is on the line
1312   if (aVec1->squareModulus() < aTolerance * aTolerance)
1313     return false;
1314   std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1315   std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1316   // the second point is on the line
1317   if (aVec2->squareModulus() < aTolerance * aTolerance)
1318     return false;
1319   std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1320
1321   return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1322 }
1323
1324 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1325                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint1,
1326                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint2)
1327 {
1328   static const double aTolerance = 1.e-7;
1329   std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1330   double aDistP1C = thePoint1->distance(aCenter);
1331   double aDistP2C = thePoint2->distance(aCenter);
1332   return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1333 }
1334
1335 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1336     const FeaturePtr& theMacroFeature,
1337     Events_InfoMessage& theError) const
1338 {
1339   static const std::string aErrorPointsOnLine(
1340       "Selected points are on the same line");
1341
1342   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1343   threePointsOfFeature(theMacroFeature, aPoints);
1344
1345   if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1346     theError = aErrorPointsOnLine;
1347     return false;
1348   }
1349   return true;
1350 }
1351
1352 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1353     const FeaturePtr& theMacroFeature,
1354     const std::list<std::string>& theArguments,
1355     Events_InfoMessage& theError) const
1356 {
1357   static const std::string aErrorPointsApart(
1358       "Selected entity is lying between first two points");
1359
1360   AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1361   FeaturePtr aRefByThird;
1362   if (aThirdPointRef->isObject())
1363     aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1364   if (!aRefByThird)
1365     return true;
1366
1367   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1368   threePointsOfFeature(theMacroFeature, aPoints);
1369
1370   std::shared_ptr<GeomAPI_Edge> aThirdShape =
1371       std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1372   if (!aThirdShape)
1373     return true;
1374
1375   SketchPlugin_Sketch* aSketch =
1376       std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1377   std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1378   std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1379
1380   bool isOk = true;
1381   if (aThirdShape->isLine())
1382     isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1383   else if (aThirdShape->isCircle() || aThirdShape->isArc())
1384     isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1385
1386   if (!isOk)
1387     theError = aErrorPointsApart;
1388   return isOk;
1389 }
1390
1391 bool SketchPlugin_ArcEndPointValidator::isValid(
1392     const AttributePtr& theAttribute,
1393     const std::list<std::string>& theArguments,
1394     Events_InfoMessage& theError) const
1395 {
1396   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1397   AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1398
1399   if(!anEndPointRef.get()) {
1400     return true;
1401   }
1402
1403   ObjectPtr anObject = anEndPointRef->object();
1404   AttributePtr anAttr = anEndPointRef->attr();
1405   if(!anObject.get() && !anAttr.get()) {
1406     return true;
1407   }
1408
1409   if(anEndPointRef->attr().get()) {
1410     return false;
1411   }
1412
1413   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1414   if(aResult.get()) {
1415     GeomShapePtr aShape = aResult->shape();
1416     if(aShape.get() && aShape->isVertex()) {
1417       return false;
1418     }
1419   }
1420
1421   aFeature = ModelAPI_Feature::feature(anObject);
1422   if(aFeature.get()) {
1423     if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1424       return false;
1425     }
1426   }
1427
1428   return true;
1429 }
1430
1431 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1432 {
1433   if(!theShape.get()) {
1434     return theShape;
1435   }
1436
1437   if(!theShape->isEdge()) {
1438     return theShape;
1439   }
1440
1441   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1442
1443   if(!anEdge.get()) {
1444     return theShape;
1445   }
1446
1447   if(anEdge->isLine()) {
1448     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1449     GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1450     return aShape;
1451   }
1452
1453   if(anEdge->isCircle() || anEdge->isArc()) {
1454     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1455     GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1456     return aShape;
1457   }
1458
1459   return theShape;
1460 }
1461
1462 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1463     const AttributePtr& theAttribute,
1464     const std::list<std::string>& theArguments,
1465     Events_InfoMessage& theError) const
1466 {
1467   std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1468       std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1469   AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1470
1471   if(!anEndPointRef.get()) {
1472     return true;
1473   }
1474
1475   GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1476
1477   if(!anArcShape.get() || anArcShape->isNull()) {
1478     return true;
1479   }
1480
1481   ObjectPtr anObject = anEndPointRef->object();
1482   AttributePtr anAttr = anEndPointRef->attr();
1483   if(!anObject.get() && !anAttr.get()) {
1484     return true;
1485   }
1486
1487   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1488   if(aResult.get()) {
1489     GeomShapePtr aShape = aResult->shape();
1490     if (!aShape->isEdge())
1491       return true;
1492     aShape = toInfiniteEdge(aShape);
1493     if(aShape.get() && !aShape->isNull()) {
1494       if(anArcShape->isIntersect(aShape)) {
1495         return true;
1496       }
1497     }
1498   }
1499
1500   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1501   if(aSelectedFeature.get()) {
1502     std::list<ResultPtr> aResults = aSelectedFeature->results();
1503     for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1504         anIt != aResults.cend();
1505         ++anIt)
1506     {
1507       GeomShapePtr aShape = (*anIt)->shape();
1508       if (!aShape->isEdge())
1509         return true;
1510       aShape = toInfiniteEdge(aShape);
1511       if(aShape.get() && !aShape->isNull()) {
1512         if(anArcShape->isIntersect(aShape)) {
1513           return true;
1514         }
1515       }
1516     }
1517   }
1518
1519   return false;
1520 }
1521
1522 bool SketchPlugin_HasNoConstraint::isValid(const AttributePtr& theAttribute,
1523                                            const std::list<std::string>& theArguments,
1524                                            Events_InfoMessage& theError) const
1525 {
1526   std::set<std::string> aFeatureKinds;
1527   for (std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1528        anArgIt != theArguments.end(); anArgIt++) {
1529     aFeatureKinds.insert(*anArgIt);
1530   }
1531
1532   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1533     theError = "The attribute with the %1 type is not processed";
1534     theError.arg(theAttribute->attributeType());
1535     return false;
1536   }
1537
1538   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
1539                                                                       (theAttribute);
1540   bool isObject = aRefAttr->isObject();
1541   if (!isObject) {
1542     theError = "It uses an empty object";
1543     return false;
1544   }
1545   ObjectPtr anObject = aRefAttr->object();
1546   FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1547   if (!aFeature.get()) {
1548     theError = "The feature of the checked attribute is empty";
1549     return false;
1550   }
1551
1552   FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aRefAttr->owner());
1553
1554   std::set<AttributePtr> aRefsList = anObject->data()->refsToMe();
1555   std::set<AttributePtr>::const_iterator anIt = aRefsList.begin();
1556   for (; anIt != aRefsList.end(); anIt++) {
1557     FeaturePtr aRefFeature = ModelAPI_Feature::feature((*anIt)->owner());
1558     if (aRefFeature.get() && aCurrentFeature != aRefFeature &&
1559         aFeatureKinds.find(aRefFeature->getKind()) != aFeatureKinds.end())
1560       return false; // constraint is found, that means that the check is not valid
1561   }
1562   return true;
1563 }
1564
1565 bool SketchPlugin_ReplicationReferenceValidator::isValid(
1566     const AttributePtr& theAttribute,
1567     const std::list<std::string>& theArguments,
1568     Events_InfoMessage& theError) const
1569 {
1570   AttributeRefAttrPtr aRefAttr =
1571       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1572   if (!aRefAttr)
1573   {
1574     theError = "Incorrect attribute";
1575     return false;
1576   }
1577
1578   ObjectPtr anOwner;
1579   if (aRefAttr->isObject())
1580     anOwner = aRefAttr->object();
1581   else
1582   {
1583     AttributePtr anAttr = aRefAttr->attr();
1584     anOwner = anAttr->owner();
1585   }
1586   FeaturePtr anAttrOwnerFeature = ModelAPI_Feature::feature(anOwner);
1587   if (!anAttrOwnerFeature)
1588     return true;
1589   AttributeBooleanPtr aCopyAttr = anAttrOwnerFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
1590   if (!aCopyAttr || !aCopyAttr->value())
1591     return true; // feature is not a copy, thus valid
1592
1593   // check the copy feature is already referred by the "Multi" feature
1594   FeaturePtr aMultiFeature = ModelAPI_Feature::feature(theAttribute->owner());
1595   AttributeRefListPtr aRefList = aMultiFeature->reflist(theArguments.front());
1596   for (int i = 0; i < aRefList->size(); ++i)
1597   {
1598     FeaturePtr aRefOwner = ModelAPI_Feature::feature(aRefList->object(i));
1599     if (aRefOwner == anAttrOwnerFeature)
1600     {
1601       theError = "Attribute refers to the object generated by this feature";
1602       return false;
1603     }
1604   }
1605
1606   return true;
1607 }
1608
1609 bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
1610                                                   const std::list<std::string>& theArguments,
1611                                                   Events_InfoMessage& theError) const
1612 {
1613   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1614     theError = "The attribute with the %1 type is not processed";
1615     theError.arg(theAttribute->attributeType());
1616     return false;
1617   }
1618
1619   // check the attribute refers to a sketch feature
1620   AttributeRefAttrPtr aRefAttr =
1621       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1622   bool isSketchFeature = aRefAttr->isObject();
1623   if (isSketchFeature) {
1624     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1625     isSketchFeature = aFeature.get() != NULL;
1626     if (isSketchFeature) {
1627       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1628           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1629       isSketchFeature = aSketchFeature.get() != NULL;
1630     }
1631   }
1632
1633   if (isSketchFeature)
1634     return true;
1635
1636   theError = "The object selected is not a sketch feature";
1637   return false;
1638 }