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