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