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