]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Validators.cpp
Salome HOME
Task #3230: Sketcher: create a curve passing through selected points or vertices...
[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_CurveFitting.h"
35 #include "SketchPlugin_Line.h"
36 #include "SketchPlugin_MacroArc.h"
37 #include "SketchPlugin_MacroCircle.h"
38 #include "SketchPlugin_MultiRotation.h"
39 #include "SketchPlugin_Point.h"
40 #include "SketchPlugin_Sketch.h"
41 #include "SketchPlugin_Trim.h"
42 #include "SketchPlugin_Tools.h"
43
44 #include "SketcherPrs_Tools.h"
45
46 #include <Events_InfoMessage.h>
47
48 #include <ModelAPI_Data.h>
49 #include <ModelAPI_Validator.h>
50 #include <ModelAPI_AttributeDouble.h>
51 #include <ModelAPI_AttributeInteger.h>
52 #include <ModelAPI_AttributeRefAttr.h>
53 #include <ModelAPI_AttributeRefAttrList.h>
54 #include <ModelAPI_AttributeRefList.h>
55 #include <ModelAPI_AttributeSelectionList.h>
56 #include <ModelAPI_AttributeString.h>
57 #include <ModelAPI_Session.h>
58 #include <ModelAPI_Tools.h>
59 #include <ModelAPI_ResultConstruction.h>
60
61 #include <ModelGeomAlgo_Point2D.h>
62 #include <ModelGeomAlgo_Shape.h>
63
64 #include <GeomAlgoAPI_EdgeBuilder.h>
65 #include <GeomAlgoAPI_ShapeTools.h>
66
67 #include <GeomAPI_Circ.h>
68 #include <GeomAPI_Dir2d.h>
69 #include <GeomAPI_Ellipse.h>
70 #include <GeomAPI_Lin.h>
71 #include <GeomAPI_Edge.h>
72 #include <GeomAPI_Vertex.h>
73
74 #include <GeomDataAPI_Point2D.h>
75 #include <GeomDataAPI_Point2DArray.h>
76
77 #include <algorithm>
78 #include <cmath>
79
80 #ifdef _MSC_VER
81 #pragma warning(disable: 4100)
82 #endif
83
84 const double tolerance = 1.e-7;
85
86 static bool isSpline(FeaturePtr theFeature)
87 {
88   return theFeature && (theFeature->getKind() == SketchPlugin_BSpline::ID() ||
89                         theFeature->getKind() == SketchPlugin_BSplinePeriodic::ID());
90 }
91
92
93 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
94                                                  const std::list<std::string>& theArguments,
95                                                  Events_InfoMessage& theError) const
96 {
97   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
98     theError = "The attribute with the %1 type is not processed";
99     theError.arg(theAttribute->attributeType());
100     return false;
101   }
102
103   // there is a check whether the feature contains a point and a linear edge or two point values
104   std::string aParamA = theArguments.front();
105   SessionPtr aMgr = ModelAPI_Session::get();
106   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
107
108   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
109                                                                       (theAttribute);
110   bool isObject = aRefAttr->isObject();
111   if (!isObject) {
112     // an attribute is a point. A point value is valid always for the distance
113     return true;
114   } else {
115     // 1. check whether the references object is a linear
116     ObjectPtr anObject = aRefAttr->object();
117
118     const ModelAPI_AttributeValidator* aShapeValidator =
119       dynamic_cast<const ModelAPI_AttributeValidator*>(
120       aFactory->validator("GeomValidators_ShapeType"));
121     std::list<std::string> anArguments;
122     anArguments.push_back("circle");
123     Events_InfoMessage aCircleError;
124     bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
125     // the circle line is not a valid case
126     if (aShapeValid) {
127       theError = "Circle can not be used in distance constraint";
128       return false;
129     }
130
131     anArguments.clear();
132     anArguments.push_back("line");
133     Events_InfoMessage aLineError;
134     aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
135     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
136     if (aShapeValid) {
137       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
138       // If it is a line then we have to check that first attribute id not a line
139       std::shared_ptr<SketchPlugin_Feature> aSFeature =
140         std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
141       SketchPlugin_Sketch* aSketch = aSFeature->sketch();
142       std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
143       std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
144         aFeature->data(), aParamA, aPlane);
145       if (!aPoint.get()) {
146         theError = "One of parameters of distance constraint should be a point";
147         return false;
148       }
149     }
150   }
151   return true;
152 }
153
154 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
155                                                 const std::list<std::string>& theArguments,
156                                                 Events_InfoMessage& theError) const
157 {
158   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
159     theError = "The attribute with the %1 type is not processed";
160     theError.arg(theAttribute->attributeType());
161     return false;
162   }
163
164   // there is a check whether the feature contains a point and a linear edge or two point values
165   std::string aParamA = theArguments.front();
166
167   FeaturePtr anAttributeFeature =
168     std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
169   AttributeRefAttrPtr aRefAttr =
170     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
171
172   bool isObject = aRefAttr->isObject();
173   ObjectPtr anObject = aRefAttr->object();
174   if (!isObject || !anObject.get()) {
175     theError = "It uses an empty object";
176     return false;
177   }
178
179   FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
180
181   AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
182   ObjectPtr aOtherObject = aOtherAttr->object();
183   FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
184   if (!aOtherFea)
185     return true;
186
187   if (aRefFea->getKind() == SketchPlugin_Line::ID() &&
188       aOtherFea->getKind() == SketchPlugin_Line::ID()) {
189     theError = "Two segments cannot be tangent";
190     return false;
191   }
192   else if (isSpline(aRefFea) && isSpline(aOtherFea)) {
193     theError = "Two B-splines cannot be tangent";
194     return false;
195   }
196
197   bool isValid = true;
198   bool hasSpline = isSpline(aRefFea);
199   if (!hasSpline && isSpline(aOtherFea)) {
200     hasSpline = true;
201     std::swap(aRefFea, aOtherFea);
202   }
203   if (hasSpline) {
204     auto isApplicableCoincidence = [](FeaturePtr theFeature, const std::string& theAttrName) {
205       AttributeRefAttrPtr aRefAttr = theFeature->refattr(theAttrName);
206       if (aRefAttr->isObject())
207         return false;
208       AttributePtr anAttr = aRefAttr->attr();
209       FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
210       AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
211       if (aPointAttr) {
212         return anOwner->getKind() == SketchPlugin_BSpline::ID() &&
213               (aPointAttr->id() == SketchPlugin_BSpline::START_ID() ||
214                aPointAttr->id() == SketchPlugin_BSpline::END_ID());
215       }
216
217       AttributePoint2DArrayPtr aPntArray =
218           std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(anAttr);
219       if (aPntArray && anOwner->getKind() == SketchPlugin_BSpline::ID()) {
220         // check index of the pole
221         AttributeIntegerPtr anIndex = theAttrName == SketchPlugin_Constraint::ENTITY_A() ?
222             theFeature->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_A()) :
223             theFeature->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B());
224         return anIndex && (anIndex->value() == 0 || anIndex->value() == aPntArray->size() - 1);
225       }
226       return false;
227     };
228
229     isValid = false;
230     AttributePoint2DArrayPtr aBSplinePoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
231         aRefFea->attribute(SketchPlugin_BSplineBase::POLES_ID()));
232     // additional check the B-spline edge and the other edge have a coincident boundary point
233     std::set<FeaturePtr> aCoincidences = SketchPlugin_Tools::findCoincidentConstraints(aRefFea);
234     for (std::set<FeaturePtr>::iterator anIt = aCoincidences.begin();
235          anIt != aCoincidences.end() && !isValid; ++anIt) {
236       std::set<FeaturePtr> aCoinc;
237       if (isApplicableCoincidence(*anIt, SketchPlugin_Constraint::ENTITY_A()))
238         SketchPlugin_Tools::findCoincidences(*anIt, SketchPlugin_Constraint::ENTITY_B(),
239                                              aCoinc, true);
240       else if (isApplicableCoincidence(*anIt, SketchPlugin_Constraint::ENTITY_B()))
241         SketchPlugin_Tools::findCoincidences(*anIt, SketchPlugin_Constraint::ENTITY_A(),
242                                              aCoinc, true);
243
244       isValid = aCoinc.find(aOtherFea) != aCoinc.end();
245     }
246   }
247
248   return isValid;
249 }
250
251 bool SketchPlugin_PerpendicularAttrValidator::isValid(const AttributePtr& theAttribute,
252                                                       const std::list<std::string>& theArguments,
253                                                       Events_InfoMessage& theError) const
254 {
255   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
256     theError = "The attribute with the %1 type is not processed";
257     theError.arg(theAttribute->attributeType());
258     return false;
259   }
260
261   std::string aParamA = theArguments.front();
262
263   FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
264   AttributeRefAttrPtr aRefAttr =
265       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
266
267   bool isObject = aRefAttr->isObject();
268   ObjectPtr anObject = aRefAttr->object();
269   if (isObject && anObject.get()) {
270     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
271
272     AttributeRefAttrPtr aOtherAttr = anOwner->refattr(aParamA);
273     ObjectPtr aOtherObject = aOtherAttr->object();
274     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
275
276     // at least one feature should be a line
277     if (aRefFea->getKind() != SketchPlugin_Line::ID() &&
278         aOtherFea && aOtherFea->getKind() != SketchPlugin_Line::ID()) {
279       theError = "At least one feature should be a line";
280       return false;
281     }
282     else if (isSpline(aRefFea) || isSpline(aOtherFea)) {
283       theError = "B-spline is not supported";
284       return false;
285     }
286   }
287   else {
288     theError = "It uses an empty object";
289     return false;
290   }
291
292   return true;
293 }
294
295 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
296                                              const std::list<std::string>& theArguments,
297                                              Events_InfoMessage& theError) const
298 {
299   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
300     theError = "The attribute with the %1 type is not processed";
301     theError.arg(theAttribute->attributeType());
302     return false;
303   }
304
305   std::shared_ptr<SketchPlugin_Feature> aFeature =
306       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
307   if (!aFeature)
308     return true;
309
310   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
311
312   SketchPlugin_Sketch* aSketch = aFeature->sketch();
313   int aNbFeatures = aSketch->numberOfSubs();
314   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
315     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
316     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
317       continue;
318     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
319         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
320     if (aRefAttr->isObject()) {
321       if (aRefAttr->object() == aRAttr->object()) {
322         ObjectPtr anObject = aRefAttr->object();
323         std::string aName = anObject.get() ? anObject->data()->name() : "";
324         theError = "The object %1 has been already fixed.";
325         theError.arg(aName);
326         return false;
327       }
328     }
329     else if (aRefAttr->attr() == aRAttr->attr()) {
330       AttributePtr anAttribute = aRefAttr->attr();
331       std::string aName = anAttribute.get() ? anAttribute->id() : "";
332       theError = "The attribute %1 has been already fixed.";
333       theError.arg(aName);
334       return false;
335     }
336   }
337   return true;
338 }
339
340 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
341                                               const std::list<std::string>& theArguments,
342                                               Events_InfoMessage& theError) const
343 {
344   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
345     theError = "The attribute with the %1 type is not processed";
346     theError.arg(theAttribute->attributeType());
347     return false;
348   }
349
350   std::string aParamA = theArguments.front();
351   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
352   AttributeRefAttrPtr aRefAttr[2];
353   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
354   aRefAttr[1] = aFeature->data()->refattr(aParamA);
355
356   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
357     theError = "Attributes can not be used in equal constraint";
358     return false;
359   }
360
361   std::string aType[2];
362   std::list<std::string> anArguments;
363   for (int i = 0; i < 2; i++) {
364     aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
365     if (!aFeature.get())
366       return true;
367
368     aType[i] = aFeature->getKind();
369     if (aFeature->getKind() != SketchPlugin_Line::ID() &&
370         aFeature->getKind() != SketchPlugin_Circle::ID() &&
371         aFeature->getKind() != SketchPlugin_Arc::ID() &&
372         aFeature->getKind() != SketchPlugin_Ellipse::ID() &&
373         aFeature->getKind() != SketchPlugin_EllipticArc::ID()) {
374       theError = "The %1 feature is not supported by the Equal constraint.";
375       theError.arg(aFeature->getKind());
376       // wrong type of attribute
377       return false;
378     }
379   }
380
381   bool isOk = aType[0] == aType[1];
382   if (!isOk) {
383     // circle and arc may be equal
384     isOk = (aType[0] == SketchPlugin_Arc::ID() && aType[1] == SketchPlugin_Circle::ID())
385         || (aType[0] == SketchPlugin_Circle::ID() && aType[1] == SketchPlugin_Arc::ID());
386   }
387   if (!isOk) {
388     // ellipse and elliptic arc may be equal
389     isOk = (aType[0] == SketchPlugin_EllipticArc::ID() && aType[1] == SketchPlugin_Ellipse::ID())
390         || (aType[0] == SketchPlugin_Ellipse::ID() && aType[1] == SketchPlugin_EllipticArc::ID());
391   }
392   if (!isOk) {
393     theError = "Features with kinds %1 and %2 can not be equal.";
394     theError.arg(aType[0]).arg(aType[1]);
395     return false;
396   }
397   return true;
398 }
399
400 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
401                                                const std::list<std::string>& theArguments,
402                                                Events_InfoMessage& theError) const
403 {
404   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
405     theError = "The attribute with the %1 type is not processed";
406     theError.arg(theAttribute->attributeType());
407     return false;
408   }
409
410   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
411   AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
412
413   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
414       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
415   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
416
417   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
418     ObjectPtr aSelObject = aSelAttr->object(anInd);
419
420     // B-splines are not supported in Mirror yet
421     FeaturePtr aSelFeature = ModelAPI_Feature::feature(aSelObject);
422     if (aSelFeature && (aSelFeature->getKind() == SketchPlugin_BSpline::ID() ||
423         aSelFeature->getKind() == SketchPlugin_BSplinePeriodic::ID())) {
424       theError = "Not supported";
425       return false;
426     }
427
428     std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
429     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
430     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
431       if (aSelObject == *aMirIter) {
432         theError = "The object %1 is a result of mirror";
433         theError.arg(aName);
434         return false;
435       }
436   }
437   return true;
438 }
439
440 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
441                                                     const std::list<std::string>& theArguments,
442                                                     Events_InfoMessage& theError) const
443 {
444   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
445     theError = "The attribute with the %1 type is not processed";
446     theError.arg(theAttribute->attributeType());
447     return false;
448   }
449
450   // there is a check whether the feature contains a point and a linear edge or two point values
451   std::string aParamA = theArguments.front();
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
827   FeaturePtr anAttributeFeature =
828     std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
829   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
830   AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
831
832   AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
833   int aNbPoints = 0;
834   int aNbLines = 0;
835   for (int i = 0; i < 2; ++i) {
836     if (!aRefAttrs[i]->isObject())
837       ++aNbPoints;
838     else {
839       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
840       if (!aFeature) {
841         if (aNbPoints + aNbLines != 0)
842           return true;
843         else continue;
844       }
845
846       if (aFeature->getKind() == SketchPlugin_Point::ID())
847         ++aNbPoints;
848       else if (aFeature->getKind() == SketchPlugin_Line::ID() ||
849                aFeature->getKind() == SketchPlugin_Arc::ID() ||
850                aFeature->getKind() == SketchPlugin_EllipticArc::ID())
851         ++aNbLines;
852     }
853   }
854
855   if (aNbPoints != 1 || aNbLines != 1) {
856     theError = "Middle point constraint allows points and lines only";
857     return false;
858   }
859   return true;
860 }
861
862 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
863                                                     const std::list<std::string>& /*theArguments*/,
864                                                     Events_InfoMessage& theError) const
865 {
866   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
867     theError = "The attribute with the %1 type is not processed";
868     theError.arg(theAttribute->attributeType());
869     return false;
870   }
871   FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
872   AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
873   if (anArcTypeAttr && anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE())
874     return true; // not applicable for non-tangent arcs
875
876   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
877   AttributePtr anAttr = aRefAttr->attr();
878   if (!anAttr) {
879     theError = "The attribute %1 should be a point";
880     theError.arg(theAttribute->id());
881     return false;
882   }
883
884   FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
885   const std::string& aFeatureType = anAttrFeature->getKind();
886   if (aFeatureType == SketchPlugin_Arc::ID()) {
887     // selected point should not be a center of arc
888     const std::string& aPntId = anAttr->id();
889     if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
890       theError = "The attribute %1 is not supported";
891       theError.arg(aPntId);
892       return false;
893     }
894   }
895   else if (aFeatureType == SketchPlugin_Line::ID()) {
896     // selected point should be bound point of line
897     const std::string& aPntId = anAttr->id();
898     if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
899       theError = "The attribute %1 is not supported";
900       theError.arg(aPntId);
901       return false;
902     }
903   }
904   else {
905     theError = "Unable to build tangent arc on %1";
906     theError.arg(anAttrFeature->getKind());
907     return false;
908   }
909
910   return true;
911 }
912
913 bool SketchPlugin_ArcTransversalPointValidator::isValid(
914     const AttributePtr& theAttribute,
915     const std::list<std::string>& /*theArguments*/,
916     Events_InfoMessage& theError) const
917 {
918   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
919     theError = "The attribute with the %1 type is not processed";
920     theError.arg(theAttribute->attributeType());
921     return false;
922   }
923   FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
924   AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
925   if (anArcTypeAttr &&
926       anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TRANSVERSAL_LINE())
927     return true; // not applicable for non-transversal arcs
928
929   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
930   AttributePtr anAttr = aRefAttr->attr();
931   if (!anAttr) {
932     theError = "The attribute %1 should be a point";
933     theError.arg(theAttribute->id());
934     return false;
935   }
936
937   FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
938   const std::string& aFeatureType = anAttrFeature->getKind();
939   if (aFeatureType == SketchPlugin_Line::ID()) {
940     // selected point should be bound point of line
941     const std::string& aPntId = anAttr->id();
942     if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
943       theError = "The attribute %1 is not supported";
944       theError.arg(aPntId);
945       return false;
946     }
947   }
948   else {
949     theError = "Unable to build perpendicular arc on %1";
950     theError.arg(anAttrFeature->getKind());
951     return false;
952   }
953
954   return true;
955 }
956
957 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
958                                                  const std::list<std::string>& theArguments,
959                                                  Events_InfoMessage& theError) const
960 {
961   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
962     theError = "The attribute with the %1 type is not processed";
963     theError.arg(theAttribute->attributeType());
964     return false;
965   }
966   AttributeSelectionPtr anExternalAttr =
967       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
968   std::shared_ptr<GeomAPI_Edge> anEdge;
969   if (anExternalAttr && anExternalAttr->value() && anExternalAttr->value()->isEdge()) {
970     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->value()));
971   } else if(anExternalAttr->context() && anExternalAttr->context()->shape() &&
972             anExternalAttr->context()->shape()->isEdge()) {
973     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->context()->shape()));
974   }
975
976   if (!anEdge) {
977     theError = "The attribute %1 should be an edge";
978     theError.arg(theAttribute->id());
979     return false;
980   }
981
982   // find a sketch
983   std::shared_ptr<SketchPlugin_Sketch> aSketch;
984   std::set<AttributePtr> aRefs = anExternalAttr->owner()->data()->refsToMe();
985   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
986   for (; anIt != aRefs.end(); ++anIt) {
987     CompositeFeaturePtr aComp =
988         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
989     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
990       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
991       break;
992     }
993   }
994   if (!aSketch) {
995     theError = "There is no sketch referring to the current feature";
996     return false;
997   }
998
999   // check the edge is intersected with sketch plane
1000   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1001
1002   std::list<GeomPointPtr> anIntersectionsPoints;
1003   anEdge->intersectWithPlane(aPlane, anIntersectionsPoints);
1004   if (anIntersectionsPoints.empty()) {
1005     theError = "The edge is not intersected with sketch plane";
1006     return false;
1007   }
1008   return true;
1009 }
1010
1011 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
1012                                           const std::list<std::string>& theArguments,
1013                                           Events_InfoMessage& theError) const
1014 {
1015   bool aValid = false;
1016
1017   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1018     theError = "The attribute with the %1 type is not processed";
1019     theError.arg(theAttribute->attributeType());
1020     return aValid;
1021   }
1022   AttributeReferencePtr aFeatureAttr =
1023             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1024   std::shared_ptr<SketchPlugin_Feature> aSplitFeature =
1025     std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
1026
1027   ObjectPtr anAttrObject = aFeatureAttr->value();
1028   if (!anAttrObject) {
1029     AttributePtr aPreviewAttr = aSplitFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
1030     aFeatureAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
1031     anAttrObject = aFeatureAttr->value();
1032   }
1033
1034   FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
1035   if (!anAttrFeature)
1036     return aValid;
1037
1038   // B-splines are not supported by the Split yet
1039   if (anAttrFeature->getKind() == SketchPlugin_BSpline::ID() ||
1040       anAttrFeature->getKind() == SketchPlugin_BSplinePeriodic::ID()) {
1041     theError = "Not supported";
1042     return false;
1043   }
1044
1045   std::set<ResultPtr> anEdgeShapes;
1046   ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1047   if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
1048     return aValid;
1049
1050   // coincidences to the feature
1051   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
1052   ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
1053                       SketchPlugin_ConstraintCoincidence::ID(),
1054                       aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1055
1056   GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
1057   std::shared_ptr<SketchPlugin_Feature> aSFeature =
1058                                 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
1059   if (!aSFeature || aSFeature->isCopy())
1060     return false;
1061   SketchPlugin_Sketch* aSketch = aSFeature->sketch();
1062
1063   std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
1064   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1065       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1066   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1067       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1068   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1069       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1070   std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1071
1072   typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
1073                     std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
1074                               std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
1075   PointToRefsMap aPointsInfo;
1076
1077   ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
1078                                               aX->dir(), aDirY, aPointsInfo);
1079   int aCoincidentToFeature = (int)aPointsInfo.size();
1080   if (anAttrFeature->getKind() == SketchPlugin_Circle::ID() ||
1081       anAttrFeature->getKind() == SketchPlugin_Ellipse::ID())
1082     aValid = aCoincidentToFeature >= 2;
1083   else
1084     aValid = aCoincidentToFeature >= 1;
1085
1086   return aValid;
1087 }
1088
1089 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
1090                                          const std::list<std::string>& theArguments,
1091                                          Events_InfoMessage& theError) const
1092 {
1093   bool aValid = false;
1094
1095   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1096     theError = "The attribute with the %1 type is not processed";
1097     theError.arg(theAttribute->attributeType());
1098     return aValid;
1099   }
1100   AttributeReferencePtr aBaseObjectAttr =
1101             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1102
1103   std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
1104                  std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
1105
1106   ObjectPtr aBaseObject = aBaseObjectAttr->value();
1107   if (!aBaseObject) {
1108     AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
1109     aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
1110     aBaseObject = aBaseObjectAttr->value();
1111   }
1112
1113   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1114   if (!aBaseFeature)
1115     return aValid;
1116
1117   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1118                                  std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
1119   if (!aSketchFeature.get() || aSketchFeature->isCopy())
1120     return aValid;
1121
1122   // B-splines are not supported by the Trim yet
1123   if (aBaseFeature->getKind() == SketchPlugin_BSpline::ID() ||
1124       aBaseFeature->getKind() == SketchPlugin_BSplinePeriodic::ID()) {
1125     theError = "Not supported";
1126     return false;
1127   }
1128
1129   // point on feature
1130   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1131                        aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
1132
1133   SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
1134
1135   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
1136   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
1137                                                               anAttributePnt2d->y());
1138
1139   std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
1140   std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
1141            std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
1142                      std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
1143   SketchPlugin_SegmentationTools::fillObjectShapes(
1144       aTrimFeature.get(), aBaseObject, aCashedShapes, anObjectToPoints);
1145   const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
1146
1147   return aShapes.size() > 1;
1148 }
1149
1150 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
1151                                                const std::list<std::string>& theArguments,
1152                                                Events_InfoMessage& theError) const
1153 {
1154   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
1155     theError = "The attribute with the %1 type is not processed";
1156     theError.arg(theAttribute->attributeType());
1157     return false;
1158   }
1159
1160   AttributeSelectionPtr aFeatureAttr =
1161       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
1162   std::shared_ptr<GeomAPI_Vertex> aVertex;
1163   std::shared_ptr<GeomAPI_Edge> anEdge;
1164   std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
1165   if (aFeatureAttr.get()) {
1166     GeomShapePtr aVal = aFeatureAttr->value();
1167     ResultPtr aRes = aFeatureAttr->context();
1168     if (aVal && aVal->isVertex())
1169       aVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aVal));
1170     else if (aVal && aVal->isEdge()) {
1171       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aVal));
1172     } else if(aRes && aRes->shape()) {
1173       if (aRes->shape()->isVertex())
1174         aVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aRes->shape()));
1175       else if (aRes->shape()->isEdge())
1176         anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aRes->shape()));
1177     }
1178
1179     // try to convert result to sketch feature
1180     if (aRes) {
1181       aSketchFeature =
1182         std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
1183     }
1184   }
1185   if (!aVertex && !anEdge) {
1186     theError = "The attribute %1 should be an edge or vertex";
1187     theError.arg(theAttribute->id());
1188     return false;
1189   }
1190
1191   // find a sketch
1192   std::shared_ptr<SketchPlugin_Sketch> aSketch;
1193   std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
1194   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
1195   for (; anIt != aRefs.end(); ++anIt) {
1196     CompositeFeaturePtr aComp =
1197         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1198     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1199       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1200       break;
1201     }
1202   }
1203   if (!aSketch) {
1204     theError = "There is no sketch referring to the current feature";
1205     return false;
1206   }
1207   if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
1208     theError = "Unable to project feature from the same sketch";
1209     return false;
1210   }
1211
1212   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1213   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1214   std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1215
1216   bool aValid = true;
1217   if (aVertex)
1218     aValid = true; // vertex is always could be projected
1219   else if (anEdge->isLine()) {
1220     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1221     std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1222     double aDot = fabs(aNormal->dot(aLineDir));
1223     aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
1224     if (!aValid)
1225       theError = "Error: Line is orthogonal to the sketch plane.";
1226   }
1227   else if (anEdge->isCircle() || anEdge->isArc()) {
1228     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1229     std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1230     double aDot = fabs(aNormal->dot(aCircNormal));
1231     aValid = aDot >= tolerance * tolerance;
1232     if (!aValid)
1233       theError.arg(anEdge->isCircle() ? "Error: Circle is orthogonal to the sketch plane."
1234                                       : "Error: Arc is orthogonal to the sketch plane.");
1235   }
1236   else if (anEdge->isEllipse()) {
1237     std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
1238     std::shared_ptr<GeomAPI_Dir> anEllipseNormal = anEllipse->normal();
1239     double aDot = fabs(aNormal->dot(anEllipseNormal));
1240     aValid = fabs(aDot - 1.0) <= tolerance * tolerance;
1241     if (!aValid)
1242       theError.arg(anEdge->isClosed() ? "Error: Ellipse is orthogonal to the sketch plane."
1243                                       : "Error: Elliptic Arc is orthogonal to the sketch plane.");
1244   }
1245
1246   return aValid;
1247 }
1248
1249
1250 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1251                                    const std::set<FeaturePtr>& theSet2)
1252 {
1253   std::set<FeaturePtr> aCommon;
1254   if (theSet1.empty() || theSet2.empty())
1255     return aCommon;
1256
1257   std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1258   for (; anIt2 != theSet2.end(); ++anIt2)
1259     if (theSet1.find(*anIt2) != theSet1.end())
1260       aCommon.insert(*anIt2);
1261   return aCommon;
1262 }
1263
1264 bool SketchPlugin_DifferentReferenceValidator::isValid(
1265     const AttributePtr& theAttribute,
1266     const std::list<std::string>& theArguments,
1267     Events_InfoMessage& theError) const
1268 {
1269   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1270
1271   int aNbFeaturesReferred = 0;
1272   int aNbAttributesReferred = 0;
1273   std::set<FeaturePtr> aCommonReferredFeatures;
1274
1275   // find all features referred by attributes listed in theArguments
1276   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1277   for (; anArgIt != theArguments.end(); ++anArgIt) {
1278     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1279     if (!aRefAttr)
1280       continue;
1281
1282     std::set<FeaturePtr> aCoincidentFeatures;
1283     if (aRefAttr->isObject()) {
1284       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1285       if (aFeature) {
1286         aCoincidentFeatures.insert(aFeature);
1287         aNbFeaturesReferred += 1;
1288       }
1289     } else {
1290       AttributePoint2DPtr aPoint =
1291           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1292       if (aPoint) {
1293         aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1294         aNbAttributesReferred += 1;
1295       }
1296     }
1297
1298     if (aCommonReferredFeatures.empty())
1299       aCommonReferredFeatures = aCoincidentFeatures;
1300     else
1301       aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1302
1303     if (aCommonReferredFeatures.empty())
1304       return true;
1305   }
1306
1307   bool isOk = aNbFeaturesReferred < 1;
1308   if (aNbFeaturesReferred == 1) {
1309     if (aCommonReferredFeatures.size() == 1) {
1310       FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1311       isOk = aNbAttributesReferred <= 1 ||
1312              aFeature->getKind() == SketchPlugin_Circle::ID() ||
1313              aFeature->getKind() == SketchPlugin_Arc::ID();
1314     } else
1315       isOk = false;
1316   }
1317
1318   if (!isOk)
1319     theError = "Attributes are referred to the same feature";
1320   return isOk;
1321 }
1322
1323 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1324     const AttributePtr& theAttribute,
1325     const std::list<std::string>& theArguments,
1326     Events_InfoMessage& theError) const
1327 {
1328   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1329   std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1330
1331   // find all points referred by attributes listed in theArguments
1332   bool hasRefsToPoints = false;
1333   std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1334   for (; anArgIt != theArguments.end(); ++anArgIt) {
1335     AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1336     if (!aRefAttr)
1337       continue;
1338
1339     if (!aRefAttr->isObject()) {
1340       AttributePoint2DPtr aPoint =
1341           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1342       if (aReferredCoincidentPoints.empty())
1343         aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1344       else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1345         return true; // non-coincident point has been found
1346       else
1347         hasRefsToPoints = true;
1348     }
1349   }
1350
1351   if (hasRefsToPoints)
1352     theError = "Attributes are referred to the same point";
1353   return !hasRefsToPoints;
1354 }
1355
1356 bool SketchPlugin_CirclePassedPointValidator::isValid(
1357     const AttributePtr& theAttribute,
1358     const std::list<std::string>&,
1359     Events_InfoMessage& theError) const
1360 {
1361   static const std::string aErrorMessage(
1362       "Passed point refers to the same feature as a center point");
1363
1364   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1365
1366   AttributeRefAttrPtr aCenterRef =
1367       anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1368   AttributeRefAttrPtr aPassedRef =
1369       anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1370
1371   if (!aPassedRef->isObject())
1372     return true;
1373
1374   FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1375   if (!aPassedFeature)
1376     return true;
1377
1378   if (aCenterRef->isObject()) {
1379     if (aCenterRef->object() == aPassedRef->object()) {
1380       theError = aErrorMessage;
1381       return false;
1382     }
1383   } else {
1384     AttributePoint2DPtr aCenterPoint =
1385         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1386     if (aCenterPoint) {
1387       std::set<FeaturePtr> aCoincidentFeatures =
1388           SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1389       // check one of coincident features is a feature referred by passed point
1390       std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1391       for(; anIt != aCoincidentFeatures.end(); ++anIt)
1392         if (*anIt == aPassedFeature) {
1393           theError = aErrorMessage;
1394           return false;
1395         }
1396     }
1397   }
1398   return true;
1399 }
1400
1401 bool SketchPlugin_ThirdPointValidator::isValid(
1402     const AttributePtr& theAttribute,
1403     const std::list<std::string>& theArguments,
1404     Events_InfoMessage& theError) const
1405 {
1406   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1407   return arePointsNotOnLine(anOwner, theError) &&
1408          arePointsNotSeparated(anOwner, theArguments, theError);
1409 }
1410
1411 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1412                                               const std::string& thePointAttrName,
1413                                               const std::string& theRefPointAttrName)
1414 {
1415   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1416       theMacroCircle->attribute(thePointAttrName));
1417   AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1418
1419   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1420   if (aRefAttr) {
1421     if (aRefAttr->isObject()) {
1422       // project a point onto selected feature
1423       std::shared_ptr<SketchPlugin_Feature> aFeature =
1424           std::dynamic_pointer_cast<SketchPlugin_Feature>(
1425           ModelAPI_Feature::feature(aRefAttr->object()));
1426       if (aFeature) {
1427         SketchPlugin_Sketch* aSketch = aFeature->sketch();
1428         std::shared_ptr<GeomAPI_Edge> anEdge =
1429             std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1430         if (anEdge) {
1431           std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1432           if (anEdge->isLine())
1433             aPoint3D = anEdge->line()->project(aPoint3D);
1434           else if (anEdge->isCircle())
1435             aPoint3D = anEdge->circle()->project(aPoint3D);
1436           if(aPoint3D)
1437             aPoint = aSketch->to2D(aPoint3D);
1438         }
1439       }
1440     } else {
1441       AttributePoint2DPtr anOtherPoint =
1442           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1443       if (anOtherPoint)
1444         aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1445     }
1446   }
1447
1448   return aPoint;
1449 }
1450
1451 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1452                                  std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1453 {
1454   if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1455     thePoints[0] = toPoint(theMacroFeature,
1456           SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1457           SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1458     thePoints[1] = toPoint(theMacroFeature,
1459           SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1460           SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1461     thePoints[2] = toPoint(theMacroFeature,
1462           SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1463           SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1464   } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1465     thePoints[0] = toPoint(theMacroFeature,
1466           SketchPlugin_MacroArc::START_POINT_2_ID(),
1467           SketchPlugin_MacroArc::START_POINT_REF_ID());
1468     thePoints[1] = toPoint(theMacroFeature,
1469           SketchPlugin_MacroArc::END_POINT_2_ID(),
1470           SketchPlugin_MacroArc::END_POINT_REF_ID());
1471     thePoints[2] = toPoint(theMacroFeature,
1472           SketchPlugin_MacroArc::PASSED_POINT_ID(),
1473           SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1474   }
1475 }
1476
1477 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1478                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1479                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1480 {
1481   static const double aTolerance = 1.e-7;
1482   if (thePoint1->distance(thePoint2) < aTolerance ||
1483       thePoint1->distance(thePoint3) < aTolerance)
1484     return true;
1485
1486   std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1487                                                             thePoint2->y() - thePoint1->y()));
1488   std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1489                                                             thePoint3->y() - thePoint1->y()));
1490   return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1491 }
1492
1493 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1494                          const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1495                          const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1496 {
1497   static const double aTolerance = 1.e-7;
1498   std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1499   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1500
1501   std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1502   // the first point is on the line
1503   if (aVec1->squareModulus() < aTolerance * aTolerance)
1504     return false;
1505   std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1506   std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1507   // the second point is on the line
1508   if (aVec2->squareModulus() < aTolerance * aTolerance)
1509     return false;
1510   std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1511
1512   return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1513 }
1514
1515 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1516                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint1,
1517                          const std::shared_ptr<GeomAPI_Pnt>&  thePoint2)
1518 {
1519   static const double aTolerance = 1.e-7;
1520   std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1521   double aDistP1C = thePoint1->distance(aCenter);
1522   double aDistP2C = thePoint2->distance(aCenter);
1523   return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1524 }
1525
1526 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1527     const FeaturePtr& theMacroFeature,
1528     Events_InfoMessage& theError) const
1529 {
1530   static const std::string aErrorPointsOnLine(
1531       "Selected points are on the same line");
1532
1533   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1534   threePointsOfFeature(theMacroFeature, aPoints);
1535
1536   if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1537     theError = aErrorPointsOnLine;
1538     return false;
1539   }
1540   return true;
1541 }
1542
1543 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1544     const FeaturePtr& theMacroFeature,
1545     const std::list<std::string>& theArguments,
1546     Events_InfoMessage& theError) const
1547 {
1548   static const std::string aErrorPointsApart(
1549       "Selected entity is lying between first two points");
1550
1551   AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1552   FeaturePtr aRefByThird;
1553   if (aThirdPointRef->isObject())
1554     aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1555   if (!aRefByThird)
1556     return true;
1557
1558   std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1559   threePointsOfFeature(theMacroFeature, aPoints);
1560
1561   std::shared_ptr<GeomAPI_Edge> aThirdShape =
1562       std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1563   if (!aThirdShape)
1564     return true;
1565
1566   SketchPlugin_Sketch* aSketch =
1567       std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1568   std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1569   std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1570
1571   bool isOk = true;
1572   if (aThirdShape->isLine())
1573     isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1574   else if (aThirdShape->isCircle() || aThirdShape->isArc())
1575     isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1576
1577   if (!isOk)
1578     theError = aErrorPointsApart;
1579   return isOk;
1580 }
1581
1582 bool SketchPlugin_ArcEndPointValidator::isValid(
1583     const AttributePtr& theAttribute,
1584     const std::list<std::string>& theArguments,
1585     Events_InfoMessage& theError) const
1586 {
1587   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1588   AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1589
1590   if(!anEndPointRef.get()) {
1591     return true;
1592   }
1593
1594   ObjectPtr anObject = anEndPointRef->object();
1595   AttributePtr anAttr = anEndPointRef->attr();
1596   if(!anObject.get() && !anAttr.get()) {
1597     return true;
1598   }
1599
1600   if(anEndPointRef->attr().get()) {
1601     return false;
1602   }
1603
1604   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1605   if(aResult.get()) {
1606     GeomShapePtr aShape = aResult->shape();
1607     if(aShape.get() && aShape->isVertex()) {
1608       return false;
1609     }
1610   }
1611
1612   aFeature = ModelAPI_Feature::feature(anObject);
1613   if(aFeature.get()) {
1614     if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1615       return false;
1616     }
1617   }
1618
1619   return true;
1620 }
1621
1622 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1623 {
1624   if(!theShape.get()) {
1625     return theShape;
1626   }
1627
1628   if(!theShape->isEdge()) {
1629     return theShape;
1630   }
1631
1632   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1633
1634   if(!anEdge.get()) {
1635     return theShape;
1636   }
1637
1638   if(anEdge->isLine()) {
1639     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1640     GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1641     return aShape;
1642   }
1643
1644   if(anEdge->isCircle() || anEdge->isArc()) {
1645     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1646     GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1647     return aShape;
1648   }
1649
1650   return theShape;
1651 }
1652
1653 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1654     const AttributePtr& theAttribute,
1655     const std::list<std::string>& theArguments,
1656     Events_InfoMessage& theError) const
1657 {
1658   std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1659       std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1660   AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1661
1662   if(!anEndPointRef.get()) {
1663     return true;
1664   }
1665
1666   GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1667
1668   if(!anArcShape.get() || anArcShape->isNull()) {
1669     return true;
1670   }
1671
1672   ObjectPtr anObject = anEndPointRef->object();
1673   AttributePtr anAttr = anEndPointRef->attr();
1674   if(!anObject.get() && !anAttr.get()) {
1675     return true;
1676   }
1677
1678   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1679   if(aResult.get()) {
1680     GeomShapePtr aShape = aResult->shape();
1681     if (!aShape->isEdge())
1682       return true;
1683     aShape = toInfiniteEdge(aShape);
1684     if(aShape.get() && !aShape->isNull()) {
1685       if(anArcShape->isIntersect(aShape)) {
1686         return true;
1687       }
1688     }
1689   }
1690
1691   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1692   if(aSelectedFeature.get()) {
1693     std::list<ResultPtr> aResults = aSelectedFeature->results();
1694     for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1695         anIt != aResults.cend();
1696         ++anIt)
1697     {
1698       GeomShapePtr aShape = (*anIt)->shape();
1699       if (!aShape->isEdge())
1700         return true;
1701       aShape = toInfiniteEdge(aShape);
1702       if(aShape.get() && !aShape->isNull()) {
1703         if(anArcShape->isIntersect(aShape)) {
1704           return true;
1705         }
1706       }
1707     }
1708   }
1709
1710   return false;
1711 }
1712
1713 bool SketchPlugin_HasNoConstraint::isValid(const AttributePtr& theAttribute,
1714                                            const std::list<std::string>& theArguments,
1715                                            Events_InfoMessage& theError) const
1716 {
1717   std::set<std::string> aFeatureKinds;
1718   for (std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1719        anArgIt != theArguments.end(); anArgIt++) {
1720     aFeatureKinds.insert(*anArgIt);
1721   }
1722
1723   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1724     theError = "The attribute with the %1 type is not processed";
1725     theError.arg(theAttribute->attributeType());
1726     return false;
1727   }
1728
1729   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
1730                                                                       (theAttribute);
1731   bool isObject = aRefAttr->isObject();
1732   if (!isObject) {
1733     theError = "It uses an empty object";
1734     return false;
1735   }
1736   ObjectPtr anObject = aRefAttr->object();
1737   FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1738   if (!aFeature.get()) {
1739     theError = "The feature of the checked attribute is empty";
1740     return false;
1741   }
1742
1743   FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aRefAttr->owner());
1744
1745   std::set<AttributePtr> aRefsList = anObject->data()->refsToMe();
1746   std::set<AttributePtr>::const_iterator anIt = aRefsList.begin();
1747   for (; anIt != aRefsList.end(); anIt++) {
1748     FeaturePtr aRefFeature = ModelAPI_Feature::feature((*anIt)->owner());
1749     if (aRefFeature.get() && aCurrentFeature != aRefFeature &&
1750         aFeatureKinds.find(aRefFeature->getKind()) != aFeatureKinds.end())
1751       return false; // constraint is found, that means that the check is not valid
1752   }
1753   return true;
1754 }
1755
1756 bool SketchPlugin_ReplicationReferenceValidator::isValid(
1757     const AttributePtr& theAttribute,
1758     const std::list<std::string>& theArguments,
1759     Events_InfoMessage& theError) const
1760 {
1761   AttributeRefAttrPtr aRefAttr =
1762       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1763   if (!aRefAttr)
1764   {
1765     theError = "Incorrect attribute";
1766     return false;
1767   }
1768
1769   ObjectPtr anOwner;
1770   if (aRefAttr->isObject())
1771     anOwner = aRefAttr->object();
1772   else
1773   {
1774     AttributePtr anAttr = aRefAttr->attr();
1775     anOwner = anAttr->owner();
1776   }
1777   FeaturePtr anAttrOwnerFeature = ModelAPI_Feature::feature(anOwner);
1778   if (!anAttrOwnerFeature)
1779     return true;
1780   AttributeBooleanPtr aCopyAttr = anAttrOwnerFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
1781   if (!aCopyAttr || !aCopyAttr->value())
1782     return true; // feature is not a copy, thus valid
1783
1784   // check the copy feature is already referred by the "Multi" feature
1785   FeaturePtr aMultiFeature = ModelAPI_Feature::feature(theAttribute->owner());
1786   AttributeRefListPtr aRefList = aMultiFeature->reflist(theArguments.front());
1787   for (int i = 0; i < aRefList->size(); ++i)
1788   {
1789     FeaturePtr aRefOwner = ModelAPI_Feature::feature(aRefList->object(i));
1790     if (aRefOwner == anAttrOwnerFeature)
1791     {
1792       theError = "Attribute refers to the object generated by this feature";
1793       return false;
1794     }
1795   }
1796
1797   return true;
1798 }
1799
1800 bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
1801                                                   const std::list<std::string>& theArguments,
1802                                                   Events_InfoMessage& theError) const
1803 {
1804   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId() &&
1805       theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1806     theError = "The attribute with the %1 type is not processed";
1807     theError.arg(theAttribute->attributeType());
1808     return false;
1809   }
1810
1811   // check the attribute refers to a sketch feature
1812   bool isSketchFeature = false;
1813   AttributeRefAttrPtr aRefAttr =
1814       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1815   if (aRefAttr) {
1816     isSketchFeature = aRefAttr->isObject();
1817     if (isSketchFeature) {
1818       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1819       isSketchFeature = aFeature.get() != NULL;
1820       if (isSketchFeature) {
1821         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1822             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1823         isSketchFeature = aSketchFeature.get() != NULL;
1824       }
1825     }
1826   }
1827   else {
1828     AttributeReferencePtr aReference =
1829       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1830     if (aReference) {
1831       FeaturePtr aFeature = ModelAPI_Feature::feature(aReference->value());
1832       isSketchFeature = aFeature.get() && aFeature->getKind() == SketchPlugin_Sketch::ID();
1833     }
1834   }
1835
1836   if (!isSketchFeature)
1837     theError = "The object selected is not a sketch feature";
1838   return isSketchFeature;
1839 }
1840
1841 bool SketchPlugin_MultiRotationAngleValidator::isValid(const AttributePtr& theAttribute,
1842                                                        const std::list<std::string>& theArguments,
1843                                                        Events_InfoMessage& theError) const
1844 {
1845   if (theAttribute->attributeType() != ModelAPI_AttributeDouble::typeId()) {
1846     theError = "The attribute with the %1 type is not processed";
1847     theError.arg(theAttribute->attributeType());
1848     return false;
1849   }
1850
1851   AttributeDoublePtr anAngleAttr =
1852     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1853
1854   FeaturePtr aMultiRotation = ModelAPI_Feature::feature(theAttribute->owner());
1855   AttributeStringPtr anAngleType =
1856       aMultiRotation->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
1857   AttributeIntegerPtr aNbCopies =
1858       aMultiRotation->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID());
1859
1860   if (anAngleType->value() != "FullAngle")
1861   {
1862     double aFullAngleValue = anAngleAttr->value() * (aNbCopies->value() - 1);
1863     if (aFullAngleValue < -1.e-7 || aFullAngleValue > 359.9999999)
1864     {
1865       theError = "Rotation single angle should produce full angle less than 360 degree";
1866       return false;
1867     }
1868   }
1869   else
1870   {
1871     double aFullAngleValue = anAngleAttr->value();
1872     if (aFullAngleValue < -1.e-7 || aFullAngleValue > 360.0000001)
1873     {
1874       theError = "Rotation full angle should be in range [0, 360]";
1875       return false;
1876     }
1877   }
1878
1879   return true;
1880 }
1881
1882 bool SketchPlugin_BSplineValidator::isValid(const AttributePtr& theAttribute,
1883                                             const std::list<std::string>& theArguments,
1884                                             Events_InfoMessage& theError) const
1885 {
1886   AttributePoint2DArrayPtr aPolesArray =
1887       std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(theAttribute);
1888   if (!aPolesArray)
1889     return false;
1890
1891   if (aPolesArray->size() < 2) {
1892     theError = "Number of B-spline poles should be 2 or more";
1893     return false;
1894   }
1895
1896   return true;
1897 }
1898
1899 bool SketchPlugin_CurveFittingValidator::isValid(const FeaturePtr& theFeature,
1900                                                  const std::list<std::string>& theArguments,
1901                                                  Events_InfoMessage& theError) const
1902 {
1903   AttributeRefAttrListPtr aRefAttrList =
1904       theFeature->refattrlist(SketchPlugin_CurveFitting::POINTS_ID());
1905   AttributeBooleanPtr aPeriodicAttr =
1906       theFeature->boolean(SketchPlugin_CurveFitting::PERIODIC_ID());
1907
1908   // check number of selected entities
1909   int aMinNbPoints = aPeriodicAttr->value() ? 3 : 2;
1910   if (aRefAttrList->size() < aMinNbPoints) {
1911     theError = "Not enough points selected. Need at least %1 points.";
1912     theError.arg(aMinNbPoints);
1913     return false;
1914   }
1915   return true;
1916 }