Salome HOME
Fix: gcc issue: default parameter value for a reference from a constructor
[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 #include "SketchPlugin_ConstraintDistance.h"
9 #include "SketchPlugin_ConstraintCoincidence.h"
10 #include "SketchPlugin_ConstraintRigid.h"
11 #include "SketchPlugin_Line.h"
12 #include "SketchPlugin_Arc.h"
13 #include "SketchPlugin_Circle.h"
14 #include "SketchPlugin_Point.h"
15
16 #include "SketcherPrs_Tools.h"
17
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Validator.h>
20 #include <ModelAPI_AttributeDouble.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Session.h>
25
26 #include <GeomValidators_ShapeType.h>
27
28 #include <GeomDataAPI_Point2D.h>
29
30
31 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute, 
32                                                  const std::list<std::string>& theArguments,
33                                                  std::string& theError) const
34 {
35   // there is a check whether the feature contains a point and a linear edge or two point values
36   std::string aParamA = theArguments.front();
37   SessionPtr aMgr = ModelAPI_Session::get();
38   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
39
40   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
41   if (!aRefAttr)
42     return false;
43
44   bool isObject = aRefAttr->isObject();
45   if (!isObject) {
46     // an attribute is a point. A point value is valid always for the distance
47     return true;
48   } else {
49     // 1. check whether the references object is a linear
50     ObjectPtr anObject = aRefAttr->object();
51
52     const ModelAPI_AttributeValidator* aShapeValidator = 
53       dynamic_cast<const GeomValidators_ShapeType*>(aFactory->validator("GeomValidators_ShapeType"));
54     std::list<std::string> anArguments;
55     anArguments.push_back("circle");
56     std::string aCircleError;
57     bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
58     // the circle line is not a valid case
59     if (aShapeValid)
60       return false;
61       
62     anArguments.clear();
63     anArguments.push_back("line");
64     std::string aLineError;
65     aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
66     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
67     if (!aShapeValid)
68       return true;
69
70     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
71     // If it is a line then we have to check that first attribute id not a line
72     std::shared_ptr<SketchPlugin_Feature> aSFeature =
73                             std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
74     SketchPlugin_Sketch* aSketch = aSFeature->sketch();
75     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
76     std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
77                                                                aFeature->data(), aParamA, aPlane);
78     if (aPoint)
79       return true;
80   }
81   return false;
82 }
83
84 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute, 
85                                                 const std::list<std::string>& theArguments,
86                                                 std::string& theError) const
87 {
88   // there is a check whether the feature contains a point and a linear edge or two point values
89   std::string aParamA = theArguments.front();
90   SessionPtr aMgr = ModelAPI_Session::get();
91   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
92
93   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
94   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
95   if (!aRefAttr)
96     return false;
97
98   bool isObject = aRefAttr->isObject();
99   ObjectPtr anObject = aRefAttr->object();
100   if (isObject && anObject) {
101     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
102
103     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
104     ObjectPtr aOtherObject = aOtherAttr->object();
105     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
106
107     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
108       if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
109         return false;
110     } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
111       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
112           aOtherFea->getKind() != SketchPlugin_Arc::ID())
113         return false;
114     } else
115       return false;
116
117     return true;
118   }
119   return false;
120 }
121
122 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute, 
123                                              const std::list<std::string>& theArguments,
124                                              std::string& theError) const
125 {
126   std::shared_ptr<SketchPlugin_Feature> aFeature =
127       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
128   if (!aFeature)
129     return true;
130
131   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
132   if (!aRefAttr)
133     return false;
134
135   SketchPlugin_Sketch* aSketch = aFeature->sketch();
136   int aNbFeatures = aSketch->numberOfSubs();
137   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
138     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
139     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
140       continue;
141     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
142         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
143     if (aRefAttr->isObject()) {
144       if (aRefAttr->object() == aRAttr->object())
145         return false;
146     } else if (aRefAttr->attr() == aRAttr->attr())
147       return false;
148   }
149   return true;
150 }
151
152 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute, 
153                                               const std::list<std::string>& theArguments,
154                                               std::string& theError) const
155 {
156   std::string aParamA = theArguments.front();
157   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
158   AttributeRefAttrPtr aRefAttr[2];
159   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
160   if (!aRefAttr)
161     return false;
162   aRefAttr[1] = aFeature->data()->refattr(aParamA);
163
164   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
165     return false;
166
167   int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
168   std::list<std::string> anArguments;
169   for (int i = 0; i < 2; i++) {
170     ObjectPtr anObject = aRefAttr[i]->object();
171     aFeature = ModelAPI_Feature::feature(anObject);
172     if (!aFeature)
173       return false;
174
175     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
176       aType[i] = 1;
177       continue;
178     }
179     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
180       aType[i] = 2;
181       continue;
182     }
183     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
184       aType[i] = 3;
185       continue;
186     }
187     // wrong type of attribute
188     return false;
189   }
190
191   if ((aType[0] == 1 && aType[1] == 2) ||
192       (aType[0] == 2 && aType[1] == 1))
193     return false;
194   return true;
195 }
196
197 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute, 
198                                                const std::list<std::string>& theArguments,
199                                                std::string& theError) const
200 {
201   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
202   AttributeSelectionListPtr aSelAttr = 
203     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
204   if (!aSelAttr)
205     return false;
206
207   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
208       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
209   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
210
211   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
212     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
213     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
214     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
215       if (aSelect->context() == *aMirIter)
216         return false;
217   }
218   return true;
219 }
220
221
222 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute, 
223                                                     const std::list<std::string>& theArguments,
224                                                     std::string& theError) const
225 {
226   // there is a check whether the feature contains a point and a linear edge or two point values
227   std::string aParamA = theArguments.front();
228   SessionPtr aMgr = ModelAPI_Session::get();
229   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
230
231   FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
232   AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
233   if (!aRefAttrA)
234     return false;
235
236   AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
237   if (!aRefAttrB)
238     return false;
239
240   // first attribute is a point, it may coincide with any object
241   if (!aRefAttrA->isObject())
242     return true;
243   else {
244     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
245     if (!aFeature)
246       return false;
247     if (aFeature->getKind() == SketchPlugin_Point::ID())
248       return true;
249   }
250
251   // second attribute is a point, it may coincide with any object
252   if (!aRefAttrB->isObject())
253     return true;
254   else {
255     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
256     if (!aFeature)
257       return false;
258     if (aFeature->getKind() == SketchPlugin_Point::ID())
259       return true;
260   }
261
262   return false;
263 }
264
265
266 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute, 
267                                          const std::list<std::string>& theArguments,
268                                          std::string& theError) const
269 {
270   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
271   AttributeSelectionListPtr aSelAttr = 
272     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
273   if (!aSelAttr)
274     return false;
275
276   AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
277       aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
278   AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
279       aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
280   std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
281   std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
282
283   std::list<ObjectPtr>::iterator anObjIter;
284   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
285     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
286     anObjIter = anInitialObjects.begin();
287     for (; anObjIter != anInitialObjects.end(); anObjIter++)
288       if (aSelect->context() == *anObjIter)
289         break;
290     if (anObjIter != anInitialObjects.end())
291       continue;
292     anObjIter = aCopiedObjects.begin();
293     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
294       if (aSelect->context() == *anObjIter)
295         return false;
296   }
297   return true;
298 }
299