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