Salome HOME
Protect usage of B-spline in Mirror, LinearCopy and AngularCopy features.
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Validators.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchPlugin_Validators.h"
21
22 #include "SketchPlugin_Arc.h"
23 #include "SketchPlugin_BSpline.h"
24 #include "SketchPlugin_BSplinePeriodic.h"
25 #include "SketchPlugin_Circle.h"
26 #include "SketchPlugin_ConstraintCoincidence.h"
27 #include "SketchPlugin_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_Edge> anEdge;
1165   std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
1166   if (aFeatureAttr.get()) {
1167     GeomShapePtr aVal = aFeatureAttr->value();
1168     ResultPtr aRes = aFeatureAttr->context();
1169     if (aVal && aVal->isVertex())
1170       return true; // vertex is always could be projected
1171     if (aVal && aVal->isEdge()) {
1172       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
1173     } else if(aRes && aRes->shape()) {
1174       if (aRes->shape()->isVertex())
1175         return true; // vertex is always could be projected
1176       else if (aRes->shape()->isEdge())
1177         anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
1178     }
1179
1180     // try to convert result to sketch feature
1181     if (aRes) {
1182       aSketchFeature =
1183         std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
1184     }
1185   }
1186   if (!anEdge) {
1187     theError = "The attribute %1 should be an edge or vertex";
1188     theError.arg(theAttribute->id());
1189     return false;
1190   }
1191
1192   // find a sketch
1193   std::shared_ptr<SketchPlugin_Sketch> aSketch;
1194   std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
1195   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
1196   for (; anIt != aRefs.end(); ++anIt) {
1197     CompositeFeaturePtr aComp =
1198         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1199     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1200       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1201       break;
1202     }
1203   }
1204   if (!aSketch) {
1205     theError = "There is no sketch referring to the current feature";
1206     return false;
1207   }
1208   if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
1209     theError = "Unable to project feature from the same sketch";
1210     return false;
1211   }
1212
1213   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1214   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1215   std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1216
1217   bool aValid = true;
1218   if (anEdge->isLine()) {
1219     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1220     std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1221     double aDot = fabs(aNormal->dot(aLineDir));
1222     aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
1223     if (!aValid)
1224       theError = "Error: Line is orthogonal to the sketch plane.";
1225   }
1226   else if (anEdge->isCircle() || anEdge->isArc()) {
1227     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1228     std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1229     double aDot = fabs(aNormal->dot(aCircNormal));
1230     aValid = aDot >= tolerance * tolerance;
1231     if (!aValid)
1232       theError.arg(anEdge->isCircle() ? "Error: Circle is orthogonal to the sketch plane."
1233                                       : "Error: Arc is orthogonal to the sketch plane.");
1234   }
1235   else if (anEdge->isEllipse()) {
1236     std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
1237     std::shared_ptr<GeomAPI_Dir> anEllipseNormal = anEllipse->normal();
1238     double aDot = fabs(aNormal->dot(anEllipseNormal));
1239     aValid = fabs(aDot - 1.0) <= tolerance * tolerance;
1240     if (!aValid)
1241       theError.arg(anEdge->isClosed() ? "Error: Ellipse is orthogonal to the sketch plane."
1242                                       : "Error: Elliptic Arc is orthogonal to the sketch plane.");
1243   }
1244
1245   return aValid;
1246 }
1247
1248
1249 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1250                                    const std::set<FeaturePtr>& theSet2)
1251 {
1252   std::set<FeaturePtr> aCommon;
1253   if (theSet1.empty() || theSet2.empty())
1254     return aCommon;
1255
1256   std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1257   for (; anIt2 != theSet2.end(); ++anIt2)
1258     if (theSet1.find(*anIt2) != theSet1.end())
1259       aCommon.insert(*anIt2);
1260   return aCommon;
1261 }
1262
1263 bool SketchPlugin_DifferentReferenceValidator::isValid(
1264     const AttributePtr& theAttribute,
1265     const std::list<std::string>& theArguments,
1266     Events_InfoMessage& theError) const
1267 {
1268   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1269
1270   int aNbFeaturesReferred = 0;
1271   int aNbAttributesReferred = 0;
1272   std::set<FeaturePtr> aCommonReferredFeatures;
1273
1274   // find all features referred by attributes listed in theArguments
1275   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1276   for (; anArgIt != theArguments.end(); ++anArgIt) {
1277     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1278     if (!aRefAttr)
1279       continue;
1280
1281     std::set<FeaturePtr> aCoincidentFeatures;
1282     if (aRefAttr->isObject()) {
1283       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1284       if (aFeature) {
1285         aCoincidentFeatures.insert(aFeature);
1286         aNbFeaturesReferred += 1;
1287       }
1288     } else {
1289       AttributePoint2DPtr aPoint =
1290           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1291       if (aPoint) {
1292         aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1293         aNbAttributesReferred += 1;
1294       }
1295     }
1296
1297     if (aCommonReferredFeatures.empty())
1298       aCommonReferredFeatures = aCoincidentFeatures;
1299     else
1300       aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1301
1302     if (aCommonReferredFeatures.empty())
1303       return true;
1304   }
1305
1306   bool isOk = aNbFeaturesReferred < 1;
1307   if (aNbFeaturesReferred == 1) {
1308     if (aCommonReferredFeatures.size() == 1) {
1309       FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1310       isOk = aNbAttributesReferred <= 1 ||
1311              aFeature->getKind() == SketchPlugin_Circle::ID() ||
1312              aFeature->getKind() == SketchPlugin_Arc::ID();
1313     } else
1314       isOk = false;
1315   }
1316
1317   if (!isOk)
1318     theError = "Attributes are referred to the same feature";
1319   return isOk;
1320 }
1321
1322 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1323     const AttributePtr& theAttribute,
1324     const std::list<std::string>& theArguments,
1325     Events_InfoMessage& theError) const
1326 {
1327   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1328   std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1329
1330   // find all points referred by attributes listed in theArguments
1331   bool hasRefsToPoints = false;
1332   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1333   for (; anArgIt != theArguments.end(); ++anArgIt) {
1334     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1335     if (!aRefAttr)
1336       continue;
1337
1338     if (!aRefAttr->isObject()) {
1339       AttributePoint2DPtr aPoint =
1340           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1341       if (aReferredCoincidentPoints.empty())
1342         aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1343       else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1344         return true; // non-coincident point has been found
1345       else
1346         hasRefsToPoints = true;
1347     }
1348   }
1349
1350   if (hasRefsToPoints)
1351     theError = "Attributes are referred to the same point";
1352   return !hasRefsToPoints;
1353 }
1354
1355 bool SketchPlugin_CirclePassedPointValidator::isValid(
1356     const AttributePtr& theAttribute,
1357     const std::list<std::string>&,
1358     Events_InfoMessage& theError) const
1359 {
1360   static const std::string aErrorMessage(
1361       "Passed point refers to the same feature as a center point");
1362
1363   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1364
1365   AttributeRefAttrPtr aCenterRef =
1366       anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1367   AttributeRefAttrPtr aPassedRef =
1368       anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1369
1370   if (!aPassedRef->isObject())
1371     return true;
1372
1373   FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1374   if (!aPassedFeature)
1375     return true;
1376
1377   if (aCenterRef->isObject()) {
1378     if (aCenterRef->object() == aPassedRef->object()) {
1379       theError = aErrorMessage;
1380       return false;
1381     }
1382   } else {
1383     AttributePoint2DPtr aCenterPoint =
1384         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1385     if (aCenterPoint) {
1386       std::set<FeaturePtr> aCoincidentFeatures =
1387           SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1388       // check one of coincident features is a feature referred by passed point
1389       std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1390       for(; anIt != aCoincidentFeatures.end(); ++anIt)
1391         if (*anIt == aPassedFeature) {
1392           theError = aErrorMessage;
1393           return false;
1394         }
1395     }
1396   }
1397   return true;
1398 }
1399
1400 bool SketchPlugin_ThirdPointValidator::isValid(
1401     const AttributePtr& theAttribute,
1402     const std::list<std::string>& theArguments,
1403     Events_InfoMessage& theError) const
1404 {
1405   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1406   return arePointsNotOnLine(anOwner, theError) &&
1407          arePointsNotSeparated(anOwner, theArguments, theError);
1408 }
1409
1410 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1411                                               const std::string& thePointAttrName,
1412                                               const std::string& theRefPointAttrName)
1413 {
1414   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1415       theMacroCircle->attribute(thePointAttrName));
1416   AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1417
1418   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1419   if (aRefAttr) {
1420     if (aRefAttr->isObject()) {
1421       // project a point onto selected feature
1422       std::shared_ptr<SketchPlugin_Feature> aFeature =
1423           std::dynamic_pointer_cast<SketchPlugin_Feature>(
1424           ModelAPI_Feature::feature(aRefAttr->object()));
1425       if (aFeature) {
1426         SketchPlugin_Sketch* aSketch = aFeature->sketch();
1427         std::shared_ptr<GeomAPI_Edge> anEdge =
1428             std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1429         if (anEdge) {
1430           std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1431           if (anEdge->isLine())
1432             aPoint3D = anEdge->line()->project(aPoint3D);
1433           else if (anEdge->isCircle())
1434             aPoint3D = anEdge->circle()->project(aPoint3D);
1435           if(aPoint3D)
1436             aPoint = aSketch->to2D(aPoint3D);
1437         }
1438       }
1439     } else {
1440       AttributePoint2DPtr anOtherPoint =
1441           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1442       if (anOtherPoint)
1443         aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1444     }
1445   }
1446
1447   return aPoint;
1448 }
1449
1450 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1451                                  std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1452 {
1453   if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1454     thePoints[0] = toPoint(theMacroFeature,
1455           SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1456           SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1457     thePoints[1] = toPoint(theMacroFeature,
1458           SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1459           SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1460     thePoints[2] = toPoint(theMacroFeature,
1461           SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1462           SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1463   } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1464     thePoints[0] = toPoint(theMacroFeature,
1465           SketchPlugin_MacroArc::START_POINT_2_ID(),
1466           SketchPlugin_MacroArc::START_POINT_REF_ID());
1467     thePoints[1] = toPoint(theMacroFeature,
1468           SketchPlugin_MacroArc::END_POINT_2_ID(),
1469           SketchPlugin_MacroArc::END_POINT_REF_ID());
1470     thePoints[2] = toPoint(theMacroFeature,
1471           SketchPlugin_MacroArc::PASSED_POINT_ID(),
1472           SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1473   }
1474 }
1475
1476 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1477                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1478                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1479 {
1480   static const double aTolerance = 1.e-7;
1481   if (thePoint1->distance(thePoint2) < aTolerance ||
1482       thePoint1->distance(thePoint3) < aTolerance)
1483     return true;
1484
1485   std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1486                                                             thePoint2->y() - thePoint1->y()));
1487   std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1488                                                             thePoint3->y() - thePoint1->y()));
1489   return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1490 }
1491
1492 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1493                          const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1494                          const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1495 {
1496   static const double aTolerance = 1.e-7;
1497   std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1498   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1499
1500   std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1501   // the first point is on the line
1502   if (aVec1->squareModulus() < aTolerance * aTolerance)
1503     return false;
1504   std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1505   std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1506   // the second point is on the line
1507   if (aVec2->squareModulus() < aTolerance * aTolerance)
1508     return false;
1509   std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1510
1511   return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1512 }
1513
1514 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1515                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint1,
1516                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint2)
1517 {
1518   static const double aTolerance = 1.e-7;
1519   std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1520   double aDistP1C = thePoint1->distance(aCenter);
1521   double aDistP2C = thePoint2->distance(aCenter);
1522   return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1523 }
1524
1525 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1526     const FeaturePtr& theMacroFeature,
1527     Events_InfoMessage& theError) const
1528 {
1529   static const std::string aErrorPointsOnLine(
1530       "Selected points are on the same line");
1531
1532   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1533   threePointsOfFeature(theMacroFeature, aPoints);
1534
1535   if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1536     theError = aErrorPointsOnLine;
1537     return false;
1538   }
1539   return true;
1540 }
1541
1542 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1543     const FeaturePtr& theMacroFeature,
1544     const std::list<std::string>& theArguments,
1545     Events_InfoMessage& theError) const
1546 {
1547   static const std::string aErrorPointsApart(
1548       "Selected entity is lying between first two points");
1549
1550   AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1551   FeaturePtr aRefByThird;
1552   if (aThirdPointRef->isObject())
1553     aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1554   if (!aRefByThird)
1555     return true;
1556
1557   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1558   threePointsOfFeature(theMacroFeature, aPoints);
1559
1560   std::shared_ptr<GeomAPI_Edge> aThirdShape =
1561       std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1562   if (!aThirdShape)
1563     return true;
1564
1565   SketchPlugin_Sketch* aSketch =
1566       std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1567   std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1568   std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1569
1570   bool isOk = true;
1571   if (aThirdShape->isLine())
1572     isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1573   else if (aThirdShape->isCircle() || aThirdShape->isArc())
1574     isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1575
1576   if (!isOk)
1577     theError = aErrorPointsApart;
1578   return isOk;
1579 }
1580
1581 bool SketchPlugin_ArcEndPointValidator::isValid(
1582     const AttributePtr& theAttribute,
1583     const std::list<std::string>& theArguments,
1584     Events_InfoMessage& theError) const
1585 {
1586   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1587   AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1588
1589   if(!anEndPointRef.get()) {
1590     return true;
1591   }
1592
1593   ObjectPtr anObject = anEndPointRef->object();
1594   AttributePtr anAttr = anEndPointRef->attr();
1595   if(!anObject.get() && !anAttr.get()) {
1596     return true;
1597   }
1598
1599   if(anEndPointRef->attr().get()) {
1600     return false;
1601   }
1602
1603   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1604   if(aResult.get()) {
1605     GeomShapePtr aShape = aResult->shape();
1606     if(aShape.get() && aShape->isVertex()) {
1607       return false;
1608     }
1609   }
1610
1611   aFeature = ModelAPI_Feature::feature(anObject);
1612   if(aFeature.get()) {
1613     if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1614       return false;
1615     }
1616   }
1617
1618   return true;
1619 }
1620
1621 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1622 {
1623   if(!theShape.get()) {
1624     return theShape;
1625   }
1626
1627   if(!theShape->isEdge()) {
1628     return theShape;
1629   }
1630
1631   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1632
1633   if(!anEdge.get()) {
1634     return theShape;
1635   }
1636
1637   if(anEdge->isLine()) {
1638     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1639     GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1640     return aShape;
1641   }
1642
1643   if(anEdge->isCircle() || anEdge->isArc()) {
1644     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1645     GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1646     return aShape;
1647   }
1648
1649   return theShape;
1650 }
1651
1652 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1653     const AttributePtr& theAttribute,
1654     const std::list<std::string>& theArguments,
1655     Events_InfoMessage& theError) const
1656 {
1657   std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1658       std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1659   AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1660
1661   if(!anEndPointRef.get()) {
1662     return true;
1663   }
1664
1665   GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1666
1667   if(!anArcShape.get() || anArcShape->isNull()) {
1668     return true;
1669   }
1670
1671   ObjectPtr anObject = anEndPointRef->object();
1672   AttributePtr anAttr = anEndPointRef->attr();
1673   if(!anObject.get() && !anAttr.get()) {
1674     return true;
1675   }
1676
1677   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1678   if(aResult.get()) {
1679     GeomShapePtr aShape = aResult->shape();
1680     if (!aShape->isEdge())
1681       return true;
1682     aShape = toInfiniteEdge(aShape);
1683     if(aShape.get() && !aShape->isNull()) {
1684       if(anArcShape->isIntersect(aShape)) {
1685         return true;
1686       }
1687     }
1688   }
1689
1690   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1691   if(aSelectedFeature.get()) {
1692     std::list<ResultPtr> aResults = aSelectedFeature->results();
1693     for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1694         anIt != aResults.cend();
1695         ++anIt)
1696     {
1697       GeomShapePtr aShape = (*anIt)->shape();
1698       if (!aShape->isEdge())
1699         return true;
1700       aShape = toInfiniteEdge(aShape);
1701       if(aShape.get() && !aShape->isNull()) {
1702         if(anArcShape->isIntersect(aShape)) {
1703           return true;
1704         }
1705       }
1706     }
1707   }
1708
1709   return false;
1710 }
1711
1712 bool SketchPlugin_HasNoConstraint::isValid(const AttributePtr& theAttribute,
1713                                            const std::list<std::string>& theArguments,
1714                                            Events_InfoMessage& theError) const
1715 {
1716   std::set<std::string> aFeatureKinds;
1717   for (std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1718        anArgIt != theArguments.end(); anArgIt++) {
1719     aFeatureKinds.insert(*anArgIt);
1720   }
1721
1722   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1723     theError = "The attribute with the %1 type is not processed";
1724     theError.arg(theAttribute->attributeType());
1725     return false;
1726   }
1727
1728   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
1729                                                                       (theAttribute);
1730   bool isObject = aRefAttr->isObject();
1731   if (!isObject) {
1732     theError = "It uses an empty object";
1733     return false;
1734   }
1735   ObjectPtr anObject = aRefAttr->object();
1736   FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1737   if (!aFeature.get()) {
1738     theError = "The feature of the checked attribute is empty";
1739     return false;
1740   }
1741
1742   FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aRefAttr->owner());
1743
1744   std::set<AttributePtr> aRefsList = anObject->data()->refsToMe();
1745   std::set<AttributePtr>::const_iterator anIt = aRefsList.begin();
1746   for (; anIt != aRefsList.end(); anIt++) {
1747     FeaturePtr aRefFeature = ModelAPI_Feature::feature((*anIt)->owner());
1748     if (aRefFeature.get() && aCurrentFeature != aRefFeature &&
1749         aFeatureKinds.find(aRefFeature->getKind()) != aFeatureKinds.end())
1750       return false; // constraint is found, that means that the check is not valid
1751   }
1752   return true;
1753 }
1754
1755 bool SketchPlugin_ReplicationReferenceValidator::isValid(
1756     const AttributePtr& theAttribute,
1757     const std::list<std::string>& theArguments,
1758     Events_InfoMessage& theError) const
1759 {
1760   AttributeRefAttrPtr aRefAttr =
1761       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1762   if (!aRefAttr)
1763   {
1764     theError = "Incorrect attribute";
1765     return false;
1766   }
1767
1768   ObjectPtr anOwner;
1769   if (aRefAttr->isObject())
1770     anOwner = aRefAttr->object();
1771   else
1772   {
1773     AttributePtr anAttr = aRefAttr->attr();
1774     anOwner = anAttr->owner();
1775   }
1776   FeaturePtr anAttrOwnerFeature = ModelAPI_Feature::feature(anOwner);
1777   if (!anAttrOwnerFeature)
1778     return true;
1779   AttributeBooleanPtr aCopyAttr = anAttrOwnerFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
1780   if (!aCopyAttr || !aCopyAttr->value())
1781     return true; // feature is not a copy, thus valid
1782
1783   // check the copy feature is already referred by the "Multi" feature
1784   FeaturePtr aMultiFeature = ModelAPI_Feature::feature(theAttribute->owner());
1785   AttributeRefListPtr aRefList = aMultiFeature->reflist(theArguments.front());
1786   for (int i = 0; i < aRefList->size(); ++i)
1787   {
1788     FeaturePtr aRefOwner = ModelAPI_Feature::feature(aRefList->object(i));
1789     if (aRefOwner == anAttrOwnerFeature)
1790     {
1791       theError = "Attribute refers to the object generated by this feature";
1792       return false;
1793     }
1794   }
1795
1796   return true;
1797 }
1798
1799 bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
1800                                                   const std::list<std::string>& theArguments,
1801                                                   Events_InfoMessage& theError) const
1802 {
1803   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1804     theError = "The attribute with the %1 type is not processed";
1805     theError.arg(theAttribute->attributeType());
1806     return false;
1807   }
1808
1809   // check the attribute refers to a sketch feature
1810   AttributeRefAttrPtr aRefAttr =
1811       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1812   bool isSketchFeature = aRefAttr->isObject();
1813   if (isSketchFeature) {
1814     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1815     isSketchFeature = aFeature.get() != NULL;
1816     if (isSketchFeature) {
1817       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1818           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1819       isSketchFeature = aSketchFeature.get() != NULL;
1820     }
1821   }
1822
1823   if (isSketchFeature)
1824     return true;
1825
1826   theError = "The object selected is not a sketch feature";
1827   return false;
1828 }
1829
1830 bool SketchPlugin_MultiRotationAngleValidator::isValid(const AttributePtr& theAttribute,
1831                                                        const std::list<std::string>& theArguments,
1832                                                        Events_InfoMessage& theError) const
1833 {
1834   if (theAttribute->attributeType() != ModelAPI_AttributeDouble::typeId()) {
1835     theError = "The attribute with the %1 type is not processed";
1836     theError.arg(theAttribute->attributeType());
1837     return false;
1838   }
1839
1840   AttributeDoublePtr anAngleAttr =
1841     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1842
1843   FeaturePtr aMultiRotation = ModelAPI_Feature::feature(theAttribute->owner());
1844   AttributeStringPtr anAngleType =
1845       aMultiRotation->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
1846   AttributeIntegerPtr aNbCopies =
1847       aMultiRotation->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID());
1848
1849   if (anAngleType->value() != "FullAngle")
1850   {
1851     double aFullAngleValue = anAngleAttr->value() * (aNbCopies->value() - 1);
1852     if (aFullAngleValue < -1.e-7 || aFullAngleValue > 359.9999999)
1853     {
1854       theError = "Rotation single angle should produce full angle less than 360 degree";
1855       return false;
1856     }
1857   }
1858   else
1859   {
1860     double aFullAngleValue = anAngleAttr->value();
1861     if (aFullAngleValue < -1.e-7 || aFullAngleValue > 360.0000001)
1862     {
1863       theError = "Rotation full angle should be in range [0, 360]";
1864       return false;
1865     }
1866   }
1867
1868   return true;
1869 }
1870
1871 bool SketchPlugin_BSplineValidator::isValid(const AttributePtr& theAttribute,
1872                                             const std::list<std::string>& theArguments,
1873                                             Events_InfoMessage& theError) const
1874 {
1875   AttributePoint2DArrayPtr aPolesArray =
1876       std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(theAttribute);
1877   if (!aPolesArray)
1878     return false;
1879
1880   if (aPolesArray->size() < 2) {
1881     theError = "Number of B-spline poles should be 2 or more";
1882     return false;
1883   }
1884
1885   return true;
1886 }