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