Salome HOME
Bug #1098: Create fillet problem
[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_ConstraintRigid.h"
14 #include "SketchPlugin_Line.h"
15 #include "SketchPlugin_Point.h"
16 #include "SketchPlugin_Sketch.h"
17 #include "SketchPlugin_Tools.h"
18
19 #include "SketcherPrs_Tools.h"
20
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeRefAttr.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_Session.h>
29
30 #include <GeomDataAPI_Point2D.h>
31
32
33 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute, 
34                                                  const std::list<std::string>& theArguments,
35                                                  std::string& theError) const
36 {
37   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
38     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
39     return false;
40   }
41
42   // there is a check whether the feature contains a point and a linear edge or two point values
43   std::string aParamA = theArguments.front();
44   SessionPtr aMgr = ModelAPI_Session::get();
45   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
46
47   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
48   bool isObject = aRefAttr->isObject();
49   if (!isObject) {
50     // an attribute is a point. A point value is valid always for the distance
51     return true;
52   } else {
53     // 1. check whether the references object is a linear
54     ObjectPtr anObject = aRefAttr->object();
55
56     const ModelAPI_AttributeValidator* aShapeValidator = 
57       dynamic_cast<const ModelAPI_AttributeValidator*>(aFactory->validator("GeomValidators_ShapeType"));
58     std::list<std::string> anArguments;
59     anArguments.push_back("circle");
60     std::string aCircleError;
61     bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
62     // the circle line is not a valid case
63     if (aShapeValid) {
64       theError = "Circle can not be used in distance constraint";
65       return false;
66     }
67       
68     anArguments.clear();
69     anArguments.push_back("line");
70     std::string aLineError;
71     aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
72     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
73     if (aShapeValid) {
74       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
75       // If it is a line then we have to check that first attribute id not a line
76       std::shared_ptr<SketchPlugin_Feature> aSFeature =
77         std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
78       SketchPlugin_Sketch* aSketch = aSFeature->sketch();
79       std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
80       std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
81         aFeature->data(), aParamA, aPlane);
82       if (!aPoint.get()) {
83         theError = "One of parameters of distance constraint should be a point";
84         return false;
85       }
86     }
87   }
88   return true;
89 }
90
91 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute, 
92                                                 const std::list<std::string>& theArguments,
93                                                 std::string& theError) const
94 {
95   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
96     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
97     return false;
98   }
99
100   // there is a check whether the feature contains a point and a linear edge or two point values
101   std::string aParamA = theArguments.front();
102   SessionPtr aMgr = ModelAPI_Session::get();
103   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
104
105   FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
106   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
107
108   bool isObject = aRefAttr->isObject();
109   ObjectPtr anObject = aRefAttr->object();
110   if (isObject && anObject.get()) {
111     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
112
113     AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
114     ObjectPtr aOtherObject = aOtherAttr->object();
115     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
116
117     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
118       if (aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
119         theError = "It refers to a " + SketchPlugin_Line::ID() + ", but " + aParamA + " is not an "
120           + SketchPlugin_Arc::ID();
121         return false;
122       }
123     }
124     else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
125       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
126         aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
127         theError = "It refers to an " + SketchPlugin_Arc::ID() + ", but " + aParamA + " is not a "
128           + SketchPlugin_Line::ID() + " or an " + SketchPlugin_Arc::ID();
129         return false;
130       }
131     }
132     else {
133       theError = "It refers to " + aRefFea->getKind() + "but should refer to " + SketchPlugin_Line::ID()
134         + " or " + SketchPlugin_Arc::ID();
135       return false;
136     }
137     return true;
138   }
139   else {
140     theError = "It uses an empty object";
141     return false;
142   }
143
144   return true;
145 }
146
147 bool SketchPlugin_NotFixedValidator::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   std::shared_ptr<SketchPlugin_Feature> aFeature =
157       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
158   if (!aFeature)
159     return true;
160
161   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
162
163   SketchPlugin_Sketch* aSketch = aFeature->sketch();
164   int aNbFeatures = aSketch->numberOfSubs();
165   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
166     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
167     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
168       continue;
169     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
170         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
171     if (aRefAttr->isObject()) {
172       if (aRefAttr->object() == aRAttr->object()) {
173         ObjectPtr anObject = aRefAttr->object();
174         std::string aName = anObject.get() ? anObject->data()->name() : "";
175         theError = "The object " + aName + " has been already fixed.";
176         return false;
177       }
178     }
179     else if (aRefAttr->attr() == aRAttr->attr()) {
180       AttributePtr anAttribute = aRefAttr->attr();
181       std::string aName = anAttribute.get() ? anAttribute->id() : "";
182       theError = "The attribute " + aName + " has been already fixed.";
183       return false;
184     }
185   }
186   return true;
187 }
188
189 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute, 
190                                               const std::list<std::string>& theArguments,
191                                               std::string& theError) const
192 {
193   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
194     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
195     return false;
196   }
197
198   std::string aParamA = theArguments.front();
199   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
200   AttributeRefAttrPtr aRefAttr[2];
201   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
202   aRefAttr[1] = aFeature->data()->refattr(aParamA);
203
204   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
205     theError = "Attributes can not be used in equal constraint";
206     return false;
207   }
208
209   int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
210   std::list<std::string> anArguments;
211   for (int i = 0; i < 2; i++) {
212     ObjectPtr anObject = aRefAttr[i]->object();
213     if (!anObject.get()) {
214       theError = "An empty object is used.";
215       return false;
216     }
217
218     aFeature = ModelAPI_Feature::feature(anObject);
219     if (!aFeature.get()) {
220       theError = "An empty feature is used.";
221       return false;
222     }
223
224     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
225       aType[i] = 1;
226       continue;
227     }
228     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
229       aType[i] = 2;
230       continue;
231     }
232     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
233       aType[i] = 3;
234       continue;
235     }
236     theError = "The " + aFeature->getKind() + " feature kind of attribute is wrong. It should be " +
237                SketchPlugin_Line::ID() + " or " + SketchPlugin_Circle::ID() + " or " + 
238                SketchPlugin_Arc::ID();
239     // wrong type of attribute
240     return false;
241   }
242
243   if ((aType[0] == 1 && aType[1] == 2) ||
244       (aType[0] == 2 && aType[1] == 1)) {
245     theError = "Feature with kinds " + SketchPlugin_Line::ID() + " and " +
246                SketchPlugin_Circle::ID() + "can not be equal.";
247     return false;
248   }
249   return true;
250 }
251
252 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute, 
253                                                const std::list<std::string>& theArguments,
254                                                std::string& theError) const
255 {
256   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
257     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
258     return false;
259   }
260
261   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
262   AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
263
264   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
265       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
266   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
267
268   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
269     ObjectPtr aSelObject = aSelAttr->object(anInd);
270     std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
271     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
272     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
273       if (aSelObject == *aMirIter) {
274         theError = "The object " + aName + " is a result of mirror";
275         return false;
276       }
277   }
278   return true;
279 }
280
281
282 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute, 
283                                                     const std::list<std::string>& theArguments,
284                                                     std::string& theError) const
285 {
286   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
287     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
288     return false;
289   }
290
291   // there is a check whether the feature contains a point and a linear edge or two point values
292   std::string aParamA = theArguments.front();
293   SessionPtr aMgr = ModelAPI_Session::get();
294   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
295
296   FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
297   AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
298   if (!aRefAttrA) {
299     theError = "The " + aParamA + " attribute " + " should be " + ModelAPI_AttributeRefAttr::typeId();
300     return false;
301   }
302
303   AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
304
305   // first attribute is a point, it may coincide with any object
306   if (!aRefAttrA->isObject())
307     return true;
308   else {
309     ObjectPtr anObject = aRefAttrA->object();
310     if (!anObject.get()) {
311       theError = aParamA + " attribute has an empty object";
312       return false;
313     }
314     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
315     if (!aFeature.get()) {
316       theError = aParamA + " attribute has an empty feature";
317       return false;
318     }
319
320     if (aFeature->getKind() == SketchPlugin_Point::ID())
321       return true;
322   }
323
324   // second attribute is a point, it may coincide with any object
325   if (!aRefAttrB->isObject())
326     return true;
327   else {
328     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
329     if (!aFeature) {
330       theError = theAttribute->id() + " attribute has an empty object";
331       return false;
332     }
333     if (aFeature->getKind() == SketchPlugin_Point::ID())
334       return true;
335   }
336   theError = "There is no an attribute filled by a point";
337   return false;
338 }
339
340
341 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute, 
342                                          const std::list<std::string>& theArguments,
343                                          std::string& theError) const
344 {
345   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
346     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
347     return false;
348   }
349
350   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
351   AttributeRefListPtr aSelAttr = 
352     std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
353
354   AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
355       aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
356   AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
357       aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
358   std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
359   std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
360
361   std::list<ObjectPtr>::iterator anObjIter;
362   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
363     ObjectPtr aSelObject = aSelAttr->object(anInd);
364     anObjIter = anInitialObjects.begin();
365     for (; anObjIter != anInitialObjects.end(); anObjIter++)
366       if (aSelObject == *anObjIter)
367         break;
368     if (anObjIter != anInitialObjects.end())
369       continue;
370     anObjIter = aCopiedObjects.begin();
371     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
372       if (aSelObject == *anObjIter) {
373         std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
374         theError = "The object " + aName + " is a result of copy";
375         return false;
376       }
377   }
378   return true;
379 }
380
381 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
382                                                 const std::list<std::string>& theArguments,
383                                                 std::string& theError) const
384 {
385   AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
386
387   if (!aAttributeString->value().empty()) {
388     theError = aAttributeString->value();
389     return false;
390   }
391
392   return true;
393 }
394
395 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
396 {
397   return true;
398 }
399
400 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
401                                                  const std::list<std::string>& theArguments,
402                                                  std::string& theError) const
403 {
404   if(!theAttribute.get()) {
405     return false;
406   }
407
408   AttributeRefAttrPtr aBase = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
409   if(aBase->isObject()) {
410     return false;
411   }
412
413   // If we alredy have some result then all ok
414   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
415   AttributePtr aBaseLinesAttribute = aFeature->attribute(SketchPlugin_Constraint::ENTITY_C());
416   AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aBaseLinesAttribute);
417   if(!aRefListOfBaseLines->list().empty()) {
418     return true;
419   }
420
421   AttributePtr anAttrBase = aBase->attr();
422   const std::set<AttributePtr>& aRefsList = anAttrBase->owner()->data()->refsToMe();
423   std::set<AttributePtr>::const_iterator aIt;
424   FeaturePtr aCoincident;
425   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
426     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
427     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
428     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
429       AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
430         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
431       AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
432         aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
433       if(anAttrRefA.get() && !anAttrRefA->isObject()) {
434         AttributePtr anAttrA = anAttrRefA->attr();
435         if(anAttrBase == anAttrA) {
436           aCoincident = aConstrFeature;
437           break;
438         }
439       }
440       if(anAttrRefA.get() && !anAttrRefB->isObject()) {
441         AttributePtr anAttrB = anAttrRefB->attr();
442         if(anAttrBase == anAttrB) {
443           aCoincident = aConstrFeature;
444           break;
445         }
446       }
447     }
448   }
449
450   if(!aCoincident.get()) {
451     return false;
452   }
453
454   std::set<FeaturePtr> aCoinsides;
455   SketchPlugin_Tools::findCoincidences(aCoincident,
456                                        SketchPlugin_ConstraintCoincidence::ENTITY_A(),
457                                        aCoinsides);
458   SketchPlugin_Tools::findCoincidences(aCoincident,
459                                        SketchPlugin_ConstraintCoincidence::ENTITY_B(),
460                                        aCoinsides);
461   // Remove points
462   std::set<FeaturePtr> aNewLines;
463   for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
464     if((*anIt)->getKind() != SketchPlugin_Point::ID()) {
465       aNewLines.insert(*anIt);
466     }
467   }
468   aCoinsides = aNewLines;
469
470   // Remove auxilary lines
471   if(aCoinsides.size() > 2) {
472     aNewLines.clear();
473     for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
474       if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
475         aNewLines.insert(*anIt);
476       }
477     }
478     aCoinsides = aNewLines;
479   }
480
481   if(aCoinsides.size() != 2) {
482     return false;
483   }
484
485   // Check that lines not collinear
486   std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
487   FeaturePtr aFirstFeature = *anIt++;
488   FeaturePtr aSecondFeature = *anIt;
489   if(aFirstFeature->getKind() == SketchPlugin_Line::ID() && aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
490     std::string aStartAttr = SketchPlugin_Line::START_ID();
491     std::string anEndAttr = SketchPlugin_Line::END_ID();
492     std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
493     aFirstStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(aStartAttr))->pnt();
494     aFirstEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
495     aSecondStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(aStartAttr))->pnt();
496     aSecondEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(anEndAttr))->pnt();
497     double aCheck1 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondStartPnt->y() - aFirstStartPnt->y()) -
498       (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
499     double aCheck2 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondEndPnt->y() - aFirstStartPnt->y()) -
500       (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
501     if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
502       return false;
503     }
504   }
505
506   return true;
507 }