]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Validators.cpp
Salome HOME
Issue #2026: Removal of construction lines automatically created by Fillet
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Validators.cpp
4 // Created:     01 Aug 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketchPlugin_Validators.h"
8
9 #include "SketchPlugin_Arc.h"
10 #include "SketchPlugin_Circle.h"
11 #include "SketchPlugin_ConstraintCoincidence.h"
12 #include "SketchPlugin_ConstraintDistance.h"
13 #include "SketchPlugin_Fillet.h"
14 #include "SketchPlugin_ConstraintRigid.h"
15 #include "SketchPlugin_ConstraintTangent.h"
16 #include "SketchPlugin_Line.h"
17 #include "SketchPlugin_Point.h"
18 #include "SketchPlugin_Sketch.h"
19 #include "SketchPlugin_Tools.h"
20
21 #include "SketcherPrs_Tools.h"
22
23 #include <Events_InfoMessage.h>
24
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_AttributeRefAttr.h>
29
30 #include <ModelAPI_AttributeRefAttrList.h>
31 #include <ModelAPI_AttributeRefList.h>
32 #include <ModelAPI_AttributeSelectionList.h>
33 #include <ModelAPI_AttributeString.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_ResultConstruction.h>
37
38 #include <ModelGeomAlgo_Point2D.h>
39
40 #include <GeomAPI_Circ.h>
41 #include <GeomAPI_Lin.h>
42 #include <GeomAPI_Edge.h>
43 #include <GeomAPI_Vertex.h>
44 #include <GeomDataAPI_Point2D.h>
45
46 #include <algorithm>
47 #include <cmath>
48
49 const double tolerance = 1.e-7;
50
51 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
52                                                  const std::list<std::string>& theArguments,
53                                                  Events_InfoMessage& theError) const
54 {
55   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
56     theError = "The attribute with the %1 type is not processed";
57     theError.arg(theAttribute->attributeType());
58     return false;
59   }
60
61   // there is a check whether the feature contains a point and a linear edge or two point values
62   std::string aParamA = theArguments.front();
63   SessionPtr aMgr = ModelAPI_Session::get();
64   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
65
66   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
67                                                                       (theAttribute);
68   bool isObject = aRefAttr->isObject();
69   if (!isObject) {
70     // an attribute is a point. A point value is valid always for the distance
71     return true;
72   } else {
73     // 1. check whether the references object is a linear
74     ObjectPtr anObject = aRefAttr->object();
75
76     const ModelAPI_AttributeValidator* aShapeValidator =
77       dynamic_cast<const ModelAPI_AttributeValidator*>(
78       aFactory->validator("GeomValidators_ShapeType"));
79     std::list<std::string> anArguments;
80     anArguments.push_back("circle");
81     Events_InfoMessage aCircleError;
82     bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
83     // the circle line is not a valid case
84     if (aShapeValid) {
85       theError = "Circle can not be used in distance constraint";
86       return false;
87     }
88
89     anArguments.clear();
90     anArguments.push_back("line");
91     Events_InfoMessage aLineError;
92     aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
93     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
94     if (aShapeValid) {
95       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
96       // If it is a line then we have to check that first attribute id not a line
97       std::shared_ptr<SketchPlugin_Feature> aSFeature =
98         std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
99       SketchPlugin_Sketch* aSketch = aSFeature->sketch();
100       std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
101       std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
102         aFeature->data(), aParamA, aPlane);
103       if (!aPoint.get()) {
104         theError = "One of parameters of distance constraint should be a point";
105         return false;
106       }
107     }
108   }
109   return true;
110 }
111
112 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
113                                                 const std::list<std::string>& theArguments,
114                                                 Events_InfoMessage& theError) const
115 {
116   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
117     theError = "The attribute with the %1 type is not processed";
118     theError.arg(theAttribute->attributeType());
119     return false;
120   }
121
122   // there is a check whether the feature contains a point and a linear edge or two point values
123   std::string aParamA = theArguments.front();
124   SessionPtr aMgr = ModelAPI_Session::get();
125   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
126
127   FeaturePtr anAttributeFeature =
128     std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
129   AttributeRefAttrPtr aRefAttr =
130     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
131
132   bool isObject = aRefAttr->isObject();
133   ObjectPtr anObject = aRefAttr->object();
134   if (isObject && anObject.get()) {
135     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
136
137     AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
138     ObjectPtr aOtherObject = aOtherAttr->object();
139     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
140     if (!aOtherFea)
141       return true;
142
143     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
144       if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
145           aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
146         theError = "It refers to a %1, but %2 is neither an %3 nor %4";
147         theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
148             .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
149         return false;
150       }
151     }
152     else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
153              aRefFea->getKind() == SketchPlugin_Circle::ID()) {
154       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
155           aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
156           aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
157         theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
158         theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
159             .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
160             .arg(SketchPlugin_Circle::ID());
161         return false;
162       }
163     }
164     else {
165       theError = "It refers to %1, but should refer to %2 or %3 or %4";
166       theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
167           .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
168       return false;
169     }
170     return true;
171   }
172   else {
173     theError = "It uses an empty object";
174     return false;
175   }
176
177   return true;
178 }
179
180 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
181                                              const std::list<std::string>& theArguments,
182                                              Events_InfoMessage& theError) const
183 {
184   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
185     theError = "The attribute with the %1 type is not processed";
186     theError.arg(theAttribute->attributeType());
187     return false;
188   }
189
190   std::shared_ptr<SketchPlugin_Feature> aFeature =
191       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
192   if (!aFeature)
193     return true;
194
195   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
196
197   SketchPlugin_Sketch* aSketch = aFeature->sketch();
198   int aNbFeatures = aSketch->numberOfSubs();
199   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
200     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
201     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
202       continue;
203     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
204         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
205     if (aRefAttr->isObject()) {
206       if (aRefAttr->object() == aRAttr->object()) {
207         ObjectPtr anObject = aRefAttr->object();
208         std::string aName = anObject.get() ? anObject->data()->name() : "";
209         theError = "The object %1 has been already fixed.";
210         theError.arg(aName);
211         return false;
212       }
213     }
214     else if (aRefAttr->attr() == aRAttr->attr()) {
215       AttributePtr anAttribute = aRefAttr->attr();
216       std::string aName = anAttribute.get() ? anAttribute->id() : "";
217       theError = "The attribute %1 has been already fixed.";
218       theError.arg(aName);
219       return false;
220     }
221   }
222   return true;
223 }
224
225 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
226                                               const std::list<std::string>& theArguments,
227                                               Events_InfoMessage& theError) const
228 {
229   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
230     theError = "The attribute with the %1 type is not processed";
231     theError.arg(theAttribute->attributeType());
232     return false;
233   }
234
235   std::string aParamA = theArguments.front();
236   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
237   AttributeRefAttrPtr aRefAttr[2];
238   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
239   aRefAttr[1] = aFeature->data()->refattr(aParamA);
240
241   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
242     theError = "Attributes can not be used in equal constraint";
243     return false;
244   }
245
246   std::string aType[2];
247   std::list<std::string> anArguments;
248   for (int i = 0; i < 2; i++) {
249     ObjectPtr anObject = aRefAttr[i]->object();
250     if (!anObject.get()) {
251       theError = "An empty object is used.";
252       return false;
253     }
254
255     aFeature = ModelAPI_Feature::feature(anObject);
256     if (!aFeature.get()) {
257       theError = "An empty feature is used.";
258       return false;
259     }
260
261     aType[i] = aFeature->getKind();
262     if (aFeature->getKind() != SketchPlugin_Line::ID() &&
263         aFeature->getKind() != SketchPlugin_Circle::ID() &&
264         aFeature->getKind() != SketchPlugin_Arc::ID()) {
265       theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
266       theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
267           .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
268       // wrong type of attribute
269       return false;
270     }
271   }
272
273   if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
274       aType[0] != aType[1]) {
275     theError = "Feature with kinds %1 and %2 can not be equal.";
276     theError.arg(aType[0]).arg(aType[1]);
277     return false;
278   }
279   return true;
280 }
281
282 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
283                                                const std::list<std::string>& theArguments,
284                                                Events_InfoMessage& theError) const
285 {
286   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
287     theError = "The attribute with the %1 type is not processed";
288     theError.arg(theAttribute->attributeType());
289     return false;
290   }
291
292   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
293   AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
294
295   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
296       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
297   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
298
299   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
300     ObjectPtr aSelObject = aSelAttr->object(anInd);
301     std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
302     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
303     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
304       if (aSelObject == *aMirIter) {
305         theError = "The object %1 is a result of mirror";
306         theError.arg(aName);
307         return false;
308       }
309   }
310   return true;
311 }
312
313 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
314                                                     const std::list<std::string>& theArguments,
315                                                     Events_InfoMessage& theError) const
316 {
317   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
318     theError = "The attribute with the %1 type is not processed";
319     theError.arg(theAttribute->attributeType());
320     return false;
321   }
322
323   // there is a check whether the feature contains a point and a linear edge or two point values
324   std::string aParamA = theArguments.front();
325   SessionPtr aMgr = ModelAPI_Session::get();
326   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
327
328   FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
329   AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
330   if (!aRefAttrA) {
331     theError = "The %1 attribute should be %2";
332     theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
333     return false;
334   }
335
336   AttributeRefAttrPtr aRefAttrB =
337     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
338
339   // first attribute is a point, it may coincide with any object
340   if (!aRefAttrA->isObject())
341     return true;
342   else {
343     ObjectPtr anObject = aRefAttrA->object();
344     if (!anObject.get()) {
345       theError = "%1 attribute has an empty object";
346       theError.arg(aParamA);
347       return false;
348     }
349     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
350     if (!aFeature.get()) {
351       theError = "%1 attribute has an empty feature";
352       theError.arg(aParamA);
353       return false;
354     }
355
356     if (aFeature->getKind() == SketchPlugin_Point::ID())
357       return true;
358   }
359
360   // second attribute is a point, it may coincide with any object
361   if (!aRefAttrB->isObject())
362     return true;
363   else {
364     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
365     if (!aFeature) {
366       theError = "%1 attribute has an empty object";
367       theError.arg(theAttribute->id());
368       return false;
369     }
370     if (aFeature->getKind() == SketchPlugin_Point::ID())
371       return true;
372   }
373   theError = "There is no an attribute filled by a point";
374   return false;
375 }
376
377
378 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
379                                          const std::list<std::string>& theArguments,
380                                          Events_InfoMessage& theError) const
381 {
382   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
383     theError = "The attribute with the %1 type is not processed";
384     theError.arg(theAttribute->attributeType());
385     return false;
386   }
387
388   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
389   AttributeRefListPtr aSelAttr =
390     std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
391
392   AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
393       aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
394   AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
395       aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
396   std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
397   std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
398
399   std::list<ObjectPtr>::iterator anObjIter;
400   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
401     ObjectPtr aSelObject = aSelAttr->object(anInd);
402     anObjIter = anInitialObjects.begin();
403     for (; anObjIter != anInitialObjects.end(); anObjIter++)
404       if (aSelObject == *anObjIter)
405         break;
406     if (anObjIter != anInitialObjects.end())
407       continue;
408     anObjIter = aCopiedObjects.begin();
409     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
410       if (aSelObject == *anObjIter) {
411         std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
412         theError = "The object %1 is a result of copy";
413         theError.arg(aName);
414         return false;
415       }
416   }
417   return true;
418 }
419
420 bool SketchPlugin_SolverErrorValidator::isValid(
421   const std::shared_ptr<ModelAPI_Feature>& theFeature,
422   const std::list<std::string>& theArguments,
423   Events_InfoMessage& theError) const
424 {
425   AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
426
427   if (!aAttributeString->value().empty()) {
428     theError = aAttributeString->value();
429     return false;
430   }
431
432   return true;
433 }
434
435 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
436                                                         std::string theAttribute)
437 {
438   return true;
439 }
440
441 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
442                                   const FeaturePtr theFeature)
443 {
444   for(std::set<AttributePtr>::const_iterator
445       anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
446     std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
447     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
448     if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
449       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
450         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
451       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
452         aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
453       if(anAttrRefA.get()) {
454         ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
455         if(aResA.get()) {
456           DocumentPtr aDoc = aResA->document();
457           if(aDoc.get()) {
458             FeaturePtr aFeatureA = aDoc->feature(aResA);
459             if(aFeatureA.get() && aFeatureA == theFeature) {
460               return true;
461             }
462           }
463         }
464       }
465       if(anAttrRefB.get()) {
466         ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
467         if(aResB.get()) {
468           DocumentPtr aDoc = aResB->document();
469           if(aDoc.get()) {
470             FeaturePtr aFeatureB = aDoc->feature(aResB);
471             if(aFeatureB.get() && aFeatureB == theFeature) {
472               return true;
473             }
474           }
475         }
476       }
477     }
478   }
479   return false;
480 }
481
482 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
483                                                  const std::list<std::string>& theArguments,
484                                                  Events_InfoMessage& theError) const
485 {
486   AttributeRefAttrPtr aPointRefAttr =
487     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
488   if(!aPointRefAttr.get()) {
489     theError = "Error: Point not selected.";
490     return false;
491   }
492
493   AttributePtr aPointAttribute = aPointRefAttr->attr();
494   if (!aPointAttribute.get()) {
495     theError = "Error: Bad point selected.";
496     return false;
497   }
498   std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
499     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
500
501   // Obtain constraint coincidence for the fillet point.
502   const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
503   FeaturePtr aConstraintCoincidence;
504   for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
505       anIt != aRefsList.cend(); ++anIt) {
506     std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
507     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
508     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
509       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
510         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
511       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
512         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
513       if(anAttrRefA.get() && !anAttrRefA->isObject()) {
514         AttributePtr anAttrA = anAttrRefA->attr();
515         if(aPointAttribute == anAttrA) {
516           aConstraintCoincidence = aConstrFeature;
517           break;
518         }
519       }
520       if(anAttrRefB.get() && !anAttrRefB->isObject()) {
521         AttributePtr anAttrB = anAttrRefB->attr();
522         if(aPointAttribute == anAttrB) {
523           aConstraintCoincidence = aConstrFeature;
524           break;
525         }
526       }
527     }
528   }
529
530   if(!aConstraintCoincidence.get()) {
531     theError = "Error: one of the selected point does not have coicidence.";
532     return false;
533   }
534
535   // Get coincides from constraint.
536   std::set<FeaturePtr> aCoinsides;
537   SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
538                                         SketchPlugin_ConstraintCoincidence::ENTITY_A(),
539                                         aCoinsides);
540   SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
541                                         SketchPlugin_ConstraintCoincidence::ENTITY_B(),
542                                         aCoinsides);
543
544   // Remove points from set of coincides.
545   std::set<FeaturePtr> aNewSetOfCoincides;
546   for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
547       anIt != aCoinsides.end(); ++anIt) {
548     std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
549       std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
550     if(aSketchEntity.get() && aSketchEntity->isCopy()) {
551       continue;
552     }
553     if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
554         (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
555           continue;
556     }
557     if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
558       AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
559       std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
560         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
561       double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
562       if(aDistSelectedArcCenter < tolerance) {
563         continue;
564       }
565     }
566     aNewSetOfCoincides.insert(*anIt);
567   }
568   aCoinsides = aNewSetOfCoincides;
569
570   // If we still have more than two coincides remove auxilary entities from set of coincides.
571   if(aCoinsides.size() > 2) {
572     aNewSetOfCoincides.clear();
573     for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
574         anIt != aCoinsides.end(); ++anIt) {
575       if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
576         aNewSetOfCoincides.insert(*anIt);
577       }
578     }
579     aCoinsides = aNewSetOfCoincides;
580   }
581
582   if(aCoinsides.size() != 2) {
583     theError = "Error: One of the selected points does not have two suitable edges for fillet.";
584     return false;
585   }
586
587   // Check that selected edges don't have tangent constraint.
588   std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
589   FeaturePtr aFirstFeature = *anIt++;
590   FeaturePtr aSecondFeature = *anIt;
591   const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
592   if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
593     theError = "Error: Edges in selected point has tangent constraint.";
594     return false;
595   }
596
597   std::list<ResultPtr> aFirstResults = aFirstFeature->results();
598   for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
599       aResIt != aFirstResults.end(); ++aResIt) {
600     ResultPtr aRes = *aResIt;
601     const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
602     if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
603       theError = "Error: Edges in selected point has tangent constraint.";
604       return false;
605     }
606   }
607
608   // Check that lines not collinear
609   if(aFirstFeature->getKind() == SketchPlugin_Line::ID() &&
610       aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
611     std::string aStartAttr = SketchPlugin_Line::START_ID();
612     std::string anEndAttr = SketchPlugin_Line::END_ID();
613     std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
614     aFirstStartPnt =
615       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
616       aFirstFeature->attribute(aStartAttr))->pnt();
617     aFirstEndPnt =
618       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
619     aSecondStartPnt =
620       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
621       aSecondFeature->attribute(aStartAttr))->pnt();
622     aSecondEndPnt =
623       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
624       aSecondFeature->attribute(anEndAttr))->pnt();
625     double aCheck1 =
626       fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
627       (aSecondStartPnt->y() - aFirstStartPnt->y()) -
628       (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
629     double aCheck2 =
630       fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
631       (aSecondEndPnt->y() - aFirstStartPnt->y()) -
632       (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
633     if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
634       return false;
635     }
636   }
637
638
639   return true;
640 }
641
642 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
643                                                     const std::list<std::string>& theArguments,
644                                                     Events_InfoMessage& theError) const
645 {
646   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
647     theError = "The attribute with the %1 type is not processed";
648     theError.arg(theAttribute->attributeType());
649     return false;
650   }
651
652   // there is a check whether the feature contains a point and a linear edge or two point values
653   std::string aParamA = theArguments.front();
654   SessionPtr aMgr = ModelAPI_Session::get();
655   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
656
657   FeaturePtr anAttributeFeature =
658     std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
659   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
660   AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
661
662   AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
663   int aNbPoints = 0;
664   int aNbLines = 0;
665   for (int i = 0; i < 2; ++i) {
666     if (!aRefAttrs[i]->isObject())
667       ++aNbPoints;
668     else {
669       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
670       if (!aFeature) {
671         if (aNbPoints + aNbLines != 0)
672           return true;
673         else continue;
674       }
675
676       if (aFeature->getKind() == SketchPlugin_Point::ID())
677         ++aNbPoints;
678       else if (aFeature->getKind() == SketchPlugin_Line::ID())
679         ++aNbLines;
680     }
681   }
682
683   if (aNbPoints != 1 || aNbLines != 1) {
684     theError = "Middle point constraint allows points and lines only";
685     return false;
686   }
687   return true;
688 }
689
690 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
691                                                     const std::list<std::string>& /*theArguments*/,
692                                                     Events_InfoMessage& theError) const
693 {
694   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
695     theError = "The attribute with the %1 type is not processed";
696     theError.arg(theAttribute->attributeType());
697     return false;
698   }
699   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
700   AttributePtr anAttr = aRefAttr->attr();
701   if (!anAttr) {
702     theError = "The attribute %1 should be a point";
703     theError.arg(theAttribute->id());
704     return false;
705   }
706
707   FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
708   const std::string& aFeatureType = anAttrFeature->getKind();
709   if (aFeatureType == SketchPlugin_Arc::ID()) {
710     // selected point should not be a center of arc
711     const std::string& aPntId = anAttr->id();
712     if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
713       theError = "The attribute %1 is not supported";
714       theError.arg(aPntId);
715       return false;
716     }
717   }
718   else if (aFeatureType == SketchPlugin_Line::ID()) {
719     // selected point should be bound point of line
720     const std::string& aPntId = anAttr->id();
721     if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
722       theError = "The attribute %1 is not supported";
723       theError.arg(aPntId);
724       return false;
725     }
726   }
727   else {
728     theError = "Unable to build tangent arc on %1";
729     theError.arg(anAttrFeature->getKind());
730     return false;
731   }
732
733   // Check the tangent point is equal to arc end
734   FeaturePtr anArc = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefAttr->owner());
735   std::shared_ptr<GeomDataAPI_Point2D> anEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
736       anArc->attribute(SketchPlugin_Arc::END_ID()));
737   if (anEndPoint->isInitialized()) {
738     std::shared_ptr<GeomDataAPI_Point2D> aTangPt =
739         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
740     if (aTangPt->pnt()->distance(anEndPoint->pnt()) < tolerance) {
741       theError = "Unable to build arc on same points";
742       return false;
743     }
744   }
745
746   return true;
747 }
748
749 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
750                                                  const std::list<std::string>& theArguments,
751                                                  Events_InfoMessage& theError) const
752 {
753   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
754     theError = "The attribute with the %1 type is not processed";
755     theError.arg(theAttribute->attributeType());
756     return false;
757   }
758   AttributeSelectionPtr aLineAttr =
759                        std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
760   std::shared_ptr<GeomAPI_Edge> anEdge;
761   if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
762     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
763   } else if(aLineAttr->context() &&
764             aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
765     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
766   }
767
768   if (!anEdge || !anEdge->isLine()) {
769     theError = "The attribute %1 should be a line";
770     theError.arg(theAttribute->id());
771     return false;
772   }
773
774   std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
775
776   // find a sketch
777   std::shared_ptr<SketchPlugin_Sketch> aSketch;
778   std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
779   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
780   for (; anIt != aRefs.end(); ++anIt) {
781     CompositeFeaturePtr aComp =
782         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
783     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
784       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
785       break;
786     }
787   }
788   if (!aSketch) {
789     theError = "There is no sketch referring to the current feature";
790     return false;
791   }
792
793   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
794   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
795   return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
796 }
797
798 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
799                                           const std::list<std::string>& theArguments,
800                                           Events_InfoMessage& theError) const
801 {
802   bool aValid = false;
803
804   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
805     theError = "The attribute with the %1 type is not processed";
806     theError.arg(theAttribute->attributeType());
807     return aValid;
808   }
809   AttributeReferencePtr aFeatureAttr =
810             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
811
812   ObjectPtr anAttrObject = aFeatureAttr->value();
813   FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
814   if (!anAttrFeature)
815     return aValid;
816
817   std::string aKind = anAttrFeature->getKind();
818   if (aKind == SketchPlugin_Line::ID() ||
819       aKind == SketchPlugin_Arc::ID() ||
820       aKind == SketchPlugin_Circle::ID()) {
821
822     std::set<ResultPtr> anEdgeShapes;
823     ModelAPI_Tools::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
824     if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
825       return aValid;
826
827     // coincidences to the feature
828     std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
829     ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
830                         SketchPlugin_ConstraintCoincidence::ID(),
831                         aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
832
833     GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
834     std::shared_ptr<SketchPlugin_Feature> aSFeature =
835                                  std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
836     SketchPlugin_Sketch* aSketch = aSFeature->sketch();
837
838     std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
839     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
840         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
841     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
842         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
843     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
844         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
845     std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
846
847     typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
848                      std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
849                                std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
850     PointToRefsMap aPointsInfo;
851
852     ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
853                                                 aX->dir(), aDirY, aPointsInfo);
854     int aCoincidentToFeature = (int)aPointsInfo.size();
855     if (aKind == SketchPlugin_Circle::ID())
856       aValid = aCoincidentToFeature >= 2;
857     else
858       aValid = aCoincidentToFeature >= 1;
859   }
860
861   return aValid;
862 }
863
864 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
865                                                const std::list<std::string>& theArguments,
866                                                Events_InfoMessage& theError) const
867 {
868   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
869     theError = "The attribute with the %1 type is not processed";
870     theError.arg(theAttribute->attributeType());
871     return false;
872   }
873
874   AttributeSelectionPtr aFeatureAttr =
875       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
876   std::shared_ptr<GeomAPI_Edge> anEdge;
877   if (aFeatureAttr.get()) {
878     GeomShapePtr aVal = aFeatureAttr->value();
879     ResultPtr aRes = aFeatureAttr->context();
880     if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
881       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
882     } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
883               aFeatureAttr->context()->shape()->isEdge()) {
884       anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
885     }
886   }
887   if (!anEdge) {
888     theError = "The attribute %1 should be an edge";
889     theError.arg(theAttribute->id());
890     return false;
891   }
892
893   // find a sketch
894   std::shared_ptr<SketchPlugin_Sketch> aSketch;
895   std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
896   std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
897   for (; anIt != aRefs.end(); ++anIt) {
898     CompositeFeaturePtr aComp =
899         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
900     if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
901       aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
902       break;
903     }
904   }
905   if (!aSketch) {
906     theError = "There is no sketch referring to the current feature";
907     return false;
908   }
909
910   std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
911   std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
912   std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
913
914   if (anEdge->isLine()) {
915     std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
916     std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
917     std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
918     double aDot = aNormal->dot(aLineDir);
919     double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
920     bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
921            (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
922     if (!aValid)
923       theError = "Error: Edge is already in the sketch plane.";
924     return aValid;
925   }
926   else if (anEdge->isCircle() || anEdge->isArc()) {
927     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
928     std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
929     std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
930     double aDot = fabs(aNormal->dot(aCircNormal));
931     double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
932     bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
933     if (!aValid)
934       theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
935                                       : "Error: Arc is already in the sketch plane.");
936     return aValid;
937   }
938
939   theError = "Error: Selected object is not line, circle or arc.";
940   return false;
941 }