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