Salome HOME
It provides error messages for validators.
[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
18 #include "SketcherPrs_Tools.h"
19
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Validator.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeRefAttr.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Session.h>
28
29 #include <GeomValidators_ShapeType.h>
30
31 #include <GeomDataAPI_Point2D.h>
32
33
34 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute, 
35                                                  const std::list<std::string>& theArguments,
36                                                  std::string& theError) const
37 {
38   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
39     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
40     return false;
41   }
42
43   // there is a check whether the feature contains a point and a linear edge or two point values
44   std::string aParamA = theArguments.front();
45   SessionPtr aMgr = ModelAPI_Session::get();
46   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
47
48   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
49   bool isObject = aRefAttr->isObject();
50   if (!isObject) {
51     // an attribute is a point. A point value is valid always for the distance
52     return true;
53   } else {
54     // 1. check whether the references object is a linear
55     ObjectPtr anObject = aRefAttr->object();
56
57     const ModelAPI_AttributeValidator* aShapeValidator = 
58       dynamic_cast<const GeomValidators_ShapeType*>(aFactory->validator("GeomValidators_ShapeType"));
59     std::list<std::string> anArguments;
60     anArguments.push_back("circle");
61     std::string aCircleError;
62     bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
63     // the circle line is not a valid case
64     if (aShapeValid) {
65       theError = "Circle can not be used in distance constraint";
66       return false;
67     }
68       
69     anArguments.clear();
70     anArguments.push_back("line");
71     std::string aLineError;
72     aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
73     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
74     if (aShapeValid) {
75       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
76       // If it is a line then we have to check that first attribute id not a line
77       std::shared_ptr<SketchPlugin_Feature> aSFeature =
78         std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
79       SketchPlugin_Sketch* aSketch = aSFeature->sketch();
80       std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
81       std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
82         aFeature->data(), aParamA, aPlane);
83       if (!aPoint.get()) {
84         theError = "One of parameters of distance constraint should be a point";
85         return false;
86       }
87     }
88   }
89   return true;
90 }
91
92 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute, 
93                                                 const std::list<std::string>& theArguments,
94                                                 std::string& theError) const
95 {
96   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
97     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
98     return false;
99   }
100
101   // there is a check whether the feature contains a point and a linear edge or two point values
102   std::string aParamA = theArguments.front();
103   SessionPtr aMgr = ModelAPI_Session::get();
104   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
105
106   FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
107   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
108
109   bool isObject = aRefAttr->isObject();
110   ObjectPtr anObject = aRefAttr->object();
111   if (isObject && anObject.get()) {
112     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
113
114     AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
115     ObjectPtr aOtherObject = aOtherAttr->object();
116     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
117
118     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
119       if (aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
120         theError = "It refers to a " + SketchPlugin_Line::ID() + ", but " + aParamA + " is not an "
121           + SketchPlugin_Arc::ID();
122         return false;
123       }
124     }
125     else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
126       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
127         aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
128         theError = "It refers to an " + SketchPlugin_Arc::ID() + ", but " + aParamA + " is not a "
129           + SketchPlugin_Line::ID() + " or an " + SketchPlugin_Arc::ID();
130         return false;
131       }
132     }
133     else {
134       theError = "It refers to " + aRefFea->getKind() + "but should refer to " + SketchPlugin_Line::ID()
135         + " or " + SketchPlugin_Arc::ID();
136       return false;
137     }
138     return true;
139   }
140   else {
141     theError = "It uses an empty object";
142     return false;
143   }
144
145   return true;
146 }
147
148 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute, 
149                                              const std::list<std::string>& theArguments,
150                                              std::string& theError) const
151 {
152   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
153     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
154     return false;
155   }
156
157   std::shared_ptr<SketchPlugin_Feature> aFeature =
158       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
159   if (!aFeature)
160     return true;
161
162   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
163
164   SketchPlugin_Sketch* aSketch = aFeature->sketch();
165   int aNbFeatures = aSketch->numberOfSubs();
166   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
167     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
168     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
169       continue;
170     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
171         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
172     if (aRefAttr->isObject()) {
173       if (aRefAttr->object() == aRAttr->object()) {
174         ObjectPtr anObject = aRefAttr->object();
175         std::string aName = anObject.get() ? anObject->data()->name() : "";
176         theError = "The object " + aName + " has been already fixed.";
177         return false;
178       }
179     }
180     else if (aRefAttr->attr() == aRAttr->attr()) {
181       AttributePtr anAttribute = aRefAttr->attr();
182       std::string aName = anAttribute.get() ? anAttribute->id() : "";
183       theError = "The attribute " + aName + " has been already fixed.";
184       return false;
185     }
186   }
187   return true;
188 }
189
190 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute, 
191                                               const std::list<std::string>& theArguments,
192                                               std::string& theError) const
193 {
194   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
195     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
196     return false;
197   }
198
199   std::string aParamA = theArguments.front();
200   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
201   AttributeRefAttrPtr aRefAttr[2];
202   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
203   aRefAttr[1] = aFeature->data()->refattr(aParamA);
204
205   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
206     theError = "Attributes can not be used in equal constraint";
207     return false;
208   }
209
210   int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
211   std::list<std::string> anArguments;
212   for (int i = 0; i < 2; i++) {
213     ObjectPtr anObject = aRefAttr[i]->object();
214     if (!anObject.get()) {
215       theError = "An empty object is used.";
216       return false;
217     }
218
219     aFeature = ModelAPI_Feature::feature(anObject);
220     if (!aFeature.get()) {
221       theError = "An empty feature is used.";
222       return false;
223     }
224
225     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
226       aType[i] = 1;
227       continue;
228     }
229     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
230       aType[i] = 2;
231       continue;
232     }
233     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
234       aType[i] = 3;
235       continue;
236     }
237     theError = "The " + aFeature->getKind() + " feature kind of attribute is wrong. It should be " +
238                SketchPlugin_Line::ID() + " or " + SketchPlugin_Circle::ID() + " or " + 
239                SketchPlugin_Arc::ID();
240     // wrong type of attribute
241     return false;
242   }
243
244   if ((aType[0] == 1 && aType[1] == 2) ||
245       (aType[0] == 2 && aType[1] == 1)) {
246     theError = "Feature with kinds " + SketchPlugin_Line::ID() + " and " +
247                SketchPlugin_Circle::ID() + "can not be equal.";
248     return false;
249   }
250   return true;
251 }
252
253 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute, 
254                                                const std::list<std::string>& theArguments,
255                                                std::string& theError) const
256 {
257   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
258     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
259     return false;
260   }
261
262   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
263   AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
264
265   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
266       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
267   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
268
269   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
270     ObjectPtr aSelObject = aSelAttr->object(anInd);
271     std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
272     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
273     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
274       if (aSelObject == *aMirIter) {
275         theError = "The object " + aName + " is a result of mirror";
276         return false;
277       }
278   }
279   return true;
280 }
281
282
283 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute, 
284                                                     const std::list<std::string>& theArguments,
285                                                     std::string& theError) const
286 {
287   if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
288     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
289     return false;
290   }
291
292   // there is a check whether the feature contains a point and a linear edge or two point values
293   std::string aParamA = theArguments.front();
294   SessionPtr aMgr = ModelAPI_Session::get();
295   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
296
297   FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
298   AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
299   if (!aRefAttrA) {
300     theError = "The " + aParamA + " attribute " + " should be " + ModelAPI_AttributeRefAttr::typeId();
301     return false;
302   }
303
304   AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
305
306   // first attribute is a point, it may coincide with any object
307   if (!aRefAttrA->isObject())
308     return true;
309   else {
310     ObjectPtr anObject = aRefAttrA->object();
311     if (!anObject.get()) {
312       theError = aParamA + " attribute has an empty object";
313       return false;
314     }
315     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
316     if (!aFeature.get()) {
317       theError = aParamA + " attribute has an empty feature";
318       return false;
319     }
320
321     if (aFeature->getKind() == SketchPlugin_Point::ID())
322       return true;
323   }
324
325   // second attribute is a point, it may coincide with any object
326   if (!aRefAttrB->isObject())
327     return true;
328   else {
329     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
330     if (!aFeature) {
331       theError = theAttribute->id() + " attribute has an empty object";
332       return false;
333     }
334     if (aFeature->getKind() == SketchPlugin_Point::ID())
335       return true;
336   }
337   theError = "There is no an attribute filled by a point";
338   return false;
339 }
340
341
342 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute, 
343                                          const std::list<std::string>& theArguments,
344                                          std::string& theError) const
345 {
346   if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
347     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
348     return false;
349   }
350
351   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
352   AttributeRefListPtr aSelAttr = 
353     std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
354
355   AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
356       aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
357   AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
358       aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
359   std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
360   std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
361
362   std::list<ObjectPtr>::iterator anObjIter;
363   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
364     ObjectPtr aSelObject = aSelAttr->object(anInd);
365     anObjIter = anInitialObjects.begin();
366     for (; anObjIter != anInitialObjects.end(); anObjIter++)
367       if (aSelObject == *anObjIter)
368         break;
369     if (anObjIter != anInitialObjects.end())
370       continue;
371     anObjIter = aCopiedObjects.begin();
372     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
373       if (aSelObject == *anObjIter) {
374         std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
375         theError = "The object " + aName + " is a result of copy";
376         return false;
377       }
378   }
379   return true;
380 }
381
382 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
383                                                 const std::list<std::string>& theArguments,
384                                                 std::string& theError) const
385 {
386   AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
387
388   if (!aAttributeString->value().empty()) {
389     theError = aAttributeString->value();
390     return false;
391   }
392
393   return true;
394 }
395
396 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
397 {
398   return true;
399 }
400