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