]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Validators.cpp
Salome HOME
597f17d47a7e6d63a0df4f0d63a987e10099b801
[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 bool SketchPlugin_TangentAttrValidator::isValid(
81   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
82 {
83   // there is a check whether the feature contains a point and a linear edge or two point values
84   std::string aParamA = theArguments.front();
85   SessionPtr aMgr = ModelAPI_Session::get();
86   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
87
88   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
89   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
90   if (!aRefAttr)
91     return false;
92
93   bool isObject = aRefAttr->isObject();
94   ObjectPtr anObject = aRefAttr->object();
95   if (isObject && anObject) {
96     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
97
98     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
99     ObjectPtr aOtherObject = aOtherAttr->object();
100     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
101
102     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
103       if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
104         return false;
105     } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
106       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
107           aOtherFea->getKind() != SketchPlugin_Arc::ID())
108         return false;
109     } else
110       return false;
111
112     // check that both have coincidence
113     FeaturePtr aConstrFeature;
114     std::set<FeaturePtr> aCoinList;
115     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
116     std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
117     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
118       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
119       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
120       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
121         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
122         AttributePtr aAR = aRAttr->attr();
123         if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
124           aCoinList.insert(aConstrFeature);
125       }
126     }
127     // if there is no coincidence then it is not valid
128     if (aCoinList.size() == 0)
129       return false;
130
131     // find that coincedence is the same
132     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
133     std::set<FeaturePtr>::const_iterator aCoinsIt;
134     for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
135       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
136       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
137       aCoinsIt = aCoinList.find(aConstrFeature);
138       if (aCoinsIt != aCoinList.end()) {
139         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
140         AttributePtr aAR = aRAttr->attr();
141         if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
142           return true;
143       }
144     }
145   }
146   return false;
147 }
148
149 bool SketchPlugin_NotFixedValidator::isValid(
150     const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
151 {
152   std::shared_ptr<SketchPlugin_Feature> aFeature =
153       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
154   if (!aFeature)
155     return true;
156
157   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
158   if (!aRefAttr)
159     return false;
160
161   SketchPlugin_Sketch* aSketch = aFeature->sketch();
162   int aNbFeatures = aSketch->numberOfSubs();
163   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
164     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
165     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
166       continue;
167     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
168         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
169     if (aRefAttr->isObject()) {
170       if (aRefAttr->object() == aRAttr->object())
171         return false;
172     } else if (aRefAttr->attr() == aRAttr->attr())
173       return false;
174   }
175   return true;
176 }
177
178 bool SketchPlugin_EqualAttrValidator::isValid(
179   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
180 {
181   std::string aParamA = theArguments.front();
182   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
183   AttributeRefAttrPtr aRefAttr[2];
184   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
185   if (!aRefAttr)
186     return false;
187   aRefAttr[1] = aFeature->data()->refattr(aParamA);
188
189   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
190     return false;
191
192   int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
193   std::list<std::string> anArguments;
194   for (int i = 0; i < 2; i++) {
195     ObjectPtr anObject = aRefAttr[i]->object();
196     aFeature = ModelAPI_Feature::feature(anObject);
197     if (!aFeature)
198       return false;
199
200     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
201       aType[i] = 1;
202       continue;
203     }
204     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
205       aType[i] = 2;
206       continue;
207     }
208     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
209       aType[i] = 3;
210       continue;
211     }
212     // wrong type of attribute
213     return false;
214   }
215
216   if ((aType[0] == 1 && aType[1] == 2) ||
217       (aType[0] == 2 && aType[1] == 1))
218     return false;
219   return true;
220 }
221
222 bool SketchPlugin_MirrorAttrValidator::isValid(
223   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
224 {
225   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
226   AttributeSelectionListPtr aSelAttr = 
227     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
228   if (!aSelAttr)
229     return false;
230
231   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
232       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
233   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
234
235   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
236     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
237     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
238     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
239       if (aSelect->context() == *aMirIter)
240         return false;
241   }
242   return true;
243 }
244
245
246 bool SketchPlugin_CoincidenceAttrValidator::isValid(
247   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
248 {
249   // there is a check whether the feature contains a point and a linear edge or two point values
250   std::string aParamA = theArguments.front();
251   SessionPtr aMgr = ModelAPI_Session::get();
252   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
253
254   FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
255   AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
256   if (!aRefAttrA)
257     return false;
258
259   AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
260   if (!aRefAttrB)
261     return false;
262
263   // first attribute is a point, it may coincide with any object
264   if (!aRefAttrA->isObject())
265     return true;
266   else {
267     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
268     if (!aFeature)
269       return false;
270     if (aFeature->getKind() == SketchPlugin_Point::ID())
271       return true;
272   }
273
274   // second attribute is a point, it may coincide with any object
275   if (!aRefAttrB->isObject())
276     return true;
277   else {
278     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
279     if (!aFeature)
280       return false;
281     if (aFeature->getKind() == SketchPlugin_Point::ID())
282       return true;
283   }
284
285   return false;
286 }
287
288
289 bool SketchPlugin_CopyValidator::isValid(
290   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
291 {
292   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
293   AttributeSelectionListPtr aSelAttr = 
294     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
295   if (!aSelAttr)
296     return false;
297
298   AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
299       aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
300   AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
301       aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
302   std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
303   std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
304
305   std::list<ObjectPtr>::iterator anObjIter;
306   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
307     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
308     anObjIter = anInitialObjects.begin();
309     for (; anObjIter != anInitialObjects.end(); anObjIter++)
310       if (aSelect->context() == *anObjIter)
311         break;
312     if (anObjIter != anInitialObjects.end())
313       continue;
314     anObjIter = aCopiedObjects.begin();
315     for (; anObjIter != aCopiedObjects.end(); anObjIter++)
316       if (aSelect->context() == *anObjIter)
317         return false;
318   }
319   return true;
320 }
321