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